Skip to content

Commit

Permalink
Use the reference workflow run to infer branch information (#12)
Browse files Browse the repository at this point in the history
* Drop the use of payload

* Use the reference workflow run to infer branch information
  • Loading branch information
viveklak committed Mar 21, 2023
1 parent 891defe commit 67069b5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
17 changes: 7 additions & 10 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: 1 addition & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ function getInput(

async function main(): Promise<void> {
const {
repo: {owner, repo},
payload
repo: {owner, repo}
} = github.context
const lastSuccessfulRun = getInput('last-successful-run-id', {
required: false
Expand All @@ -49,7 +48,6 @@ async function main(): Promise<void> {
currentWorkflowRunId: Number(
mustGetEnvOrInput('GITHUB_RUN_ID', 'workflow-run-id')
),
payload,
limitToPreviousSuccessfulRunCommit: getBooleanInput(
'limit-to-previous-successful-run-commit'
),
Expand Down
16 changes: 7 additions & 9 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as core from '@actions/core'
import * as github from '@actions/github'
import {WebhookPayload} from '@actions/github/lib/interfaces'
/* eslint-disable import/no-unresolved */
import {components} from '@octokit/openapi-types'

Expand All @@ -11,7 +10,6 @@ export interface RunOpts {
repo: string
githubToken: string
currentWorkflowRunId: number
payload: WebhookPayload
limitToPreviousSuccessfulRunCommit: boolean
lastSuccessfulRunId?: number
status?: workflowRunStatus
Expand All @@ -24,20 +22,20 @@ interface workflowRun {
}

export async function run(opts: RunOpts): Promise<void> {
const {owner, repo, payload} = opts
let branch = ''
if (payload.pull_request) {
branch = payload.pull_request.head.ref
} else if (payload.workflow_run) {
branch = payload.workflow_run.head_branch
}
const {owner, repo} = opts

const octokit = github.getOctokit(opts.githubToken)

const repoInfo = await octokit.rest.repos.get({owner, repo})
const defaultBranch = repoInfo.data.default_branch

const {data: current_run} = await octokit.rest.actions.getWorkflowRun({
owner,
repo,
run_id: opts.currentWorkflowRunId
})
const branch = current_run.head_branch ?? defaultBranch
core.info(`Resolved run ${opts.currentWorkflowRunId} to branch: ${branch}`)

const workflow_id = String(current_run.workflow_id)
try {
Expand Down

0 comments on commit 67069b5

Please sign in to comment.