1
1
import { AirbyteConfig , AirbyteLogger } from 'faros-airbyte-cdk' ;
2
- import jenkinsClient , { JenkinsPromisifiedAPI } from 'jenkins' ;
2
+ import jenkinsClient from 'jenkins' ;
3
3
import { Memoize } from 'typescript-memoize' ;
4
4
import { URL } from 'url' ;
5
5
import util from 'util' ;
6
+ import { VError } from 'verror' ;
6
7
7
- export const DEFAULT_PAGE_SIZE = 10 ;
8
+ const DEFAULT_PAGE_SIZE = 10 ;
8
9
const FEED_ALL_FIELDS_PATTERN = `name,fullName,url,lastCompletedBuild[number],%s[id,displayName,number,building,result,timestamp,duration,url,actions[lastBuiltRevision[SHA1],remoteUrls],fullName,fullDisplayName],jobs[*]` ;
9
10
const FEED_JOBS_COUNT_PATTERN = 'jobs[name]' ;
10
11
const FEED_MAX_DEPTH_CALC_PATTERN = 'fullName,jobs[*]' ;
@@ -51,16 +52,16 @@ export interface JenkinsState {
51
52
52
53
export class Jenkins {
53
54
constructor (
54
- private readonly client : any ,
55
+ private readonly client : any , // It should be 'JenkinsPromisifiedAPI' instead of any, but we could not make it work
55
56
private readonly logger : AirbyteLogger
56
57
) { }
57
58
58
- static parse ( str : string , ...args : any [ ] ) : string {
59
+ private static parse ( str : string , ...args : any [ ] ) : string {
59
60
let i = 0 ;
60
61
return str . replace ( / % s / g, ( ) => args [ i ++ ] ) ;
61
62
}
62
63
63
- static validateInteger (
64
+ private static validateInteger (
64
65
value : number
65
66
) : [ true | undefined , string | undefined ] {
66
67
if ( value ) {
@@ -72,44 +73,30 @@ export class Jenkins {
72
73
return [ true , undefined ] ;
73
74
}
74
75
75
- static async make (
76
- config : JenkinsConfig ,
77
- logger : AirbyteLogger
78
- ) : Promise < Jenkins | undefined > {
79
- const [ client , errorMessage ] = await Jenkins . validateClient ( config ) ;
80
- if ( ! client ) {
81
- logger . error ( errorMessage || '' ) ;
82
- return undefined ;
83
- }
84
- return new Jenkins ( client , logger ) ;
85
- }
86
-
87
- static async validateClient (
88
- config : JenkinsConfig
89
- ) : Promise < [ JenkinsPromisifiedAPI | undefined , string | undefined ] > {
76
+ static instance ( config : JenkinsConfig , logger : AirbyteLogger ) : Jenkins {
90
77
if ( typeof config . server_url !== 'string' ) {
91
- return [ undefined , 'server_url: must be a string' ] ;
78
+ throw new VError ( 'server_url: must be a string' ) ;
92
79
}
93
80
if ( typeof config . user !== 'string' ) {
94
- return [ undefined , 'User : must be a string'] ;
81
+ throw new VError ( 'user : must be a string') ;
95
82
}
96
83
if ( typeof config . token !== 'string' ) {
97
- return [ undefined , 'Token : must be a string'] ;
84
+ throw new VError ( 'token : must be a string') ;
98
85
}
99
86
const depthCheck = Jenkins . validateInteger ( config . depth ) ;
100
87
if ( ! depthCheck [ 0 ] ) {
101
- return [ undefined , depthCheck [ 1 ] ] ;
88
+ throw new VError ( depthCheck [ 1 ] ) ;
102
89
}
103
90
const pageSizeCheck = Jenkins . validateInteger ( config . pageSize ) ;
104
91
if ( ! pageSizeCheck [ 0 ] ) {
105
- return [ undefined , pageSizeCheck [ 1 ] ] ;
92
+ throw new VError ( pageSizeCheck [ 1 ] ) ;
106
93
}
107
94
108
- let jenkinsUrl ;
95
+ let jenkinsUrl : URL ;
109
96
try {
110
97
jenkinsUrl = new URL ( config . server_url ) ;
111
98
} catch ( error ) {
112
- return [ undefined , 'server_url: must be a valid url' ] ;
99
+ throw new VError ( 'server_url: must be a valid url' ) ;
113
100
}
114
101
115
102
jenkinsUrl . username = config . user ;
@@ -121,15 +108,18 @@ export class Jenkins {
121
108
promisify : true ,
122
109
} ) ;
123
110
111
+ return new Jenkins ( client , logger ) ;
112
+ }
113
+
114
+ async checkConnection ( ) : Promise < void > {
124
115
try {
125
- await client . info ( ) ;
126
- } catch ( error ) {
127
- return [
128
- undefined ,
129
- ' Please verify your server_url and user/token are correct' ,
130
- ] ;
116
+ await this . client . info ( ) ;
117
+ } catch ( error : any ) {
118
+ const err = error ?. message ?? JSON . stringify ( error ) ;
119
+ throw new VError (
120
+ ` Please verify your server_url and user/token are correct. Error: ${ err } `
121
+ ) ;
131
122
}
132
- return [ client , undefined ] ;
133
123
}
134
124
135
125
async * syncBuilds (
0 commit comments