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

chore: refactor ci info #266

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "testbeats",
"version": "2.1.7",
"version": "2.2.0",
"description": "Publish test results to Microsoft Teams, Google Chat, Slack and InfluxDB",
"main": "src/index.js",
"types": "./src/index.d.ts",
Expand Down
54 changes: 27 additions & 27 deletions src/helpers/ci/azure-devops.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
const { BaseCI } = require('./base.ci');

const ENV = process.env;

/**
* @returns {import('../../extensions/extensions').ICIInfo}
*/
function info() {
const azure_devops = {
ci: 'AZURE_DEVOPS_PIPELINES',
git: 'AZURE_DEVOPS_REPOS',
repository_url: ENV.BUILD_REPOSITORY_URI,
repository_name: ENV.BUILD_REPOSITORY_NAME,
repository_ref: ENV.BUILD_SOURCEBRANCH,
repository_commit_sha: ENV.BUILD_SOURCEVERSION,
branch_url: '',
branch_name: '',
pull_request_url:'',
pull_request_name: '',
build_url: ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + ENV.SYSTEM_TEAMPROJECT + '/_build/results?buildId=' + ENV.BUILD_BUILDID,
build_number: ENV.BUILD_BUILDNUMBER,
build_name: ENV.BUILD_DEFINITIONNAME,
build_reason: ENV.BUILD_REASON,
user: ENV.BUILD_REQUESTEDFOR
class AzureDevOpsCI extends BaseCI {
constructor() {
super();
this.init();
}

azure_devops.branch_url = azure_devops.repository_url + azure_devops.repository_ref.replace('refs/heads/', '/tree/');
azure_devops.branch_name = azure_devops.repository_ref.replace('refs/heads/', '');
init() {
this.setInfo(this.targets.ci, 'AZURE_DEVOPS_PIPELINES');
this.setInfo(this.targets.git, 'AZURE_DEVOPS_REPOS');
this.setInfo(this.targets.repository_url, ENV.BUILD_REPOSITORY_URI);
this.setInfo(this.targets.repository_name, ENV.BUILD_REPOSITORY_NAME);
this.setInfo(this.targets.repository_ref, ENV.BUILD_SOURCEBRANCH);
this.setInfo(this.targets.repository_commit_sha, ENV.BUILD_SOURCEVERSION);
this.setInfo(this.targets.build_url, ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + ENV.SYSTEM_TEAMPROJECT + '/_build/results?buildId=' + ENV.BUILD_BUILDID);
this.setInfo(this.targets.build_number, ENV.BUILD_BUILDNUMBER);
this.setInfo(this.targets.build_name, ENV.BUILD_DEFINITIONNAME);
this.setInfo(this.targets.build_reason, ENV.BUILD_REASON);

this.setInfo(this.targets.branch_url, this.repository_url + this.repository_ref.replace('refs/heads/', '/tree/'));
this.setInfo(this.targets.branch_name, this.repository_ref.replace('refs/heads/', ''));

if (this.repository_ref.includes('refs/pull')) {
this.setInfo(this.targets.pull_request_url, this.repository_url + this.repository_ref.replace('refs/pull/', '/pull/'));
this.setInfo(this.targets.pull_request_name, this.repository_ref.replace('refs/pull/', '').replace('/merge', ''));
}

if (azure_devops.repository_ref.includes('refs/pull')) {
azure_devops.pull_request_url = azure_devops.repository_url + azure_devops.repository_ref.replace('refs/pull/', '/pull/');
azure_devops.pull_request_name = azure_devops.repository_ref.replace('refs/pull/', '').replace('/merge', '');
this.setInfo(this.targets.user, ENV.BUILD_REQUESTEDFOR, true);
}
return azure_devops;
}

module.exports = {
info
AzureDevOpsCI
}
134 changes: 134 additions & 0 deletions src/helpers/ci/base.ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
const os = require('os');
const pkg = require('../../../package.json');

const ENV = process.env;

class BaseCI {
ci = '';
git = '';
repository_url = '';
repository_name = '';
repository_ref = '';
repository_commit_sha = '';
branch_url = '';
branch_name = '';
pull_request_url = '';
pull_request_name = '';
build_url = '';
build_number = '';
build_name = '';
build_reason = '';
user = '';
runtime = '';
runtime_version = '';
os = '';
os_version = '';
testbeats_version = '';

targets = {
ci: 'ci',
git: 'git',
repository_url: 'repository_url',
repository_name: 'repository_name',
repository_ref: 'repository_ref',
repository_commit_sha: 'repository_commit_sha',
branch_url: 'branch_url',
branch_name: 'branch_name',
pull_request_url: 'pull_request_url',
pull_request_name: 'pull_request_name',
build_url: 'build_url',
build_number: 'build_number',
build_name: 'build_name',
build_reason: 'build_reason',
user: 'user',
runtime: 'runtime',
runtime_version: 'runtime_version',
os: 'os',
os_version: 'os_version',
testbeats_version: 'testbeats_version'
}

constructor() {
this.setDefaultInformation();
}

setDefaultInformation() {
this.ci = ENV.TEST_BEATS_CI_NAME;
this.git = ENV.TEST_BEATS_CI_GIT;
this.repository_url = ENV.TEST_BEATS_CI_REPOSITORY_URL;
this.repository_name = ENV.TEST_BEATS_CI_REPOSITORY_NAME;
this.repository_ref = ENV.TEST_BEATS_CI_REPOSITORY_REF;
this.repository_commit_sha = ENV.TEST_BEATS_CI_REPOSITORY_COMMIT_SHA;
this.branch_url = ENV.TEST_BEATS_BRANCH_URL;
this.branch_name = ENV.TEST_BEATS_BRANCH_NAME;
this.pull_request_url = ENV.TEST_BEATS_PULL_REQUEST_URL;
this.pull_request_name = ENV.TEST_BEATS_PULL_REQUEST_NAME;
this.build_url = ENV.TEST_BEATS_CI_BUILD_URL;
this.build_number = ENV.TEST_BEATS_CI_BUILD_NUMBER;
this.build_name = ENV.TEST_BEATS_CI_BUILD_NAME;
this.build_reason = ENV.TEST_BEATS_CI_BUILD_REASON;
this.user = ENV.TEST_BEATS_CI_USER || os.userInfo().username;

const runtime = this.#getRuntimeInfo();
this.runtime = runtime.name;
this.runtime_version = runtime.version;
this.os = os.platform();
this.os_version = os.release();
this.testbeats_version = pkg.version;

}

#getRuntimeInfo() {
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
return { name: 'node', version: process.versions.node };
} else if (typeof Deno !== 'undefined') {
return { name: 'deno', version: Deno.version.deno };
} else if (typeof Bun !== 'undefined') {
return { name: 'bun', version: Bun.version };
} else {
return { name: 'unknown', version: 'unknown' };
}
}

setInfo(target, value, force = false) {
if (force && value) {
this[target] = value;
return;
}
if (!this[target]) {
this[target] = value;
}
}

/**
*
* @returns {import('../../extensions/extensions').ICIInfo}
*/
info() {
return {
ci: this.ci,
git: this.git,
repository_url: this.repository_url,
repository_name: this.repository_name,
repository_ref: this.repository_ref,
repository_commit_sha: this.repository_commit_sha,
branch_url: this.branch_url,
branch_name: this.branch_name,
pull_request_url: this.pull_request_url,
pull_request_name: this.pull_request_name,
build_url: this.build_url,
build_number: this.build_number,
build_name: this.build_name,
build_reason: this.build_reason,
user: this.user,
runtime: this.runtime,
runtime_version: this.runtime_version,
os: this.os,
os_version: this.os_version,
testbeats_version: this.testbeats_version
}
}

}

module.exports = { BaseCI };
47 changes: 25 additions & 22 deletions src/helpers/ci/circle-ci.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
const { BaseCI } = require('./base.ci');

const ENV = process.env;

/**
* @returns {import('../../extensions/extensions').ICIInfo}
*/
function info() {
const circle = {
ci: 'CIRCLE_CI',
git: '',
repository_url: ENV.CIRCLE_REPOSITORY_URL,
repository_name: ENV.CIRCLE_PROJECT_REPONAME, // Need to find a better match
repository_ref: ENV.CIRCLE_BRANCH,
repository_commit_sha: ENV.CIRCLE_SHA1,
branch_url: '',
branch_name: ENV.CIRCLE_BRANCH,
pull_request_url:'',
pull_request_name: '',
build_url: ENV.CIRCLE_BUILD_URL,
build_number: ENV.CIRCLE_BUILD_NUM,
build_name: ENV.CIRCLE_JOB,
build_reason: 'Push',
user: ENV.CIRCLE_USERNAME,
class CircleCI extends BaseCI {
constructor() {
super();
this.init();
}

init() {
this.setInfo(this.targets.ci, 'CIRCLE_CI');
this.setInfo(this.targets.git, '');
this.setInfo(this.targets.repository_url, ENV.CIRCLE_REPOSITORY_URL);
this.setInfo(this.targets.repository_name, ENV.CIRCLE_PROJECT_REPONAME);
this.setInfo(this.targets.repository_ref, ENV.CIRCLE_BRANCH);
this.setInfo(this.targets.repository_commit_sha, ENV.CIRCLE_SHA1);
this.setInfo(this.targets.branch_url, '');
this.setInfo(this.targets.branch_name, ENV.CIRCLE_BRANCH);
this.setInfo(this.targets.pull_request_url,'');
this.setInfo(this.targets.pull_request_name, '');
this.setInfo(this.targets.build_url, ENV.CIRCLE_BUILD_URL);
this.setInfo(this.targets.build_number, ENV.CIRCLE_BUILD_NUM);
this.setInfo(this.targets.build_name, ENV.CIRCLE_JOB);
this.setInfo(this.targets.build_reason, 'Push');
this.setInfo(this.targets.user, ENV.CIRCLE_USERNAME, true);
}
return circle
}

module.exports = {
info
CircleCI
}
54 changes: 26 additions & 28 deletions src/helpers/ci/github.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
const { BaseCI } = require('./base.ci');

const ENV = process.env;

/**
* @returns {import('../../extensions/extensions').ICIInfo}
*/
function info() {
const github = {
ci: 'GITHUB_ACTIONS',
git: 'GITHUB',
repository_url: ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY,
repository_name: ENV.GITHUB_REPOSITORY,
repository_ref: ENV.GITHUB_REF,
repository_commit_sha: ENV.GITHUB_SHA,
branch_url: '',
branch_name: '',
pull_request_url:'',
pull_request_name: '',
build_url: ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY + '/actions/runs/' + ENV.GITHUB_RUN_ID,
build_number: ENV.GITHUB_RUN_NUMBER,
build_name: ENV.GITHUB_WORKFLOW,
build_reason: ENV.GITHUB_EVENT_NAME,
user: ENV.GITHUB_ACTOR,
class GitHubCI extends BaseCI {
constructor() {
super();
this.init();
}

github.branch_url = github.repository_url + github.repository_ref.replace('refs/heads/', '/tree/');
github.branch_name = github.repository_ref.replace('refs/heads/', '');
init() {
this.setInfo(this.targets.ci, 'GITHUB_ACTIONS');
this.setInfo(this.targets.git, 'GITHUB');
this.setInfo(this.targets.repository_url, ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY);
this.setInfo(this.targets.repository_name, ENV.GITHUB_REPOSITORY);
this.setInfo(this.targets.repository_ref, ENV.GITHUB_REF);
this.setInfo(this.targets.repository_commit_sha, ENV.GITHUB_SHA);
this.setInfo(this.targets.build_url, ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY + '/actions/runs/' + ENV.GITHUB_RUN_ID);
this.setInfo(this.targets.build_number, ENV.GITHUB_RUN_NUMBER);
this.setInfo(this.targets.build_name, ENV.GITHUB_WORKFLOW);
this.setInfo(this.targets.build_reason, ENV.GITHUB_EVENT_NAME);
this.setInfo(this.targets.user, ENV.GITHUB_ACTOR, true);

if (github.repository_ref.includes('refs/pull')) {
github.pull_request_url = github.repository_url + github.repository_ref.replace('refs/pull/', '/pull/');
github.pull_request_name = github.repository_ref.replace('refs/pull/', '').replace('/merge', '');
}
this.setInfo(this.targets.branch_url, this.repository_url + this.repository_ref.replace('refs/heads/', '/tree/'));
this.setInfo(this.targets.branch_name, this.repository_ref.replace('refs/heads/', ''));

return github
if (this.repository_ref.includes('refs/pull')) {
this.setInfo(this.targets.pull_request_url, this.repository_url + this.repository_ref.replace('refs/pull/', '/pull/'));
this.setInfo(this.targets.pull_request_name, this.repository_ref.replace('refs/pull/', '').replace('/merge', ''));
}
}
}

module.exports = {
info
GitHubCI
}
Loading
Loading