Skip to content

Commit

Permalink
Merge pull request #28 from urcomputeringpal/actor-branch-event
Browse files Browse the repository at this point in the history
support filtering workflow runs
  • Loading branch information
jnewland authored Aug 2, 2023
2 parents 094e693 + 5f937e7 commit 23c8936
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 15 deletions.
25 changes: 23 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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) }}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
13 changes: 13 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ export async function summarizeHistory(args: GitHubScriptArguments): Promise<voi
});
const workflow_id = run.data.workflow_id;

// TODO link to the workflow history
// const workflowResponse = await github.rest.actions.getWorkflow({
// ...context.repo,
// workflow_id,
// });
const actor = process.env.FILTER_ACTOR == "" ? undefined : process.env.FILTER_ACTOR;
const branch = process.env.FILTER_BRANCH == "" ? undefined : process.env.FILTER_BRANCH;
const event = process.env.FILTER_EVENT == "" ? undefined : process.env.FILTER_EVENT;

getWorkflowRuns(workflow_id, { github, context, core }).then(groupedWorkflowRuns => {
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
Expand Down
6 changes: 2 additions & 4 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -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"]
}
7 changes: 5 additions & 2 deletions src/workflowGroup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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),
});

Expand Down
11 changes: 10 additions & 1 deletion src/workflowGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ export type GroupedWorkflowRuns = Map<string, WorkflowGroup>;
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<GroupedWorkflowRuns> {
export async function getWorkflowRuns(
workflow_id: number,
actor: string | undefined = undefined,
branch: string | undefined = undefined,
event: string | undefined = undefined,
args: GitHubScriptArguments
): Promise<GroupedWorkflowRuns> {
const workflowRuns: WorkflowRun[] = [];
const { github, context, core } = args;
if (github === undefined || context == undefined || core === undefined) {
Expand All @@ -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;
Expand Down

0 comments on commit 23c8936

Please sign in to comment.