microsoft/typespec
Publicmirrored fromhttps://github.com/microsoft/typespecAvailable
eng/scripts/create-tryit-comment.js
128lines · modecode
| 1 | // @ts-check |
| 2 | import { execSync } from "child_process"; |
| 3 | import https from "https"; |
| 4 | |
| 5 | const AZP_USERID = "azure-pipelines[bot]"; |
| 6 | const TRY_ID_COMMENT_IDENTIFIER = "_CADL_TRYIT_COMMENT_"; |
| 7 | main().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 | */ |
| 17 | function 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 | |
| 25 | async 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(TRY_ID_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 = [ |
| 44 | `<!-- ${TRY_ID_COMMENT_IDENTIFIER} -->`, |
| 45 | `You can try these changes at https://cadlplayground.z22.web.core.windows.net${folderName}/prs/${prNumber}/`, |
| 46 | "", |
| 47 | `Check the website changes at https://cadlwebsite.z1.web.core.windows.net${folderName}/prs/${prNumber}/`, |
| 48 | ].join("\n"); |
| 49 | await writeComment(repo, prNumber, comment, ghAuth); |
| 50 | } |
| 51 | |
| 52 | async function listComments(repo, prNumber, ghAuth) { |
| 53 | const url = `https://api.github.com/repos/${repo}/issues/${prNumber}/comments?per_page=100`; |
| 54 | const result = await request("GET", url, { ghAuth }); |
| 55 | return JSON.parse(result); |
| 56 | } |
| 57 | |
| 58 | async function writeComment(repo, prNumber, comment, ghAuth) { |
| 59 | const url = `https://api.github.com/repos/${repo}/issues/${prNumber}/comments`; |
| 60 | const body = { |
| 61 | body: comment, |
| 62 | }; |
| 63 | const headers = { |
| 64 | "Content-Type": "application/json", |
| 65 | }; |
| 66 | |
| 67 | const response = await request("POST", url, { |
| 68 | headers, |
| 69 | body: JSON.stringify(body), |
| 70 | ghAuth, |
| 71 | }); |
| 72 | |
| 73 | console.log("Comment created", response); |
| 74 | } |
| 75 | |
| 76 | async function request(method, url, data) { |
| 77 | const lib = https; |
| 78 | const value = new URL(url); |
| 79 | |
| 80 | const params = { |
| 81 | method, |
| 82 | host: value.host, |
| 83 | port: 443, |
| 84 | path: value.pathname, |
| 85 | headers: { |
| 86 | "User-Agent": "nodejs", |
| 87 | ...data.headers, |
| 88 | }, |
| 89 | }; |
| 90 | |
| 91 | console.log("Params", params); |
| 92 | |
| 93 | params.headers.Authorization = data.ghAuth; |
| 94 | |
| 95 | return new Promise((resolve, reject) => { |
| 96 | const req = lib.request(params, (res) => { |
| 97 | const data = []; |
| 98 | |
| 99 | res.on("data", (chunk) => { |
| 100 | data.push(chunk); |
| 101 | }); |
| 102 | |
| 103 | res.on("end", () => { |
| 104 | if (res.statusCode && (res.statusCode < 200 || res.statusCode >= 300)) { |
| 105 | return reject( |
| 106 | new Error( |
| 107 | `Status Code: ${res.statusCode}, statusMessage: ${ |
| 108 | res.statusMessage |
| 109 | }, headers: ${JSON.stringify(res.headers, null, 2)}, body: ${Buffer.concat( |
| 110 | data |
| 111 | ).toString()}` |
| 112 | ) |
| 113 | ); |
| 114 | } else { |
| 115 | resolve(Buffer.concat(data).toString()); |
| 116 | } |
| 117 | }); |
| 118 | }); |
| 119 | |
| 120 | req.on("error", reject); |
| 121 | |
| 122 | if (data.body) { |
| 123 | req.write(data.body); |
| 124 | } |
| 125 | |
| 126 | req.end(); |
| 127 | }); |
| 128 | } |
| 129 | |