Skip to content

Commit

Permalink
chore: show_repository_non_common (#227)
Browse files Browse the repository at this point in the history
* chore: show_repository_non_common

* chore: indentation
  • Loading branch information
ASaiAnudeep authored Jul 21, 2024
1 parent 0fa1a5c commit 602ab5d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 15 deletions.
60 changes: 45 additions & 15 deletions src/extensions/ci-info.extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { getCIInformation } = require('../helpers/ci');
const { getMetaDataText } = require("../helpers/metadata.helper");
const { STATUS, HOOK } = require("../helpers/constants");

const COMMON_BRANCH_NAMES = ['main', 'master', 'dev', 'develop', 'qa', 'test'];

class CIInfoExtension extends BaseExtension {

constructor(target, extension, result, payload, root_payload) {
Expand All @@ -17,52 +19,80 @@ class CIInfoExtension extends BaseExtension {
}

#setDefaultOptions() {
this.default_options.hook = HOOK.AFTER_SUMMARY,
this.default_options.hook = HOOK.AFTER_SUMMARY;
this.default_options.condition = STATUS.PASS_OR_FAIL;
}

#setDefaultInputs() {
this.default_inputs.title = '';
this.default_inputs.title_link = '';
this.default_inputs.show_repository = true;
this.default_inputs.show_repository_branch = true;
this.default_inputs.show_repository_non_common = true;
this.default_inputs.show_repository = false;
this.default_inputs.show_repository_branch = false;
this.default_inputs.show_build = true;
}

async run() {
this.ci = getCIInformation();

this.setRepositoryElements();
this.setBuildElements();
this.#setRepositoryElements();
this.#setBuildElements();

const repository_text = await getMetaDataText({ elements: this.repository_elements, target: this.target, extension: this.extension, result: this.result, default_condition: this.default_options.condition });
const build_text = await getMetaDataText({ elements: this.build_elements, target: this.target, extension: this.extension, result: this.result, default_condition: this.default_options.condition });
this.text = this.mergeTexts([repository_text, build_text]);
this.attach();
}

setRepositoryElements() {
#setRepositoryElements() {
if (!this.ci) {
return;
}
if (!this.ci.repository_url || !this.ci.repository_name || !this.ci.repository_ref) {
return;
}

if (this.extension.inputs.show_repository && this.ci.repository_url && this.ci.repository_name) {
this.repository_elements.push({ label: 'Repository', key: this.ci.repository_name, value: this.ci.repository_url, type: 'hyperlink' });
if (this.extension.inputs.show_repository) {
this.#setRepositoryElement();
}
if (this.extension.inputs.show_repository_branch) {
if (this.ci.repository_ref.includes('refs/pull')) {
this.#setPullRequestElement();
} else {
this.#setRepositoryBranchElement();
}
}
if (this.extension.inputs.show_repository_branch && this.ci.repository_ref) {
if (!this.extension.inputs.show_repository && !this.extension.inputs.show_repository_branch && this.extension.inputs.show_repository_non_common) {
if (this.ci.repository_ref.includes('refs/pull')) {
const pr_url = this.ci.repository_url + this.ci.repository_ref.replace('refs/pull/', '/pull/');
const pr_name = this.ci.repository_ref.replace('refs/pull/', '').replace('/merge', '');
this.repository_elements.push({ label: 'Pull Request', key: pr_name, value: pr_url, type: 'hyperlink' });
this.#setRepositoryElement();
this.#setPullRequestElement();
} else {
const branch_url = this.ci.repository_url + this.ci.repository_ref.replace('refs/heads/', '/tree/');
const branch_name = this.ci.repository_ref.replace('refs/heads/', '');
this.repository_elements.push({ label: 'Branch', key: branch_name, value: branch_url, type: 'hyperlink' });
if (!COMMON_BRANCH_NAMES.includes(branch_name.toLowerCase())) {
this.#setRepositoryElement();
this.#setRepositoryBranchElement();
}
}
}
}

setBuildElements() {
#setRepositoryElement() {
this.repository_elements.push({ label: 'Repository', key: this.ci.repository_name, value: this.ci.repository_url, type: 'hyperlink' });
}

#setPullRequestElement() {
const pr_url = this.ci.repository_url + this.ci.repository_ref.replace('refs/pull/', '/pull/');
const pr_name = this.ci.repository_ref.replace('refs/pull/', '').replace('/merge', '');
this.repository_elements.push({ label: 'Pull Request', key: pr_name, value: pr_url, type: 'hyperlink' });
}

#setRepositoryBranchElement() {
const branch_url = this.ci.repository_url + this.ci.repository_ref.replace('refs/heads/', '/tree/');
const branch_name = this.ci.repository_ref.replace('refs/heads/', '');
this.repository_elements.push({ label: 'Branch', key: branch_name, value: branch_url, type: 'hyperlink' });
}

#setBuildElements() {
if (!this.ci) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface MentionInputs extends ExtensionInputs {
}

export interface CIInfoInputs extends ExtensionInputs {
show_repository_non_common?: boolean;
show_repository?: boolean;
show_repository_branch?: boolean;
show_build?: boolean;
Expand Down
39 changes: 39 additions & 0 deletions test/ext-ci-info.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,45 @@ describe('extensions - ci-info', () => {
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with build details when branch is a common branch', async () => {
process.env.GITHUB_ACTIONS = 'GITHUB_ACTIONS';
process.env.GITHUB_SERVER_URL = 'https://github.com';
process.env.GITHUB_REPOSITORY = 'test/test';
process.env.GITHUB_REF = 'refs/heads/master';
process.env.GITHUB_SHA = 'sha';
process.env.GITHUB_RUN_ID = 'id-123';
process.env.GITHUB_RUN_NUMBER = 'number-123';
process.env.GITHUB_WORKFLOW = 'Build';

const id = mock.addInteraction('post test-summary with only build ci-info to teams');
await publish({
config: {
targets: [
{
name: 'teams',
inputs: {
url: 'http://localhost:9393/message'
},
extensions: [
{
name: 'ci-info'
}
]
}
],
results: [
{
type: 'testng',
files: [
'test/data/testng/single-suite.xml'
]
}
]
}
});
assert.equal(mock.getInteraction(id).exercised, true);
});

it('should send test-summary with github ci information to teams', async () => {
process.env.GITHUB_ACTIONS = 'GITHUB_ACTIONS';
process.env.GITHUB_SERVER_URL = 'https://github.com';
Expand Down
40 changes: 40 additions & 0 deletions test/mocks/teams.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,46 @@ addInteractionHandler('post test-summary with ci-info to teams', () => {
}
});

addInteractionHandler('post test-summary with only build ci-info to teams', () => {
return {
request: {
method: 'POST',
path: '/message',
body: {
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"@DATA:TEMPLATE@": "TEAMS_ROOT_TITLE_SINGLE_SUITE"
},
{
"@DATA:TEMPLATE@": "TEAMS_ROOT_RESULTS_SINGLE_SUITE",
},
{
"type": "TextBlock",
"text": "**Build:** [Build #number-123](https://github.com/test/test/actions/runs/id-123)",
"wrap": true,
"separator": true
}
],
"actions": []
}
}
]
}
},
response: {
status: 200
}
}
});

addInteractionHandler('post test-summary with beats to teams with ai failure summary', () => {
return {
request: {
Expand Down

0 comments on commit 602ab5d

Please sign in to comment.