generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
93 lines (82 loc) · 2.32 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 fetch = require("node-fetch");
async function github_query(github_token, query, variables) {
return fetch("https://api.github.com/graphql", {
method: "POST",
body: JSON.stringify({ query, variables }),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `bearer ${github_token}`,
},
}).then(function (response) {
return response.json();
});
}
async function run() {
try {
const issue = core.getInput("issue");
const repository = core.getInput("repository");
const project = core.getInput("project");
const github_token = core.getInput("github_token");
const owner = repository.split("/")[0];
const name = repository.split("/")[1];
const get_issue_id = `
query($owner:String!, $name:String!, $number:Int!){
repository(owner: $owner, name: $name) {
issue(number:$number) {
id
}
}
}`;
const issue_vars = {
owner,
name,
number: parseInt(issue),
};
const issue_resp = await github_query(
github_token,
get_issue_id,
issue_vars,
);
console.log(JSON.stringify(issue_resp));
const issue_id = issue_resp["data"]["repository"]["issue"]["id"];
const get_project_id = `
query($owner:String!, $name:String!, $number:Int!){
repository(owner: $owner, name: $name) {
project(number: $number) {
id
}
}
}`;
const project_vars = {
owner,
name,
number: parseInt(project),
};
const project_resp = await github_query(
github_token,
get_project_id,
project_vars,
);
console.log(JSON.stringify(project_resp));
const project_id = project_resp["data"]["repository"]["project"]["id"];
console.log(`Adding issue ${issue} to project ${project}`);
console.log("");
query = `
mutation($issueId:ID!, $projectId:ID!) {
updateIssue(input:{id:$issueId, projectIds:[$projectId]}) {
issue {
id
}
}
}`;
variables = { issueId: issue_id, projectId: project_id };
response = await github_query(github_token, query, variables);
console.log(JSON.stringify(response));
console.log(`Done!`);
} catch (error) {
core.setFailed(error.message);
}
}
run();