-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathgithub.ts
128 lines (121 loc) · 4.12 KB
/
github.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import logger from 'common/logger';
import request from 'service-worker/request';
import type {
CIBuild,
CIPipeline,
CIServiceDefinition,
CIServiceSettings,
} from 'common/types';
const getPipelines = async (settings: CIServiceSettings): Promise<CIPipeline[]> => {
logger.log('github.getPipelines', settings);
const response = await requestWorkflows(settings);
const { workflows } = response.body;
const pipelines: CIPipeline[] = workflows.map(workflow => {
return {
id: `${workflow.id} | ${workflow.name}`,
name: workflow.name,
isDisabled: workflow.state != 'active',
};
});
return pipelines;
};
const getLatestBuilds = async (settings: CIServiceSettings): Promise<CIBuild[]> => {
logger.log('github.getLatestBuilds', settings);
const pipelines = settings.pipelines.map(
async (pipelineId: string): Promise<CIBuild> => {
try {
const [id] = pipelineId.split(' |');
const response = await requestWorkflowRuns(settings, id);
const [run] = response.body.workflow_runs;
if (!run) {
throw new Error('Workflow run not found');
}
return parseBuild(run);
} catch (ex: any) {
return {
id: pipelineId,
name: pipelineId,
error: { name: 'Error', message: ex.message },
};
}
},
);
logger.log('github.getLatestBuilds.pipeline', pipelines);
return await Promise.all(pipelines);
};
export default {
getDefinition: (): CIServiceDefinition => ({
typeName: 'GitHub Actions',
baseUrl: 'github',
icon: 'github.png',
logo: 'github.svg',
fields: [
{
type: 'url',
name: 'Github URL, f.e. https://github.com/AdamNowotny/BuildReactor',
},
{
type: 'token',
help: 'Create token at <a href="https://github.com/settings/personal-access-tokens/new" target="_blank">https://github.com/settings/personal-access-tokens/new</a>',
},
{ type: 'branch', name: 'Branch, f.e. main' },
],
defaultConfig: {
baseUrl: 'github',
url: '',
name: '',
pipelines: [],
token: '',
},
}),
getPipelines,
getLatestBuilds,
};
const requestWorkflowRuns = async (settings: CIServiceSettings, id: string) => {
if (!settings.url) throw new Error('No url provided');
const [_origin, owner, repo] = new URL(settings.url).pathname.split('/');
return request.get({
url: `https://api.github.com/repos/${owner}/${repo}/actions/workflows/${id}/runs`,
query: settings.branch
? {
branch: settings.branch,
}
: undefined,
headers: createHeaders(settings),
});
};
const requestWorkflows = async (settings: CIServiceSettings) => {
if (!settings.url) throw new Error('No url provided');
const [_origin, owner, repo] = new URL(settings.url).pathname.split('/');
return request.get({
url: `https://api.github.com/repos/${owner}/${repo}/actions/workflows`,
headers: createHeaders(settings),
});
};
const parseBuild = (run: any) => {
const build: CIBuild = {
changes: [
{
name: run.head_commit?.author?.name,
message: run.head_commit?.message,
},
],
id: run.id.toString(),
isBroken: run.conclusion === 'failure',
isDisabled: false,
isRunning: run.status === 'in_progress',
isWaiting: run.status === 'queued',
name: run.name,
webUrl: run.html_url,
};
return build;
};
function createHeaders(settings: CIServiceSettings): HeadersInit | undefined {
return settings.token
? {
Authorization: `Bearer ${settings.token}`,
'X-GitHub-Api-Version': '2022-11-28',
Accept: 'application/vnd.github.v3+json',
}
: undefined;
}