Skip to content

Commit d0f84d4

Browse files
authored
Provide run name optional parameter to fetch run id based on name (#6)
* use check listforref to obtain run id * proper param for run name
1 parent 891f251 commit d0f84d4

File tree

5 files changed

+56
-26
lines changed

5 files changed

+56
-26
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ All values must be strings (even if they are used as booleans or numbers in the
3434
### `repo`
3535
**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`
3636

37+
### `run-name`
38+
**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.
39+
You can specify the run name to fetch the run ID based on the actual run name.
40+
3741
### `wait-for-completion`
3842
**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.
3943

action.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ inputs:
1717
repo:
1818
description: 'Repo owner & name, slash separated, only set if invoking a workflow in a different repo'
1919
required: false
20+
run-name:
21+
description: 'If specified will select the run ID based on the run name'
22+
required: false
2023
display-workflow-run-url:
2124
description: 'Get the URL of the triggered workflow and display it in logs (useful to follow the progress of the triggered workflow)'
2225
required: false

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function computeConclusion(start: number, waitForCompletionTimeout: number, resu
6666
async function run(): Promise<void> {
6767
try {
6868
const args = getArgs();
69-
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref);
69+
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref, args.runName);
7070

7171
// Trigger workflow run
7272
await workflowHandler.triggerWorkflow(args.inputs);

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function getArgs() {
4343
const waitForCompletion = waitForCompletionStr && waitForCompletionStr === 'true';
4444
const waitForCompletionTimeout = toMilliseconds(core.getInput('wait-for-completion-timeout'));
4545
const checkStatusInterval = toMilliseconds(core.getInput('wait-for-completion-interval'));
46+
const runName = core.getInput('run-name')
4647

4748
return {
4849
token,
@@ -56,7 +57,8 @@ export function getArgs() {
5657
displayWorkflowUrlInterval,
5758
checkStatusInterval,
5859
waitForCompletion,
59-
waitForCompletionTimeout
60+
waitForCompletionTimeout,
61+
runName
6062
};
6163
}
6264

src/workflow-handler.ts

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export class WorkflowHandler {
5151
private workflowRef: string,
5252
private owner: string,
5353
private repo: string,
54-
private ref: string) {
54+
private ref: string,
55+
private runName: string) {
5556
// Get octokit client for making API calls
5657
this.octokit = github.getOctokit(token)
5758
}
@@ -125,31 +126,51 @@ export class WorkflowHandler {
125126
}
126127
try {
127128
core.debug('Get workflow run id');
128-
const workflowId = await this.getWorkflowId();
129-
const response = await this.octokit.actions.listWorkflowRuns({
130-
owner: this.owner,
131-
repo: this.repo,
132-
workflow_id: workflowId,
133-
event: 'workflow_dispatch'
134-
});
135-
debug('List Workflow Runs', response);
136-
137-
const runs = response.data.workflow_runs
138-
.filter((r: any) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
139-
debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r: any) => ({
140-
id: r.id,
141-
name: r.name,
142-
created_at: r.creatd_at,
143-
triggerDate: new Date(this.triggerDate).toISOString(),
144-
created_at_ts: new Date(r.created_at).valueOf(),
145-
triggerDateTs: this.triggerDate
146-
})));
147-
148-
if (runs.length == 0) {
149-
throw new Error('Run not found');
129+
if (this.runName) {
130+
131+
const result = await this.octokit.rest.checks.listForRef({
132+
check_name: this.runName,
133+
owner: this.owner,
134+
repo: this.repo,
135+
ref: this.ref,
136+
filter: 'latest'
137+
})
138+
139+
if (result.length == 0) {
140+
throw new Error('Run not found');
141+
}
142+
143+
this.workflowRunId = result.check_runs[0].id as number;
144+
}
145+
else {
146+
const workflowId = await this.getWorkflowId();
147+
148+
const response = await this.octokit.actions.listWorkflowRuns({
149+
owner: this.owner,
150+
repo: this.repo,
151+
workflow_id: workflowId,
152+
event: 'workflow_dispatch'
153+
});
154+
155+
debug('List Workflow Runs', response);
156+
const runs = response.data.workflow_runs
157+
.filter((r: any) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
158+
debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r: any) => ({
159+
id: r.id,
160+
name: r.name,
161+
created_at: r.creatd_at,
162+
triggerDate: new Date(this.triggerDate).toISOString(),
163+
created_at_ts: new Date(r.created_at).valueOf(),
164+
triggerDateTs: this.triggerDate
165+
})));
166+
167+
if (runs.length == 0) {
168+
throw new Error('Run not found');
169+
}
170+
171+
this.workflowRunId = runs[0].id as number;
150172
}
151173

152-
this.workflowRunId = runs[0].id as number;
153174
return this.workflowRunId;
154175
} catch (error) {
155176
debug('Get workflow run id error', error);

0 commit comments

Comments
 (0)