Skip to content

Commit

Permalink
Merge pull request #16 from yogevbd/descriptionSupport
Browse files Browse the repository at this point in the history
Add description text support
  • Loading branch information
yogevbd authored Apr 29, 2020
2 parents ec1ada8 + 48bac4f commit 8d1e170
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/enforce-labels.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
name: Enforce PR labels for Github Actions
name: Enforce PR labels

on:
pull_request:
types: [opened, labeled, unlabeled]
types: [labeled, unlabeled, opened, edited, synchronize]
jobs:
enforce-label:
runs-on: ubuntu-latest
steps:
- uses: yogevbd/enforce-label-action@master
with:
REQUIRED_LABELS_ANY: "bug,enhancement,feature"
REQUIRED_LABELS_ANY: "bug,enhancement,skip-changelog"
BANNED_LABELS: "banned"
REQUIRED_LABELS_ANY_DESCRIPTION: "Select at least one label ['bug','enhancement','feature']"
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
# Enforce PR label
# Enforce PR labels

Enforce choosing label before merging PR. Usefull for generating automatic changelog and release notes with `github-release-notes`
Enforce assigning labels before merging PR's. Usefull for generating automatic changelog and release notes with `github-release-notes`

## Example usage
Create `.github/workflows/enforce-labels.yml` containing:

```yml
name: Require PR label
name: Enforce PR labels

on:
pull_request:
types: [opened, labeled, unlabeled]

types: [labeled, unlabeled, opened, edited, synchronize]
jobs:
require-label:
enforce-label:
runs-on: ubuntu-latest
steps:
- uses: yogevbd/enforce-label-action@2.0.0
- uses: yogevbd/enforce-label-action@2.1.0
with:
REQUIRED_LABELS_ANY: "bug,enhancement,wontfix"
REQUIRED_LABELS_ALL: "required"
REQUIRED_LABELS_ANY: "bug,enhancement,skip-changelog"
REQUIRED_LABELS_ANY_DESCRIPTION: "Select at least one label ['bug','enhancement','skip-changelog']"
BANNED_LABELS: "banned"


```
**`REQUIRED_LABELS_ANY`**: Force PR's to have at least one label, accepts multiple labels seperated by a comma.
**`REQUIRED_LABELS_ALL`**: Force PR's to have all labels, accepts multiple labels seperated by a comma.
**`BANNED_LABELS`**: Prevent PR's with those labels to be merged, accepts multiple labels seperated by a comma.

### Descriptions
**`REQUIRED_LABELS_ANY_DESCRIPTION`**: Controls the required any labels failure description. Default is `Please select one of the required labels for this PR: ${requiredLabelsAny}`.
**`REQUIRED_LABELS_ALL_DESCRIPTION`**: Controls the required all labels failure description. Default is `All labels are required for this PR: ${requiredLabelsAll}`.
**`BANNED_LABELS_DESCRIPTION`**: Controls the banned failure description. Default is `${bannedLabel.name} label is banned`.
12 changes: 9 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
name: 'Enforce PR labels for Github Actions'
description: 'Enforce PR labels for Github Actions'
name: 'Enforce PR labels'
description: 'Enforce PR labels'
author: 'Yogev Ben David'
runs:
using: 'docker'
image: 'Dockerfile'
inputs:
REQUIRED_LABELS_ANY:
description: 'Require at least one label'
REQUIRED_LABELS_ANY_DESCRIPTION:
description: 'Require at least one label description'
REQUIRED_LABELS_ALL:
description: 'Require all labels'
REQUIRED_LABELS_ALL_DESCRIPTION:
description: 'Require all labels description'
BANNED_LABELS:
description: 'Banned'
description: 'Banned'
BANNED_LABELS_DESCRIPTION:
description: 'Banned description'
59 changes: 37 additions & 22 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,51 @@ import * as github from '@actions/github';

async function run() {
try {
const requiredLabelsInput = core.getInput('REQUIRED_LABELS_ANY', {required: false});
const requiredLabels = requiredLabelsInput !== '' && requiredLabelsInput.split(',');

const requiredLabelsAllInput = core.getInput('REQUIRED_LABELS_ALL', {required: false});
const requiredLabelsAll = requiredLabelsAllInput !== '' && requiredLabelsAllInput.split(',');

const bannedLabelsInput = core.getInput('BANNED_LABELS', {required: false});
const bannedLabels = bannedLabelsInput !== '' && bannedLabelsInput.split(',');
console.log(`github.context: ${JSON.stringify(github.context)}`);
const labels = github.context!.payload!.pull_request!.labels;

core.debug(`Verified PR match valid labels: ${requiredLabels}`);
enforceAnyLabels(labels);
enforceAllLabels(labels);
enforceBannedLabels(labels);

const labels = github.context!.payload!.pull_request!.labels;
console.log(`PR labels: ${JSON.stringify(github.context)}`);
} catch (error) {
core.setFailed(error.message);
}
}

if (requiredLabelsAll && !requiredLabelsAll.every(requiredLabel => labels.find(l => l.name === requiredLabel))) {
core.setFailed(`All labels are required for this PR: ${requiredLabelsAll}`);
}
function enforceAnyLabels(labels) {
const requiredLabelsAny: string[] = getInputArray('REQUIRED_LABELS_ANY');
if (requiredLabelsAny.length > 0 && !requiredLabelsAny.some(requiredLabel => labels.find((l) => l.name === requiredLabel))) {
const requiredLabelsAnyDescription = getInputString('REQUIRED_LABELS_ANY_DESCRIPTION', `Please select one of the required labels for this PR: ${requiredLabelsAny}`);
core.setFailed(requiredLabelsAnyDescription);
}
}

if (requiredLabels && !labels.some(l => requiredLabels.includes(l.name))) {
core.setFailed(`Please select one of the required labels for this PR: ${requiredLabels}`);
}
function enforceAllLabels(labels) {
const requiredLabelsAll = getInputArray('REQUIRED_LABELS_ALL');
if (!requiredLabelsAll.every(requiredLabel => labels.find(l => l.name === requiredLabel))) {
const requiredLabelsAllDescription = getInputString('REQUIRED_LABELS_ALL_DESCRIPTION', `All labels are required for this PR: ${requiredLabelsAll}`);
core.setFailed(requiredLabelsAllDescription);
}
}

let bannedLabel;
function enforceBannedLabels(labels) {
const bannedLabels = getInputArray('BANNED_LABELS');
let bannedLabel;
if (bannedLabels && (bannedLabel = labels.find(l => bannedLabels.includes(l.name)))) {
core.setFailed(`${bannedLabel.name} label is banned`);
const bannedLabelsDescription = getInputString('BANNED_LABELS_DESCRIPTION', `${bannedLabel.name} label is banned`);
core.setFailed(bannedLabelsDescription);
}
}

} catch (error) {
core.setFailed(error.message);
}
function getInputArray(name): string[] {
const rawInput = core.getInput(name, {required: false});
return rawInput !== '' ? rawInput.split(',') : [];
}

function getInputString(name, defaultValue): string {
const rawInput = core.getInput(name, {required: false});
return rawInput !== '' ? rawInput : defaultValue;
}

run();

0 comments on commit 8d1e170

Please sign in to comment.