@@ -10,10 +10,7 @@ async function bot(core, github, context, uuid) {
10
10
}
11
11
console . log ( "Comment found in payload" ) ;
12
12
13
- // user's org membership must be public for the author_association to be MEMBER
14
- // go to the org's member page, find yourself, and set the visibility to public
15
- const author = payload . comment . user . login ;
16
- const authorized = [ "OWNER" , "MEMBER" ] . includes ( payload . comment . author_association ) ;
13
+ const authorized = await isUserAuthorized ( github , payload ) ;
17
14
if ( ! authorized ) {
18
15
console . log ( `Comment author is not authorized: ${ author } ` ) ;
19
16
return ;
@@ -52,6 +49,31 @@ async function bot(core, github, context, uuid) {
52
49
}
53
50
}
54
51
52
+ /**
53
+ * @returns true if the author of this payload's comment has both:
54
+ * - an OWNER or MEMBER of the repository's organization
55
+ * - the admin or maintain roles in the repository
56
+ */
57
+ async function isUserAuthorized ( github , payload ) {
58
+ // user's org membership must be public for the author_association to be MEMBER
59
+ // go to the org's member page, find yourself, and set the visibility to public
60
+ const author = payload . comment . user . login ;
61
+ if ( ! [ "OWNER" , "MEMBER" ] . includes ( payload . comment . author_association ) ) {
62
+ console . log ( `Comment author association is not OWNER or MEMBER: ${ author } ` ) ;
63
+ return false ;
64
+ }
65
+ const authorPermissionLevel = await github . rest . repos . getCollaboratorPermissionLevel ( {
66
+ owner : payload . repository . owner . login ,
67
+ repo : payload . repository . name ,
68
+ username : author
69
+ } ) ;
70
+ if ( ! [ 'admin' , 'maintain' ] . includes ( authorPermissionLevel . data . role_name ) ) {
71
+ console . log ( `Comment author does not have the admin or maintain role for the repository: ${ author } ` ) ;
72
+ return false ;
73
+ }
74
+ return true ;
75
+ }
76
+
55
77
// replyToCommand creates a comment on the same PR that triggered this workflow
56
78
function replyToCommand ( github , payload , reply ) {
57
79
github . rest . issues . createComment ( {
0 commit comments