Skip to content

Commit

Permalink
feat: add badge_url_template input to allow for custom github CDNs
Browse files Browse the repository at this point in the history
  • Loading branch information
prototypicalpro committed Sep 13, 2020
1 parent 3c8f0a3 commit 2762305
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 19 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ This workflow will then download and save each badge to a `readme` folder in the
```
And you're all set!

### Usage With a Custom CDN

If you would like to use a custom CDN instead of `githubusercontent` (such as [githack](https://raw.githack.com/)), you can use the `badge_url_template` input to adjust how badge URLs are compiled:

```yaml
# ...
- name: Compile Badges
uses: prototypicalpro/compile-badges@v1
with:
input_markdown_file: README.md
output_markdown_file: README-compiled.md
badge_url_template: https://raw.githack.com/{repo}/{branch}/{path}/{badge}
# ...
```

The above settings will generate the following markdown output:

```md
<!-- badge-compile -->
![a badge](https://raw.githack.com/<user>/<repo>/<branch>/readme/badge-0.svg)
[![another badge with a link](https://raw.githack.com/<user>/<repo>/<branch>/readme/badge-1.svg)](https://my-project.com)
<!-- badge-compile-stop -->
```

More information about this property can be found in the [Configuration](#configuration) documentation below.

## Configuration

This action takes the following inputs:
Expand Down Expand Up @@ -96,6 +122,23 @@ This action takes the following inputs:
#
# Default: ${{ github.ref }}
current_branch: ''

# The template string used to generate URLs to the compiled badges.
# You can use this field to adjust which CDN this action should
# compile for (ex. githack instead of githubusercontent).
#
# This template is populated using the string-template library
# (https://www.npmjs.com/package/string-template) with the following
# available varibles:
# - `repo`: The string from the `current_repository` input.
# - `branch`: The branch name parsed from the `current_branch` input.
# - `path`: The path supplied by `ouput_image_dir` where badges
# are being stored. This path is automatically stripped of trailing
# slashes.
# - `badge`: The filename of the compiled badge output.
#
# Default: https://raw.githubusercontent.com/{repo}/{branch}/{path}/{badge}
badge_url_template: ''
```
## Example
Expand Down
2 changes: 2 additions & 0 deletions __tests__/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ describe('integration', () => {
)
baseEnv[getInputName(Inputs.OUTPUT_MARKDOWN_FILE)] = filePath
baseEnv[getInputName(Inputs.OUTPUT_IMG_DIR)] = 'test-svg-dir'
baseEnv[getInputName(Inputs.URL)] =
'https://raw.githubusercontent.com/{repo}/{branch}/{path}/{badge}'
})

afterEach(async () => {
Expand Down
68 changes: 68 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import run, {
scanForBadges,
generateBadgeUrl,
replaceBadgeUrls,
filterBadgeUrls,
fetchAndWriteBadge,
Expand All @@ -10,6 +11,7 @@ import * as tmp from 'tmp'
import * as path from 'path'
import * as fs from 'fs'
import rimraf from 'rimraf'
import compile from 'string-template/compile'

function getInputName(input: string): string {
return `INPUT_${input.replace(/ /g, '_').toUpperCase()}`
Expand Down Expand Up @@ -116,6 +118,62 @@ describe('scanForBadges', () => {
})
})

describe('generateBadgeUrl', () => {
it('templates a badge URL', () => {
const template = compile('https://my.cdn/{repo}/{branch}/{path}/{badge}')
const res = generateBadgeUrl(
template,
'my-repo',
'my-branch',
'my-path',
'my-badge'
)
expect(res).toEqual('https://my.cdn/my-repo/my-branch/my-path/my-badge')
})

it('handles nested paths', () => {
const template = compile('https://my.cdn/{repo}/{branch}/{path}/{badge}')
const res = generateBadgeUrl(
template,
'my-repo',
'my-branch',
'my-path/other-path',
'my-badge'
)
expect(res).toEqual(
'https://my.cdn/my-repo/my-branch/my-path/other-path/my-badge'
)
})

it('handles trailing and preceding slashes', () => {
const template = compile('https://my.cdn/{repo}/{branch}/{path}/{badge}')
const res = generateBadgeUrl(
template,
'my-repo',
'my-branch',
'./my-path/other-path/',
'my-badge'
)
expect(res).toEqual(
'https://my.cdn/my-repo/my-branch/my-path/other-path/my-badge'
)
})

it('handles trailing and preceding backslashes', () => {
const template = compile('https://my.cdn/{repo}/{branch}/{path}/{badge}')
const res = generateBadgeUrl(
template,
'my-repo',
'my-branch',
'\\my-path\\other-path\\',
'my-badge'
)
expect(res).toEqual(
'https://my.cdn/my-repo/my-branch/my-path/other-path/my-badge'
)
})
})

describe('filterBadgeUrls', () => {
it('returns the list of badge urls', () => {
const input = ['https://example.com', 'http://google.com']
Expand Down Expand Up @@ -305,6 +363,8 @@ describe('run', () => {
)
process.env[getInputName(Inputs.OUTPUT_MARKDOWN_FILE)] = filePath
process.env[getInputName(Inputs.OUTPUT_IMG_DIR)] = '__tests__/test-svg-dir'
process.env[getInputName(Inputs.URL)] =
'https://raw.githubusercontent.com/{repo}/{branch}/{path}/{badge}'
})

afterEach(async () => {
Expand Down Expand Up @@ -406,4 +466,12 @@ describe('run', () => {

expect(process.exitCode).not.toEqual(0)
})

it('throws if no url is supplied', async () => {
delete process.env[getInputName(Inputs.URL)]

await run()

expect(process.exitCode).not.toEqual(0)
})
})
18 changes: 18 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ inputs:
generate URLs to raw.githubusercontent.com from a relative path in
the repository. It is recommended this be left as the default value.
default: ${{ github.ref }}
badge_url_template:
required: false
description: |
The template string used to generate URLs to the compiled badges.
You can use this field to adjust which CDN this action should
compile for (ex. githack instead of githubusercontent).
This template is populated using the string-template library
(https://www.npmjs.com/package/string-template) with the following
available varibles:
- `repo`: The string from the `current_repository` input.
- `branch`: The branch name parsed from the `current_branch` input.
- `path`: The path supplied by `ouput_image_dir` where badges
are being stored. This path is automatically stripped of trailing
slashes.
- `badge`: The filename of the compiled badge output.
default: https://raw.githubusercontent.com/{repo}/{branch}/{path}/{badge}

runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
Loading

0 comments on commit 2762305

Please sign in to comment.