This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
/
AddReviewUrl.js
93 lines (76 loc) · 2.77 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { graphql } = require("@octokit/graphql");
const core = require('@actions/core');
const { App } = require("@octokit/app");
const token = core.getInput('repo-token');
const repo = process.env.GITHUB_REPOSITORY.toString();
const commitId = process.env.GITHUB_REF;
let repoNameWithOwnerArray = repo.split("/", 2);
const owner = repoNameWithOwnerArray[0];
const actualRepo = repoNameWithOwnerArray[1];
const host = core.getInput('host');
const headref = process.env.GITHUB_HEAD_REF.toString();
const bodyprime = `You can see a private version of the changes made in this pull request here:\nhttp://${headref}.s.${host}/`;
/// Function to get the Pull number from the environment variable: process.env.GITHUB_REF
function getPullNumber(){
let commitIdArray = commitId.split("/", 3);
const pullNumber = commitIdArray[2];
return parseInt(pullNumber, 10);
}
/// Get graphql with Auth
function getGraphqlWithAuth(token) {
return graphql.defaults({
headers: {
authorization: `token ${token}`
}
});
}
///Add Comment to the Pull Request
function addCommentToPullRequest(body, pullRequestId){
let obj = JSON.parse(JSON.stringify(pullRequestId));
const graphqlWithAuth = getGraphqlWithAuth(token);
graphqlWithAuth(pullRequestCommentMutation, {
subjectId:obj.repository.pullRequest.id,
body: body
}
).catch(err => console.log(err)).then(result => console.log(result));
}
///Find the Pull Request and add comment
function findPullRequestAndAddComment(owner, repo, commentBody) {
const graphqlWithAuth = getGraphqlWithAuth(token);
const findPullRequestIdQuery = findPullRequestQuery();
graphqlWithAuth(findPullRequestIdQuery, {
owner: owner,
repo: repo,
pullNumber: getPullNumber()
}
).catch(err => console.log(err)).then(pullRequestId => addCommentToPullRequest(commentBody, pullRequestId));
}
let pullRequestCommentMutation = addPullRequestCommentMutation();
function addPullRequestCommentMutation() {
return `mutation AddPullRequestComment($subjectId: ID!, $body: String!) {
addComment(input:{subjectId:$subjectId, body: $body}) {
commentEdge {
node {
createdAt
body
}
}
subject {
id
}
}
}`;
}
function findPullRequestQuery() {
return `query FindPullRequestID ($owner: String!, $repo: String!, $pullNumber: Int!){
repository(owner:$owner, name:$repo) {
pullRequest(number:$pullNumber) {
id
}
}
}`;
}
/// This is the entry point
findPullRequestAndAddComment(owner,actualRepo,bodyprime);