-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
369 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
const { STATUS, HOOK } = require("../helpers/constants"); | ||
const { getCIInformation } = require('../helpers/ci'); | ||
const { addTeamsExtension, addSlackExtension, addChatExtension } = require('../helpers/extension.helper'); | ||
const { getTeamsMetaDataText, getSlackMetaDataText, getChatMetaDataText } = require('../helpers/metadata.helper'); | ||
|
||
/** | ||
* | ||
* @param {object} param0 - the payload object | ||
* @param {import('..').Extension} param0.extension - The result object | ||
* | ||
*/ | ||
async function run({ target, extension, payload, result }) { | ||
extension.inputs = Object.assign({}, default_inputs, extension.inputs); | ||
if (target.name === 'teams') { | ||
extension.inputs = Object.assign({}, default_inputs_teams, extension.inputs); | ||
const text = await get_text({ target, extension, result }); | ||
if (text) { | ||
addTeamsExtension({ payload, extension, text }); | ||
} | ||
} else if (target.name === 'slack') { | ||
extension.inputs = Object.assign({}, default_inputs_slack, extension.inputs); | ||
const text = await get_text({ target, extension, result }); | ||
if (text) { | ||
addSlackExtension({ payload, extension, text }); | ||
} | ||
} else if (target.name === 'chat') { | ||
extension.inputs = Object.assign({}, default_inputs_chat, extension.inputs); | ||
const text = await get_text({ target, extension, result }); | ||
if (text) { | ||
addChatExtension({ payload, extension, text }); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param {import('..').CIInfoInputs} inputs | ||
*/ | ||
function get_repository_elements(inputs) { | ||
const elements = []; | ||
const ci = getCIInformation(); | ||
if (inputs.show_repository && ci && ci.repository_url && ci.repository_name) { | ||
elements.push({ label: 'Repository', key: ci.repository_name, value: ci.repository_url, type: 'hyperlink' }); | ||
} | ||
if (inputs.show_repository_branch && ci && ci.repository_ref) { | ||
elements.push({ key: 'Branch', value: ci.repository_ref.replace('refs/heads/', '') }); | ||
} | ||
return elements; | ||
} | ||
|
||
/** | ||
* | ||
* @param {import('..').CIInfoInputs} inputs | ||
*/ | ||
function get_build_elements(inputs) { | ||
let elements = []; | ||
const ci = getCIInformation(); | ||
if (inputs.show_build && ci && ci.build_url) { | ||
const name = (ci.build_name || 'Build') + (ci.build_number ? ` #${ci.build_number}` : ''); | ||
elements.push({ key: name, value: ci.build_url, type: 'hyperlink' }); | ||
} | ||
if (inputs.data) { | ||
elements = elements.concat(inputs.data); | ||
} | ||
return elements; | ||
} | ||
|
||
async function get_text({ target, extension, result }) { | ||
const repository_elements = get_repository_elements(extension.inputs); | ||
const build_elements = get_build_elements(extension.inputs); | ||
if (target.name === 'teams') { | ||
const repository_text = await getTeamsMetaDataText({ elements: repository_elements, target, extension, result, default_condition: default_options.condition }); | ||
const build_text = await getTeamsMetaDataText({ elements: build_elements, target, extension, result, default_condition: default_options.condition }); | ||
if (build_text) { | ||
return `${repository_text ? `${repository_text}\n\n` : '' }${build_text}`; | ||
} else { | ||
return repository_text; | ||
} | ||
} else if (target.name === 'slack') { | ||
const repository_text = await getSlackMetaDataText({ elements: repository_elements, target, extension, result, default_condition: default_options.condition }); | ||
const build_text = await getSlackMetaDataText({ elements: build_elements, target, extension, result, default_condition: default_options.condition }); | ||
if (build_text) { | ||
return `${repository_text ? `${repository_text}\n` : '' }${build_text}`; | ||
} else { | ||
return repository_text; | ||
} | ||
} else if (target.name === 'chat') { | ||
const repository_text = await getChatMetaDataText({ elements: repository_elements, target, extension, result, default_condition: default_options.condition }); | ||
const build_text = await getChatMetaDataText({ elements: build_elements, target, extension, result, default_condition: default_options.condition }); | ||
if (build_text) { | ||
return `${repository_text ? `${repository_text}<br>` : '' }${build_text}`; | ||
} else { | ||
return repository_text; | ||
} | ||
} | ||
} | ||
|
||
const default_options = { | ||
hook: HOOK.END, | ||
condition: STATUS.PASS_OR_FAIL, | ||
} | ||
|
||
const default_inputs = { | ||
title: '', | ||
show_repository: true, | ||
show_repository_branch: true, | ||
show_build: true, | ||
} | ||
|
||
const default_inputs_teams = { | ||
|
||
separator: true | ||
} | ||
|
||
const default_inputs_slack = { | ||
separator: false | ||
} | ||
|
||
const default_inputs_chat = { | ||
separator: true | ||
} | ||
|
||
module.exports = { | ||
run, | ||
default_options | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
const { mock } = require('pactum'); | ||
const assert = require('assert'); | ||
const { publish } = require('../src'); | ||
|
||
describe('extensions - ci-info', () => { | ||
|
||
beforeEach(() => { | ||
process.env.GITHUB_ACTIONS = ''; | ||
process.env.GITHUB_SERVER_URL = ''; | ||
process.env.GITHUB_REPOSITORY = ''; | ||
process.env.GITHUB_REF = ''; | ||
process.env.GITHUB_SHA = ''; | ||
process.env.GITHUB_RUN_ID = ''; | ||
process.env.GITHUB_RUN_NUMBER = ''; | ||
process.env.GITHUB_WORKFLOW = ''; | ||
|
||
process.env.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = ''; | ||
process.env.BUILD_REPOSITORY_URI = ''; | ||
process.env.BUILD_REPOSITORY_NAME = ''; | ||
process.env.BUILD_SOURCEBRANCH = ''; | ||
process.env.BUILD_SOURCEVERSION = ''; | ||
process.env.BUILD_BUILDID = ''; | ||
process.env.BUILD_BUILDNUMBER = ''; | ||
process.env.BUILD_DEFINITIONNAME = ''; | ||
}); | ||
|
||
it('should send test-summary with ci-info to teams with no ci information', async () => { | ||
const id = mock.addInteraction('post test-summary 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'; | ||
process.env.GITHUB_REPOSITORY = 'test/test'; | ||
process.env.GITHUB_REF = '/refs/heads/feature-test'; | ||
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 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 azure devops ci information to slack', async () => { | ||
process.env.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = 'https://dev.azure.com/'; | ||
process.env.SYSTEM_TEAMPROJECT = 'test'; | ||
process.env.BUILD_REPOSITORY_URI = 'https://github.com/test/test'; | ||
process.env.BUILD_REPOSITORY_NAME = 'test/test'; | ||
process.env.BUILD_SOURCEBRANCH = '/refs/heads/feature-test'; | ||
process.env.BUILD_SOURCEVERSION = 'sha'; | ||
process.env.BUILD_BUILDID = 'id-123'; | ||
process.env.BUILD_BUILDNUMBER = 'number-123'; | ||
process.env.BUILD_DEFINITIONNAME = 'Build'; | ||
const id = mock.addInteraction('post test-summary with ci-info to slack'); | ||
await publish({ | ||
config: { | ||
targets: [ | ||
{ | ||
name: 'slack', | ||
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); | ||
}); | ||
|
||
afterEach(() => { | ||
mock.clearInteractions(); | ||
}); | ||
|
||
}); |
Oops, something went wrong.