Skip to content

Commit

Permalink
refactor: move the try catch to the bottom of the script
Browse files Browse the repository at this point in the history
This is mostly for my readability. I found this to be a nice flow when
recently working on our planningcenter/plus-one-action GitHub action.
  • Loading branch information
wassimk committed Jun 4, 2024
1 parent b68d0d8 commit 9cf6a95
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions src/action.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
const core = require('@actions/core')
const github = require('@actions/github')

const run = async () => {
try {
const githubToken = core.getInput('github_token')
async function run() {
const githubToken = core.getInput('github_token')

if (!githubToken) throw Error(`input 'github_token' is required`)
if (!githubToken) throw Error(`input 'github_token' is required`)

const client = github.getOctokit(githubToken)
const owner = github.context.payload.repository.owner.login
const repo = github.context.payload.repository.name
const issue_number = github.context.payload.pull_request.number
const client = github.getOctokit(githubToken)
const owner = github.context.payload.repository.owner.login
const repo = github.context.payload.repository.name
const issue_number = github.context.payload.pull_request.number

const pr = github.context.payload.pull_request
const assignees = pr.assignees
const author = pr.user
const pr = github.context.payload.pull_request
const assignees = pr.assignees
const author = pr.user

console.log(`pr: ${JSON.stringify(pr, undefined, 2)}`)
console.log(`pr: ${JSON.stringify(pr, undefined, 2)}`)

if (
(!assignees || assignees.length === 0) &&
!author.is_bot
) {
await client.request(
`POST /repos/${owner}/${repo}/issues/${issue_number}/assignees`,
{ owner, repo, issue_number, assignees: [author.login] }
)
}
} catch (error) {
console.log('Error:', error)
core.error(error)
core.setFailed(error)
if (
(!assignees || assignees.length === 0) &&
!author.is_bot
) {
await client.request(
`POST /repos/${owner}/${repo}/issues/${issue_number}/assignees`,
{ owner, repo, issue_number, assignees: [author.login] }
)
}
}

run()
try {
run()
} catch (error) {
console.log('Error:', error)
core.error(error)
core.setFailed(error.message)
}

0 comments on commit 9cf6a95

Please sign in to comment.