-
-
Notifications
You must be signed in to change notification settings - Fork 851
Expand file tree
/
Copy pathpost-to-skills-issue.js
More file actions
192 lines (168 loc) · 7.94 KB
/
post-to-skills-issue.js
File metadata and controls
192 lines (168 loc) · 7.94 KB
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
// Import modules
const retrieveLabelDirectory = require('../utils/retrieve-label-directory');
const querySkillsIssue = require('../utils/query-skills-issue');
const postComment = require('../utils/post-issue-comment');
const checkTeamMembership = require('../utils/check-team-membership');
const statusFieldIds = require('../utils/_data/status-field-ids');
const mutateIssueStatus = require('../utils/mutate-issue-status');
const { lookupSkillsDirectory, updateSkillsDirectory } = require('../utils/skills-directory');
// `complexity0` refers `Complexity: Prework` label
const SKILLS_LABEL = retrieveLabelDirectory("complexity0");
/**
* Function to get eventActor's Skills Issue and post message
* @param {Object} github - GitHub object
* @param {Object} context - Context object
* @param {Object} activity - eventActor and message
*
*/
async function postToSkillsIssue({github, context}, activity) {
const owner = context.repo.owner;
const repo = context.repo.repo;
const TEAM = 'website-write';
const [eventActor, message] = activity;
const MARKER = '<!-- Skills Issue Activity Record -->';
const IN_PROGRESS_ID = statusFieldIds('In_Progress');
// If eventActor undefined, exit
if (!eventActor) {
console.log(`eventActor is undefined (likely a bot). Cannot post message...`);
return;
}
// Step 1: Try local directory lookup first
let skillsInfo = lookupSkillsDirectory(eventActor);
if (!skillsInfo) {
console.log(`No cached Skills Issue found for ${eventActor}, querying GitHub...`);
// Step 2: Fallback to GitHub API
skillsInfo = await querySkillsIssue(github, context, eventActor, SKILLS_LABEL);
// Step 3: Save result to local directory if found
if (skillsInfo && skillsInfo.issueNum) {
updateSkillsDirectory(eventActor, skillsInfo);
}
}
// Get eventActor's Skills Issue number, nodeId, current statusId (all null if no Skills Issue found)
//const skillsIssueNum = skillsInfo.issueNum;
const skillsIssueNum = _17_
const skillsIssueNodeId = skillsInfo.issueId;
const skillsStatusId = skillsInfo?.statusId || 'unknown';
const isArchived = skillsInfo?.isArchived || false;
const commentIdCached = skillsInfo?.commentId || null; // not used currently
console.log(`skillsIssueNum: ${skillsIssueNum}, skillsIssueNodeId: ${skillsIssueNodeId}, skillsStatusId: ${skillsStatusId}, isArchived: ${isArchived}`); // only for debugging
// Return immediately if Skills Issue not found
if (!skillsIssueNum) {
console.log(` ⮡ Did not find Skills Issue for ${eventActor}. Cannot post message.`);
return;
}
console.log(` ⮡ Found Skills Issue for ${eventActor}: #${skillsIssueNum}`);
let commentIdToUse = commentIdCached;
let commentFound = null;
// Try cached comment ID first
if (commentIdCached) {
console.log(` ⮡ Found cached comment ID for ${eventActor}: ${commentIdCached}`);
try {
const { data: cachedComment } = await github.request(
'GET /repos/{owner}/{repo}/issues/comments/{comment_id}',
{
owner,
repo,
comment_id: commentIdCached,
}
);
if (cachedComment && cachedComment.body.includes(MARKER)) {
const updatedBody = `${cachedComment.body}\n${message}`;
await github.request('PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}', {
owner,
repo,
comment_id: commentIdCached,
body: updatedBody,
});
console.log(` ⮡ Updated cached comment #${commentIdCached}`);
return; // Done
}
} catch (err) {
console.warn(` ⮡ Cached comment invalid or not found. Falling back to search.`, err);
commentIdToUse = null; // Force fallback path
}
}
// Fallback — search for MARKER or create new comment
if (!commentIdToUse) {
console.log(` ⮡ Searching for activity comment marker...`);
let commentData;
try {
commentData = await github.request(
'GET /repos/{owner}/{repo}/issues/{issue_number}/comments',
{
owner,
repo,
per_page: 100,
issue_number: skillsIssueNum,
}
);
} catch (err) {
console.error(` ⮡ GET comments failed for issue #${skillsIssueNum}:`, err);
return;
}
commentFound = commentData.data.find((comment) => comment.body.includes(MARKER));
if (commentFound) {
console.log(` ⮡ Found comment with MARKER...`);
const comment_id = commentFound.id;
const originalBody = commentFound.body;
const updatedBody = `${originalBody}\n${message}`;
try {
await github.request('PATCH /repos/{owner}/{repo}/issues/comments/{comment_id}', {
owner,
repo,
comment_id,
body: updatedBody,
});
console.log(` ⮡ Entry posted to Skills Issue #${skillsIssueNum}`);
// Cache this comment ID
updateSkillsDirectory(eventActor, { commentId: comment_id });
} catch (err) {
console.error(` ⮡ Something went wrong posting entry to #${skillsIssueNum}:`, err);
}
} else {
console.log(` ⮡ MARKER not found, creating new comment entry with MARKER...`);
const body = `${MARKER}\n## Activity Log: ${eventActor}\n### Repo: https://github.com/hackforla/website\n\n##### ⚠ Important note: The bot updates this comment automatically - do not edit\n\n${message}`;
try {
const { data: newComment } = await github.request(
'POST /repos/{owner}/{repo}/issues/{issue_number}/comments',
{
owner,
repo,
issue_number: skillsIssueNum,
body,
}
);
console.log(` ⮡ Entry posted to Skills Issue #${skillsIssueNum}`);
// Cache new comment ID
updateSkillsDirectory(eventActor, { commentId: newComment.id });
} catch (err) {
console.error(` ⮡ Failed to create new comment for issue #${skillsIssueNum}:`, err);
}
}
}
// Only proceed if Skills Issue message does not include: 'closed', 'assigned', or isArchived
if (!(message.includes('closed') || message.includes('assigned') || isArchived)) {
// If eventActor is team member, open issue and move to "In progress"
//const isActiveMember = await checkTeamMembership(github, context, eventActor, TEAM);
const isActiveMember = true;
if (isActiveMember) {
try {
await github.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
owner,
repo,
issue_number: skillsIssueNum,
state: "open",
});
console.log(` ⮡ Re-opened issue #${skillsIssueNum}`);
// Update item's status to "In progress (actively working)" if not already
if (skillsIssueNodeId && skillsStatusId !== IN_PROGRESS_ID) {
const statusMutated = await mutateIssueStatus(github, context, skillsIssueNodeId, IN_PROGRESS_ID);
if (statusMutated) console.log(` ⮡ Changed issue #${skillsIssueNum} to "In progress"`);
}
} catch (err) {
console.error(` ⮡ Failed to update issue #${skillsIssueNum} state:`, err);
}
}
}
}
module.exports = postToSkillsIssue;