-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
95 lines (80 loc) · 3.73 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import * as core from '@actions/core';
import axios from 'axios';
import moment from 'moment';
type Headers = {
'Content-Type': string;
Authorization: string;
}
interface AnnotationPayload {
tags: string[];
text: string;
dashboardId?: number;
panelId?: number;
timeEnd?: number;
}
export const run = async (): Promise<void> => {
try {
let globalAnnotation = true;
const grafanaHost: string = core.getInput('grafanaHost', { required: true });
const grafanaToken: string = core.getInput('grafanaToken', { required: true });
const grafanaTags: string[] = core.getInput('grafanaTags').split('\n').filter(x => x !== '');
const grafanaDashboardID: number | undefined = Number.parseInt(core.getInput('grafanaDashboardID'), 10) || undefined;
const grafanaPanelID: number | undefined = Number.parseInt(core.getInput('grafanaPanelID'), 10) || undefined;
const grafanaAnnotationID: number | undefined = Number.parseInt(core.getInput('grafanaAnnotationID'), 10) || undefined;
const grafanaText: string = core.getInput('grafanaText', { required: grafanaAnnotationID === undefined });
const headers: Headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${grafanaToken}`,
};
if (grafanaAnnotationID === undefined) {
console.log('Creating a new annotation');
if ((grafanaDashboardID === undefined && grafanaPanelID !== undefined) ||
(grafanaDashboardID !== undefined && grafanaPanelID === undefined)) {
return core.error('Must supply both grafanaDashboardID, grafanaPanelID or none.');
}
if (grafanaDashboardID !== undefined && grafanaPanelID !== undefined) {
console.log('Dashboard and panel specified, non-global annotation will be created.');
globalAnnotation = false;
}
const payload: AnnotationPayload = {
tags: grafanaTags,
text: grafanaText,
};
if (!globalAnnotation) {
payload.dashboardId = grafanaDashboardID;
payload.panelId = grafanaPanelID;
}
console.log('Payload: ' + JSON.stringify(payload));
// Using async/await for axios POST request
try {
const response = await axios.post(`${grafanaHost}/api/annotations`, payload, { headers });
const annotationId = response.data.id;
console.log(`Successfully created an annotation with the following id [${annotationId}]`);
core.setOutput('annotation-id', annotationId);
} catch (err) {
console.error('Error in POST /api/annotations:', err);
core.setFailed(`Error in POST /api/annotations: ${err}`);
}
} else {
console.log('Updating the end time of existing annotation');
const payload: Partial<AnnotationPayload> = {
timeEnd: moment().valueOf(),
};
console.log(`Updating the 'time-end' of annotation [${grafanaAnnotationID}]`);
try {
await axios.patch(`${grafanaHost}/api/annotations/${grafanaAnnotationID}`, payload, { headers });
console.log('Successfully updated the annotation with time-end');
} catch (err) {
console.error('Error in PATCH /api/annotations:', err);
core.setFailed(`Error in PATCH /api/annotations: ${err}`);
}
}
} catch (err) {
if (err instanceof Error) {
core.setFailed(err.message);
} else {
console.error('An unexpected error occurred');
}
}
};
run();