forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-tryit-comment.js
132 lines (112 loc) · 3.67 KB
/
create-tryit-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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// @ts-check
import { execSync } from "child_process";
import https from "https";
const AZP_USERID = "azure-pipelines[bot]";
const TRY_ID_COMMENT_IDENTIFIER = "_CADL_TRYIT_COMMENT_";
main().catch((e) => {
console.error(e);
// @ts-ignore
process.exit(1);
});
/**
* Retrieve github authentication header from the git config.
* MUST have `persistCredentials: true` in the checkout step.
*/
function getGithubAuthHeader(repo) {
const stdout = execSync(`git config --get http.https://github.com/${repo}.extraheader`)
.toString()
.trim();
const authHeader = stdout.split(": ")[1];
return authHeader;
}
async function main() {
const folderName = process.argv.length > 2 ? `/${process.argv[2]}` : "";
const repo = process.env["BUILD_REPOSITORY_ID"];
const prNumber = process.env["SYSTEM_PULLREQUEST_PULLREQUESTNUMBER"];
const ghToken = process.env.GH_TOKEN;
if (ghToken === undefined) {
throw new Error("GH_TOKEN environment variable is not set");
}
const ghAuth = `Bearer ${ghToken}`;
console.log("Looking for comments in", { repo, prNumber });
const data = await listComments(repo, prNumber, ghAuth);
const azoComments = data.filter((x) => x.user?.login === AZP_USERID);
console.log(`Found ${azoComments.length} comment(s) from Azure Pipelines.`);
const tryItComments = data.filter((x) => x.body.includes(TRY_ID_COMMENT_IDENTIFIER));
console.log(`Found ${azoComments.length} Cadl Try It comment(s)`);
if (tryItComments.length > 0) {
console.log("##vso[task.setvariable variable=SKIP_COMMENT;]true");
return;
}
const comment = [
`<!-- ${TRY_ID_COMMENT_IDENTIFIER} -->`,
`You can try these changes at https://cadlplayground.z22.web.core.windows.net${folderName}/prs/${prNumber}/`,
"",
`Check the website changes at https://tspwebsitepr.z22.web.core.windows.net${folderName}/prs/${prNumber}/`,
].join("\n");
await writeComment(repo, prNumber, comment, ghAuth);
}
async function listComments(repo, prNumber, ghAuth) {
const url = `https://api.github.com/repos/${repo}/issues/${prNumber}/comments?per_page=100`;
const result = await request("GET", url, { ghAuth });
return JSON.parse(result);
}
async function writeComment(repo, prNumber, comment, ghAuth) {
const url = `https://api.github.com/repos/${repo}/issues/${prNumber}/comments`;
const body = {
body: comment,
};
const headers = {
"Content-Type": "application/json",
};
const response = await request("POST", url, {
headers,
body: JSON.stringify(body),
ghAuth,
});
console.log("Comment created", response);
}
async function request(method, url, data) {
const lib = https;
const value = new URL(url);
const params = {
method,
host: value.host,
port: 443,
path: value.pathname,
headers: {
"User-Agent": "nodejs",
...data.headers,
},
};
console.log("Params", params);
params.headers.Authorization = data.ghAuth;
return new Promise((resolve, reject) => {
const req = lib.request(params, (res) => {
const data = [];
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) {
return reject(
new Error(
`Status Code: ${res.statusCode}, statusMessage: ${
res.statusMessage
}, headers: ${JSON.stringify(res.headers, null, 2)}, body: ${Buffer.concat(
data
).toString()}`
)
);
} else {
resolve(Buffer.concat(data).toString());
}
});
});
req.on("error", reject);
if (data.body) {
req.write(data.body);
}
req.end();
});
}