-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
make-comment.js
112 lines (98 loc) · 2.89 KB
/
make-comment.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
/**
* Updates the comment matching specified pattern.
* @param {import('@octokit/rest').Octokit} octokit Octokit instance
* @param {{ owner: string; repo: string; issue_number: string; }} issueParams
* @param {string} body Comment body
* @param {string} replacePattern Pattern for finding the comment to update
*/
async function updateComment(octokit, issueParams, body, replacePattern) {
if (!replacePattern) {
return false;
}
const authenticatedUser = await octokit.users.getAuthenticated();
if (authenticatedUser.status !== 200 || !authenticatedUser.data) {
console.warn(authenticatedUser);
return false;
}
const comments = await octokit.issues.listComments(issueParams);
if (comments.status !== 200 || !comments.data) {
console.warn(comments);
return false;
}
const authedUserId = authenticatedUser.data.id;
const pattern = new RegExp(replacePattern, 'g');
const comment = comments.data.find(
({user, body}) => user.id === authedUserId && pattern.test(body),
);
if (!comment) {
return false;
}
octokit.issues.updateComment({
...issueParams,
comment_id: comment.id,
body,
});
return true;
}
/**
* Creates or updates a comment with specified pattern.
* @param {string} body Comment body
* @param {string} replacePattern Pattern for finding the comment to update
*/
async function createOrUpdateComment(body, replacePattern) {
const {
GITHUB_TOKEN,
GITHUB_OWNER,
GITHUB_REPO,
GITHUB_PR_NUMBER,
} = process.env;
if (!GITHUB_TOKEN || !GITHUB_OWNER || !GITHUB_REPO || !GITHUB_PR_NUMBER) {
if (!GITHUB_TOKEN) {
console.error(
'Missing GITHUB_TOKEN. Example: 5fd88b964fa214c4be2b144dc5af5d486a2f8c1e. PR feedback cannot be provided on GitHub without a valid token.',
);
}
if (!GITHUB_OWNER) {
console.error('Missing GITHUB_OWNER. Example: facebook');
}
if (!GITHUB_REPO) {
console.error('Missing GITHUB_REPO. Example: react-native');
}
if (!GITHUB_PR_NUMBER) {
console.error(
'Missing GITHUB_PR_NUMBER. Example: 4687. PR feedback cannot be provided on GitHub without a valid pull request number.',
);
}
process.exit(1);
}
if (!body) {
return;
}
const {Octokit} = require('@octokit/rest');
const octokit = new Octokit({auth: GITHUB_TOKEN});
const issueParams = {
owner: GITHUB_OWNER,
repo: GITHUB_REPO,
issue_number: GITHUB_PR_NUMBER,
};
if (await updateComment(octokit, issueParams, body, replacePattern)) {
return;
}
// We found no comments to replace, so we'll create a new one.
octokit.issues.createComment({
...issueParams,
body,
});
}
module.exports = {
createOrUpdateComment,
};