-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
144 lines (124 loc) · 4.04 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
const core = require("@actions/core");
const github = require("@actions/github");
const { graphql } = require("@octokit/graphql");
try {
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
}
}
}
}
}
}
}
}`;
async function getColumnIds(owner, repo, projectName) {
return graphql(columnIdQuery, {
owner: owner,
name: repo,
projectName: projectName,
headers: {
authorization: `bearer ${accessToken}`,
}
});
};
const cardIdsForIssue = `query issues($issueId: ID!) {
node(id: $issueId) {
... on Issue {
projectCards(first: 5) {
edges {
node {
id
}
}
}
}
}
}`;
async function getCardsForIssue(issueId) {
return graphql(cardIdsForIssue, {
issueId: issueId,
headers: {
authorization: `bearer ${accessToken}`,
}
});
}
const updateCardColumnMutation = `mutation updateProjectCard($cardId: String!, $columnId: String!) {
moveProjectCard(input:{cardId: $cardId, columnId: $columnId}) {
clientMutationId
}
}`;
async function moveCardToColumn(cardId, columnId) {
return graphql(updateCardColumnMutation, {
cardId: cardId,
columnId: columnId,
headers: {
authorization: `bearer ${accessToken}`,
}
});
}
const run = async () => {
try {
// Set input constants
const inputIssues = core.getInput("issues");
const parsedInput = JSON.parse(inputIssues);
const project = core.getInput("project-name");
const columnName = core.getInput("target-column");
const columnId = core.getInput("target-column-id");
const payload = parsedInput.length != 0 ? parsedInput : github.context.payload;
core.info(`payload: ${payload}`);
const issues = Array.isArray(payload) ? payload : [payload];
const issueSample = issues[0].issue;
// Early return if a member of payload doesn't respond to `issue`
if (typeof issueSample === 'undefined') {
core.info('No issues to move');
return;
}
const repoUrl = issueSample.repository_url;
const splitUrl = repoUrl.split('/');
const repoOwner = splitUrl[4];
const repo = splitUrl[5];
// Find target column
const { repository: { projects: { edges: projectEdges } } }= await getColumnIds(repoOwner, repo, project);
const columns = projectEdges.flatMap(p => p.node.columns.edges).map(c => c.node);
const targetColumn = if (typeof columnId !== 'undefined') {
columns.find(c => c.id == columnId);
} else {
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 name");
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);
}
};
run();
}
catch (error) {
core.setFailed(error.message);
}