forked from alex-page/github-project-automation-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
120 lines (106 loc) · 3.03 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
const core = require('@actions/core');
const github = require('@actions/github');
const token = core.getInput('repo-token');
const project = core.getInput('project');
const column = core.getInput('column');
const octokit = new github.GitHub(token);
const getData = () => {
const {eventName, payload} = github.context;
if (eventName !== 'pull_request' && eventName !== 'issues') {
throw new Error(`Only pull requests or issues allowed, received:\n${eventName}`);
}
const githubData = eventName === 'issues' ?
payload.issue :
payload.pull_request;
return {
eventName,
action: payload.action,
nodeId: githubData.node_id,
url: githubData.html_url
};
};
(async () => {
try {
const {eventName, action, nodeId, url} = getData();
// Get the column ID from searching for the project and card Id if it exists
const fetchColumnQuery = `query {
resource( url: "${url}" ) {
... on ${eventName === 'issues' ? 'Issue' : 'PullRequest'} {
projectCards {
nodes {
id
}
}
repository {
projects( search: "${project}", first: 10, states: [OPEN] ) {
nodes {
id
columns( first: 100 ) {
nodes {
id
name
}
}
}
}
owner {
... on ProjectOwner {
projects( search: "${project}", first: 10, states: [OPEN] ) {
nodes {
id
columns( first: 100 ) {
nodes {
id
name
}
}
}
}
}
}
}
}
}
}`;
const {resource} = await octokit.graphql(fetchColumnQuery);
// All the matching projects found
const repoProjects = resource.repository.projects.nodes || [];
const orgProjects = (resource.repository.owner &&
resource.repository.owner.projects &&
resource.repository.owner.projects.nodes) ||
[];
// Search the projects for columns with a name that matches
const columns = [...repoProjects, ...orgProjects]
.flatMap(projects => {
return projects.columns.nodes ?
projects.columns.nodes.filter(projectColumn => projectColumn.name === column) :
[];
});
const cardId = (resource.projectCards.nodes &&
resource.projectCards.nodes[0] &&
resource.projectCards.nodes[0].id) ||
null;
if (columns.length === 0) {
throw new Error(`Could not find ${column} in ${project}`);
}
// If a card already exists, move it to the column
if (cardId) {
await Promise.all(
columns.map(column => octokit.graphql(`mutation {
moveProjectCard( input: { cardId: "${cardId}", columnId: "${column.id}"
}) { clientMutationId } }`))
);
// If the card does not exist, add it to the column
} else {
await Promise.all(
columns.map(column => octokit.graphql(`mutation {
addProjectCard( input: { contentId: "${nodeId}", projectColumnId: "${column.id}"
}) { clientMutationId } }`))
);
}
console.log(`✅ ${action === 'opened' ? 'Added' : 'Moved'} card to ${column} in ${project}`);
} catch (error) {
core.error(error);
core.setFailed(error.message);
}
})();