-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub-client.ts
116 lines (97 loc) · 3.51 KB
/
github-client.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
import * as core from '@actions/core';
import { context, getOctokit } from '@actions/github';
let githubToken: string;
let issueTitle: string;
let issueLabel: string | undefined;
try {
githubToken = core.getInput('github-token');
issueTitle = core.getInput('issue-title', { required: true });
issueLabel = core.getInput('issue-label');
} catch (error) {
core.setFailed(error.message);
}
/**
* Client for handling `octokit` requests.
* Provides easier API for response data.
*/
class GithubClient {
private octokit: ReturnType<typeof getOctokit>;
/** Indicates how many times failed request is retried */
private MAX_RETRIES = 30;
/** Indicates how many seconds should be waited before failed request is retried */
private RETRY_SEEP_TIME_MS = 10000;
constructor() {
this.octokit = getOctokit(githubToken);
}
private async requestAndRetry<T>(request: () => Promise<T>): Promise<T> {
for (let retryCount = 1; retryCount <= this.MAX_RETRIES; retryCount++) {
try {
return await request();
} catch (error) {
core.info(
`Request failed. Retrying ${retryCount}/${this.MAX_RETRIES}.`
);
await sleep(this.RETRY_SEEP_TIME_MS);
}
}
return await request();
}
/**
* Post results to existing issue as comment or create new issue using
* results as body
*/
async postResults(body: string): Promise<void> {
const existingIssue = await this.getExistingIssue();
if (existingIssue === undefined) {
core.info('No existing issue found. Creating new one.');
return this.createIssue(body);
}
core.info(`Reusing existing issue #${existingIssue}`);
await this.requestAndRetry(() =>
this.octokit.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: existingIssue,
body,
})
);
}
private async getExistingIssue(): Promise<number | undefined> {
// Look for existing issues based on issue label if present. Otherwise use issue title
const query = issueLabel
? `label:"${issueLabel}"`
: `${issueTitle} in:title`;
const response = await this.requestAndRetry(() =>
this.octokit.search.issuesAndPullRequests({
sort: 'created',
order: 'desc',
q: [
query,
'is:issue',
'is:open',
`repo:${context.repo.owner}/${context.repo.repo}`,
].join(' '),
})
);
const { items } = response.data;
core.info(`Found ${items.length} open issues matcing query (${query})`);
// In case of many matches use the latest issue
const issue = items[0];
return issue ? issue.number : undefined;
}
private async createIssue(body: string): Promise<void> {
await this.requestAndRetry(() =>
this.octokit.issues.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: issueTitle,
labels: issueLabel ? [issueLabel] : undefined,
body,
})
);
}
}
async function sleep(timeMs: number): Promise<void> {
await new Promise(resolve => setTimeout(resolve, timeMs));
}
export default new GithubClient();