import * as rp from 'request-promise'; import { Auth, AuthMethod } from './auth'; import * as merge from 'lodash/merge'; import { log } from './log'; export { RequestPromise, Options as RequestOptions } from "request-promise"; export { StatusCodeError } from "request-promise/errors"; /* bitbucket uses - basic - oauth github uses - basic - oauth2 - token bbserver uses - basic - oauth - session (not supported */ export interface IConnection { supportedAuthMethods: any[] } export abstract class Connection { abstract getSupportedAuthMethods(): string[]; protected defaultRequestOptions: rp.Options = { baseUrl : '', uri : '', auth : { user : null, password : null }, method : 'GET', headers : { 'Content-Type' : 'application/json' }, json : true }; constructor( public auth: Auth, protected options: any = {} ) { this.init() } protected init() { } request( options: rp.Options ): rp.RequestPromise { var request = rp(merge(this.defaultRequestOptions, options)); request.catch(( reason ) => { log.error(reason); }) return request; } get( endpoint: string, params: any = {} ): rp.RequestPromise { return this.request({ uri : endpoint, qs : params }); } put( endpoint: string, payload: any = {} ): rp.RequestPromise { return this.request({ method : 'PUT', uri : endpoint, body : payload, json : true }) } patch( endpoint: string, payload: any = {} ): rp.RequestPromise { return this.request({ method : 'PATCH', uri : endpoint, body : payload, json : true }) } post( endpoint: string, payload: any = {} ): rp.RequestPromise { return this.request({ method : 'POST', uri : endpoint, body : payload, json : true }) } delete( endpoint: string, params: any = {} ): rp.RequestPromise { return this.request({ method : 'DELETE', uri : endpoint, qs : params }); } } export class BitbucketConnection extends Connection { getSupportedAuthMethods(): any { return [AuthMethod.basic, AuthMethod.oauth2, AuthMethod.oauth]; } protected init() { merge(this.defaultRequestOptions, { baseUrl : 'https://api.bitbucket.org/2.0', auth : { username : this.auth.key, password : this.auth.secret } }) } } export class BitbucketServerConnection extends Connection { getSupportedAuthMethods(): any { return [AuthMethod.basic, AuthMethod.oauth2, AuthMethod.oauth]; } protected init() { merge(this.defaultRequestOptions, { baseUrl : this.auth.extra, auth : { username : this.auth.key, password : this.auth.secret } }) } } export class GithubConnection extends Connection { getSupportedAuthMethods(): any { return [AuthMethod.basic, AuthMethod.oauth, AuthMethod.token]; } protected init() { merge(this.defaultRequestOptions, { baseUrl : 'https://api.github.com/', auth : { username : this.auth.key, password : this.auth.secret }, headers: { 'User-Agent': 'Commando', //'Accept': 'application/vnd.github.v3+json' } }) } } export class JenkinsConnection extends Connection { getSupportedAuthMethods(): any { return [AuthMethod.basic]; } protected init() { merge(this.defaultRequestOptions, { baseUrl : this.auth.extra, auth : { username : this.auth.key, password : this.auth.secret } }) } request( options: rp.Options ): rp.RequestPromise { options['uri'] += '/api/json' return super.request(options); } } export class JiraConnection extends Connection { getSupportedAuthMethods(): any { return [AuthMethod.basic, AuthMethod.oauth2]; } protected init() { merge(this.defaultRequestOptions, { baseUrl : this.auth.extra + '/rest/api/2/', auth : { username : this.auth.key, password : this.auth.secret } }) } } export class PackagistConnection extends Connection { getSupportedAuthMethods(): any { return [AuthMethod.basic, AuthMethod.token]; } }