Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Github Enterprise #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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', () => {
Expand All @@ -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);
}
Expand All @@ -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]) {
Expand All @@ -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;
Expand All @@ -94,51 +100,51 @@ 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 {
if (err) {
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) {
Expand Down