forked from kin/gh-action-move-issues-to-column
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (125 loc) · 3.98 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const core = require("@actions/core");
const github = require("@actions/github");
const { graphql } = require("@octokit/graphql");
const accessToken = core.getInput("access-token");
const columnIdQuery = `query columns($owner: String!, $name: String!, $projectName: String!) {
repository(owner: $owner, name: $name) {
projects(search: $projectName, last: 1) {
edges {
node {
columns(first: 20) {
edges {
node {
id
name
}
}
}
}
}
}
}
}`;
const cardIdsForIssue = `query issues($issueId: ID!) {
node(id: $issueId) {
... on Issue {
projectCards(first: 5) {
edges {
node {
id
}
}
}
}
}
}`;
(async function () {
try {
// Set input constants
const inputIssues = JSON.parse(core.getInput("issues"));
const project = core.getInput("project-name");
const columnName = core.getInput("target-column");
const columnId = core.getInput("target-column-id");
const payload = inputIssues.length ? inputIssues : github.context.payload;
const issues = Array.isArray(payload) ? payload : [payload];
core.info(`Issues: ${JSON.stringify(issues, null, 2)}`);
// Early return if a member of payload doesn't respond to `issue`
if (typeof issues[0].issue === "undefined") {
core.info("No issues to move");
return;
}
const repoUrl = issues[0].issue.repository_url;
const splitUrl = repoUrl.split("/");
const repoOwner = splitUrl[4];
const repo = splitUrl[5];
// Find target column
const edges = await getColumnIds(repoOwner, repo, project);
const columns = edges
.flatMap((p) => p.node.columns.edges)
.map((c) => c.node);
const targetColumn = columnId
? columns.find((c) => c.id === columnId)
: columns.find((c) => c.name.toLowerCase() === columnName.toLowerCase());
// Find card ids for issues
const issueIds = issues.map((i) => i.issue.node_id);
const cardPromises = await Promise.all(issueIds.map(getCardsForIssue));
const cardNodes = cardPromises.flatMap((c) => c.node);
// Filter nodes before proceeding in case the issue does not have card associated.
const cardIds = cardNodes
.filter((node) => node.projectCards != null)
.flatMap((filtered) => filtered.projectCards.edges)
.flatMap((e) => e.node.id);
// Update cards only if the column exists
if (typeof targetColumn === "undefined") {
core.setFailed(
"Target column does not exist on project. Please use a different column selector:\n" +
`target-column: ${JSON.stringify(columnName)}\n` +
`target-column-id: ${JSON.stringify(columnId)}`
);
return;
}
const targetColumnId = targetColumn.id;
core.info(
`Moving ${cardIds.length} cards to ${columnName} (node_id: ${targetColumnId}) in project ${project}`
);
cardIds.forEach((cardId) => {
moveCardToColumn(cardId, targetColumnId);
core.info(`Moving cardId: ${cardId}`);
});
} catch (error) {
core.setFailed(error.message);
}
})();
async function getColumnIds(owner, repo, projectName) {
const { repository } = await graphql(columnIdQuery, {
owner: owner,
name: repo,
projectName: projectName,
headers: {
authorization: `bearer ${accessToken}`,
},
});
return repository.projects.edges;
}
async function getCardsForIssue(issueId) {
return graphql(cardIdsForIssue, {
issueId: issueId,
headers: {
authorization: `bearer ${accessToken}`,
},
});
}
const updateCardColumnMutation = `mutation updateProjectCard($cardId: ID!, $columnId: ID!) {
moveProjectCard(input:{cardId: $cardId, columnId: $columnId}) {
clientMutationId
}
}`;
async function moveCardToColumn(cardId, columnId) {
return graphql(updateCardColumnMutation, {
cardId: cardId,
columnId: columnId,
headers: {
authorization: `bearer ${accessToken}`,
},
});
}