-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
200 lines (189 loc) · 8.39 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const github = require("@actions/github");
const core = require("@actions/core");
const graphql = require("@octokit/graphql");
async function run() {
const myToken = core.getInput("action-token");
const projectUrl = core.getInput("project-url");
const columnName = core.getInput("column-name");
const octokit = new github.GitHub(myToken);
const payload = github.context.payload;
const contentType = payload.issue !== undefined ? "Issue" : "PullRequest";
const contentId = contentType === "Issue" ?
payload.issue.id : payload.pull_request.id;
const contentNumber = contentType === "Issue" ?
payload.issue.number : payload.pull_request.number;
console.log(`Action triggered by ${contentType} #${contentNumber}`);
const info = await getColumnAndIssueInformation(columnName, projectUrl, myToken, contentId, contentType);
if (info.cardId != null) {
return `No action being taken. A card already exists in the project for the issue. Column:${info.currentColumnName}, cardId:${info.cardId}.`;
} else if(info.columnId != null) {
try {
return await createNewCard(octokit, info.columnId, contentId, contentType);
} catch (error) {
// In case the action was triggered by multiple events, skip if other actions already added it
const info = await getColumnAndIssueInformation(columnName, projectUrl, myToken, contentId, contentType);
if (info.cardId != null) {
return `New card already added, possibly with other in-flight actions. Column:${info.currentColumnName}, cardId:${info.cardId}.`
}
throw (error);
}
} else {
throw `Unable to find a columnId for the column ${columnName}, with Url:${projectUrl}`;
}
}
async function createNewCard(octokit, columnId, issueId, contentType){
console.log(`Attempting to create a card in column ${columnId}, for an issue with the corresponding id #${issueId}`);
await octokit.projects.createCard({
column_id: columnId,
content_id: issueId,
content_type: contentType
});
return `Successfully created a new card in column #${columnId} for an issue with the corresponding id:${issueId} !`;
}
async function getColumnAndIssueInformation(columnName, projectUrl, token, contentDatabaseId, contentType){
var columnId = null;
var cardId = null;
var currentColumnName = null;
var splitUrl = projectUrl.split("/");
var projectNumber = parseInt(splitUrl[6], 10);
// check if repo or org project
if(splitUrl[3] == "orgs"){
// Org url will be in the format: https://github.com/orgs/github/projects/910
var orgLogin = splitUrl[4];
console.log(`This project is configured at the org level. Org Login:${orgLogin}, project #${projectNumber}`);
var orgInformation = await getOrgInformation(orgLogin, projectNumber, token, contentType);
orgInformation.organization.project.columns.nodes.forEach(function(columnNode){
if(columnNode.name == columnName){
columnId = columnNode.databaseId;
}
// check each column if there is a card that exists for the issue
columnNode.cards.edges.forEach(function(card){
// card level
if (card.node.content != null){
// only issues and pull requests have content
if(card.node.content.databaseId == contentDatabaseId){
cardId = card.node.databaseId;
currentColumnName = columnNode.name;
}
}
});
});
} else {
// Repo url will be in the format: https://github.com/bbq-beets/konradpabjan-test/projects/1
var repoOwner = splitUrl[3];
var repoName = splitUrl[4];
console.log(`This project is configured at the repo level. Repo Owner:${repoOwner}, repo name:${repoName} project #${projectNumber}`);
var repoColumnInfo = await getRepoInformation(repoOwner, repoName, projectNumber, token, contentType);
repoColumnInfo.repository.project.columns.nodes.forEach(function(columnNode){
if(columnNode.name == columnName){
columnId = columnNode.databaseId;
}
// check each column if there is a card that exists for the issue
columnNode.cards.edges.forEach(function(card){
// card level
if (card.node.content != null){
// only issues and pull requests have content
if(card.node.content.databaseId == contentDatabaseId){
cardId = card.node.databaseId;
currentColumnName = columnNode.name;
}
}
});
});
}
return { columnId: columnId,
cardId: cardId,
currentColumnName: currentColumnName};
}
async function getOrgInformation(organizationLogin, projectNumber, token, contentType){
// GraphQL query to get all of the cards in each column for a project
// https://developer.github.com/v4/explorer/ is good to play around with
const response = await graphql(
`query ($loginVariable: String!, $projectVariable: Int!){
organization(login:$loginVariable) {
name
project(number:$projectVariable) {
databaseId
name
url
columns(first:100){
nodes{
databaseId
name
cards {
edges {
node {
databaseId
content {
... on ${contentType} {
databaseId
number
}
}
}
}
}
}
}
}
}
}`, {
loginVariable: organizationLogin,
projectVariable: projectNumber,
headers: {
authorization: `bearer ${token}`
}
});
return response;
}
async function getRepoInformation(repositoryOwner, repositoryName, projectNumber, token, contentType){
// GraphQL query to get all of the columns in a project that is setup at that org level
// https://developer.github.com/v4/explorer/ is good to play around with
const response = await graphql(
`query ($ownerVariable: String!, $nameVariable: String!, $projectVariable: Int!){
repository(owner:$ownerVariable, name:$nameVariable) {
project(number:$projectVariable){
id
number
databaseId
name
url
columns(first:100){
nodes{
databaseId
name
cards {
edges {
node {
databaseId
content {
... on ${contentType} {
databaseId
number
}
}
}
}
}
}
}
}
}
}`, {
ownerVariable: repositoryOwner,
nameVariable: repositoryName,
projectVariable: projectNumber,
headers: {
authorization: `bearer ${token}`
}
});
return response;
}
run()
.then(
(response) => { console.log(`Finished running: ${response}`); },
(error) => {
console.log(`#ERROR# ${error}`);
process.exit(1);
}
);