Skip to content

Commit

Permalink
feat: use workflow_id for workflow match
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma11hewThomas committed Dec 17, 2024
1 parent 35b1511 commit d4651c5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
22 changes: 22 additions & 0 deletions src/client/github/workflows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ import { components } from '@octokit/openapi-types'

type WorkflowRun = components['schemas']['workflow-run']

/**
* Fetches workflow run for a specific run.
* @param owner - The owner of the repository.
* @param repo - The name of the repository.
* @param run_id - The ID of the workflow run.
* @returns An array of workflow runs.
*/
export async function fetchWorkflowRun(
owner: string,
repo: string,
run_id: number
): Promise<WorkflowRun> {
const octokit = await createGitHubClient()
const response = await octokit.actions.getWorkflowRun({
owner,
repo,
run_id
})

return response.data
}

/**
* Fetches workflow runs for a specific repo.
* @param owner - The owner of the repository.
Expand Down
13 changes: 12 additions & 1 deletion src/ctrf/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { context } from '@actions/github'
import {
fetchAllWorkflowRuns,
fetchWorkflowRun,
processArtifactsFromRuns
} from '../client/github'
import { filterWorkflowRuns } from '../github'
Expand Down Expand Up @@ -228,7 +229,17 @@ export async function processPreviousResultsAndMetrics(
context.repo.repo
)

const filteredRuns = filterWorkflowRuns(workflowRuns, githubContext)
const currentWorkflowRun = await fetchWorkflowRun(
context.repo.owner,
context.repo.repo,
githubContext.run_id
)

const filteredRuns = filterWorkflowRuns(
workflowRuns,
githubContext,
currentWorkflowRun
)

const reports = await processArtifactsFromRuns(
filteredRuns,
Expand Down
10 changes: 5 additions & 5 deletions src/github/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export function validateCtrfFile(filePath: string): CtrfReport | null {
*/
export function filterWorkflowRuns(
runs: WorkflowRun[],
// TODO: use GitHub properties
githubProperties: GitHubContext
githubProperties: GitHubContext,
currentRun: WorkflowRun
): WorkflowRun[] {
return runs.filter(run => {
const isBranchMatch =
Expand All @@ -52,9 +52,9 @@ export function filterWorkflowRuns(
pr => pr.number === githubProperties.pullRequest.number
)

const isWorkflowNameMatch = run.name === githubProperties.workflow
const isWorkflowMatch = run.workflow_id === currentRun.workflow_id

if ((isBranchMatch || isPRMatch) && isWorkflowNameMatch) {
if ((isBranchMatch || isPRMatch) && isWorkflowMatch) {
console.debug(
`Match found for workflow ${run.name} with run number ${run.run_number}`
)
Expand All @@ -64,6 +64,6 @@ export function filterWorkflowRuns(
)
}

return (isBranchMatch || isPRMatch) && isWorkflowNameMatch
return (isBranchMatch || isPRMatch) && isWorkflowMatch
})
}

0 comments on commit d4651c5

Please sign in to comment.