Skip to content

Commit

Permalink
Merge pull request #7 from broadinstitute/DDO-3037-1
Browse files Browse the repository at this point in the history
DDO-3037: Add support for filtering workflow dispatch runs by run-name.
  • Loading branch information
ichengchang authored Sep 7, 2023
2 parents c5f467f + 6403d29 commit c909041
Show file tree
Hide file tree
Showing 9 changed files with 960 additions and 893 deletions.
50 changes: 39 additions & 11 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ jobs:
name: "echo-1-test [trigger|by workflow name]"
steps:
- name: Check out repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Download dist
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: build
path: dist
Expand All @@ -50,16 +50,16 @@ jobs:
token: ${{ secrets.BROADBOT_TOKEN }}
inputs: '{"message": "blah blah"}'
wait-for-completion: false

echo-2-test:
needs: [build]
runs-on: ubuntu-latest
name: "echo-2-test [trigger|by workflow filename]"
name: "echo-2-test [trigger|by workflow filename|may confuse with concurrent echo-2]"
steps:
- name: Check out repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Download dist
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: build
path: dist
Expand All @@ -70,7 +70,7 @@ jobs:
workflow: echo-02.yaml
token: ${{ secrets.BROADBOT_TOKEN }}
wait-for-completion: false

long-running-test:
needs: [build]
runs-on: ubuntu-latest
Expand Down Expand Up @@ -108,9 +108,9 @@ jobs:
name: "failing-test [trigger+wait|by workflow filename|shoud report failure]"
steps:
- name: Check out repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Download dist
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: build
path: dist
Expand Down Expand Up @@ -140,9 +140,9 @@ jobs:
name: "timeout-test [trigger+wait|by workflow filename|shoud report timed_out]"
steps:
- name: Check out repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Download dist
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: build
path: dist
Expand All @@ -164,3 +164,31 @@ jobs:
with:
expected: failure
actual: ${{ steps.timeout-workflow.outcome }}

concurrent-echo-2-test:
needs: [build]
strategy:
matrix:
msg : [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" ]
runs-on: ubuntu-latest
name: "concurrent-echo-2-test [trigger|by workflow filename|confusing workflow run id should not happen]"
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Download dist
uses: actions/download-artifact@v3
with:
name: build
path: dist
- name: Invoke concurrent echo-2 workflows using this action
id: concurrent-echo-2
uses: ./
with:
ref: ${{ github.event.pull_request.head.ref }}
run-name: "echo-02-${{ matrix.msg }}"
workflow: echo-02.yaml
token: ${{ secrets.BROADBOT_TOKEN }}
inputs: '{
"message": "${{ matrix.msg }}",
"run-name": "echo-02-${{ matrix.msg }}"
}'
6 changes: 5 additions & 1 deletion .github/workflows/echo-02.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
name: Message Echo 2

run-name: ${{ inputs.run-name }}
on:
workflow_dispatch:
inputs:
message:
description: "Message to echo"
required: false
default: "this is echo 2"
run-name:
description: 'Run Name of workflow dispatch'
required: false
default: ''

