Skip to content

Commit

Permalink
Merge pull request #61 from ctrf-io/feat/filter-workflow-id
Browse files Browse the repository at this point in the history
feat: use workflow_id for workflow match
  • Loading branch information
Ma11hewThomas authored Dec 18, 2024
2 parents 35b1511 + 0e5385b commit de74e8c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,9 @@ current workflow and job names:
Filtering is applied as follows:

- Runs from the same branch for events of type push and schedule from the same
workflow name
workflow id
- Runs from the same pull request for events of type pull_request from the same
workflow name
workflow id

This ensures that you only see workflow runs that are related to your current
branch or pull request
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "github-actions-ctrf",
"version": "0.0.53",
"version": "0.0.54",
"description": "View test results directly within your GitHub workflow summary and Pull Requests",
"main": "index.js",
"scripts": {
Expand Down
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 de74e8c

Please sign in to comment.