This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
182 lines (155 loc) · 4.62 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
// For more information on building apps:
// https://probot.github.io/docs/
// To get your app running against GitHub, see:
// https://probot.github.io/docs/development/
// Helpers
const { parseAction, parseIssueNumbers } = require("./utils/parse");
const { printJSON } = require("./utils/utils");
// Queries
const { addIssueToProject, getIssueProjects } = require("./graphql/projects");
// Define actions
const syncEpic = "syncEpic";
// Create a map of actions
const actions = new Map();
actions.set(
syncEpic,
"syncEpic syncs an epic issue's labels, milestones, and projects to its task list issues."
);
const botName = "celbot";
/**
* This is the main entrypoint to your Probot app
* @param {import('probot').Probot} app
*/
module.exports = (app) => {
app.log.info("Yay, the app was loaded!");
app.on("issue_comment.created", async (context) => {
app.log.info(`
####################################################
Received issue comment event
`);
// Get the issue comment
const comment = context.payload.comment.body;
app.log.info(`Received comment: ${comment}`);
// Extract the action from the comment
const action = parseAction(comment, botName);
if (!action) {
app.log.info("Unable to parse action from comment");
return;
}
// Process the action
app.log.info(`Received ${action} action`);
switch (action) {
case syncEpic:
await handleSyncEpic(app, context);
break;
default:
app.log.info(`Received unknown action: ${action}`);
app.log.info("Available actions are:");
// Loop over the actions and print the available actions
actions.forEach((action, description) => {
app.log.info(` ${action}: ${description}`);
});
break;
}
app.log.info(`
Issue comment event complete
####################################################
`);
});
};
// NOTE: this only works for subissues that are in the same repo as the epic
// issue as the owner and repo are pulled from the context of the epic issue.
async function handleSyncEpic(app, context) {
// Get the issue details
let issue;
try {
issue = await context.octokit.issues.get(context.issue());
} catch (error) {
app.log.info(`Unable to get issue details: ${error}`);
return;
}
// Extract the labels, milestone, and projects
const labels = issue.data.labels.map((label) => label.name);
const milestone = issue.data.milestone ? issue.data.milestone.number : null;
// Get projects related to the repository
let projects;
try {
projects = await getIssueProjects(context, issue.data.node_id);
} catch (error) {
app.log.info(`Unable to get projects: ${error}`);
return;
}
// Extract the issue numbers from the task list
const issueNumbers = parseIssueNumbers(issue.data.body);
app.log.info(`handleSyncEpic extracted the following information:
labels: ${labels}
milestone: ${milestone}
projects: ${printJSON(projects)}
issueNumbers: ${issueNumbers}
`);
if (!issueNumbers) {
app.log.info("Now issue numbers found in the epic issue body");
return;
}
// Apply the labels, milestone, and projects to the related issues
for (const issueNumber of issueNumbers) {
try {
await applyAttributes(
app,
context,
issueNumber,
labels,
milestone,
projects
);
} catch (error) {
app.log.info(
`Unable to apply attributes for issue #${issueNumber}: ${error}`
);
}
}
}
async function applyAttributes(
app,
context,
issueNumber,
labels,
milestone,
projects
) {
// Apply the labels
if (labels.length > 0) {
app.log.info(`Applying labels: ${labels}`);
await context.octokit.issues.addLabels(
context.issue({ issue_number: issueNumber, labels })
);
} else {
app.log.info("No labels to apply");
}
// Apply the milestone
if (milestone) {
app.log.info(`Applying milestone: ${milestone}`);
await context.octokit.issues.update(
context.issue({ issue_number: issueNumber, milestone })
);
} else {
app.log.info("No milestone to apply");
}
// Get the issue for the node_id
const issue = await context.octokit.rest.issues.get({
owner: context.repo().owner,
repo: context.repo().repo,
issue_number: issueNumber,
});
// Apply the projects
for (const project of projects) {
app.log.info(`Applying project: ${project.title}`);
try {
await addIssueToProject(context, issue.data.node_id, project.id);
} catch (error) {
app.log.error(
`Error adding issue #${issueNumber} to project '${project.title}': ${error}`
);
}
}
}