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
177 lines (161 loc) · 4.91 KB
/
github.js
File metadata and controls
177 lines (161 loc) · 4.91 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
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 {Buffer} = require('buffer');
const fs = require('fs');
const unzip = require('unzipper');
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 listWorkflowRunArtifacts(repoOwner, repoName, runId) {
try {
const workflowRunArtifacts = await this._octokit.actions.listWorkflowRunArtifacts(
{owner: repoOwner, repo: repoName, run_id: runId}
);
const artifact = workflowRunArtifacts.data.artifacts.find(el => el.name == "arvos-report");
if (!artifact) {
return null
}
const response = await this._octokit.actions.downloadArtifact({
owner: repoOwner,
repo: repoName,
artifact_id: artifact.id,
archive_format: "zip",
})
if (response.status == 200) {
await fs.promises.writeFile('/tmp/arvos-report.zip', Buffer.from(response.data));
const fsStream = fs.createReadStream('/tmp/arvos-report.zip')
const unzipper = fsStream.pipe(unzip.Extract({ path: '/tmp/' }));
await new Promise((resolve, reject) => {
const endOnError = (error) => reject(error);
unzipper.on('close', () => resolve());
fsStream.on('error', endOnError);
unzipper.on('error', endOnError);
});
} else {
console.log(`ERROR >> ${res.status}`);
}
return workflowRunArtifacts;
} catch (e) {
console.error('Error getting workflow run artifacts', e)
return [];
}
}
async downloadWorflowArtifact(repoOwner, repoName, artifactId) {
try {
const output = await this._octokit.actions.downloadArtifact({
owner: repoOwner,
repo: repoName,
artifact_id: artifactId,
archive_format: "zip",
});
return output;
} catch (e) {
console.error("Error getting runs", 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;