Skip to content

Commit 926d2bf

Browse files
committed
add in error handling, minor changes
1 parent ef5987a commit 926d2bf

4 files changed

Lines changed: 48 additions & 50 deletions

File tree

.github/workflows/activity-trigger.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ jobs:
2626
with:
2727
github-token: ${{ secrets.HACKFORLA_GRAPHQL_TOKEN }}
2828
script: |
29-
const script = require('./github-actions/activity-trigger/activity-trigger.js')
30-
const activities = script({g: github, c: context})
31-
return activities
29+
const script = require('./github-actions/activity-trigger/activity-trigger.js');
30+
const activities = script({github, context});
31+
return activities;
3232
3333
- if: ${{ steps.gather-event-details.outputs.result != '[]' }}
3434
name: Post to Skills Issue
@@ -40,5 +40,5 @@ jobs:
4040
const activities = JSON.parse(${{ steps.gather-event-details.outputs.result }});
4141
const script = require('./github-actions/activity-trigger/post-to-skills-issue.js');
4242
for (const activity of activities) {
43-
await script({g: github, c: context}, activity);
43+
await script({github, context}, activity);
4444
}

github-actions/activity-trigger/activity-trigger.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
// Global variables
2-
var github;
3-
var context;
4-
5-
6-
71
/**
82
* This function parses the triggered event to determine the trigger eventName and eventAction
93
* and from this information decide the eventActor (user who is credited for the event).
104
* @param {Object} github - GitHub object from function calling activity-trigger.js
115
* @param {Object} context - Context of the function calling activity-trigger.js
126
* @returns {Object} - An object containing the eventActor and a message
137
*/
14-
async function activityTrigger({g, c}) {
15-
16-
github = g;
17-
context = c;
8+
async function activityTrigger({github, context}) {
189

1910
let issueNum = '';
2011
let assignee = '';
@@ -110,7 +101,7 @@ async function activityTrigger({g, c}) {
110101
}
111102

112103
// Only if issue is closed, and eventActor != assignee, return assignee and message
113-
if (eventAction.includes('Closed-') && (eventActor != assignee)) {
104+
if (eventAction.includes('Closed-') && (eventActor !== assignee)) {
114105
message = `- ${assignee} issue ${action}: ${eventUrl} at ${localTime}`;
115106
activities.push([assignee, message]);
116107
}

github-actions/activity-trigger/post-to-skills-issue.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ const checkTeamMembership = require('../utils/check-team-membership');
66
const statusFieldIds = require('../utils/_data/status-field-ids');
77
const mutateIssueStatus = require('../utils/mutate-issue-status');
88

9-
// Global variables
10-
var github;
11-
var context;
12-
139
// `complexity0` refers `Complexity: Prework` label
1410
const SKILLS_LABEL = retrieveLabelDirectory("complexity0");
1511

@@ -19,20 +15,16 @@ const SKILLS_LABEL = retrieveLabelDirectory("complexity0");
1915
* Function to get eventActor's Skills Issue and post message
2016
* @param {Object} github - GitHub object
2117
* @param {Object} context - Context object
22-
* @param {Object} package - eventActor and message
18+
* @param {Object} activity - username and message
2319
*
2420
*/
25-
async function postToSkillsIssue({g, c}, activity) {
26-
27-
github = g;
28-
context = c;
21+
async function postToSkillsIssue({github, context}, activity) {
2922

3023
const owner = context.repo.owner;
3124
const repo = context.repo.repo;
3225
const TEAM = 'website-write';
3326

34-
const username = activity[0];
35-
const message = activity[1];
27+
const [username, message] = activity;
3628
const MARKER = '<!-- Skills Issue Activity Record -->';
3729
const IN_PROGRESS_ID = statusFieldIds('In_Progress');
3830

@@ -44,19 +36,25 @@ async function postToSkillsIssue({g, c}, activity) {
4436

4537
// Return immediately if Skills Issue not found
4638
if (skillsIssueNum) {
47-
console.log(`Found Skills Issue for ${username}: ${skillsIssueNum}`);
39+
console.log(`Found Skills Issue for ${username}: #${skillsIssueNum}`);
4840
} else {
4941
console.log(`Did not find Skills Issue for ${username}. Cannot post message.`);
5042
return;
5143
}
5244

5345
// Get all comments from the Skills Issue
54-
// https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#list-issue-comments
55-
const commentData = await github.request('GET /repos/{owner}/{repo}/issues/{issueNum}/comments', {
56-
owner,
57-
repo,
58-
issueNum: skillsIssueNum,
59-
});
46+
let commentData;
47+
try {
48+
// https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#list-issue-comments
49+
commentData = await github.request('GET /repos/{owner}/{repo}/issues/{issue_number}/comments', {
50+
owner,
51+
repo,
52+
issue_number: skillsIssueNum,
53+
});
54+
} catch (err) {
55+
console.error(`GET comments failed for issue #${skillsIssueNum}:`, err);
56+
return;
57+
}
6058

6159
// Find the comment that includes the MARKER text and append message
6260
const commentFound = commentData.data.find(comment => comment.body.includes(MARKER));
@@ -67,36 +65,45 @@ async function postToSkillsIssue({g, c}, activity) {
6765
const commentId = commentFoundId;
6866
const originalBody = commentFound.body;
6967
const updatedBody = `${originalBody}\n${message}`;
70-
// https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#update-an-issue-comment
71-
await github.request('PATCH /repos/{owner}/{repo}/issues/comments/{commentId}', {
72-
owner,
73-
repo,
74-
commentId,
75-
body: updatedBody
76-
});
68+
try {
69+
// https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#update-an-issue-comment
70+
await github.request('PATCH /repos/{owner}/{repo}/issues/comments/{commentId}', {
71+
owner,
72+
repo,
73+
commentId,
74+
body: updatedBody
75+
});
76+
} catch (err) {
77+
console.error(`Something went wrong updating comment:`, err);
78+
}
79+
7780
} else {
7881
console.log(`MARKER not found in comments, creating new comment with MARKER...`);
7982
const body = `${MARKER}\n## Activity Log: ${username}\n### Repo: https://github.com/hackforla/website\n\n##### ⚠ Important note: The bot updates this comment automatically - do not edit\n\n${message}`;
8083
await postComment(skillsIssueNum, body, github, context);
8184
}
8285

8386
// If eventActor is team member, open issue and move to "In progress". Else, close issue
84-
const isActiveMember = await checkTeamMembership(github, username, TEAM);
87+
const isActiveMember = await checkTeamMembership(github, context, username, TEAM);
8588
let skillsIssueState = "closed";
8689

8790
if (isActiveMember) {
8891
skillsIssueState = "open";
8992
// Update item's status to "In progress (actively working)" if not already
90-
if (skillsStatusId != IN_PROGRESS_ID) {
93+
if (skillsIssueNodeId && skillsStatusId !== IN_PROGRESS_ID) {
9194
await mutateIssueStatus(github, context, skillsIssueNodeId, IN_PROGRESS_ID);
9295
}
9396
}
94-
await github.request('PATCH /repos/{owner}/{repo}/issues/{issueNum}', {
95-
owner,
96-
repo,
97-
issueNum: skillsIssueNum,
98-
state: skillsIssueState,
99-
});
97+
try {
98+
await github.request('PATCH /repos/{owner}/{repo}/issues/{issue_number}', {
99+
owner,
100+
repo,
101+
issue_number: skillsIssueNum,
102+
state: skillsIssueState,
103+
});
104+
} catch (err) {
105+
console.error(`Failed to update issue #${skillsIssueNum} state:`, err)
106+
}
100107
}
101108

102109
module.exports = postToSkillsIssue;

github-actions/utils/check-team-membership.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ Lack of permission will result in a 403 error.
1111
docs on printing context information into the log.
1212
*/
1313

14-
async function isMemberOfTeam(github, githubUsername, team) {
14+
async function isMemberOfTeam(github, context, githubUsername, team) {
1515
try {
1616
await github.rest.teams.getMembershipForUserInOrg({
17-
org: 'hackforla',
17+
org: context.repo.owner,
1818
team_slug: team,
1919
username: githubUsername
2020
});

0 commit comments

Comments
 (0)