-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (91 loc) · 2.46 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
94
95
96
97
98
99
100
101
const core = require("@actions/core");
const github = require("@actions/github");
const { graphql } = require("@octokit/graphql");
const run = async () => {
try {
const targetProjectId = core.getInput("target-project");
const accessToken = core.getInput("access-token");
const payload = github.context.payload;
const pullRequest = payload.pull_request;
const orgName = payload.organization.login;
const repositoryName = payload.repository.name;
const data = await getIssuesFromPullRequest(
pullRequest.number,
orgName,
repositoryName,
accessToken
);
const issueNodes =
data.repository.pullRequest.closingIssuesReferences.nodes;
if (issueNodes.length <= 0) {
core.info(
"No linked issues. Pull Request needs issue number with closing keyword in description. For example: Resolves #1234"
);
} else {
await Promise.all(
data.repository.pullRequest.closingIssuesReferences.nodes.map(
({ id }) => addIssueToProject(targetProjectId, id, accessToken)
)
);
core.info(
`Pull request issues have been added to the '${targetProjectId}' board.`
);
}
} catch (e) {
console.log("payload info")
console.log(github.context.payload)
console.log(e);
core.setFailed(e.message);
}
};
// Requests
async function addIssueToProject(projectId, contentId, accessToken) {
return graphql(addIssueToProjectMutation, {
projectId: projectId,
contentId: contentId,
headers: {
authorization: `bearer ${accessToken}`,
},
});
}
async function getIssuesFromPullRequest(
pullRequestNumber,
owner,
repository,
accessToken
) {
return graphql(getResolvingIssueQuery, {
owner: owner,
repository: repository,
number: pullRequestNumber,
headers: {
authorization: `bearer ${accessToken}`,
},
});
}
// Queries & Mutations
const getResolvingIssueQuery = `
query GetResolvingIssue($owner: String!, $repository: String!, $number: Int!) {
repository(owner:$owner, name:$repository) {
pullRequest(number: $number) {
id
title
closingIssuesReferences(last: 10) {
nodes {
id
}
}
}
}
}
`;
const addIssueToProjectMutation = `
mutation AddIssueToProject($projectId: ID!, $contentId: ID!) {
addProjectNextItem(input: {projectId: $projectId, contentId: $contentId}) {
projectNextItem {
id
}
}
}
`;
run();