This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
/
AddReviewUrl.js
96 lines (83 loc) · 2.48 KB
/
AddReviewUrl.js
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const {graphql} = require('@octokit/graphql');
const core = require('@actions/core');
class AddComment {
constructor(comment)
{
this.addComment(comment);
}
getOwnerAndRepo() {
if (process.env.GITHUB_REPOSITORY) {
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
return {'owner': owner, 'repo': repo};
} else {
throw new Error('not able to obtain GITHUB_REPOSITORY from process.env');
}
}
addPullRequestCommentMutation() {
return `mutation AddPullRequestComment($subjectId: ID!, $body: String!) {
addComment(input:{subjectId:$subjectId, body: $body}) {
commentEdge {
node {
createdAt
body
}
}
subject {
id
}
}
}`;
}
getPullNumber() {
if (process.env.GITHUB_REF) {
return parseInt(process.env.GITHUB_REF.split('/')[2]);
}
}
getGraphqlWithAuth() {
const token = core.getInput('repo-token');
return graphql.defaults({
headers: {
authorization: `token ${token}`,
},
});
}
findPullRequestQuery() {
return `query FindPullRequestID ($owner: String!, $repo: String!, $pullNumber: Int!){
repository(owner:$owner, name:$repo) {
pullRequest(number:$pullNumber) {
id
}
}
}`;
}
addCommentUsingSubjectId(pullRequestId, comment) {
let obj = JSON.parse(JSON.stringify(pullRequestId));
const graphqlWithAuth = this.getGraphqlWithAuth();
graphqlWithAuth(this.addPullRequestCommentMutation(), {
subjectId: obj.repository.pullRequest.id,
body: comment,
},
);
}
async addComment(comment) {
const nameAndRepo = this.getOwnerAndRepo();
const graphqlWithAuth = this.getGraphqlWithAuth();
const findPullRequestIdQuery = this.findPullRequestQuery();
try {
const subjectId = await this.getSubjectId(graphqlWithAuth, findPullRequestIdQuery, nameAndRepo);
await this.addCommentUsingSubjectId(subjectId, comment);
} catch (e) {
}
}
getSubjectId(graphqlWithAuth, findPullRequestIdQuery, nameAndRepo) {
return graphqlWithAuth(findPullRequestIdQuery, {
owner: nameAndRepo.owner,
repo: nameAndRepo.repo,
pullNumber: this.getPullNumber(),
},
);
}
}
export default AddComment;