Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the capability to call the API to get the list of labels #15

Merged
merged 1 commit into from Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -42,6 +42,19 @@ jobs:
labels: "semver:patch, semver:minor, semver:major"
```

By default this actions reads `event.json`, which will not detect when a label is added in an earlier step.
To force an API call, set the `GITHUB_TOKEN` environment variable like so:

```yaml
- uses: mheap/github-action-required-labels@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
mode: exactly
count: 1
labels: "semver:patch, semver:minor, semver:major"
```

### Prevent merging if a label exists

```yaml
Expand Down
13 changes: 10 additions & 3 deletions index.js
Expand Up @@ -27,9 +27,16 @@ Toolkit.run(async (tools) => {
return;
}

const appliedLabels = tools.context.payload.pull_request.labels.map(
(label) => label.name
);
// If a token is provided, call the API, otherwise read the event.json file
let labels;
if (process.env.GITHUB_TOKEN) {
labels = (await tools.github.issues.listLabelsOnIssue(tools.context.issue))
.data;
} else {
labels = tools.context.payload.pull_request.labels;
}

const appliedLabels = labels.map((label) => label.name);

let intersection = allowedLabels.filter((x) => appliedLabels.includes(x));

Expand Down