-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.js
72 lines (67 loc) · 2.28 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
/**
*
* @param { string } githubIssueUrl
* @returns {{owner: string, repo: string, issueNumber: number}}
*/
export const issueUrlComponents = (githubIssueUrl) => {
const url = new URL(githubIssueUrl);
const [, owner, repo, , issueNumber] = url.pathname.split('/')
return {owner, repo, issueNumber: parseInt(issueNumber, 10)};
}
/**
*
* @param {import("octokit").Octokit} octokitClient
* @param { string } githubIssueUrl
* @returns {Promise<string | undefined>}
*/
export const getIssueStatus = async (octokitClient, githubIssueUrl) => {
try {
const { owner, repo, issueNumber } = issueUrlComponents(githubIssueUrl);
const issue = await octokitClient.rest.issues.get({owner, repo, issue_number: issueNumber});
return issue.data.state;
} catch (e) {
return undefined
}
}
/**
* @param {import("octokit").Octokit} octokitClient
* @param {string} githubIssueUrl
* @param {string} comment
* @returns {Promise<void>}
*/
export const postCommentOnIssue = async (octokitClient, githubIssueUrl, comment) => {
const { owner, repo, issueNumber } = issueUrlComponents(githubIssueUrl);
await octokitClient.rest.issues.createComment({owner, repo, issue_number: issueNumber, body: comment});
}
/**
*
* @param {import("octokit").Octokit} octokitClient
* @param { string } githubIssueUrl
* @returns {Promise<string | undefined>}
*/
export const getLastComment = async (octokitClient, githubIssueUrl) => {
try {
const { owner, repo, issueNumber } = issueUrlComponents(githubIssueUrl);
const comments = await octokitClient.request(`GET /repos/${owner}/${repo}/issues/comments`, {
owner,
repo,
sort:'created',
direction: 'desc',
per_page: '1',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
});
if (comments.data.length > 0) {
// The last comment is the first item due to the sorting
const lastComment = comments.data[0];
return lastComment.body;
} else {
console.log('No comments found.');
return undefined;
}
} catch (e) {
console.error('Unable to get comments', e)
return undefined
}
}