Skip to content

Commit

Permalink
feat: detect github server url (#76)
Browse files Browse the repository at this point in the history
* feat: autodetect github server url

* test: add check for private github server url

* fix: use serverUrl from octokit

* chore: add debug output for github url

* chore: update dist with latest changes
  • Loading branch information
c0un7-z3r0 authored Nov 23, 2023
1 parent b326885 commit e99a9b5
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 22 deletions.
26 changes: 26 additions & 0 deletions __tests__/coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('get coverage report', () => {
const options: Options = {
token: 'token_123',
repository: 'MishaKav/jest-coverage-comment',
serverUrl: 'https://github.com',
commit: '05953710b21d222efa4f4535424a7af367be5a57',
watermark: '<!-- Jest Coverage Comment: 1 -->\n',
summaryTitle: '',
Expand Down Expand Up @@ -40,6 +41,31 @@ describe('get coverage report', () => {
`"<details><summary>Coverage Report (<b>71%</b>)</summary><table><tr><th>File</th><th>% Stmts</th><th>% Branch</th><th>% Funcs</th><th>% Lines</th><th>Uncovered Line #s</th></tr><tbody><tr><td><b>All files</b></td><td><b>70.21</b></td><td><b>100</b></td><td><b>28.57</b></td><td><b>71.73</b></td><td>&nbsp;</td></tr><tr><td>src</td><td>68.29</td><td>100</td><td>33.33</td><td>68.29</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/controller.js">controller.js</a></td><td>46.66</td><td>100</td><td>33.33</td><td>46.66</td><td><a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/controller.js#L5-L9">5&ndash;9</a>, <a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/controller.js#L23-L27">23&ndash;27</a></td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/index.js">index.js</a></td><td>85.71</td><td>100</td><td>0</td><td>85.71</td><td><a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/index.js#L9">9</a></td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/router.js">router.js</a></td><td>100</td><td>100</td><td>100</td><td>100</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/service.js">service.js</a></td><td>69.23</td><td>100</td><td>50</td><td>69.23</td><td><a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/service.js#L16-L20">16&ndash;20</a></td></tr><tr><td>src/utils</td><td>83.33</td><td>100</td><td>0</td><td>100</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/utils/config.js">config.js</a></td><td>100</td><td>100</td><td>100</td><td>100</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/utils/utils.js">utils.js</a></td><td>75</td><td>100</td><td>0</td><td>100</td><td>&nbsp;</td></tr></tbody></table></details>"`
)
})
test('should return coverage report with custom github server url', () => {
const {
coverageHtml,
coverage,
color,
branches,
functions,
lines,
statements,
} = getCoverageReport({
...options,
serverUrl: 'https://private.azure.github.com',
})

expect(lines).toBe(coverage)
expect(coverage).toBe(71)
expect(color).toBe('yellow')
expect(branches).toBe(100)
expect(functions).toBe(28)
expect(lines).toBe(71)
expect(statements).toBe(70)
expect(coverageHtml).toMatchInlineSnapshot(
`"<details><summary>Coverage Report (<b>71%</b>)</summary><table><tr><th>File</th><th>% Stmts</th><th>% Branch</th><th>% Funcs</th><th>% Lines</th><th>Uncovered Line #s</th></tr><tbody><tr><td><b>All files</b></td><td><b>70.21</b></td><td><b>100</b></td><td><b>28.57</b></td><td><b>71.73</b></td><td>&nbsp;</td></tr><tr><td>src</td><td>68.29</td><td>100</td><td>33.33</td><td>68.29</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/controller.js">controller.js</a></td><td>46.66</td><td>100</td><td>33.33</td><td>46.66</td><td><a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/controller.js#L5-L9">5&ndash;9</a>, <a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/controller.js#L23-L27">23&ndash;27</a></td></tr><tr><td>&nbsp; &nbsp;<a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/index.js">index.js</a></td><td>85.71</td><td>100</td><td>0</td><td>85.71</td><td><a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/index.js#L9">9</a></td></tr><tr><td>&nbsp; &nbsp;<a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/router.js">router.js</a></td><td>100</td><td>100</td><td>100</td><td>100</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/service.js">service.js</a></td><td>69.23</td><td>100</td><td>50</td><td>69.23</td><td><a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/service.js#L16-L20">16&ndash;20</a></td></tr><tr><td>src/utils</td><td>83.33</td><td>100</td><td>0</td><td>100</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/utils/config.js">config.js</a></td><td>100</td><td>100</td><td>100</td><td>100</td><td>&nbsp;</td></tr><tr><td>&nbsp; &nbsp;<a href="https://private.azure.github.com/MishaKav/jest-coverage-comment/blob/05953710b21d222efa4f4535424a7af367be5a57/src/utils/utils.js">utils.js</a></td><td>75</td><td>100</td><td>0</td><td>100</td><td>&nbsp;</td></tr></tbody></table></details>"`
)
})

test('should return coverage report without links to files', () => {
const { coverageHtml } = getCoverageReport({
Expand Down
1 change: 1 addition & 0 deletions __tests__/junit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('parse junit and check report output', () => {
const options = {
token: 'token_123',
repository: 'MishaKav/jest-coverage-comment',
serverUrl: 'https://github.com',
commit: '05953710b21d222efa4f4535424a7af367be5a57',
watermark: '<!-- Jest Coverage Comment: 1 -->\n',
summaryTitle: '',
Expand Down
5 changes: 4 additions & 1 deletion __tests__/summary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ describe('summary to td', () => {
describe('parse summary', () => {
const options: Options = {
token: 'token_123',
serverUrl: 'https://github.com',
repository: 'MishaKav/jest-coverage-comment',
commit: '05953710b21d222efa4f4535424a7af367be5a57',
watermark: '<!-- Jest Coverage Comment: 1 -->\n',
Expand Down Expand Up @@ -106,7 +107,7 @@ describe('should parse summary', () => {
expect(parsedSummary).toBeNull()
expect(spyCore.error).toHaveBeenCalledTimes(1)
expect(spyCore.error).toHaveBeenCalledWith(
'Parse summary report. Unexpected token b in JSON at position 0'
'Parse summary report. Unexpected token \'b\', "bad content" is not valid JSON'
)
})
})
Expand All @@ -115,6 +116,7 @@ describe('summary to markdown', () => {
const options: Options = {
token: 'token_123',
repository: 'MishaKav/jest-coverage-comment',
serverUrl: 'https://github.com',
commit: '05953710b21d222efa4f4535424a7af367be5a57',
watermark: '<!-- Jest Coverage Comment: 1 -->\n',
summaryTitle: '',
Expand Down Expand Up @@ -150,6 +152,7 @@ describe('coverage when have default values', () => {
const options: Options = {
token: 'token_123',
repository: 'MishaKav/jest-coverage-comment',
serverUrl: 'https://github.com',
commit: '05953710b21d222efa4f4535424a7af367be5a57',
watermark: '<!-- Jest Coverage Comment: 1 -->\n',
summaryTitle: '',
Expand Down
23 changes: 14 additions & 9 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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jest-coverage-comment",
"version": "1.0.23",
"version": "1.0.25",
"description": "Comments a pull request or commit with the jest code coverage badge, full report and tests summary",
"author": "Misha Kav <[email protected]>",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ async function main(): Promise<void> {
const options: Options = {
token: 'token_123',
repository: 'MishaKav/jest-coverage-comment',
serverUrl: 'https://github.com',
commit: '05953710b21d222efa4f4535424a7af367be5a57',
watermark: '<!-- Jest Coverage Comment: 1 -->\n',
title: 'Jest Coverage Comment',
Expand Down
25 changes: 20 additions & 5 deletions src/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,21 @@ function toFileNameTd(
indent = false,
options: Options
): string {
const relative = line.file.replace(options.prefix, '')
const href = `https://github.com/${options.repository}/blob/${options.commit}/${options.coveragePathPrefix}${relative}`
const {
serverUrl = 'https://github.com',
repository,
prefix,
commit,
coveragePathPrefix,
removeLinksToFiles,
} = options
const relative = line.file.replace(prefix, '')
const href = `${serverUrl}/${repository}/blob/${commit}/${coveragePathPrefix}${relative}`
const parts = relative.split('/')
const last = parts[parts.length - 1]
const space = indent ? '&nbsp; &nbsp;' : ''

return options.removeLinksToFiles
return removeLinksToFiles
? `${space}${last}`
: `${space}<a href="${href}">${last}</a>`
}
Expand All @@ -148,13 +156,20 @@ function toMissingTd(line: CoverageLine, options: Options): string {

return line.uncoveredLines
.map((range) => {
const {
serverUrl = 'https://github.com',
repository,
commit,
coveragePathPrefix,
removeLinksToLines,
} = options
const [start, end = start] = range.split('-')
const fragment = start === end ? `L${start}` : `L${start}-L${end}`
const relative = line.file
const href = `https://github.com/${options.repository}/blob/${options.commit}/${options.coveragePathPrefix}${relative}#${fragment}`
const href = `${serverUrl}/${repository}/blob/${commit}/${coveragePathPrefix}${relative}#${fragment}`
const text = start === end ? start : `${start}&ndash;${end}`

return options.removeLinksToLines ? text : `<a href="${href}">${text}</a>`
return removeLinksToLines ? text : `<a href="${href}">${text}</a>`
})
.join(', ')
}
Expand Down
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getMultipleJunitReport } from './multi-junit-files'
async function main(): Promise<void> {
try {
const token = core.getInput('github-token', { required: true })

const title = core.getInput('title', { required: false })
const badgeTitle = core.getInput('badge-title', { required: false })
const hideSummary = core.getBooleanInput('hide-summary', {
Expand Down Expand Up @@ -59,6 +60,9 @@ async function main(): Promise<void> {
required: false,
})

const serverUrl = context.serverUrl || 'https://github.com'
core.info(`Uses Github URL: ${serverUrl}`)

const { repo, owner } = context.repo
const { eventName, payload } = context
const watermarkUniqueId = uniqueIdForComment
Expand All @@ -70,6 +74,7 @@ async function main(): Promise<void> {
const options: Options = {
token,
repository: `${owner}/${repo}`,
serverUrl,
prefix: `${process.env.GITHUB_WORKSPACE}/`,
commit: '',
watermark,
Expand Down
14 changes: 10 additions & 4 deletions src/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ export function summaryToMarkdown(
options: Options,
withoutHeader = false
): string {
const { repository, commit, badgeTitle } = options
const {
repository,
commit,
badgeTitle,
serverUrl = 'https://github.com',
summaryTitle,
} = options
const { statements, functions, branches } = summary
const { color, coverage } = getCoverage(summary)
const readmeHref = `https://github.com/${repository}/blob/${commit}/README.md`
const readmeHref = `${serverUrl}/${repository}/blob/${commit}/README.md`
const badge = `<a href="${readmeHref}"><img alt="${badgeTitle}: ${coverage}%" src="https://img.shields.io/badge/${badgeTitle}-${coverage}%25-${color}.svg" /></a><br/>`

const tableHeader =
Expand All @@ -60,8 +66,8 @@ export function summaryToMarkdown(
return tableBody
}

if (options.summaryTitle) {
return `## ${options.summaryTitle}\n\n${table}`
if (summaryTitle) {
return `## ${summaryTitle}\n\n${table}`
}

return table
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface Options {
token: string
repository: string
serverUrl: string
summaryFile: string
summaryTitle?: string
badgeTitle: string
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"skipLibCheck": true
},
"exclude": ["node_modules", "**/*.test.ts", "__tests__/setup.ts"]
}

0 comments on commit e99a9b5

Please sign in to comment.