Skip to content

Commit 928e175

Browse files
chore(pr): Some adjustments after merge of PRs
1 parent d3d8b1f commit 928e175

File tree

5 files changed

+68
-51
lines changed

5 files changed

+68
-51
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ 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`
37+
### `run-name` (since 3.0.0)
3838
**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.
3939
You can specify the run name to fetch the run ID based on the actual run name.
4040

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "workflow-dispatch-and-wait",
3-
"version": "2.1.1",
3+
"version": "3.0.0",
44
"description": "Trigger running GitHub Actions workflows and wait for result",
55
"main": "dist/index.js",
66
"scripts": {

src/debug.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import * as core from '@actions/core'
33
export function debug(title: string, content: any) {
44
if (core.isDebug()) {
55
core.info(`::group::${title}`)
6-
core.debug(JSON.stringify(content, null, 3));
6+
try {
7+
core.debug(JSON.stringify(content, null, 3));
8+
} catch(e) {
9+
core.debug(`Failed to serialize object, trying toString. Cause: ${e}`)
10+
core.debug(content?.toString());
11+
}
712
core.info('::endgroup::')
813
}
914
}

src/utils.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum TimeUnit {
88
}
99

1010
function toMilliseconds(timeWithUnit: string): number {
11-
const unitStr = timeWithUnit.substr(timeWithUnit.length-1);
11+
const unitStr = timeWithUnit.substring(timeWithUnit.length-1);
1212
const unit = TimeUnit[unitStr.toUpperCase() as keyof typeof TimeUnit];
1313
if (!unit) {
1414
throw new Error('Unknown time unit '+unitStr);
@@ -17,6 +17,16 @@ function toMilliseconds(timeWithUnit: string): number {
1717
return time * unit;
1818
}
1919

20+
function parse(inputsJson: string) {
21+
if(inputsJson) {
22+
try {
23+
return JSON.parse(inputsJson);
24+
} catch(e) {
25+
throw new Error(`Failed to parse 'inputs' parameter. Muse be a valid JSON.\nCause: ${e}`)
26+
}
27+
}
28+
return {}
29+
}
2030
export function getArgs() {
2131
// Required inputs
2232
const token = core.getInput('token');
@@ -28,11 +38,7 @@ export function getArgs() {
2838
: [github.context.repo.owner, github.context.repo.repo];
2939

3040
// Decode inputs, this MUST be a valid JSON string
31-
let inputs = {};
32-
const inputsJson = core.getInput('inputs');
33-
if(inputsJson) {
34-
inputs = JSON.parse(inputsJson);
35-
}
41+
let inputs = parse(core.getInput('inputs'));
3642

3743
const displayWorkflowUrlStr = core.getInput('display-workflow-run-url');
3844
const displayWorkflowUrl = displayWorkflowUrlStr && displayWorkflowUrlStr === 'true';

src/workflow-handler.ts

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -127,48 +127,9 @@ export class WorkflowHandler {
127127
try {
128128
core.debug('Get workflow run id');
129129
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;
130+
this.workflowRunId = await this.findWorklowRunIdFromRunName(this.runName);
131+
} else {
132+
this.workflowRunId = await this.findWorkflowRunIdFromFirstRunOfSameWorkflowId();
172133
}
173134

174135
return this.workflowRunId;
@@ -179,6 +140,51 @@ export class WorkflowHandler {
179140

180141
}
181142

143+
private async findWorkflowRunIdFromFirstRunOfSameWorkflowId(): Promise<number> {
144+
const workflowId = await this.getWorkflowId();
145+
146+
const response = await this.octokit.actions.listWorkflowRuns({
147+
owner: this.owner,
148+
repo: this.repo,
149+
workflow_id: workflowId,
150+
event: 'workflow_dispatch'
151+
});
152+
153+
debug('List Workflow Runs', response);
154+
const runs = response.data.workflow_runs
155+
.filter((r: any) => new Date(r.created_at).setMilliseconds(0) >= this.triggerDate);
156+
debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r: any) => ({
157+
id: r.id,
158+
name: r.name,
159+
created_at: r.creatd_at,
160+
triggerDate: new Date(this.triggerDate).toISOString(),
161+
created_at_ts: new Date(r.created_at).valueOf(),
162+
triggerDateTs: this.triggerDate
163+
})));
164+
165+
if (runs.length == 0) {
166+
throw new Error('Run not found');
167+
}
168+
169+
return runs[0].id as number;
170+
}
171+
172+
private async findWorklowRunIdFromRunName(runName: string): Promise<number> {
173+
const result = await this.octokit.rest.checks.listForRef({
174+
check_name: runName,
175+
owner: this.owner,
176+
repo: this.repo,
177+
ref: this.ref,
178+
filter: 'latest'
179+
});
180+
181+
if (result.length == 0) {
182+
throw new Error('Run not found');
183+
}
184+
185+
return result.check_runs[0].id as number;
186+
}
187+
182188
private async getWorkflowId(): Promise<number | string> {
183189
if (this.workflowId) {
184190
return this.workflowId;

0 commit comments

Comments
 (0)