diff --git a/.github/main.workflow b/.github/main.workflow index 506e27a..8fddb9d 100644 --- a/.github/main.workflow +++ b/.github/main.workflow @@ -1,8 +1,12 @@ workflow "Verify labels" { - on = "push" - resolves = "Verify" + on = "pull_request" + resolves = "VerifyLabels" } -action "Verify" { - uses = "yogevbd/enforce-label-action@master" +action "VerifyLabels" { + uses = "yogevbd/enforce-label-action@test" + secrets = ["GITHUB_TOKEN"] + env = { + VALID_LABELS = "bug,enhancement,feature" + } } \ No newline at end of file diff --git a/README.md b/README.md index 2aa126e..62c4f3a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,21 @@ # Enforce PR label Enforce choosing label before merging PR. Usefull for generating automatic changelog and release notes with `github-release-notes` + +## Example usage +Create `.github/main.workflow` containing: + +``` +workflow "Verify labels" { + on = "pull_request" + resolves = "VerifyLabels" +} + +action "VerifyLabels" { + uses = "yogevbd/enforce-label-action@master" + secrets = ["GITHUB_TOKEN"] + env = { + VALID_LABELS = "bug,enhancement,feature" + } +} +``` \ No newline at end of file diff --git a/lib/run.js b/lib/run.js index e2b785c..af66ffc 100644 --- a/lib/run.js +++ b/lib/run.js @@ -1,8 +1,11 @@ const request = require('./request') -const { GITHUB_SHA, GITHUB_EVENT_PATH, GITHUB_TOKEN, GITHUB_WORKSPACE } = process.env +const { GITHUB_SHA, GITHUB_EVENT_PATH, GITHUB_TOKEN } = process.env const event = require(GITHUB_EVENT_PATH) -const { repository } = event +const { repository, pull_request } = event +const labels = pull_request.labels.map((l) => l.name); +const validLabels = process.env.VALID_LABELS.split(','); + const { owner: { login: owner } } = repository @@ -16,7 +19,7 @@ const headers = { Authorization: `Bearer ${GITHUB_TOKEN}`, 'User-Agent': 'enforce-label-action' } - + async function createCheck() { const body = { name: checkName, @@ -35,11 +38,16 @@ async function createCheck() { } function verifyLabel() { + let success = 0; + if (labels.some(l => validLabels.includes(l))) { + success = 1; + } + return { - conclusion: 'success', + conclusion: success == 1 ? 'success' : 'failure', output: { - title: 'checkName', - summary: `${'errorCount'} error(s), ${'warningCount'} warning(s) found` + title: `Please add at least one valid label - ${validLabels}`, + summary: `` } } } @@ -73,9 +81,9 @@ async function run() { const id = await createCheck() try { const { conclusion, output } = verifyLabel() - console.log(output.summary) await updateCheck(id, conclusion, output) if (conclusion === 'failure') { + console.log(conclusion) process.exit(78) } } catch (err) {