Skip to content

Commit

Permalink
Provide run name optional parameter to fetch run id based on name (#6)
Browse files Browse the repository at this point in the history
* use check listforref to obtain run id

* proper param for run name
  • Loading branch information
robbertvdg authored Jun 8, 2023
1 parent 891f251 commit d0f84d4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ 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`
**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
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
69 changes: 45 additions & 24 deletions src/workflow-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -125,31 +126,51 @@ export class WorkflowHandler {
}
try {
core.debug('Get workflow run id');
const workflowId = await this.getWorkflowId();
const response = await this.octokit.actions.listWorkflowRuns({
owner: this.owner,
repo: this.repo,
workflow_id: workflowId,
event: 'workflow_dispatch'
});
debug('List Workflow Runs', response);

const runs = response.data.workflow_runs
.filter((r: any) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
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,
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');
if (this.runName) {

const result = await this.octokit.rest.checks.listForRef({
check_name: this.runName,
owner: this.owner,
repo: this.repo,
ref: this.ref,
filter: 'latest'
})

if (result.length == 0) {
throw new Error('Run not found');
}

this.workflowRunId = result.check_runs[0].id as number;
}
else {
const workflowId = await this.getWorkflowId();

const response = await this.octokit.actions.listWorkflowRuns({
owner: this.owner,
repo: this.repo,
workflow_id: workflowId,
event: 'workflow_dispatch'
});

debug('List Workflow Runs', response);
const runs = response.data.workflow_runs
.filter((r: any) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
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,
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');
}

this.workflowRunId = runs[0].id as number;
}

this.workflowRunId = runs[0].id as number;
return this.workflowRunId;
} catch (error) {
debug('Get workflow run id error', error);
Expand Down

0 comments on commit d0f84d4

Please sign in to comment.