1- "use strict" ;
2-
3- var _interopRequireDefault = require ( "@babel/runtime/helpers/interopRequireDefault" ) ;
4-
5- Object . defineProperty ( exports , "__esModule" , {
6- value : true
7- } ) ;
8- exports [ "default" ] = void 0 ;
9-
10- var _regenerator = _interopRequireDefault ( require ( "@babel/runtime/regenerator" ) ) ;
11-
12- var _asyncToGenerator2 = _interopRequireDefault ( require ( "@babel/runtime/helpers/asyncToGenerator" ) ) ;
13-
14- var _slicedToArray2 = _interopRequireDefault ( require ( "@babel/runtime/helpers/slicedToArray" ) ) ;
15-
16- var _classCallCheck2 = _interopRequireDefault ( require ( "@babel/runtime/helpers/classCallCheck" ) ) ;
17-
18- var _createClass2 = _interopRequireDefault ( require ( "@babel/runtime/helpers/createClass" ) ) ;
19-
20- // Copyright (c) Microsoft Corporation. All rights reserved.
21- // Licensed under the MIT License.
22- var _require = require ( '@octokit/graphql' ) ,
23- graphql = _require . graphql ;
24-
25- var core = require ( '@actions/core' ) ;
26-
27- var AddComment =
28- /*#__PURE__*/
29- function ( ) {
30- function AddComment ( comment ) {
31- ( 0 , _classCallCheck2 [ "default" ] ) ( this , AddComment ) ;
32- this . addComment ( comment ) ;
33- }
34-
35- ( 0 , _createClass2 [ "default" ] ) ( AddComment , [ {
36- key : "getOwnerAndRepo" ,
37- value : function getOwnerAndRepo ( ) {
38- if ( process . env . GITHUB_REPOSITORY ) {
39- var _process$env$GITHUB_R = process . env . GITHUB_REPOSITORY . split ( '/' ) ,
40- _process$env$GITHUB_R2 = ( 0 , _slicedToArray2 [ "default" ] ) ( _process$env$GITHUB_R , 2 ) ,
41- owner = _process$env$GITHUB_R2 [ 0 ] ,
42- repo = _process$env$GITHUB_R2 [ 1 ] ;
43-
44- return {
45- 'owner' : owner ,
46- 'repo' : repo
47- } ;
48- } else {
49- throw new Error ( 'not able to obtain GITHUB_REPOSITORY from process.env' ) ;
50- }
51- }
52- } , {
53- key : "addPullRequestCommentMutation" ,
54- value : function addPullRequestCommentMutation ( ) {
55- return "mutation AddPullRequestComment($subjectId: ID!, $body: String!) {\n addComment(input:{subjectId:$subjectId, body: $body}) {\n commentEdge {\n node {\n createdAt\n body\n }\n }\n subject {\n id\n }\n }\n }" ;
56- }
57- } , {
58- key : "getPullNumber" ,
59- value : function getPullNumber ( ) {
60- if ( process . env . GITHUB_REF ) {
61- return parseInt ( process . env . GITHUB_REF . split ( '/' ) [ 2 ] ) ;
62- }
63- }
64- } , {
65- key : "getGraphqlWithAuth" ,
66- value : function getGraphqlWithAuth ( ) {
67- var token = core . getInput ( 'repo-token' ) ;
68- return graphql . defaults ( {
69- headers : {
70- authorization : "token " . concat ( token )
71- }
72- } ) ;
73- }
74- } , {
75- key : "findPullRequestQuery" ,
76- value : function findPullRequestQuery ( ) {
77- return "query FindPullRequestID ($owner: String!, $repo: String!, $pullNumber: Int!){\n repository(owner:$owner, name:$repo) {\n pullRequest(number:$pullNumber) {\n id\n }\n }\n }" ;
78- }
79- } , {
80- key : "addCommentUsingSubjectId" ,
81- value : function addCommentUsingSubjectId ( pullRequestId , comment ) {
82- var obj = JSON . parse ( JSON . stringify ( pullRequestId ) ) ;
83- var graphqlWithAuth = this . getGraphqlWithAuth ( ) ;
84- graphqlWithAuth ( this . addPullRequestCommentMutation ( ) , {
85- subjectId : obj . repository . pullRequest . id ,
86- body : comment
87- } ) ;
88- }
89- } , {
90- key : "addComment" ,
91- value : function ( ) {
92- var _addComment = ( 0 , _asyncToGenerator2 [ "default" ] ) (
93- /*#__PURE__*/
94- _regenerator [ "default" ] . mark ( function _callee ( comment ) {
95- var nameAndRepo , graphqlWithAuth , findPullRequestIdQuery , subjectId ;
96- return _regenerator [ "default" ] . wrap ( function _callee$ ( _context ) {
97- while ( 1 ) {
98- switch ( _context . prev = _context . next ) {
99- case 0 :
100- nameAndRepo = this . getOwnerAndRepo ( ) ;
101- graphqlWithAuth = this . getGraphqlWithAuth ( ) ;
102- findPullRequestIdQuery = this . findPullRequestQuery ( ) ;
103- _context . prev = 3 ;
104- _context . next = 6 ;
105- return this . getSubjectId ( graphqlWithAuth , findPullRequestIdQuery , nameAndRepo ) ;
106-
107- case 6 :
108- subjectId = _context . sent ;
109- _context . next = 9 ;
110- return this . addCommentUsingSubjectId ( subjectId , comment ) ;
111-
112- case 9 :
113- _context . next = 13 ;
114- break ;
115-
116- case 11 :
117- _context . prev = 11 ;
118- _context . t0 = _context [ "catch" ] ( 3 ) ;
119-
120- case 13 :
121- case "end" :
122- return _context . stop ( ) ;
123- }
124- }
125- } , _callee , this , [ [ 3 , 11 ] ] ) ;
126- } ) ) ;
127-
128- function addComment ( _x ) {
129- return _addComment . apply ( this , arguments ) ;
130- }
131-
132- return addComment ;
133- } ( )
134- } , {
135- key : "getSubjectId" ,
136- value : function getSubjectId ( graphqlWithAuth , findPullRequestIdQuery , nameAndRepo ) {
137- return graphqlWithAuth ( findPullRequestIdQuery , {
138- owner : nameAndRepo . owner ,
139- repo : nameAndRepo . repo ,
140- pullNumber : this . getPullNumber ( )
141- } ) ;
142- }
143- } ] ) ;
144- return AddComment ;
145- } ( ) ;
146-
147- var _default = AddComment ;
148- exports [ "default" ] = _default ;
1+ "use strict" ;
2+ var __awaiter = ( this && this . __awaiter ) || function ( thisArg , _arguments , P , generator ) {
3+ function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
4+ return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
5+ function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
6+ function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
7+ function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
8+ step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
9+ } ) ;
10+ } ;
11+ var __importStar = ( this && this . __importStar ) || function ( mod ) {
12+ if ( mod && mod . __esModule ) return mod ;
13+ var result = { } ;
14+ if ( mod != null ) for ( var k in mod ) if ( Object . hasOwnProperty . call ( mod , k ) ) result [ k ] = mod [ k ] ;
15+ result [ "default" ] = mod ;
16+ return result ;
17+ } ;
18+ Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
19+ // Copyright (c) Microsoft Corporation. All rights reserved.
20+ // Licensed under the MIT License.
21+ const core = __importStar ( require ( "@actions/core" ) ) ;
22+ const graphql_1 = require ( "@octokit/graphql" ) ;
23+ class AddComment {
24+ getOwnerAndRepo ( ) {
25+ core . debug ( `process.env.GITHUB_REPOSITORY ${ process . env . GITHUB_REPOSITORY } ` ) ;
26+ if ( process . env . GITHUB_REPOSITORY ) {
27+ return process . env . GITHUB_REPOSITORY . split ( '/' ) ;
28+ }
29+ else {
30+ core . debug ( 'Error in getOwnerAndRepo' ) ;
31+ throw new Error ( 'not able to obtain GITHUB_REPOSITORY from process.env' ) ;
32+ }
33+ }
34+ addPullRequestCommentMutation ( ) {
35+ return `mutation AddPullRequestComment($subjectId: ID!, $body: String!) {
36+ addComment(input:{subjectId:$subjectId, body: $body}) {
37+ commentEdge {
38+ node {
39+ createdAt
40+ body
41+ }
42+ }
43+ subject {
44+ id
45+ }
46+ }
47+ }` ;
48+ }
49+ getPullNumber ( ) {
50+ if ( process . env . GITHUB_REF ) {
51+ return parseInt ( process . env . GITHUB_REF . split ( '/' ) [ 2 ] ) ;
52+ }
53+ else {
54+ throw new Error ( 'GITHUB_REF is missing in process.env' ) ;
55+ }
56+ }
57+ findPullRequestQuery ( ) {
58+ return `query FindPullRequestID ($owner: String!, $repo: String!, $pullNumber: Int!){
59+ repository(owner:$owner, name:$repo) {
60+ pullRequest(number:$pullNumber) {
61+ id
62+ }
63+ }
64+ }` ;
65+ }
66+ addCommentUsingSubjectId ( pullRequestId , comment ) {
67+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
68+ core . debug ( `pullRequestId ===>>>> ${ pullRequestId } ` ) ;
69+ let data = JSON . parse ( JSON . stringify ( pullRequestId ) ) ;
70+ core . debug ( `Parsed pull request id ${ data } ` ) ;
71+ const token = core . getInput ( 'repo-token' ) ;
72+ let graphQlResponse = graphql_1 . graphql ( this . addPullRequestCommentMutation ( ) , {
73+ headers : {
74+ authorization : `token ${ token } ` ,
75+ } ,
76+ subjectId : data . repository . pullRequest . id ,
77+ body : comment ,
78+ } ) ;
79+ core . debug ( `Adding the comment ...` ) ;
80+ return yield graphQlResponse ;
81+ } ) ;
82+ }
83+ getSubjectId ( findPullRequestIdQuery , nameAndRepo ) {
84+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
85+ core . debug ( 'Inside getSubjectId' ) ;
86+ const token = core . getInput ( 'repo-token' ) ;
87+ let newVar = yield graphql_1 . graphql ( findPullRequestIdQuery , {
88+ headers : {
89+ authorization : `token ${ token } ` ,
90+ } ,
91+ owner : nameAndRepo [ 0 ] ,
92+ repo : nameAndRepo [ 1 ] ,
93+ pullNumber : this . getPullNumber ( ) ,
94+ } ) ;
95+ core . debug ( `Exiting getSubject Id` ) ;
96+ return newVar ;
97+ } ) ;
98+ }
99+ addComment ( comment ) {
100+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
101+ core . debug ( 'Inside addComment' ) ;
102+ const nameAndRepo = this . getOwnerAndRepo ( ) ;
103+ const [ name , repo ] = nameAndRepo ;
104+ core . debug ( `Name is ${ name } and repo is ${ repo } ` ) ;
105+ const findPullRequestIdQuery = this . findPullRequestQuery ( ) ;
106+ try {
107+ const subjectId = yield this . getSubjectId ( findPullRequestIdQuery , nameAndRepo ) ;
108+ return yield this . addCommentUsingSubjectId ( subjectId , comment ) ;
109+ }
110+ catch ( error ) {
111+ core . setFailed ( error . message ) ;
112+ }
113+ } ) ;
114+ }
115+ }
116+ exports . default = AddComment ;
0 commit comments