Skip to content

Commit

Permalink
Refactor options (#14)
Browse files Browse the repository at this point in the history
* extract options

* fix input to pass pr_url instead of calculated pr_number
This change makes workflow scripts simpler

* apply this change

* add type annotation
  • Loading branch information
tnyo43 authored Mar 5, 2024
1 parent a2ff615 commit 408beaf
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 30 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,9 @@ jobs:
id: checkout
uses: actions/checkout@v4

- name: Get PR_NUMBER for issue_comment triggered events
id: get-pr-number
if: github.event.issue.pull_request
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_URL="${{ github.event.issue.pull_request.url }}"
PR_NUM=${PR_URL##*/}
echo "pr-number=$PR_NUM" >> "$GITHUB_OUTPUT"
- name: Test Local Action
id: test-action
uses: ./
with:
pr_number: ${{ steps.get-pr-number.outputs.pr-number }}

- name: Print Output
id: output
Expand Down
8 changes: 5 additions & 3 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ inputs:
description: 'a github access token'
required: true
default: ${{ github.token }}
pr_number:
pr_url:
description:
'a target pr number. This is set for events triggered by issue_comment'
required: false
'the target pr url. This is used for taking pr number in workflows
triggered by "issue_comment" event'
required: true
default: ${{ github.event.issue.pull_request.url }}

# Define your outputs here.
outputs:
Expand Down
42 changes: 35 additions & 7 deletions dist/index.js

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

10 changes: 2 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as core from '@actions/core'
import { getOctokit, context } from '@actions/github'
import { getOption } from './option'

const uniqueStringArray = (texts: string[]): string[] => {
if (texts.length === 0) return []
Expand All @@ -22,15 +23,8 @@ const uniqueStringArray = (texts: string[]): string[] => {
*/
export async function run(): Promise<void> {
try {
const prNumber =
context.payload.pull_request?.number ||
Number(core.getInput('pr_number', { required: false }))
if (isNaN(prNumber) || prNumber === 0) {
core.setFailed('pr number is not set properly')
return
}
const { token, prNumber } = getOption()

const token = core.getInput('github_token', { required: true })
const octokit = getOctokit(token)
const owner = context.repo.owner
const repo = context.repo.repo
Expand Down
31 changes: 31 additions & 0 deletions src/option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getInput, setFailed } from '@actions/core'
import { context } from '@actions/github'

type Option = {
token: string
prNumber: number
}

function getPrNumber(): number {
if (typeof context.payload.pull_request?.number === 'number') {
return context.payload.pull_request.number
}

const url = getInput('pr_url', { required: false })
const numberFromUrl = Number(url.substring(url.lastIndexOf('/') + 1))
return numberFromUrl
}

export function getOption(): Option {
const token = getInput('github_token', { required: true })

const prNumber = getPrNumber()
if (isNaN(prNumber) || prNumber === 0) {
setFailed('pr number is not set properly')
}

return {
token,
prNumber
}
}

0 comments on commit 408beaf

Please sign in to comment.