forked from chriskinsman/github-action-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
201 lines (175 loc) · 6.66 KB
/
github.js
File metadata and controls
201 lines (175 loc) · 6.66 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const { createAppAuth } = require("@octokit/auth-app");
const { throttling } = require('@octokit/plugin-throttling');
const { retry } = require('@octokit/plugin-retry');
const { Octokit } = require("@octokit/rest");
const debug = require('debug')('action-dashboard:github');
const _ = require('lodash');
const dayjs = require('dayjs');
const runStatus = require('./runstatus');
const MyOctoKit = Octokit
.plugin(throttling)
.plugin(retry);
const _org = process.env.GITHUB_ORG;
const _user = process.env.GITHUB_USERNAME;
const _appId = process.env.GITHUB_APPID;
// Handles newlines \n in private key
const _privateKey = Buffer.from(process.env.GITHUB_APP_PRIVATEKEY || "", "base64").toString("utf-8");
const _clientId = process.env.GITHUB_APP_CLIENTID;
const _clientSecret = process.env.GITHUB_APP_CLIENTSECRET;
const _installationId = process.env.GITHUB_APP_INSTALLATIONID;
// Cache all workflows to speed up refresh
let _runs = [];
let _refreshingRuns = false;
const octokit = new MyOctoKit({
auth: {
appId: _appId,
privateKey: _privateKey,
clientId: _clientId,
clientSecret: _clientSecret,
installationId: _installationId
},
authStrategy: createAppAuth,
throttle: {
onRateLimit: (retryAfter, options) => {
console.error(`Request quota exhausted for request ${options.method} ${options.url}`);
if (options.request.retryCount === 0) {
// only retries once
console.error(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onAbuseLimit: (retryAfter, options) => {
console.error(`Abuse detected for request ${options.method} ${options.url}`);
}
}
});
// Allows us to use the dashboard for user based repos or org based repos
const _listRepos = _org ? octokit.repos.listForOrg : octokit.repos.listForUser;
const _owner = _org ? { org: _org } : { username: _user };
debug('Using owner:', _owner);
const GitHub = {}
GitHub.listRepos = async function listRepos() {
try {
const repos = await octokit.paginate(_listRepos, _owner);
return repos;
} catch (e) {
console.error('Error getting repos', e);
return [];
}
};
GitHub.listWorkflowsForRepo = async function listWorkflowsForRepo(repoName, repoOwner) {
try {
const workflows = await octokit.paginate(octokit.actions.listRepoWorkflows, { repo: repoName, owner: repoOwner });
return workflows;
} catch (e) {
console.error('Error getting workflows', e);
return [];
}
};
GitHub.getMostRecentRuns = async function getMostRecentRuns(repoOwner, repoName, workflowId) {
try {
const sevenDaysAgo = dayjs().subtract(7, 'day');
const runs = await octokit.paginate(octokit.actions.listWorkflowRuns, { repo: repoName, owner: repoOwner, workflow_id: workflowId });
if (runs.length > 0) {
const groupedRuns = _.groupBy(runs, 'head_branch')
const rows = _.reduce(groupedRuns, (result, runs, branch) => {
debug(`branch`, branch);
if (sevenDaysAgo.isBefore(dayjs(runs[0].created_at))) {
debug(`adding run.id: ${runs[0].id}`);
result.push({
runId: runs[0].id,
repo: runs[0].repository.name,
owner: repoOwner,
workflowId: workflowId,
runNumber: runs[0].run_number,
workflow: runs[0].name,
branch: runs[0].head_branch,
sha: runs[0].head_sha,
message: runs[0].head_commit.message,
committer: runs[0].head_commit.committer.name,
status: runs[0].status === 'completed' ? runs[0].conclusion : runs[0].status,
createdAt: runs[0].created_at,
updatedAt: runs[0].updated_at
});
}
else {
debug(`skipping run.id: ${runs[0].id} created_at: ${runs[0].created_at}`);
}
return result;
}, []);
debug(`most recent runs owner: ${repoOwner}, repo: ${repoName}, workflowId: ${workflowId}`, rows);
return rows;
}
else {
return [];
}
} catch (e) {
console.error('Error getting runs', e);
return [];
}
};
GitHub.refreshWorkflow = async function refreshWorkflow(repoOwner, repoName, workflowId) {
const runs = await GitHub.getMostRecentRuns(repoOwner, repoName, workflowId);
GitHub.mergeRuns(runs);
};
GitHub.mergeRuns = function mergeRuns(runs) {
// Merge into cache
runs.forEach((run) => {
console.dir(run);
const index = _.findIndex(_runs, { workflowId: run.workflowId, branch: run.branch });
if (index >= 0) {
_runs[index] = run;
}
else {
_runs.push(run);
}
runStatus.updatedRun(run);
});
debug('merged runs', _runs);
};
GitHub.refreshRuns = async function refreshRuns() {
// Prevent re-entrant calls
if (_refreshingRuns) {
return;
}
debug('Starting refreshing runs');
try {
_refreshingRuns = true;
const repos = await GitHub.listRepos();
for (const repo of repos) {
debug(`repo: ${repo.name}`);
const workflows = await GitHub.listWorkflowsForRepo(repo.name, repo.owner.login);
if (workflows.length > 0) {
for (const workflow of workflows) {
debug(`workflow: ${workflow.name}`);
const runs = await GitHub.getMostRecentRuns(repo.owner.login, repo.name, workflow.id);
// Not using apply or spread in case there are a large number of runs returned
GitHub.mergeRuns(runs);
}
}
}
} catch (e) {
console.error('Error getting initial data', e);
}
finally {
debug('Finished refreshing runs');
_refreshingRuns = false;
}
};
GitHub.getInitialData = function getInitialData() {
debug(`getInitialData _runs.length: ${_runs.length}`);
if (_runs.length === 0 && !_refreshingRuns) {
debug('getInitialData calling refreshRuns');
GitHub.refreshRuns();
}
return _runs;
};
if (!process.env.DOCKER_BUILD) {
debug('Performing initial refreshRuns');
// Load the initial set
GitHub.refreshRuns();
debug('Setting interval to refreshRuns at 15m');
// Refresh by default every fifteeen minutes
setInterval(GitHub.refreshRuns, 1000 * 60 * 15);
}
module.exports = GitHub;