forked from chriskinsman/github-action-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
121 lines (110 loc) · 3.14 KB
/
github.js
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
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");
class GitHub {
constructor(
_org,
_user,
_appId,
_privateKey,
_clientId,
_clientSecret,
_installationId
) {
this._org = _org;
this._user = _user;
this._appId = _appId;
this._privateKey = _privateKey;
this._clientId = _clientId;
this._clientSecret = _clientSecret;
this._installationId = _installationId;
const MyOctoKit = Octokit.plugin(throttling).plugin(retry);
this._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
this._listRepos = this._org
? this._octokit.repos.listForOrg
: this._octokit.repos.listForUser;
this._owner = this._org ? { org: this._org } : { username: this._user };
debug("Using owner:", this._owner);
}
async listRepos() {
try {
const repos = await this._octokit.paginate(this._listRepos, this._owner);
return repos;
} catch (e) {
console.error("Error getting repos", e);
return [];
}
}
async listWorkflowsForRepo(repoName, repoOwner) {
try {
const workflows = await this._octokit.paginate(
this._octokit.actions.listRepoWorkflows,
{ repo: repoName, owner: repoOwner }
);
return workflows;
} catch (e) {
console.error("Error getting workflows", e);
return [];
}
}
async getUsage(repoOwner, repoName, workflowId, run_id) {
try {
const usage = await this._octokit.actions.getWorkflowRunUsage({
repo: repoName,
owner: repoOwner,
workflow_id: workflowId,
run_id: run_id,
});
return usage.data;
} catch (e) {
console.error("Error getting usage", e);
return null;
}
}
async listWorkflowRuns(repoOwner, repoName, workflowId) {
try {
const runs = await this._octokit.paginate(
this._octokit.actions.listWorkflowRuns,
{
repo: repoName,
owner: repoOwner,
workflow_id: workflowId,
}
);
return runs;
} catch (e) {
console.error("Error getting runs", e);
return null;
}
}
}
module.exports = GitHub;