@@ -6,10 +6,6 @@ const checkTeamMembership = require('../utils/check-team-membership');
66const statusFieldIds = require ( '../utils/_data/status-field-ids' ) ;
77const mutateIssueStatus = require ( '../utils/mutate-issue-status' ) ;
88
9- // Global variables
10- var github ;
11- var context ;
12-
139// `complexity0` refers `Complexity: Prework` label
1410const 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
102109module . exports = postToSkillsIssue ;
0 commit comments