jobs:
echo:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ All values must be strings (even if they are used as booleans or numbers in the
### `repo`
**Optional.** The default behavior is to trigger workflows in the same repo as the triggering workflow, if you wish to trigger in another GitHub repo "externally", then provide the owner + repo name with slash between them e.g. `microsoft/vscode`

### `run-name` (since v3)
**Optional.** The default behavior is to get the remote run ID based on the latest workflow name and date, if you have multiple of the same workflow running at the same time it can point to an incorrect run id. You can specify the run name to fetch the run ID based on the actual run name.

### `wait-for-completion`
**Optional.** If `true`, this action will actively poll the workflow run to get the result of the triggered workflow. It is enabled by default. If the triggered workflow fails due to either `failure`, `timed_out` or `cancelled` then the step that has triggered the other workflow will be marked as failed too.

Expand Down
3 changes: 3 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ inputs:
repo:
description: 'Repo owner & name, slash separated, only set if invoking a workflow in a different repo'
required: false
run-name:
description: 'If specified will select the run ID based on the run name'
required: false
display-workflow-run-url:
description: 'Get the URL of the triggered workflow and display it in logs (useful to follow the progress of the triggered workflow)'
required: false
Expand Down
26 changes: 21 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9472,7 +9472,7 @@ function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const args = utils_1.getArgs();
const workflowHandler = new workflow_handler_1.WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref);
const workflowHandler = new workflow_handler_1.WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref, args.runName);
// Trigger workflow run
yield workflowHandler.triggerWorkflow(args.inputs);
core.info(`Workflow triggered 🚀`);
Expand Down Expand Up @@ -9568,6 +9568,7 @@ function getArgs() {
const waitForCompletion = waitForCompletionStr && waitForCompletionStr === 'true';
const waitForCompletionTimeout = toMilliseconds(core.getInput('wait-for-completion-timeout'));
const checkStatusInterval = toMilliseconds(core.getInput('wait-for-completion-interval'));
const runName = core.getInput('run-name');
return {
token,
workflowRef,
Expand All @@ -9580,7 +9581,8 @@ function getArgs() {
displayWorkflowUrlInterval,
checkStatusInterval,
waitForCompletion,
waitForCompletionTimeout
waitForCompletionTimeout,
runName
};
}
exports.getArgs = getArgs;
Expand Down Expand Up @@ -9685,11 +9687,12 @@ const ofConclusion = (conclusion) => {
return WorkflowRunConclusion[key];
};
class WorkflowHandler {
constructor(token, workflowRef, owner, repo, ref) {
constructor(token, workflowRef, owner, repo, ref, runName) {
this.workflowRef = workflowRef;
this.owner = owner;
this.repo = repo;
this.ref = ref;
this.runName = runName;
this.triggerDate = 0;
// Get octokit client for making API calls
this.octokit = github.getOctokit(token);
Expand Down Expand Up @@ -9773,12 +9776,25 @@ class WorkflowHandler {
event: 'workflow_dispatch'
});
debug_1.debug('List Workflow Runs', response);
const runs = response.data.workflow_runs
let runs = response.data.workflow_runs
.filter((r) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
if (this.runName) {
core.info(`Filter by run-name: ${this.runName}`);
// By definition: display_title is the event-specific title associated with the run
// or the run-name if set, or the value of run-name if it is set in the workflow.
// However, GitHub also seems to set workflow run name to run-name if it is set
// so for the sake of robustness we filter by either name or display_title.
// See response schema of https://docs.github.com/en/free-pro-team@latest/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow
runs = runs.filter((r) => {
var _a;
return r.name.trim().toLowerCase() === this.runName.trim().toLowerCase()
|| ((_a = r.display_title) !== null && _a !== void 0 ? _a : '').trim().toLowerCase() === this.runName.trim().toLowerCase();
});
}
debug_1.debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r) => ({
id: r.id,
name: r.name,
created_at: r.creatd_at,
created_at: r.created_at,
triggerDate: new Date(this.triggerDate).toISOString(),
created_at_ts: new Date(r.created_at).valueOf(),
triggerDateTs: this.triggerDate
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function computeConclusion(start: number, waitForCompletionTimeout: number, resu
async function run(): Promise<void> {
try {
const args = getArgs();
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref);
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref, args.runName);

// Trigger workflow run
await workflowHandler.triggerWorkflow(args.inputs);
Expand Down
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function getArgs() {
const waitForCompletion = waitForCompletionStr && waitForCompletionStr === 'true';
const waitForCompletionTimeout = toMilliseconds(core.getInput('wait-for-completion-timeout'));
const checkStatusInterval = toMilliseconds(core.getInput('wait-for-completion-interval'));
const runName = core.getInput('run-name');

return {
token,
Expand All @@ -56,7 +57,8 @@ export function getArgs() {
displayWorkflowUrlInterval,
checkStatusInterval,
waitForCompletion,
waitForCompletionTimeout
waitForCompletionTimeout,
runName
};
}

Expand Down
25 changes: 18 additions & 7 deletions src/workflow-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const ofConclusion = (conclusion: string | null): WorkflowRunConclusion => {
}

export interface WorkflowRunResult {
url: string,
status: WorkflowRunStatus,
url: string,
status: WorkflowRunStatus,
conclusion: WorkflowRunConclusion
}

Expand All @@ -51,7 +51,8 @@ export class WorkflowHandler {
private workflowRef: string,
private owner: string,
private repo: string,
private ref: string) {
private ref: string,
private runName: string) {
// Get octokit client for making API calls
this.octokit = github.getOctokit(token)
}
Expand Down Expand Up @@ -134,17 +135,27 @@ export class WorkflowHandler {
});
debug('List Workflow Runs', response);

const runs = response.data.workflow_runs
let runs = response.data.workflow_runs
.filter((r: any) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
if (this.runName) {
core.info(`Filter by run-name: ${this.runName}`);
// By definition: display_title is the event-specific title associated with the run
// or the run-name if set, or the value of run-name if it is set in the workflow.
// However, GitHub also seems to set workflow run name to run-name if it is set
// so for the sake of robustness we filter by either name or display_title.
// See response schema of https://docs.github.com/en/free-pro-team@latest/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow
runs = runs.filter((r: any) => r.name.trim().toLowerCase() === this.runName.trim().toLowerCase()
|| (r.display_title ?? '').trim().toLowerCase() === this.runName.trim().toLowerCase());
}
debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r: any) => ({
id: r.id,
name: r.name,
created_at: r.creatd_at,
created_at: r.created_at,
triggerDate: new Date(this.triggerDate).toISOString(),
created_at_ts: new Date(r.created_at).valueOf(),
triggerDateTs: this.triggerDate
})));

if (runs.length == 0) {
throw new Error('Run not found');
}
Expand All @@ -169,7 +180,7 @@ export class WorkflowHandler {
}
try {
const workflowsResp = await this.octokit.rest.actions.listRepoWorkflows({
owner: this.owner,
owner: this.owner,
repo: this.repo,
per_page: 100 //max allowed
});
Expand Down
Loading

0 comments on commit c909041

Please sign in to comment.