microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4d92e0825d81d6432d29b0fade7b161a2e2aafb7

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

eng/scripts/create-tryit-comment.js

113lines · modecode

1// @ts-check
2import { execSync } from "child_process";
3import https from "https";
4
5const AZP_USERID = "azure-pipelines[bot]";
6const TRYID_COMMENT_IDENTIFIER = "_CADL_TRYIT_COMMENT_";
7main().catch((e) => {
8 console.error(e);
9 // @ts-ignore
10 process.exit(1);
11});
12
13/**
14 * Retrieve github authentication header from the git config.
15 * MUST have `persistCredentials: true` in the checkout step.
16 */
17function getGithubAuthHeader(repo) {
18 const stdout = execSync(`git config --get http.https://github.com/${repo}.extraheader`)
19 .toString()
20 .trim();
21 const authHeader = stdout.split(": ")[1];
22 return authHeader;
23}
24
25async function main() {
26 const folderName = process.argv.length > 2 ? `/${process.argv[2]}` : "";
27 const repo = process.env["BUILD_REPOSITORY_ID"];
28 const prNumber = process.env["SYSTEM_PULLREQUEST_PULLREQUESTNUMBER"];
29 const ghAuth = getGithubAuthHeader(repo);
30
31 console.log("Looking for comments in", { repo, prNumber });
32 const data = await listComments(repo, prNumber, ghAuth);
33 const azoComments = data.filter((x) => x.user?.login === AZP_USERID);
34 console.log(`Found ${azoComments.length} comment(s) from Azure Pipelines.`);
35
36 const tryItComments = data.filter((x) => x.body.includes(TRYID_COMMENT_IDENTIFIER));
37 console.log(`Found ${azoComments.length} Cadl Try It comment(s)`);
38 if (tryItComments.length > 0) {
39 console.log("##vso[task.setvariable variable=SKIP_COMMENT;]true");
40 return;
41 }
42
43 const comment = `<!-- ${TRYID_COMMENT_IDENTIFIER} -->\nYou can try these changes at https://cadlplayground.z22.web.core.windows.net${folderName}/prs/${prNumber}/`;
44 await writeComment(repo, prNumber, comment, ghAuth);
45}
46
47async function listComments(repo, prNumber, ghAuth) {
48 const url = `https://api.github.com/repos/${repo}/issues/${prNumber}/comments?per_page=100`;
49 const result = await request("GET", url, { ghAuth });
50 return JSON.parse(result);
51}
52
53async function writeComment(repo, prNumber, comment, ghAuth) {
54 const url = `https://api.github.com/repos/${repo}/issues/${prNumber}/comments`;
55 const body = {
56 body: comment,
57 };
58 const headers = {
59 "Content-Type": "application/json",
60 };
61
62 const response = await request("POST", url, {
63 headers,
64 body: JSON.stringify(body),
65 ghAuth,
66 });
67
68 console.log("Comment created", response);
69}
70
71async function request(method, url, data) {
72 const lib = https;
73 const value = new URL(url);
74
75 const params = {
76 method,
77 host: value.host,
78 port: 443,
79 path: value.pathname,
80 headers: {
81 "User-Agent": "nodejs",
82 ...data.headers,
83 },
84 };
85
86 console.log("Params", params);
87
88 params.headers.Authorization = data.ghAuth;
89
90 return new Promise((resolve, reject) => {
91 const req = lib.request(params, (res) => {
92 if (res.statusCode < 200 || res.statusCode >= 300) {
93 return reject(new Error(`Status Code: ${res.statusCode}`));
94 }
95
96 const data = [];
97
98 res.on("data", (chunk) => {
99 data.push(chunk);
100 });
101
102 res.on("end", () => resolve(Buffer.concat(data).toString()));
103 });
104
105 req.on("error", reject);
106
107 if (data.body) {
108 req.write(data.body);
109 }
110
111 req.end();
112 });
113}