-
Notifications
You must be signed in to change notification settings - Fork 1
/
project-actions.js
224 lines (197 loc) · 9.15 KB
/
project-actions.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const { graphql } = require("@octokit/graphql");
const core = require('@actions/core');
const github = require('@actions/github');
const fetch = require('node-fetch')
const _ = require('lodash');
class ProjectActions {
async addItemToProject(octokit, context, projectNumber) {
const { itemType, itemNumber, itemId } = context;
if (!projectNumber) {
throw new Error(`projectNumber is required.`);
}
if (!itemId) {
throw new Error(`itemId is required.`);
}
const projectId = await this.findProjectId(octokit, context, projectNumber);
if (!projectId) {
throw new Error(`Error adding item to project: projectNumber ${projectNumber} not found`);
}
console.log(`Creating a new item for ${itemType} number ${itemNumber}, ID ${itemId} in project [${projectNumber}], owner "${context.owner}"`);
await this.createItem(octokit, projectId, itemId);
}
async removeIssuesFromProject(octokit, context, projectNumber) {
const { itemType, itemNumber } = context;
if (!projectNumber) {
throw new Error(`projectNumber is required.`);
}
if (!itemNumber) {
throw new Error(`itemNumber is required.`);
}
if (itemType !== 'Issue') {
// TODO Implement support for pull requests some day
throw new Error(`only type 'Issue' is supported for removal`);
}
const projectId = await this.findProjectId(octokit, context, projectNumber);
if (!projectId) {
throw new Error(`Error removing item from project: projectNumber ${projectNumber} not found`);
}
// 'itemNumber is the issue number in this context
console.log(`findProjectItemsForIssueNumber(octokit, ${context.owner}, ${context.repo}, ${itemNumber})`);
const projectItemIds = await this.findProjectItemsForIssueNumber(octokit, context.owner, context.repo, itemNumber);
console.log(`Removing items ${JSON.stringify(projectItemIds)} from project [${projectNumber}], owner "${context.owner}"`);
projectItemIds.forEach(async (projectItemId) => {
await this.removeItem(octokit, projectId, projectItemId);
});
}
async removeItem(octokit, projectId, itemId) {
try {
const mutation = `
mutation deleteItem($projectId: ID!, $itemId: ID!) {
deleteProjectV2Item(input: {
projectId: $projectId
itemId: $itemId
}) {
deletedItemId
}
}`
const params = {projectId: projectId, itemId: itemId};
console.log(`Delete item mutation:\n${mutation}`);
console.log(`Params: ${JSON.stringify(params)}`);
// Octokit will throw an error if GraphQL returns any error messages
const response = await octokit(mutation, params);
console.log(`Remove item response:\n${JSON.stringify(response)}`);
return _.get(response, 'deleteProjectV2Item.deletedItemId');
} catch (error) {
throw new Error(`Error removing item with ID '${itemId}' in project ${projectId}: ${error.message}`);
}
}
async findProjectId(octokit, context, projectNumber) {
try {
const query = `query findProjectId($owner: String!, $projectNumber: Int!) {
organization(login: $owner) {
projectV2(number: $projectNumber) {
id
}
}
}`;
const params = { owner: context.owner, projectNumber: projectNumber };
console.log(`Query for project ID:\n${query}`);
console.log(`Params: ${JSON.stringify(params)}`);
const response = await octokit(query, params);
console.log(`Response from query for project ID:\n${JSON.stringify(response, null, 2)}`);
return _.get(response, 'organization.projectV2.id');
} catch (error) {
throw new Error(`Error querying project ID for project number ${projectNumber}: ${error.message} \n error.request: ${JSON.stringify(error.request)}`);
}
}
async findProjectItemsForIssueNumber(octokit, owner, repo, issueNumber) {
try {
const query = `query findProjectItemsForIssueNumber($owner: String!, $repo: String!, $issueNumber:Int!) {
viewer {
organization(login:$owner) {
repository(name:$repo) {
issue(number:$issueNumber) {
projectItems(first:50) {
nodes {
id
}
}
}
}
}
}
}`;
const params = { owner: owner, repo: repo, issueNumber: issueNumber };
console.log(`Query for findItemByIssueNumber:\n${query}`);
console.log(`Params: ${JSON.stringify(params)}`);
const response = await octokit(query, params);
console.log(`Response from query for project items:\n${JSON.stringify(response, null, 2)}`);
const projectItems = _.get(response, 'viewer.organization.repository.issue.projectItems.nodes') || [];
if (projectItems.length == 50) {
throw new Error(`Too many project items for issue number ${issueNumber}`);
}
const projectItemIds = projectItems.map((item) => {
return item['id'];
});
return projectItemIds;
} catch (error) {
throw new Error(`Error querying project items for issue number ${issueNumber}: ${error.message} \n error.request: ${JSON.stringify(error.request)}`);
}
}
async createItem(octokit, projectId, contentId) {
try {
const mutation = `
mutation createItem($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {projectId: $projectId contentId: $contentId}) {
item {
id
}
}
}`;
console.log(`Create item mutation:\n${mutation}`);
// Octokit will throw an error if GraphQL returns any error messages
const response = await octokit(mutation, {projectId: projectId, contentId: contentId});
console.log(`Create item response:\n${JSON.stringify(response)}`);
return _.get(response, 'addProjectV2ItemById.item.id');
} catch (error) {
throw new Error(`Error creating item for item ID [${contentId}] in project ${projectId}: ${error.message}`);
}
}
normalizedGithubContext(githubContext) {
const { repository, label, action, issue, pull_request } = githubContext.payload;
const context = {
owner: repository && repository.owner && repository.owner.login,
repo: repository && repository.name,
label: label && label.name,
action: action,
}
if (githubContext.eventName == "issues") {
context.itemType = 'Issue';
context.itemNumber = issue && issue.number;
context.itemId = issue && issue.node_id;
}
else if (githubContext.eventName == "pull_request") {
context.itemType = 'Pull request';
context.itemNumber = pull_request && pull_request.number;
context.itemId = pull_request && pull_request.node_id;
}
return context;
}
getConfigs() {
return JSON.parse(core.getInput('config')) || [];
}
async run() {
console.log('Running');
const baseUrl = process.env.GRAPHQL_API_BASE || 'https://api.github.com'
const headers = {
Authorization: `Bearer ${process.env.PAT_TOKEN || process.env.GITHUB_TOKEN}`
}
const octokit = graphql.defaults({
baseUrl,
headers,
});
try {
const configs = this.getConfigs();
console.log(`Using config: ${JSON.stringify(configs)}`);
const context = this.normalizedGithubContext(github.context);
console.log(`Context: ${JSON.stringify(context)}`);
if (context.action === "labeled") {
for (const config of configs) {
if (context.label === config.label) {
await this.addItemToProject(octokit, context, config.projectNumber);
}
};
} else if (context.action == "unlabeled") {
for (const config of configs) {
if (context.label === config.label) {
await this.removeIssuesFromProject(octokit, context, config.projectNumber);
}
};
}
} catch (error) {
const ghContext = JSON.stringify(github.context, undefined, 2);
core.setFailed(`Project label assigner action failed with error: ${error.message}\n Event context:\n\n${ghContext}`);
}
}
}
module.exports = ProjectActions;