refactor and simplify [AzureDevOpsTests] (#7076)
* refactor: simplify AzureDevOpsTests service tests via helpers * refactor: apply standard service class patterns to AzureDevOpsTests class Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
This commit is contained in:
@@ -2,11 +2,24 @@ import Joi from 'joi'
|
||||
import {
|
||||
testResultQueryParamSchema,
|
||||
renderTestResultBadge,
|
||||
documentation as commonDocumentation,
|
||||
} from '../test-results.js'
|
||||
import AzureDevOpsBase from './azure-devops-base.js'
|
||||
|
||||
const commonAttrs = {
|
||||
keywords: ['vso', 'vsts', 'azure-devops'],
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
branch: 'master',
|
||||
},
|
||||
queryParams: {
|
||||
passed_label: 'passed',
|
||||
failed_label: 'failed',
|
||||
skipped_label: 'skipped',
|
||||
compact_message: null,
|
||||
},
|
||||
documentation: `
|
||||
<p>
|
||||
To obtain your own badge, you need to get 3 pieces of information:
|
||||
@@ -26,19 +39,7 @@ const commonAttrs = {
|
||||
Optionally, you can specify a named branch:
|
||||
<code>https://img.shields.io/azure-devops/tests/ORGANIZATION/PROJECT/DEFINITION_ID/NAMED_BRANCH.svg</code>.
|
||||
</p>
|
||||
<p>
|
||||
You may change the "passed", "failed" and "skipped" text on this badge by supplying query parameters <code>&passed_label=</code>, <code>&failed_label=</code> and <code>&skipped_label=</code> respectively.
|
||||
<br>
|
||||
There is also a <code>&compact_message</code> query parameter, which will default to displaying ✔, ✘ and ➟, separated by a horizontal bar |.
|
||||
<br>
|
||||
For example, if you want to use a different terminology:
|
||||
<br>
|
||||
<code>/azure-devops/tests/ORGANIZATION/PROJECT/DEFINITION_ID.svg?passed_label=good&failed_label=bad&skipped_label=n%2Fa</code>
|
||||
<br>
|
||||
Or, use symbols:
|
||||
<br>
|
||||
<code>/azure-devops/tests/ORGANIZATION/PROJECT/DEFINITION_ID.svg?compact_message&passed_label=%F0%9F%8E%89&failed_label=%F0%9F%92%A2&skipped_label=%F0%9F%A4%B7</code>
|
||||
</p>
|
||||
${commonDocumentation}
|
||||
`,
|
||||
}
|
||||
|
||||
@@ -71,29 +72,6 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
static examples = [
|
||||
{
|
||||
title: 'Azure DevOps tests',
|
||||
pattern: ':organization/:project/:definitionId',
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
},
|
||||
staticPreview: this.render({
|
||||
passed: 20,
|
||||
failed: 1,
|
||||
skipped: 1,
|
||||
total: 22,
|
||||
}),
|
||||
...commonAttrs,
|
||||
},
|
||||
{
|
||||
title: 'Azure DevOps tests (branch)',
|
||||
pattern: ':organization/:project/:definitionId/:branch',
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
branch: 'master',
|
||||
},
|
||||
staticPreview: this.render({
|
||||
passed: 20,
|
||||
failed: 1,
|
||||
@@ -104,15 +82,6 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
},
|
||||
{
|
||||
title: 'Azure DevOps tests (compact)',
|
||||
pattern: ':organization/:project/:definitionId',
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
},
|
||||
queryParams: {
|
||||
compact_message: null,
|
||||
},
|
||||
staticPreview: this.render({
|
||||
passed: 20,
|
||||
failed: 1,
|
||||
@@ -124,16 +93,11 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
},
|
||||
{
|
||||
title: 'Azure DevOps tests with custom labels',
|
||||
pattern: ':organization/:project/:definitionId',
|
||||
namedParams: {
|
||||
organization: 'azuredevops-powershell',
|
||||
project: 'azuredevops-powershell',
|
||||
definitionId: '1',
|
||||
},
|
||||
queryParams: {
|
||||
passed_label: 'good',
|
||||
failed_label: 'bad',
|
||||
skipped_label: 'n/a',
|
||||
compact_message: null,
|
||||
},
|
||||
staticPreview: this.render({
|
||||
passed: 20,
|
||||
@@ -172,15 +136,16 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
})
|
||||
}
|
||||
|
||||
async handle(
|
||||
{ organization, project, definitionId, branch },
|
||||
{
|
||||
compact_message: compactMessage,
|
||||
passed_label: passedLabel,
|
||||
failed_label: failedLabel,
|
||||
skipped_label: skippedLabel,
|
||||
}
|
||||
) {
|
||||
static transform({ aggregatedResultsAnalysis }) {
|
||||
const { totalTests: total, resultsByOutcome } = aggregatedResultsAnalysis
|
||||
const passed = resultsByOutcome.Passed ? resultsByOutcome.Passed.count : 0
|
||||
const failed = resultsByOutcome.Failed ? resultsByOutcome.Failed.count : 0
|
||||
// assume the rest has been skipped
|
||||
const skipped = total - passed - failed
|
||||
return { passed, failed, skipped, total }
|
||||
}
|
||||
|
||||
async fetchTestResults({ organization, project, definitionId, branch }) {
|
||||
const errorMessages = {
|
||||
404: 'build pipeline or test result summary not found',
|
||||
}
|
||||
@@ -193,8 +158,7 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
)
|
||||
|
||||
// https://dev.azure.com/azuredevops-powershell/azuredevops-powershell/_apis/test/ResultSummaryByBuild?buildId=20
|
||||
|
||||
const json = await this.fetch({
|
||||
return await this.fetch({
|
||||
url: `https://dev.azure.com/${organization}/${project}/_apis/test/ResultSummaryByBuild`,
|
||||
options: {
|
||||
qs: { buildId },
|
||||
@@ -202,24 +166,24 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
schema: buildTestResultSummarySchema,
|
||||
errorMessages,
|
||||
})
|
||||
}
|
||||
|
||||
const total = json.aggregatedResultsAnalysis.totalTests
|
||||
|
||||
let passed = 0
|
||||
const passedTests = json.aggregatedResultsAnalysis.resultsByOutcome.Passed
|
||||
if (passedTests) {
|
||||
passed = passedTests.count
|
||||
async handle(
|
||||
{ organization, project, definitionId, branch },
|
||||
{
|
||||
compact_message: compactMessage,
|
||||
passed_label: passedLabel,
|
||||
failed_label: failedLabel,
|
||||
skipped_label: skippedLabel,
|
||||
}
|
||||
|
||||
let failed = 0
|
||||
const failedTests = json.aggregatedResultsAnalysis.resultsByOutcome.Failed
|
||||
if (failedTests) {
|
||||
failed = failedTests.count
|
||||
}
|
||||
|
||||
// assume the rest has been skipped
|
||||
const skipped = total - passed - failed
|
||||
const isCompact = compactMessage !== undefined
|
||||
) {
|
||||
const json = await this.fetchTestResults({
|
||||
organization,
|
||||
project,
|
||||
definitionId,
|
||||
branch,
|
||||
})
|
||||
const { passed, failed, skipped, total } = this.constructor.transform(json)
|
||||
return this.constructor.render({
|
||||
passed,
|
||||
failed,
|
||||
@@ -228,7 +192,7 @@ export default class AzureDevOpsTests extends AzureDevOpsBase {
|
||||
passedLabel,
|
||||
failedLabel,
|
||||
skippedLabel,
|
||||
isCompact,
|
||||
isCompact: compactMessage !== undefined,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user