From 602ab5d9c72eb292b6f49998b93750888bc18e8f Mon Sep 17 00:00:00 2001 From: Anudeep Date: Sun, 21 Jul 2024 22:22:15 +0530 Subject: [PATCH] chore: show_repository_non_common (#227) * chore: show_repository_non_common * chore: indentation --- src/extensions/ci-info.extension.js | 60 +++++++++++++++++++++-------- src/index.d.ts | 1 + test/ext-ci-info.spec.js | 39 +++++++++++++++++++ test/mocks/teams.mock.js | 40 +++++++++++++++++++ 4 files changed, 125 insertions(+), 15 deletions(-) diff --git a/src/extensions/ci-info.extension.js b/src/extensions/ci-info.extension.js index aed93ef..dd06cb6 100644 --- a/src/extensions/ci-info.extension.js +++ b/src/extensions/ci-info.extension.js @@ -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) { @@ -17,23 +19,24 @@ 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 }); @@ -41,28 +44,55 @@ class CIInfoExtension extends BaseExtension { 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; } diff --git a/src/index.d.ts b/src/index.d.ts index d2457da..6b2d95c 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -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; diff --git a/test/ext-ci-info.spec.js b/test/ext-ci-info.spec.js index 539fb74..493b2ba 100644 --- a/test/ext-ci-info.spec.js +++ b/test/ext-ci-info.spec.js @@ -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'; diff --git a/test/mocks/teams.mock.js b/test/mocks/teams.mock.js index dd73842..b1d055d 100644 --- a/test/mocks/teams.mock.js +++ b/test/mocks/teams.mock.js @@ -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: {