From d4651c5bdcbba23449df93369a9a7e965536cce1 Mon Sep 17 00:00:00 2001 From: Matthew Thomas Date: Tue, 17 Dec 2024 22:43:25 +0000 Subject: [PATCH 1/2] feat: use workflow_id for workflow match --- src/client/github/workflows.ts | 22 ++++++++++++++++++++++ src/ctrf/metrics.ts | 13 ++++++++++++- src/github/helpers.ts | 10 +++++----- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/client/github/workflows.ts b/src/client/github/workflows.ts index e1f79187..43aa029d 100644 --- a/src/client/github/workflows.ts +++ b/src/client/github/workflows.ts @@ -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 { + 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. diff --git a/src/ctrf/metrics.ts b/src/ctrf/metrics.ts index 6f045f12..d50595b5 100644 --- a/src/ctrf/metrics.ts +++ b/src/ctrf/metrics.ts @@ -1,6 +1,7 @@ import { context } from '@actions/github' import { fetchAllWorkflowRuns, + fetchWorkflowRun, processArtifactsFromRuns } from '../client/github' import { filterWorkflowRuns } from '../github' @@ -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, diff --git a/src/github/helpers.ts b/src/github/helpers.ts index ec08843b..8b535ea4 100644 --- a/src/github/helpers.ts +++ b/src/github/helpers.ts @@ -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 = @@ -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}` ) @@ -64,6 +64,6 @@ export function filterWorkflowRuns( ) } - return (isBranchMatch || isPRMatch) && isWorkflowNameMatch + return (isBranchMatch || isPRMatch) && isWorkflowMatch }) } From 0e5385b6f56084c0b037ffbe8f9b77e6d52efb9d Mon Sep 17 00:00:00 2001 From: Matthew Thomas Date: Tue, 17 Dec 2024 22:46:53 +0000 Subject: [PATCH 2/2] ci: increment version --- README.md | 4 ++-- package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b9f65be0..034d8206 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package-lock.json b/package-lock.json index d843c6f2..ae369534 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "github-actions-ctrf", - "version": "0.0.53", + "version": "0.0.54", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "github-actions-ctrf", - "version": "0.0.53", + "version": "0.0.54", "license": "MIT", "dependencies": { "@actions/core": "^1.11.1", diff --git a/package.json b/package.json index 984f9ce8..44897a5e 100644 --- a/package.json +++ b/package.json @@ -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": {