add ci_hostname variable, split up collection of data

This commit is contained in:
2023-12-02 18:38:00 +01:00
parent 68341bd8b8
commit 494a52a721
9 changed files with 243 additions and 146 deletions
+3
View File
@@ -0,0 +1,3 @@
import {collect_all} from './index';
collect_all(true, true);
+29
View File
@@ -0,0 +1,29 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as process from 'process';
import {result_to_output} from './lib';
export function collect_ci(debug = false, output = false) {
const r = {
ci_hostname: false
};
try {
const url = new URL(github.context.serverUrl);
r.ci_hostname = url.hostname;
if (debug) {
console.log('results for output:', JSON.stringify(r, null, 2));
}
if (output) {
result_to_output(r);
}
return r;
}
catch (error) {
console.log('partial results before error: ', JSON.stringify(r, null, 2));
core.setFailed(error.message);
process.exit(1);
}
}
+112
View File
@@ -0,0 +1,112 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as process from 'process';
import * as semverParser from 'semver-parser';
import {result_to_output} from './lib';
export function collect_git(debug = false, output = false) {
const r = {
git_is_branch : false,
git_is_tag : false,
git_is_pull_request : false,
git_current_branch : false,
git_ref_branch : false,
git_is_default_branch: false,
git_tag : false,
semver_valid : false,
semver_major : false,
semver_minor : false,
semver_patch : false,
semver_build : false,
semver_pre : false
};
try {
const stripTagPrefix = core.getInput('strip_tag_prefix');
if (debug) {
console.log(JSON.stringify(github.context, null, 2));
}
r.git_is_tag = github.context.ref.startsWith('refs/tags/');
r.git_default_branch = github.context.payload?.repository?.defaultBranch;
r.git_is_pull_request = github.context.payload.pull_request != null;
if (r.git_is_pull_request) {
const baseRef = github.context.baseRef || process.env.BODY_REF;
const headRef =
github.context.payload?.pull_request?.head?.ref || process.env.HEAD_REF;
if (debug) {
console.log('baseRef=' + baseRef);
}
if (debug) {
console.log('headRef=' + headRef);
}
// TODO support PR data
}
else if (!r.git_is_tag) {
// regular branches
r.git_is_branch = true;
}
// gather regular branch info
if (r.git_is_branch) {
if (!github.context.ref.startsWith('refs/heads/')) {
throw new Error(
'Failed to determine branch for non-PR and non-Tag action'
);
}
r.git_current_branch = github.context.ref.slice('refs/heads/'.length);
r.git_ref_branch = r.git_current_branch; // same as current branch for non-PR, non-Tag
r.git_is_default_branch = r.git_current_branch == r.git_default_branch;
}
// parse semver for tags
if (r.git_is_tag) {
r.git_tag = github.context.ref.slice('refs/tags/'.length);
const parsed = semverParser.parseSemVer(r.git_tag);
if (debug) {
console.log('semver=' + JSON.stringify(parsed));
}
if (parsed.matches) {
r.semver_valid = parsed.matches;
r.semver_major = parsed.major;
r.semver_minor = parsed.minor;
r.semver_patch = parsed.patch;
r.semver_build = parsed.build;
r.semver_pre = parsed.pre;
}
}
else if (r.git_is_branch) {
const parsed = semverParser.parseSemVer(r.git_current_branch);
if (debug) {
console.log('semver=' + JSON.stringify(parsed));
}
if (parsed.matches) {
r.semver_valid = parsed.matches;
r.semver_major = parsed.major;
r.semver_minor = parsed.minor;
r.semver_patch = parsed.patch;
r.semver_build = parsed.build;
r.semver_pre = parsed.pre;
}
}
if (debug) {
console.log('results for output:', JSON.stringify(r, null, 2));
}
if (output) {
result_to_output(r);
}
return r;
}
catch (error) {
console.log("partial results before error: ", JSON.stringify(r, null, 2));
core.setFailed(error.message);
process.exit(1);
}
}
-108
View File
@@ -1,108 +0,0 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import * as process from "process";
import * as semverParser from "semver-parser";
export function result_to_output(results) {
for (const key in results) {
if (!results.hasOwnProperty(key)) {
continue;
}
const value = results[key];
if (typeof value !== "string") {
core.setOutput(key, JSON.stringify(value)); // TODO support nested objects with key chaining
} else {
core.setOutput(key, value);
}
}
}
export function gather_information(debug = false, output = false) {
const r = {
git_is_branch: false,
git_is_tag: false,
git_is_pull_request: false,
git_current_branch: false,
git_ref_branch: false,
git_is_default_branch: false,
git_tag: false,
semver_valid: false,
semver_major: false,
semver_minor: false,
semver_patch: false,
semver_build: false,
semver_pre: false,
};
try {
const stripTagPrefix = core.getInput("strip_tag_prefix");
if (debug) console.log(JSON.stringify(github.context, null, 2));
r.git_is_tag = github.context.ref.startsWith("refs/tags/");
r.git_default_branch = github.context.payload?.repository?.defaultBranch;
r.git_is_pull_request = github.context.payload.pull_request != null;
if (r.git_is_pull_request) {
const baseRef = github.context.baseRef || process.env.BODY_REF;
const headRef =
github.context.payload?.pull_request?.head?.ref || process.env.HEAD_REF;
if (debug) console.log("baseRef=" + baseRef);
if (debug) console.log("headRef=" + headRef);
// TODO support PR data
} else if (!r.git_is_tag) {
// regular branches
r.git_is_branch = true;
}
// gather regular branch info
if (r.git_is_branch) {
if (!github.context.ref.startsWith("refs/heads/")) {
throw new Error(
"Failed to determine branch for non-PR and non-Tag action",
);
}
r.git_current_branch = github.context.ref.slice("refs/heads/".length);
r.git_ref_branch = r.git_current_branch; // same as current branch for non-PR, non-Tag
r.git_is_default_branch = r.git_current_branch == r.git_default_branch;
}
// parse semver for tags
if (r.git_is_tag) {
r.git_tag = github.context.ref.slice("refs/tags/".length);
const parsed = semverParser.parseSemVer(r.git_tag);
if (debug) console.log("semver=" + JSON.stringify(parsed));
if (parsed.matches) {
r.semver_valid = parsed.matches;
r.semver_major = parsed.major;
r.semver_minor = parsed.minor;
r.semver_patch = parsed.patch;
r.semver_build = parsed.build;
r.semver_pre = parsed.pre;
}
} else if (r.git_is_branch) {
const parsed = semverParser.parseSemVer(r.git_current_branch);
if (debug) console.log("semver=" + JSON.stringify(parsed));
if (parsed.matches) {
r.semver_valid = parsed.matches;
r.semver_major = parsed.major;
r.semver_minor = parsed.minor;
r.semver_patch = parsed.patch;
r.semver_build = parsed.build;
r.semver_pre = parsed.pre;
}
}
if (debug) console.log("results for output:", JSON.stringify(r, null, 2));
if (output) result_to_output(r);
return r;
} catch (error) {
console.log("partial results before error: ", JSON.stringify(r, null, 2));
core.setFailed(error.message);
process.exit(1);
}
}
+11 -2
View File
@@ -1,3 +1,12 @@
import * as gather from "./gather";
import {collect_ci} from './collect_ci';
import {collect_git} from './collect_git';
export {result_to_output} from './lib';
export {collect_git} from './collect_git';
export {collect_ci} from './collect_ci';
export function collect_all(debug = false, output = false) {
collect_ci(debug, output);
collect_git(debug, output);
}
gather.gather_information(true, true);
+16
View File
@@ -0,0 +1,16 @@
import * as core from '@actions/core';
export function result_to_output(results) {
for (const key in results) {
if (!results.hasOwnProperty(key)) {
continue;
}
const value = results[key];
if (typeof value !== 'string') {
core.setOutput(key, JSON.stringify(value)); // TODO support nested objects with key chaining
}
else {
core.setOutput(key, value);
}
}
}