Skip to content

Commit ffa5a00

Browse files
authored
Add Default Value (#87)
- Adds a default value (set to the issue or PR number if the triggering event is an `issues` or `pull_request` event). - Updates some linting configuration.
2 parents a888247 + f5f96ed commit ffa5a00

File tree

9 files changed

+75
-91
lines changed

9 files changed

+75
-91
lines changed

.mega-linter.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ DISABLE:
2020
# https://megalinter.io/latest/config-activation/
2121
DISABLE_LINTERS:
2222
- MARKDOWN_MARKDOWN_TABLE_FORMATTER
23+
- REPOSITORY_KICS
24+
- REPOSITORY_TRIVY
2325
- TYPESCRIPT_STANDARD
2426

2527
# List of enabled but not blocking linters keys
@@ -135,7 +137,7 @@ VALIDATE_ALL_CODEBASE: true
135137
JAVASCRIPT_ES_CONFIG_FILE: .eslintrc.yml
136138
JAVASCRIPT_PRETTIER_CONFIG_FILE: prettierrc.yml
137139
JSON_PRETTIER_CONFIG_FILE: prettierrc.yml
138-
MARKDOWN_MARKDOWNLINT_CONFIG_FILE: .markdownlint.yml
140+
MARKDOWN_MARKDOWNLINT_CONFIG_FILE: .markdown-lint.yml
139141
TYPESCRIPT_ES_CONFIG_FILE: .eslintrc.yml
140142
TYPESCRIPT_PRETTIER_CONFIG_FILE: .prettierrc.yml
141143
YAML_PRETTIER_CONFIG_FILE: .prettierrc.yml

README.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# IssueOps Labeler
22

3-
[![Check dist/](https://github.com/issue-ops/labeler/actions/workflows/check-dist.yml/badge.svg)](https://github.com/issue-ops/labeler/actions/workflows/check-dist.yml)
4-
[![CodeQL](https://github.com/issue-ops/labeler/actions/workflows/codeql.yml/badge.svg)](https://github.com/issue-ops/labeler/actions/workflows/codeql.yml)
5-
[![Continuous Integration](https://github.com/issue-ops/labeler/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/issue-ops/labeler/actions/workflows/continuous-integration.yml)
6-
[![Continuous Delivery](https://github.com/issue-ops/labeler/actions/workflows/continuous-delivery.yml/badge.svg)](https://github.com/issue-ops/labeler/actions/workflows/continuous-delivery.yml)
7-
[![Linter](https://github.com/issue-ops/labeler/actions/workflows/linter.yml/badge.svg)](https://github.com/issue-ops/labeler/actions/workflows/linter.yml)
8-
[![Code Coverage](./badges/coverage.svg)](./badges/coverage.svg)
3+
![Check dist/](https://github.com/issue-ops/labeler/actions/workflows/check-dist.yml/badge.svg)
4+
![CodeQL](https://github.com/issue-ops/labeler/actions/workflows/codeql.yml/badge.svg)
5+
![Continuous Integration](https://github.com/issue-ops/labeler/actions/workflows/continuous-integration.yml/badge.svg)
6+
![Continuous Delivery](https://github.com/issue-ops/labeler/actions/workflows/continuous-delivery.yml/badge.svg)
7+
![Linter](https://github.com/issue-ops/labeler/actions/workflows/linter.yml/badge.svg)
8+
![Code Coverage](./badges/coverage.svg)
99

1010
Manage labels for issues and pull requests
1111

@@ -23,6 +23,11 @@ Here is a simple example of how to use this action in your workflow. Make sure
2323
to replace `vX.X.X` with the latest version of this action.
2424

2525
```yaml
26+
on:
27+
issues:
28+
types:
29+
- opened
30+
2631
jobs:
2732
example:
2833
name: Example
@@ -39,7 +44,7 @@ jobs:
3944
uses: issue-ops/[email protected]
4045
with:
4146
action: add
42-
issue_number: 1
47+
issue_number: ${{ github.event.issue.number }}
4348
labels: |
4449
enhancement
4550
great-first-issue
@@ -50,7 +55,7 @@ jobs:
5055
uses: issue-ops/[email protected]
5156
with:
5257
action: remove
53-
issue_number: 1
58+
issue_number: ${{ github.event.issue.number }}
5459
labels: |
5560
enhancement
5661
great-first-issue
@@ -65,14 +70,14 @@ jobs:
6570

6671
## Inputs
6772

68-
| Input | Default | Description |
69-
| -------------- | -------------------------- | ------------------------------ |
70-
| `action` | `add` | The action (`add` or `remove`) |
71-
| `create` | `'false'` | Create label, if not present |
72-
| `github_token` | `${{ github.token }}` | The GitHub API token to use |
73-
| `labels` | `label1` | **Line-separated** label list |
74-
| `issue_number` | | The issue or PR numer |
75-
| `repository` | `${{ github.repository }}` | The repository (`owner/repo`) |
73+
| Input | Default | Description |
74+
| -------------- | ------------------------------------------------------------------------------- | ------------------------------ |
75+
| `action` | `add` | The action (`add` or `remove`) |
76+
| `create` | `'false'` | Create label, if not present |
77+
| `github_token` | `${{ github.token }}` | The GitHub API token to use |
78+
| `labels` | `label1` | **Line-separated** label list |
79+
| `issue_number` | `${{ github.event.issue.number }}` or `${{ github.event.pull_request.number }}` | The issue or PR numer |
80+
| `repository` | `${{ github.repository }}` | The repository (`owner/repo`) |
7681

7782
> [!WARNING]
7883
>

__tests__/main.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ describe('Add Labels', () => {
9898

9999
await main.run()
100100

101-
expect(core.error).toHaveBeenCalledWith({
102-
status: 500,
103-
message: 'API error'
104-
})
105101
expect(core.setFailed).toHaveBeenCalledWith('API error')
106102
})
107103

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ inputs:
2525
issue_number:
2626
description: The issue or PR number
2727
required: true
28+
default:
29+
${{ github.event.issue.number || github.event.pull_request.number }}
2830
repository:
2931
description: The repository to use
3032
required: false

dist/index.js

Lines changed: 22 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

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

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "labeler",
33
"description": "Manage labels for issues and pull requests",
4-
"version": "2.0.0",
4+
"version": "2.1.0",
55
"type": "module",
66
"author": "Nick Alteen <[email protected]>",
77
"homepage": "https://github.com/issue-ops/labeler#readme",

src/main.ts

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,34 @@ import { Octokit } from '@octokit/rest'
55
* The entrypoint for the action
66
*/
77
export async function run(): Promise<void> {
8-
// Get input: action
8+
// Get inputs
99
const action: string = core.getInput('action', { required: true })
10-
11-
// Verify action is `add` or `remove`
12-
if (!['add', 'remove'].includes(action)) {
13-
core.setFailed(`Invalid action: ${action}`)
14-
return
15-
}
16-
17-
// Get input: create
1810
const create: boolean = core.getInput('create') === 'true'
19-
20-
// Get input: github_token
2111
const githubToken: string = core.getInput('github_token', { required: true })
22-
23-
// Get input: labels
2412
const labels: string[] = core
2513
.getInput('labels', { required: true })
2614
.trim()
2715
.split('\n')
28-
29-
// Get input: number
3016
const issueNumber: number = parseInt(
3117
core.getInput('issue_number', { required: true })
3218
)
33-
34-
// Get input: repository
35-
const repositoryInput: string = core.getInput('repository', {
19+
const repository: string = core.getInput('repository', {
3620
required: true
3721
})
38-
const owner: string = repositoryInput.split('/')[0]
39-
const repository: string = repositoryInput.split('/')[1]
22+
23+
core.info('Running action with the following inputs:')
24+
core.info(` - Action: ${action}`)
25+
core.info(` - Create: ${create}`)
26+
core.info(` - Issue Number: ${issueNumber}`)
27+
core.info(` - Labels: ${labels.join(', ')}`)
28+
core.info(` - Repository: ${repository}`)
29+
30+
// Verify action is `add` or `remove`
31+
if (!['add', 'remove'].includes(action))
32+
return core.setFailed(`Invalid action: ${action}`)
33+
34+
const owner: string = repository.split('/')[0]
35+
const repo: string = repository.split('/')[1]
4036

4137
// Create the Octokit client
4238
const github: Octokit = new Octokit({ auth: githubToken })
@@ -50,44 +46,39 @@ export async function run(): Promise<void> {
5046
await github.rest.issues.getLabel({
5147
name: label,
5248
owner,
53-
repo: repository
49+
repo
5450
})
5551
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5652
} catch (error: any) {
5753
// Raise error if it's not a 404
58-
if (error.status !== 404) {
59-
core.error(error)
60-
core.setFailed(error.message)
61-
return
62-
}
54+
if (error.status !== 404) return core.setFailed(error.message)
6355

6456
// Label doesn't exist
6557
missingLabels.push(label)
6658
}
6759
}
6860

6961
// Create missing labels (assign a random-ish color)
70-
if (create && missingLabels.length > 0) {
62+
if (create)
7163
for (const label of missingLabels) {
7264
await github.rest.issues.createLabel({
7365
name: label,
7466
owner,
75-
repo: repository,
67+
repo,
7668
color: Math.floor((Math.random() * 0xffffff) << 0)
7769
.toString(16)
7870
.padStart(6, '0')
7971
})
8072

8173
core.info(`Created label: ${label}`)
8274
}
83-
}
8475

8576
// Add the labels to the issue
8677
await github.rest.issues.addLabels({
8778
issue_number: issueNumber,
8879
labels,
8980
owner,
90-
repo: repository
81+
repo
9182
})
9283

9384
core.info(`Added labels to #${issueNumber}: ${labels.join(', ')}`)
@@ -98,16 +89,12 @@ export async function run(): Promise<void> {
9889
issue_number: issueNumber,
9990
name: label,
10091
owner,
101-
repo: repository
92+
repo
10293
})
10394
// eslint-disable-next-line @typescript-eslint/no-explicit-any
10495
} catch (error: any) {
10596
// Raise error if it's not a 404
106-
if (error.status !== 404) {
107-
core.error(error)
108-
core.setFailed(error.message)
109-
return
110-
}
97+
if (error.status !== 404) return core.setFailed(error.message)
11198
}
11299
}
113100

0 commit comments

Comments
 (0)