From 5f937e719bd8f639b448fe7f7fae56b913c07c0f Mon Sep 17 00:00:00 2001 From: Jesse Newland Date: Wed, 2 Aug 2023 22:44:52 +0000 Subject: [PATCH] support filtering workflow runs --- .github/workflows/test.yml | 25 +++++++++++++++++++++++-- README.md | 4 ++++ action.yml | 13 +++++++++++++ index.ts | 10 ++++------ renovate.json | 6 ++---- src/workflowGroup.test.ts | 7 +++++-- src/workflowGroup.ts | 11 ++++++++++- 7 files changed, 61 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9c0abd2..e9ba0da 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,16 +14,37 @@ jobs: runs-on: ubuntu-latest steps: - run: sleep 5 - workflow-run-history: - name: Workflow + + push: + name: Push + needs: test + runs-on: ubuntu-latest + if: github.event_name == 'push' + steps: + - name: Checkout + uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2 + - uses: ./ + id: summarizeHistory + timeout-minutes: 2 + with: + filter-event: push + - run: echo "$HISTORY_OUTPUTS" + env: + HISTORY_OUTPUTS: ${{ toJSON(steps.summarizeHistory.outputs) }} + + pull_request: + name: Pull Request needs: test runs-on: ubuntu-latest + if: github.event_name == 'pull_request' steps: - name: Checkout uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2 - uses: ./ id: summarizeHistory timeout-minutes: 2 + with: + filter-event: pull_request - run: echo "$HISTORY_OUTPUTS" env: HISTORY_OUTPUTS: ${{ toJSON(steps.summarizeHistory.outputs) }} diff --git a/README.md b/README.md index 8a4598f..f6589f5 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,10 @@ jobs: target-default-success-rate: "99" # This workflow should complete 90% of the time on PRs. target-pr-success-rate: "90" + + # filter-actor: "urcomputeringpal" + # filter-branch: "main" + # filter-event: "push" - run: | echo "This workflow run hit its target performance: ${{ steps.history.outputs.hit-target-seconds }}" echo "This workflow has historically hit its target performance on PRs: ${{ steps.history.outputs.hit-target-pr-success-percentile }}" diff --git a/action.yml b/action.yml index 1083475..2b57ee0 100644 --- a/action.yml +++ b/action.yml @@ -55,6 +55,16 @@ inputs: description: "Target percentile for PR failure runtime. Defaults to `target-pr-percentile`." required: false + filter-actor: + description: "Filter by actor" + required: false + filter-branch: + description: "Filter by branch" + required: false + filter-event: + description: "Filter by event" + required: false + outputs: hit-target-default-success-rate: description: "Hit target default success rate" @@ -120,6 +130,9 @@ runs: TARGET_PR_PERCENTILE: ${{ inputs.target-pr-percentile }} TARGET_PR_FAILURE_SECONDS: ${{ inputs.target-pr-failure-seconds }} TARGET_PR_FAILURE_PERCENTILE: ${{ inputs.target-pr-failure-percentile }} + FILTER_ACTOR: ${{ inputs.filter-actor }} + FILTER_BRANCH: ${{ inputs.filter-branch }} + FILTER_EVENT: ${{ inputs.filter-event }} with: github-token: ${{ inputs.github-token }} retries: 3 diff --git a/index.ts b/index.ts index 10ad24e..bb54f00 100644 --- a/index.ts +++ b/index.ts @@ -17,13 +17,11 @@ export async function summarizeHistory(args: GitHubScriptArguments): Promise { + getWorkflowRuns(workflow_id, actor, branch, event, { github, context, core }).then(groupedWorkflowRuns => { const totalRuns = Array.from(groupedWorkflowRuns.values()).reduce( (total, group) => total + group.runs.length, 0 diff --git a/renovate.json b/renovate.json index cc492dc..cd77be4 100644 --- a/renovate.json +++ b/renovate.json @@ -1,6 +1,4 @@ { - "$schema": "https://docs.renovatebot.com/renovate-schema.json", - "extends": [ - "github>urcomputeringpal/.github" - ] + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": ["github>urcomputeringpal/.github"] } diff --git a/src/workflowGroup.test.ts b/src/workflowGroup.test.ts index f69bbec..ae7456b 100644 --- a/src/workflowGroup.test.ts +++ b/src/workflowGroup.test.ts @@ -260,7 +260,7 @@ describe("getWorkflowRuns", () => { core, }; - const groupedRuns = await getWorkflowRuns(1234, mockArgs); + const groupedRuns = await getWorkflowRuns(1234, undefined, undefined, undefined, mockArgs); expect(mockGithub.paginate.iterator).toHaveBeenCalledWith(mockGithub.rest.actions.listWorkflowRuns, { owner: "status", @@ -315,12 +315,15 @@ describe("getWorkflowRuns", () => { core, }; - const groupedRuns = await getWorkflowRuns(1234, mockArgs); + const groupedRuns = await getWorkflowRuns(1234, "actor", "branch", "event", mockArgs); expect(mockGithub.paginate.iterator).toHaveBeenCalledWith(mockGithub.rest.actions.listWorkflowRuns, { owner: "status", repo: "status", workflow_id: 1234, + actor: "actor", + branch: "branch", + event: "event", created: expect.any(String), }); diff --git a/src/workflowGroup.ts b/src/workflowGroup.ts index 4521446..4c94a10 100644 --- a/src/workflowGroup.ts +++ b/src/workflowGroup.ts @@ -64,7 +64,13 @@ export type GroupedWorkflowRuns = Map; type ListWorkflowRunsResponse = Endpoints["GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs"]["response"]["data"]["workflow_runs"]; -export async function getWorkflowRuns(workflow_id: number, args: GitHubScriptArguments): Promise { +export async function getWorkflowRuns( + workflow_id: number, + actor: string | undefined = undefined, + branch: string | undefined = undefined, + event: string | undefined = undefined, + args: GitHubScriptArguments +): Promise { const workflowRuns: WorkflowRun[] = []; const { github, context, core } = args; if (github === undefined || context == undefined || core === undefined) { @@ -85,6 +91,9 @@ export async function getWorkflowRuns(workflow_id: number, args: GitHubScriptArg ...context.repo, workflow_id, created, + actor, + branch, + event, })) { const workflowRunResponse: ListWorkflowRunsResponse = response.data.length === undefined ? (response.data as any).workflow_runs : response.data;