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

Refactor options #14

Merged
merged 4 commits into from
Mar 5, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Comment on lines -22 to -30
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this change, each user doesn't need to set this step!


- 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
}
}
Loading