1
- // import defaults from 'lodash/defaults';
2
-
3
1
// @ts -ignore
4
- import { getBackendSrv , logDebug , logInfo , logWarning , logError } from '@grafana/runtime' ;
5
-
6
2
import {
7
3
DataQueryRequest ,
8
4
DataQueryResponse ,
9
5
DataSourceApi ,
10
6
DataSourceInstanceSettings ,
11
- MutableDataFrame ,
12
7
FieldType ,
8
+ MutableDataFrame ,
13
9
} from '@grafana/data' ;
14
-
15
- import { MyQuery , MyDataSourceOptions } from './types' ;
10
+ import { getBackendSrv } from '@grafana/runtime' ;
11
+ import { GitlabDataSourceOptions , GitlabQuery } from './types' ;
16
12
17
13
const routePath = '/gitlab' ;
18
14
19
- export class DataSource extends DataSourceApi < MyQuery , MyDataSourceOptions > {
15
+ export class DataSource extends DataSourceApi < GitlabQuery , GitlabDataSourceOptions > {
20
16
url ?: string ;
21
17
22
- constructor ( instanceSettings : DataSourceInstanceSettings < MyDataSourceOptions > ) {
18
+ constructor ( instanceSettings : DataSourceInstanceSettings < GitlabDataSourceOptions > ) {
23
19
super ( instanceSettings ) ;
24
20
25
21
this . url = instanceSettings . url ;
26
22
}
27
23
28
- async query ( options : DataQueryRequest < MyQuery > ) : Promise < DataQueryResponse > {
29
- const promises = options . targets . map ( async query => {
24
+ async query ( options : DataQueryRequest < GitlabQuery > ) : Promise < DataQueryResponse > {
25
+ const promises = options . targets . map ( async ( query ) => {
30
26
const frame = await this . doRequest ( query ) ;
31
27
return frame ;
32
28
} ) ;
33
29
const data = await Promise . all ( promises ) ;
34
30
return { data } ;
35
31
}
36
32
37
- async doRequest ( query : MyQuery ) {
38
- if ( ! query . group ) {
33
+ async doRequest ( query : GitlabQuery ) {
34
+ if ( ! query . groupId ) {
39
35
return await this . fetchGroups ( query ) ;
40
- } else if ( ! query . project ) {
36
+ } else if ( ! query . projectId ) {
41
37
return await this . fetchProjects ( query ) ;
42
38
} else {
43
39
return await this . fetchTags ( query ) ;
44
40
}
45
41
}
46
42
47
- async fetchTags ( q : MyQuery ) {
43
+ async fetchTags ( q : GitlabQuery ) {
48
44
const frame = new MutableDataFrame ( {
49
45
refId : q . refId ,
50
46
fields : [
@@ -53,22 +49,17 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
53
49
] ,
54
50
} ) ;
55
51
56
- try {
57
- const { data : projects } = await this . callGitlabAPI ( `/projects/${ q . project } /repository/tags` ) ;
58
-
59
- if ( projects ) {
60
- projects . forEach ( ( r : any ) => {
61
- frame . appendRow ( [ r . name , r . message ] ) ;
62
- } ) ;
63
- }
64
- } catch ( e ) {
65
- logError ( `Failed to fetch projects. Error: ${ e } ` ) ;
52
+ const { data : projects } = await this . callGitlabAPI ( `/projects/${ q . projectId } /repository/tags` ) ;
53
+ if ( projects ) {
54
+ projects . forEach ( ( r : any ) => {
55
+ frame . appendRow ( [ r . name , r . message ] ) ;
56
+ } ) ;
66
57
}
67
58
68
59
return frame ;
69
60
}
70
61
71
- async fetchProjects ( q : MyQuery ) {
62
+ async fetchProjects ( q : GitlabQuery ) {
72
63
const frame = new MutableDataFrame ( {
73
64
refId : q . refId ,
74
65
fields : [
@@ -77,22 +68,17 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
77
68
] ,
78
69
} ) ;
79
70
80
- try {
81
- const { data : projects } = await this . callGitlabAPI ( `/groups/${ q . group } /projects` ) ;
82
-
83
- if ( projects ) {
84
- projects . forEach ( ( r : any ) => {
85
- frame . appendRow ( [ r . name , r . id ] ) ;
86
- } ) ;
87
- }
88
- } catch ( e ) {
89
- logError ( `Failed to fetch projects. Error: ${ e } ` ) ;
71
+ const { data : projects } = await this . callGitlabAPI ( `/groups/${ q . groupId } /projects` ) ;
72
+ if ( projects ) {
73
+ projects . forEach ( ( r : any ) => {
74
+ frame . appendRow ( [ r . name , r . id ] ) ;
75
+ } ) ;
90
76
}
91
77
92
78
return frame ;
93
79
}
94
80
95
- async fetchGroups ( q : MyQuery ) {
81
+ async fetchGroups ( q : GitlabQuery ) {
96
82
const frame = new MutableDataFrame ( {
97
83
refId : q . refId ,
98
84
fields : [
@@ -101,29 +87,29 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
101
87
] ,
102
88
} ) ;
103
89
104
- try {
105
- const { data : groups } = await this . callGitlabAPI ( '/groups' ) ;
106
-
107
- if ( groups ) {
108
- groups . forEach ( ( r : any ) => {
109
- frame . appendRow ( [ r . name , r . id ] ) ;
110
- } ) ;
111
- }
112
- } catch ( e ) {
113
- logError ( `Failed to fetch groups. Error: ${ e } ` ) ;
90
+ const { data : groups } = await this . callGitlabAPI ( '/groups' ) ;
91
+ if ( groups ) {
92
+ groups . forEach ( ( r : any ) => {
93
+ frame . appendRow ( [ r . name , r . id ] ) ;
94
+ } ) ;
114
95
}
115
96
116
97
return frame ;
117
98
}
118
99
119
- async callGitlabAPI ( path : string , params : any = { } ) {
120
- const response = await getBackendSrv ( ) . datasourceRequest ( {
121
- method : 'GET' ,
122
- url : this . url + routePath + path ,
123
- params,
124
- } ) ;
125
- logDebug ( `Gitlab API response: ${ response } ` ) ;
126
- return response ;
100
+ async callGitlabAPI ( path : string , params : object = { } ) {
101
+ try {
102
+ const response = await getBackendSrv ( ) . datasourceRequest ( {
103
+ method : 'GET' ,
104
+ url : this . url + routePath + path ,
105
+ params,
106
+ } ) ;
107
+ console . debug ( `Gitlab API response: ${ response } ` ) ;
108
+ return response ;
109
+ } catch ( e ) {
110
+ console . error ( `Failed to fetch groups. Error: ${ e } ` ) ;
111
+ throw e ;
112
+ }
127
113
}
128
114
129
115
async testDatasource ( ) {
@@ -134,7 +120,6 @@ export class DataSource extends DataSourceApi<MyQuery, MyDataSourceOptions> {
134
120
message : 'Success' ,
135
121
} ;
136
122
} catch ( e ) {
137
- logError ( `Failed to fetch groups. Error: ${ e } ` ) ;
138
123
return {
139
124
status : 'error' ,
140
125
message : `Error: ${ e } ` ,
0 commit comments