From b47636452b4e1462ea716e51a2337157533b71f6 Mon Sep 17 00:00:00 2001 From: gidskql6671 Date: Fri, 27 Jan 2023 10:37:51 +0900 Subject: [PATCH] support github enterprise --- main.js | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/main.js b/main.js index 2d45c2c..ed3ac8c 100644 --- a/main.js +++ b/main.js @@ -10,15 +10,21 @@ function fail(message, exitCode=1) { } function request(method, path, data, callback) { - + try { if (data) { data = JSON.stringify(data); - } + } + const urlWithoutProtocol = env.GITHUB_API_URL.replace(/(^\w+:|^)\/\//, ''); + const hostname = urlWithoutProtocol.split("/")[0]; + let pathPrefix = urlWithoutProtocol.split("/").slice(1).filter(n => n).join("/"); + if (pathPrefix.length > 0) { + pathPrefix = "/" + pathPrefix; + } const options = { - hostname: 'api.github.com', + hostname: hostname, port: 443, - path, + path: pathPrefix + path, method, headers: { 'Content-Type': 'application/json', @@ -29,7 +35,7 @@ function request(method, path, data, callback) { } } const req = https.request(options, res => { - + let chunks = []; res.on('data', d => chunks.push(d)); res.on('end', () => { @@ -46,10 +52,10 @@ function request(method, path, data, callback) { callback(null, res.statusCode, buffer.length > 0 ? JSON.parse(buffer) : null); } }); - + req.on('error', err => callback(err)); }); - + if (data) { req.write(data); } @@ -74,7 +80,7 @@ function main() { fs.writeFileSync(process.env.GITHUB_ENV, `BUILD_NUMBER=${buildNumber}`); return; } - + //Some sanity checking: for (let varName of ['INPUT_TOKEN', 'GITHUB_REPOSITORY', 'GITHUB_SHA']) { if (!env[varName]) { @@ -83,9 +89,9 @@ function main() { } request('GET', `/repos/${env.GITHUB_REPOSITORY}/git/refs/tags/${prefix}build-number-`, null, (err, status, result) => { - + let nextBuildNumber, nrTags; - + if (status === 404) { console.log('No build-number ref available, starting at 1.'); nextBuildNumber = 1; @@ -94,18 +100,18 @@ function main() { const regexString = `/${prefix}build-number-(\\d+)$`; const regex = new RegExp(regexString); nrTags = result.filter(d => d.ref.match(regex)); - + const MAX_OLD_NUMBERS = 5; //One or two ref deletes might fail, but if we have lots then there's something wrong! if (nrTags.length > MAX_OLD_NUMBERS) { fail(`ERROR: Too many ${prefix}build-number- refs in repository, found ${nrTags.length}, expected only 1. Check your tags!`); } - + //Existing build numbers: let nrs = nrTags.map(t => parseInt(t.ref.match(/-(\d+)$/)[1])); - + let currentBuildNumber = Math.max(...nrs); console.log(`Last build nr was ${currentBuildNumber}.`); - + nextBuildNumber = currentBuildNumber + 1; console.log(`Updating build counter to ${nextBuildNumber}...`); } else { @@ -113,32 +119,32 @@ function main() { fail(`Failed to get refs. Error: ${err}, status: ${status}`); } else { fail(`Getting build-number refs failed with http status ${status}, error: ${JSON.stringify(result)}`); - } + } } let newRefData = { - ref:`refs/tags/${prefix}build-number-${nextBuildNumber}`, + ref:`refs/tags/${prefix}build-number-${nextBuildNumber}`, sha: env.GITHUB_SHA }; - + request('POST', `/repos/${env.GITHUB_REPOSITORY}/git/refs`, newRefData, (err, status, result) => { if (status !== 201 || err) { fail(`Failed to create new build-number ref. Status: ${status}, err: ${err}, result: ${JSON.stringify(result)}`); } console.log(`Successfully updated build number to ${nextBuildNumber}`); - + //Setting the output and a environment variable to new build number... fs.writeFileSync(process.env.GITHUB_OUTPUT, `build_number=${nextBuildNumber}`); fs.writeFileSync(process.env.GITHUB_ENV, `BUILD_NUMBER=${nextBuildNumber}`); - + //Save to file so it can be used for next jobs... fs.writeFileSync('BUILD_NUMBER', nextBuildNumber.toString()); - + //Cleanup if (nrTags) { console.log(`Deleting ${nrTags.length} older build counters...`); - + for (let nrTag of nrTags) { request('DELETE', `/repos/${env.GITHUB_REPOSITORY}/git/${nrTag.ref}`, null, (err, status, result) => { if (status !== 204 || err) {