forked from somnolentPumpkin/issue-card-creator-and-labeler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
93 lines (87 loc) · 2.34 KB
/
index.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
const core = require("@actions/core");
const github = require("@actions/github");
const githubToken = core.getInput("github-token");
const octokit = github.getOctokit(githubToken);
const fetch = require("node-fetch");
const GRAPH_URL = "https://api.github.com/graphql";
const graphHeaders = {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `token ${githubToken}`,
};
async function getBetaProjectId(org, projectNumber) {
const query = `query {
organization(login: "${org}") {
projectV2(number: ${projectNumber}) {
id
}
}
}`;
const result = await fetch(GRAPH_URL, {
method: "POST",
headers: graphHeaders,
body: JSON.stringify({
query: query,
}),
});
const data = await result.json();
console.log(`Response from getBetaProjectId: #${JSON.stringify(data)}`);
return data.data.organization.projectV2.id;
}
async function addIssueToBetaProject(projectId, issueId) {
const query = `mutation {
addProjectV2ItemById(input: {projectId: "${projectId}" contentId: "${issueId}"}) {
item {
id
}
}
}`;
const result = await fetch(GRAPH_URL, {
method: "POST",
headers: graphHeaders,
body: JSON.stringify({
query: query,
}),
});
return await result.json();
}
async function process(dataMap, payload) {
try {
for (let item in dataMap) {
console.log(
`Adding label: ${dataMap[item].label} to Issue #${payload.issue.number}`
);
await addLabelToIssue(
dataMap[item].org,
dataMap[item].repo,
payload.issue.number,
dataMap[item].label
);
console.log(`Getting ID for project #${dataMap[item].project}`);
const projectId = await getBetaProjectId(
dataMap[item].org,
dataMap[item].project
);
const issueId = payload.issue.node_id;
await addIssueToBetaProject(projectId, issueId);
}
} catch (error) {
console.error(error);
}
}
async function addLabelToIssue(org, repo, issueNumber, label) {
return await octokit.rest.issues.addLabels({
owner: org,
repo: repo,
issue_number: issueNumber,
labels: [label],
});
}
try {
(async () => {
const data = JSON.parse(core.getInput("actions")).data;
await process(data, github.context.payload);
})();
} catch (error) {
core.setFailed(error.message);
}