Skip to content

Commit

Permalink
feat: support multiple config files (#106)
Browse files Browse the repository at this point in the history
* Allow config-file to be a list of local or remote files

* Update README.md

* Update description in actions.yml
  • Loading branch information
kenodegard authored Jan 19, 2022
1 parent 05aa72c commit e426201
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 29 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ jobs:
steps:
- uses: EndBug/label-sync@v2
with:
# If you want to use a config file, you can put its path or URL here (more info in the paragraphs below)
config-file:
# If you want to use a config file, you can put its path or URL here, multiple files are also allowed (more info in the paragraphs below)
config-file: .github/labels.yml
# as URL:
config-file: https://raw.githubusercontent.com/EndBug/label-sync/main/.github/labels.yml
# as multiple:
config-file: |
https://raw.githubusercontent.com/EndBug/label-sync/main/.github/labels.yml
.github/labels.yml
# If URL: "https://raw.githubusercontent.com/EndBug/label-sync/main/.github/labels.yml"
# If you want to use a source repo, you can put is name here (only the owner/repo format is accepted)
source-repo: owner/repo
Expand Down Expand Up @@ -105,6 +109,16 @@ You can use the "raw" link that GitHub provides for the file:
config-file: 'https://raw.githubusercontent.com/EndBug/label-sync/main/.github/labels.yml'
```

You can also specify several config files (e.g. sync a set of "global" labels as well as a set of "local" labels):

```yaml
- uses: EndBug/label-sync@v2
with:
config-file: |
https://raw.githubusercontent.com/EndBug/label-sync/main/.github/labels.yml
.github/labels.yml
```

This is different than using the `source-repo` option, since this also allows you to use aliases, if the config file has any. If you use the `source-repo` option the action will only copy over the missing labels and update colors, wihtout updating or deleting anything else.

If the URL you're using needs an `Authorization` header (like if, for example, you're fetching it from a private repo), you can put its value in the `request-token` input:
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ inputs:
default: ${{ github.token }}

config-file:
description: The path (or URL) to the JSON or YAML file containing the label config (more info in the README)
description: The path(s) (and/or URL(s)) to the JSON or YAML file containing the label config (more info in the README)
required: false
source-repo:
description: The repo to copy labels from (if not using a config file), in the 'owner/repo' format
Expand Down
28 changes: 14 additions & 14 deletions lib/index.js

Large diffs are not rendered by default.

31 changes: 20 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ const log = {
fatal: (str: string) => core.setFailed('✗ ' + str)
}

type ConfigSource = 'local' | 'remote' | 'repo'
let configSource!: ConfigSource
let configSource!: 'list' | 'repo'
;(async () => {
try {
checkInputs()

let labels: LabelInfo[]
switch (configSource) {
case 'local':
labels = readConfigFile(getInput('config-file'))
break
case 'remote':
labels = await readRemoteConfigFile(getInput('config-file'))
case 'list':
labels = []
for (const cf of getInput('config-file').split('\n')) {
console.log(cf)
if (isURL(cf)) labels.push(...(await readRemoteConfigFile(cf)))
else labels.push(...readConfigFile(cf))
}
break
case 'repo':
labels = await fetchRepoLabels(
Expand Down Expand Up @@ -236,11 +237,19 @@ function checkInputs() {
if (!!configFile && !!sourceRepo)
throw "You can't use a config file and a source repo at the same time. Choose one!"

if (configFile) configSource = isURL(configFile) ? 'remote' : 'local'
else if (sourceRepo) configSource = 'repo'
else throw 'You have to either use a config file or a source repo.'
const sources: ('remote' | 'local')[] = []
if (sourceRepo) configSource = 'repo'
else if (configFile) {
configSource = 'list'
for (const cf of configFile.split('\n')) {
if (isURL(cf)) sources.push('remote')
else sources.push('local')
}
} else throw 'You have to either use a config file or a source repo.'

log.info(`Current config mode: ${configSource}`)
log.info(
`Current config mode: ${sources ? sources.join(', ') : configSource}`
)

if (sourceRepo && sourceRepo.split('/').length != 2)
throw 'Source repo should be in the owner/repo format, like EndBug/label-sync!'
Expand Down

0 comments on commit e426201

Please sign in to comment.