-
Notifications
You must be signed in to change notification settings - Fork 2
/
GitHubContext.ts
111 lines (94 loc) 路 2.74 KB
/
GitHubContext.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
import {argumentContext} from "./ArgumentContext";
import {Octokit} from "@octokit/rest";
import {context as github} from "@actions/github";
const repoContext = require("./RepoContext")
const octokit = new Octokit({auth: process.env.PRIVAT_READ_TOKEN ?? process.env.GITHUB_TOKEN})
/**
*
* @param page
* @returns Up to 100 issues at a time
*/
async function getIssues(page: number) {
return octokit.issues.listForRepo({
...repoContext.repoObject,
per_page: 100,
state: "all",
page
})
}
/**
* @returns raw diff data
*/
async function getDiff() {
if (github.payload.commits.length) {
console.log(`${github.payload.commits.length} commits pushed`)
return await octokit.repos.compareCommitsWithBasehead({
...repoContext.repoObject,
// if payload.created is true it is most likely a new repo. But we don't want the initial commit to trigger create new issues, so it's okay if payload.before is 'empty'
basehead: `${github.payload.before}...${process.env.GITHUB_SHA}`,
headers: {Accept: 'application/vnd.github.diff'},
method: 'GET'
});
} else {
console.log('One commit added')
const commit = await octokit.repos.getCommit({
...repoContext.repoObject,
ref: github.payload.head_commit.id,
});
if(commit.data.parents.length > 1) // we don't want merges to add issues (again)
return
return await octokit.repos.getCommit({
...repoContext.repoObject,
ref: github.payload.head_commit.id,
headers: {Accept: 'application/vnd.github.diff'},
method: 'GET'
});
}
}
/**
* @returns raw diff data
*/
async function getDiffFile() {
// TODO Merge methods getDiffFile and getDiff
try {
const diff = await getDiff();
if (diff)
return diff.data
} catch (e) {
console.error(e)
console.error("Diff file is too big")
return
}
}
async function getDefaultLabel() {
const newLabel = {
...repoContext.repoObject,
name: 'todo :spiral_notepad:',
color: '00B0D8',
request: {retries: 0},
};
try {
await octokit.issues.createLabel(newLabel)
} catch (e) {
// Label already exists, ignore
}
return newLabel.name
}
async function getLabels() {
if (argumentContext.label === false)
return [];
if (argumentContext.label === true)
return [await getDefaultLabel()]
else
return argumentContext.label
}
function getUsername() {
return github.payload.head_commit?.author?.username
}
module.exports = {
getIssues,
getDiffFile,
getLabels,
getUsername,
octokit
}