Skip to content

Commit

Permalink
Add badge title customization
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Xu authored and Zach Renner committed Jan 4, 2023
1 parent 83b7f42 commit 49667db
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ inputs:
Detailed listing of test suites and test cases will be skipped.
default: 'false'
required: false
badge-title:
description: Customize badge title
required: false
default: 'tests'
token:
description: GitHub Access Token
required: false
Expand Down
18 changes: 10 additions & 8 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class TestReporter {
readonly failOnError = core.getInput('fail-on-error', {required: true}) === 'true'
readonly workDirInput = core.getInput('working-directory', {required: false})
readonly onlySummary = core.getInput('only-summary', {required: false}) === 'true'
readonly badgeTitle = core.getInput('badge-title', {required: false})
readonly token = core.getInput('token', {required: true})
readonly octokit: InstanceType<typeof GitHub>
readonly context = getCheckRunContext()
Expand Down Expand Up @@ -166,9 +167,9 @@ class TestReporter {
// })

core.info('Creating report summary')
const {listSuites, listTests, onlySummary} = this
const {listSuites, listTests, onlySummary, badgeTitle} = this
const baseUrl = ''
const summary = getReport(results, {listSuites, listTests, baseUrl, onlySummary})
const summary = getReport(results, {listSuites, listTests, baseUrl, onlySummary, badgeTitle})
core.info('Summary content:')
core.info(summary)
await fs.promises.writeFile(this.path.replace('*.trx', 'test-summary.md'), summary)
Expand Down
14 changes: 8 additions & 6 deletions src/report/get-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ export interface ReportOptions {
listTests: 'all' | 'failed' | 'none'
baseUrl: string
onlySummary: boolean
badgeTitle: string
}

const defaultOptions: ReportOptions = {
listSuites: 'all',
listTests: 'all',
baseUrl: '',
onlySummary: false
onlySummary: false,
badgeTitle: 'tests'
}

export function getReport(results: TestRunResult[], options: ReportOptions = defaultOptions): string {
Expand Down Expand Up @@ -92,7 +94,7 @@ function getByteLength(text: string): number {

function renderReport(results: TestRunResult[], options: ReportOptions): string[] {
const sections: string[] = []
const badge = getReportBadge(results)
const badge = getReportBadge(results, options)
sections.push(badge)

const runs = getTestRunsReport(results, options)
Expand All @@ -101,14 +103,14 @@ function renderReport(results: TestRunResult[], options: ReportOptions): string[
return sections
}

function getReportBadge(results: TestRunResult[]): string {
function getReportBadge(results: TestRunResult[], options: ReportOptions): string {
const passed = results.reduce((sum, tr) => sum + tr.passed, 0)
const skipped = results.reduce((sum, tr) => sum + tr.skipped, 0)
const failed = results.reduce((sum, tr) => sum + tr.failed, 0)
return getBadge(passed, failed, skipped)
return getBadge(passed, failed, skipped, options)
}

function getBadge(passed: number, failed: number, skipped: number): string {
function getBadge(passed: number, failed: number, skipped: number, options: ReportOptions): string {
const text = []
if (passed > 0) {
text.push(`${passed} passed`)
Expand All @@ -128,7 +130,7 @@ function getBadge(passed: number, failed: number, skipped: number): string {
color = 'yellow'
}
const hint = failed > 0 ? 'Tests failed' : 'Tests passed successfully'
const uri = encodeURIComponent(`tests-${message}-${color}`)
const uri = encodeURIComponent(`${options.badgeTitle}-${message}-${color}`)
return `![${hint}](https://img.shields.io/badge/${uri})`
}

Expand Down

0 comments on commit 49667db

Please sign in to comment.