Skip to content

Commit

Permalink
Add Default Value (#87)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
ncalteen authored Nov 1, 2024
2 parents a888247 + f5f96ed commit ffa5a00
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 91 deletions.
4 changes: 3 additions & 1 deletion .mega-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ DISABLE:
# https://megalinter.io/latest/config-activation/
DISABLE_LINTERS:
- MARKDOWN_MARKDOWN_TABLE_FORMATTER
- REPOSITORY_KICS
- REPOSITORY_TRIVY
- TYPESCRIPT_STANDARD

# List of enabled but not blocking linters keys
Expand Down Expand Up @@ -135,7 +137,7 @@ VALIDATE_ALL_CODEBASE: true
JAVASCRIPT_ES_CONFIG_FILE: .eslintrc.yml
JAVASCRIPT_PRETTIER_CONFIG_FILE: prettierrc.yml
JSON_PRETTIER_CONFIG_FILE: prettierrc.yml
MARKDOWN_MARKDOWNLINT_CONFIG_FILE: .markdownlint.yml
MARKDOWN_MARKDOWNLINT_CONFIG_FILE: .markdown-lint.yml
TYPESCRIPT_ES_CONFIG_FILE: .eslintrc.yml
TYPESCRIPT_PRETTIER_CONFIG_FILE: .prettierrc.yml
YAML_PRETTIER_CONFIG_FILE: .prettierrc.yml
Expand Down
37 changes: 21 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# IssueOps Labeler

[![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)
[![CodeQL](https://github.com/issue-ops/labeler/actions/workflows/codeql.yml/badge.svg)](https://github.com/issue-ops/labeler/actions/workflows/codeql.yml)
[![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)
[![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)
[![Linter](https://github.com/issue-ops/labeler/actions/workflows/linter.yml/badge.svg)](https://github.com/issue-ops/labeler/actions/workflows/linter.yml)
[![Code Coverage](./badges/coverage.svg)](./badges/coverage.svg)
![Check dist/](https://github.com/issue-ops/labeler/actions/workflows/check-dist.yml/badge.svg)
![CodeQL](https://github.com/issue-ops/labeler/actions/workflows/codeql.yml/badge.svg)
![Continuous Integration](https://github.com/issue-ops/labeler/actions/workflows/continuous-integration.yml/badge.svg)
![Continuous Delivery](https://github.com/issue-ops/labeler/actions/workflows/continuous-delivery.yml/badge.svg)
![Linter](https://github.com/issue-ops/labeler/actions/workflows/linter.yml/badge.svg)
![Code Coverage](./badges/coverage.svg)

Manage labels for issues and pull requests

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

```yaml
on:
issues:
types:
- opened

jobs:
example:
name: Example
Expand All @@ -39,7 +44,7 @@ jobs:
uses: issue-ops/[email protected]
with:
action: add
issue_number: 1
issue_number: ${{ github.event.issue.number }}
labels: |
enhancement
great-first-issue
Expand All @@ -50,7 +55,7 @@ jobs:
uses: issue-ops/[email protected]
with:
action: remove
issue_number: 1
issue_number: ${{ github.event.issue.number }}
labels: |
enhancement
great-first-issue
Expand All @@ -65,14 +70,14 @@ jobs:

## Inputs

| Input | Default | Description |
| -------------- | -------------------------- | ------------------------------ |
| `action` | `add` | The action (`add` or `remove`) |
| `create` | `'false'` | Create label, if not present |
| `github_token` | `${{ github.token }}` | The GitHub API token to use |
| `labels` | `label1` | **Line-separated** label list |
| `issue_number` | | The issue or PR numer |
| `repository` | `${{ github.repository }}` | The repository (`owner/repo`) |
| Input | Default | Description |
| -------------- | ------------------------------------------------------------------------------- | ------------------------------ |
| `action` | `add` | The action (`add` or `remove`) |
| `create` | `'false'` | Create label, if not present |
| `github_token` | `${{ github.token }}` | The GitHub API token to use |
| `labels` | `label1` | **Line-separated** label list |
| `issue_number` | `${{ github.event.issue.number }}` or `${{ github.event.pull_request.number }}` | The issue or PR numer |
| `repository` | `${{ github.repository }}` | The repository (`owner/repo`) |

> [!WARNING]
>
Expand Down
4 changes: 0 additions & 4 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ describe('Add Labels', () => {

await main.run()

expect(core.error).toHaveBeenCalledWith({
status: 500,
message: 'API error'
})
expect(core.setFailed).toHaveBeenCalledWith('API error')
})

Expand Down
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ inputs:
issue_number:
description: The issue or PR number
required: true
default:
${{ github.event.issue.number || github.event.pull_request.number }}
repository:
description: The repository to use
required: false
Expand Down
52 changes: 22 additions & 30 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "labeler",
"description": "Manage labels for issues and pull requests",
"version": "2.0.0",
"version": "2.1.0",
"type": "module",
"author": "Nick Alteen <[email protected]>",
"homepage": "https://github.com/issue-ops/labeler#readme",
Expand Down
59 changes: 23 additions & 36 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,34 @@ import { Octokit } from '@octokit/rest'
* The entrypoint for the action
*/
export async function run(): Promise<void> {
// Get input: action
// Get inputs
const action: string = core.getInput('action', { required: true })

// Verify action is `add` or `remove`
if (!['add', 'remove'].includes(action)) {
core.setFailed(`Invalid action: ${action}`)
return
}

// Get input: create
const create: boolean = core.getInput('create') === 'true'

// Get input: github_token
const githubToken: string = core.getInput('github_token', { required: true })

// Get input: labels
const labels: string[] = core
.getInput('labels', { required: true })
.trim()
.split('\n')

// Get input: number
const issueNumber: number = parseInt(
core.getInput('issue_number', { required: true })
)

// Get input: repository
const repositoryInput: string = core.getInput('repository', {
const repository: string = core.getInput('repository', {
required: true
})
const owner: string = repositoryInput.split('/')[0]
const repository: string = repositoryInput.split('/')[1]

core.info('Running action with the following inputs:')
core.info(` - Action: ${action}`)
core.info(` - Create: ${create}`)
core.info(` - Issue Number: ${issueNumber}`)
core.info(` - Labels: ${labels.join(', ')}`)
core.info(` - Repository: ${repository}`)

// Verify action is `add` or `remove`
if (!['add', 'remove'].includes(action))
return core.setFailed(`Invalid action: ${action}`)

const owner: string = repository.split('/')[0]
const repo: string = repository.split('/')[1]

// Create the Octokit client
const github: Octokit = new Octokit({ auth: githubToken })
Expand All @@ -50,44 +46,39 @@ export async function run(): Promise<void> {
await github.rest.issues.getLabel({
name: label,
owner,
repo: repository
repo
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
// Raise error if it's not a 404
if (error.status !== 404) {
core.error(error)
core.setFailed(error.message)
return
}
if (error.status !== 404) return core.setFailed(error.message)

// Label doesn't exist
missingLabels.push(label)
}
}

// Create missing labels (assign a random-ish color)
if (create && missingLabels.length > 0) {
if (create)
for (const label of missingLabels) {
await github.rest.issues.createLabel({
name: label,
owner,
repo: repository,
repo,
color: Math.floor((Math.random() * 0xffffff) << 0)
.toString(16)
.padStart(6, '0')
})

core.info(`Created label: ${label}`)
}
}

// Add the labels to the issue
await github.rest.issues.addLabels({
issue_number: issueNumber,
labels,
owner,
repo: repository
repo
})

core.info(`Added labels to #${issueNumber}: ${labels.join(', ')}`)
Expand All @@ -98,16 +89,12 @@ export async function run(): Promise<void> {
issue_number: issueNumber,
name: label,
owner,
repo: repository
repo
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
// Raise error if it's not a 404
if (error.status !== 404) {
core.error(error)
core.setFailed(error.message)
return
}
if (error.status !== 404) return core.setFailed(error.message)
}
}

Expand Down

0 comments on commit ffa5a00

Please sign in to comment.