Skip to content

Commit

Permalink
Add option to specify eventName on wait-list (#772)
Browse files Browse the repository at this point in the history
Resolves #771
  • Loading branch information
kachick committed Apr 17, 2024
1 parent 82ea973 commit a2e7af0
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 25 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/itself.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ on:
branches: [main]
paths:
- '.github/workflows/itself.yml'
- '.github/workflows/multiple-event-triggers.yml'
- 'action.yml'
- 'dist/**'
pull_request:
paths:
- '.github/workflows/itself.yml'
- '.github/workflows/multiple-event-triggers.yml'
- 'action.yml'
- 'dist/**'

Expand Down Expand Up @@ -119,6 +121,10 @@ jobs:
"jobName": "dependabot",
"optional": true
},
{
"workflowFile": "multiple-event-triggers.yml",
"eventName": "${{ github.event_name }}"
},
{
"workflowFile": "THERE_ARE_NO_FILES_AS_THIS.yml",
"optional": true
Expand Down
28 changes: 28 additions & 0 deletions .github/workflows/multiple-event-triggers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: GH-771 - Multiple event triggers
on:
push:
paths:
- '.github/workflows/multiple-event-triggers.yml'
- '.github/workflows/itself.yml'
- 'action.yml'
- 'dist/**'
pull_request:
paths:
- '.github/workflows/multiple-event-triggers.yml'
- '.github/workflows/itself.yml'
- 'action.yml'
- 'dist/**'

# Disable all permissions in workflow global as to setup clean room
# However PRs will have read permissions because this project is on a public repository
permissions: {}

jobs:
should_be_wait_this_only_in_pull_request:
runs-on: ubuntu-latest
if: ${{ github.actor != 'dependabot[bot]' && github.actor != 'renovate[bot]' }}
timeout-minutes: 5
steps:
- run: |
echo 'Triggered by ${{ github.event_name }} event'
echo 'See https://github.com/kachick/wait-other-jobs/issues/771 for the detail'
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ with:
# lists should be given with JSON formatted array, do not specify both wait-list and skip-list
# - Each item should have a "workflowFile" field, and they can optionally have a "jobName" field.
# - If no jobName is specified, all the jobs in the workflow will be targeted.
# - wait-list: If the checkRun for the specified name is not found, this action raise errors by default.
# You can disable this validation `optional: true`.
# - wait-list:
# - If the checkRun for the specified name is not found, this action raise errors by default.
# You can disable this validation with `"optional": true`.
# - Wait for all event types by default, you can change with `"eventName": "EVENT_NAME_AS_push"`.
wait-list: |
[
{
"workflowFile": "ci.yml",
"jobName": "test"
"jobName": "test",
"eventName": "${{ github.event_name }}"
},
{
"workflowFile": "release.yml",
Expand Down
32 changes: 22 additions & 10 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27049,7 +27049,12 @@ var FilterCondition = z.object({
});
var SkipFilterCondition = FilterCondition.readonly();
var WaitFilterCondition = FilterCondition.extend(
{ optional: z.boolean().optional().default(false).readonly() }
{
optional: z.boolean().optional().default(false).readonly(),
// - Intentionally avoided to use enum for now. Only GitHub knows whole eventNames and the adding plans
// - Intentionally omitted in skip-list, let me know if you have the use-case
eventName: z.string().min(1).optional()
}
).readonly();
var retryMethods = z.enum(["exponential_backoff", "equal_intervals"]);
var Options = z.object({
Expand All @@ -27074,7 +27079,8 @@ function parseInput() {
payload,
runId,
job,
sha
sha,
eventName
} = import_github.context;
const pr = payload.pull_request;
let commitSha = sha;
Expand Down Expand Up @@ -27116,7 +27122,7 @@ function parseInput() {
shouldSkipSameWorkflow,
isDryRun
});
const trigger = { ...repo, ref: commitSha, runId, jobName: job };
const trigger = { ...repo, ref: commitSha, runId, jobName: job, eventName };
const githubToken = (0, import_core.getInput)("github-token", { required: true, trimWhitespace: false });
(0, import_core.setSecret)(githubToken);
return { trigger, options, githubToken };
Expand Down Expand Up @@ -28245,6 +28251,7 @@ async function fetchChecks(token, trigger) {
conclusion
workflowRun {
databaseId
event
workflow {
name
resourcePath
Expand Down Expand Up @@ -28282,10 +28289,11 @@ async function fetchChecks(token, trigger) {
throw new Error("no checkSuiteNodes");
}
return checkSuiteNodes.flatMap((checkSuite) => {
const workflow = checkSuite.workflowRun?.workflow;
if (!workflow) {
const workflowRun = checkSuite.workflowRun;
if (!workflowRun) {
return [];
}
const workflow = workflowRun.workflow;
const checkRuns = checkSuite?.checkRuns;
if (!checkRuns) {
throw new Error("no checkRuns");
Expand All @@ -28297,19 +28305,20 @@ async function fetchChecks(token, trigger) {
if (!checkRunNodes) {
throw new Error("no runNodes");
}
return checkRunNodes.map((checkRun) => ({ checkRun, checkSuite, workflow }));
return checkRunNodes.map((checkRun) => ({ checkRun, checkSuite, workflow, workflowRun }));
});
}

// src/report.ts
import { join, relative } from "path";
function summarize(check, trigger) {
const { checkRun: run2, checkSuite: suite, workflow } = check;
const { checkRun: run2, checkSuite: suite, workflow, workflowRun } = check;
return {
acceptable: run2.conclusion == "SUCCESS" || run2.conclusion === "SKIPPED" || run2.conclusion === "NEUTRAL" && (suite.conclusion === "SUCCESS" || suite.conclusion === "SKIPPED"),
workflowPath: relative(`/${trigger.owner}/${trigger.repo}/actions/workflows/`, workflow.resourcePath),
// Another file can set same workflow name. So you should filter workfrows from runId or the filename
isSameWorkflow: suite.workflowRun?.databaseId === trigger.runId,
eventName: workflowRun.event,
checkSuiteStatus: suite.status,
checkSuiteConclusion: suite.conclusion,
runDatabaseId: run2.databaseId,
Expand All @@ -28329,7 +28338,9 @@ function generateReport(checks, trigger, { waitList, skipList, shouldSkipSameWor
const seeker = waitList.map((condition) => ({ ...condition, found: false }));
filtered = filtered.filter(
(summary) => seeker.some((target) => {
if (target.workflowFile === summary.workflowPath && (target.jobName ? target.jobName === summary.jobName : true)) {
const isMatchPath = target.workflowFile === summary.workflowPath && (target.jobName ? target.jobName === summary.jobName : true);
const isMatchEvent = target.eventName ? target.eventName === summary.eventName : true;
if (isMatchPath && isMatchEvent) {
target.found = true;
return true;
} else {
Expand Down Expand Up @@ -28451,11 +28462,12 @@ async function run() {
runConclusion,
jobName,
workflowPath,
checkRunUrl
checkRunUrl,
eventName
} = summary;
const nullStr = "(null)";
(0, import_core3.info)(
`${workflowPath}(${colorize(`${jobName}`, acceptable)}): [suiteStatus: ${checkSuiteStatus}][suiteConclusion: ${checkSuiteConclusion ?? nullStr}][runStatus: ${runStatus}][runConclusion: ${runConclusion ?? nullStr}][runURL: ${checkRunUrl}]`
`${workflowPath}(${colorize(`${jobName}`, acceptable)}): [suiteStatus: ${checkSuiteStatus}][suiteConclusion: ${checkSuiteConclusion ?? nullStr}][runStatus: ${runStatus}][runConclusion: ${runConclusion ?? nullStr}][eventName: ${eventName}][runURL: ${checkRunUrl}]`
);
}
if ((0, import_core3.isDebug)()) {
Expand Down
8 changes: 5 additions & 3 deletions src/github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function fetchChecks(
conclusion
workflowRun {
databaseId
event
workflow {
name
resourcePath
Expand Down Expand Up @@ -68,10 +69,11 @@ export async function fetchChecks(
}

return checkSuiteNodes.flatMap((checkSuite) => {
const workflow = checkSuite.workflowRun?.workflow;
if (!workflow) {
const workflowRun = checkSuite.workflowRun;
if (!workflowRun) {
return [];
}
const workflow = workflowRun.workflow;

const checkRuns = checkSuite?.checkRuns;
if (!checkRuns) {
Expand All @@ -87,6 +89,6 @@ export async function fetchChecks(
throw new Error('no runNodes');
}

return checkRunNodes.map((checkRun) => ({ checkRun, checkSuite, workflow }));
return checkRunNodes.map((checkRun) => ({ checkRun, checkSuite, workflow, workflowRun }));
});
}
3 changes: 2 additions & 1 deletion src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function parseInput(): { trigger: Trigger; options: Options; githubToken:
runId,
job,
sha,
eventName,
} = context;
const pr = payload.pull_request;
let commitSha = sha;
Expand Down Expand Up @@ -55,7 +56,7 @@ export function parseInput(): { trigger: Trigger; options: Options; githubToken:
isDryRun,
});

const trigger = { ...repo, ref: commitSha, runId, jobName: job } as const satisfies Trigger;
const trigger = { ...repo, ref: commitSha, runId, jobName: job, eventName } as const satisfies Trigger;

// `getIDToken` does not fit for this purpose. It is provided for OIDC Token
const githubToken = getInput('github-token', { required: true, trimWhitespace: false });
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,16 @@ async function run(): Promise<void> {
jobName,
workflowPath,
checkRunUrl,
eventName,
} = summary;
const nullStr = '(null)';

info(
`${workflowPath}(${colorize(`${jobName}`, acceptable)}): [suiteStatus: ${checkSuiteStatus}][suiteConclusion: ${
checkSuiteConclusion ?? nullStr
}][runStatus: ${runStatus}][runConclusion: ${runConclusion ?? nullStr}][runURL: ${checkRunUrl}]`,
}][runStatus: ${runStatus}][runConclusion: ${
runConclusion ?? nullStr
}][eventName: ${eventName}][runURL: ${checkRunUrl}]`,
);
}

Expand Down
6 changes: 6 additions & 0 deletions src/report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ test('wait-list', () => {
'runId': 8679817057,
ref: '760074f4f419b55cb864030c29ece58a689a42a2',
jobName: 'wait-list',
eventName: 'pull_request',
},
{
waitList: [
{
'workflowFile': 'lint.yml',
'optional': false,
'eventName': 'pull_request',
},
{
'workflowFile': 'merge-bot-pr.yml',
Expand Down Expand Up @@ -61,6 +63,7 @@ test('wait-list', () => {
runDatabaseId: 23799347237,
runStatus: 'COMPLETED',
workflowPath: 'lint.yml',
eventName: 'pull_request',
},
{
acceptable: true,
Expand All @@ -73,6 +76,7 @@ test('wait-list', () => {
runDatabaseId: 23799347295,
runStatus: 'COMPLETED',
workflowPath: 'lint.yml',
eventName: 'pull_request',
},
{
acceptable: true,
Expand All @@ -85,6 +89,7 @@ test('wait-list', () => {
runDatabaseId: 23799347394,
runStatus: 'COMPLETED',
workflowPath: 'merge-bot-pr.yml',
eventName: 'pull_request',
},
],
}, report);
Expand All @@ -99,6 +104,7 @@ test('skip-list', () => {
'runId': 8679817057,
ref: '760074f4f419b55cb864030c29ece58a689a42a2',
jobName: 'skip-list',
eventName: 'pull_request',
},
{
waitList: [],
Expand Down
15 changes: 10 additions & 5 deletions src/report.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CheckRun, CheckSuite } from '@octokit/graphql-schema';
import { CheckRun, CheckSuite, WorkflowRun } from '@octokit/graphql-schema';
import { Check, Options, Trigger } from './schema.js';
import { join, relative } from 'path';

Expand All @@ -7,6 +7,8 @@ interface Summary {
workflowPath: string;
isSameWorkflow: boolean;

eventName: WorkflowRun['event'];

checkSuiteStatus: CheckSuite['status'];
checkSuiteConclusion: CheckSuite['conclusion'];

Expand All @@ -24,7 +26,7 @@ export interface Report {
}

function summarize(check: Check, trigger: Trigger): Summary {
const { checkRun: run, checkSuite: suite, workflow } = check;
const { checkRun: run, checkSuite: suite, workflow, workflowRun } = check;
return {
acceptable: run.conclusion == 'SUCCESS' || run.conclusion === 'SKIPPED'
|| (run.conclusion === 'NEUTRAL'
Expand All @@ -33,6 +35,8 @@ function summarize(check: Check, trigger: Trigger): Summary {
// Another file can set same workflow name. So you should filter workfrows from runId or the filename
isSameWorkflow: suite.workflowRun?.databaseId === trigger.runId,

eventName: workflowRun.event,

checkSuiteStatus: suite.status,
checkSuiteConclusion: suite.conclusion,

Expand Down Expand Up @@ -60,9 +64,10 @@ export function generateReport(
const seeker = waitList.map((condition) => ({ ...condition, found: false }));
filtered = filtered.filter((summary) =>
seeker.some((target) => {
if (
target.workflowFile === summary.workflowPath && (target.jobName ? (target.jobName === summary.jobName) : true)
) {
const isMatchPath = target.workflowFile === summary.workflowPath
&& (target.jobName ? (target.jobName === summary.jobName) : true);
const isMatchEvent = target.eventName ? (target.eventName === summary.eventName) : true;
if (isMatchPath && isMatchEvent) {
target.found = true;
return true;
} else {
Expand Down
11 changes: 9 additions & 2 deletions src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CheckSuite, Workflow, CheckRun } from '@octokit/graphql-schema';
import { CheckSuite, Workflow, CheckRun, WorkflowRun } from '@octokit/graphql-schema';
import { z } from 'zod';

const FilterCondition = z.object({
Expand All @@ -7,7 +7,12 @@ const FilterCondition = z.object({
});
const SkipFilterCondition = FilterCondition.readonly();
const WaitFilterCondition = FilterCondition.extend(
{ optional: z.boolean().optional().default(false).readonly() },
{
optional: z.boolean().optional().default(false).readonly(),
// - Intentionally avoided to use enum for now. Only GitHub knows whole eventNames and the adding plans
// - Intentionally omitted in skip-list, let me know if you have the use-case
eventName: z.string().min(1).optional(),
},
).readonly();

const retryMethods = z.enum(['exponential_backoff', 'equal_intervals']);
Expand Down Expand Up @@ -38,10 +43,12 @@ export interface Trigger {
ref: string;
runId: number;
jobName: string;
eventName: string;
}

export interface Check {
checkRun: CheckRun;
checkSuite: CheckSuite;
workflow: Workflow;
workflowRun: WorkflowRun;
}
Loading

0 comments on commit a2e7af0

Please sign in to comment.