Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Use the reference workflow run to infer branch information #12

Merged
merged 2 commits into from
Mar 21, 2023
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
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