Skip to content

Commit bde0ac5

Browse files
committed
fix: improve logging
1 parent b5fe2cd commit bde0ac5

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
require('pretty-error').start()
22

3-
import { endGroup, getInput, info, setFailed, startGroup } from '@actions/core'
3+
import * as core from '@actions/core'
44
import githubLabelSync, { LabelInfo } from 'github-label-sync'
55
import fs from 'fs'
66
import path from 'path'
77
import yaml from 'yamljs'
88
import axios from 'axios'
99

10+
const { endGroup, getInput, startGroup } = core
11+
const log = {
12+
info: (str: string) => core.info('🛈 ' + str),
13+
success: (str: string) => core.info('✓ ' + str),
14+
warning: (str: string, showInReport = true) => core[(showInReport ? 'warning' : 'info')]('⚠ ' + str),
15+
error: (str: string, showInReport = true) => core[(showInReport ? 'error' : 'info')]('✗ ' + str),
16+
fatal: (str: string) => core.setFailed('✗ ' + str)
17+
}
1018
let usingLocalFile: boolean
1119

1220
(async () => {
@@ -28,9 +36,9 @@ let usingLocalFile: boolean
2836
})
2937

3038
startGroup('Label diff')
31-
info(JSON.stringify(diff, null, 2))
39+
core.info(JSON.stringify(diff, null, 2))
3240
endGroup()
33-
} catch (e) { setFailed(e) }
41+
} catch (e) { log.fatal(e) }
3442
})()
3543

3644
function isProperConfig(value: any): value is LabelInfo[] {
@@ -92,13 +100,13 @@ async function fetchRepoLabels(repo: string, token?: string): Promise<LabelInfo[
92100

93101
const url = `https://api.github.com/repos/${repo}/labels`,
94102
headers = token ? { Authorization: `token ${token}` } : undefined
95-
info(`Using following URL: ${url}`)
103+
log.info(`Using following URL: ${url}`)
96104

97105
const { data } = await axios.get(url, { headers })
98106
if (!data || !(data instanceof Array))
99107
throw 'Can\'t get label data from GitHub API'
100108

101-
info(`${data.length} labels fetched.`)
109+
log.success(`${data.length} labels fetched.`)
102110
endGroup()
103111

104112
return data.map(element => ({
@@ -111,24 +119,24 @@ async function fetchRepoLabels(repo: string, token?: string): Promise<LabelInfo[
111119

112120
function checkInputs() {
113121
if (!getInput('token'))
114-
setFailed('The token parameter is required.')
122+
throw 'The token parameter is required.'
115123

116124
const configFile = getInput('config-file'),
117125
sourceRepo = getInput('source-repo')
118126

119127
if (!!configFile == !!sourceRepo)
120-
setFailed('You can\'t use a config file and a source repo at the same time. Choose one!')
128+
throw 'You can\'t use a config file and a source repo at the same time. Choose one!'
121129

122130
// config-file: doesn't need evaluation, will be evaluated when parsing
123131
usingLocalFile = !!configFile
124132

125133
if (sourceRepo && sourceRepo.split('/').length != 2)
126-
setFailed('Source repo should be in the owner/repo format, like EndBug/label-sync!')
134+
throw 'Source repo should be in the owner/repo format, like EndBug/label-sync!'
127135
if (sourceRepo && !getInput('source-repo-token'))
128-
info('You\'re using a source repo without a token: if your repository is private the action won\'t be able to read the labels.')
136+
log.warning('You\'re using a source repo without a token: if your repository is private the action won\'t be able to read the labels.', false)
129137

130138
if (!['true', 'false'].includes(getInput('delete-other-labels')))
131-
setFailed('The only values you can use for the `delete-other-labels` option are `true` and `false`')
139+
throw 'The only values you can use for the `delete-other-labels` option are `true` and `false`'
132140
if (!['true', 'false'].includes(getInput('dry-run')))
133-
setFailed('The only values you can use for the `dry-run` option are `true` and `false`')
141+
throw 'The only values you can use for the `dry-run` option are `true` and `false`'
134142
}

0 commit comments

Comments
 (0)