Skip to content

Commit

Permalink
fix(trigger) Missing await + indicate that inputs MUST be strings
Browse files Browse the repository at this point in the history
  • Loading branch information
aurelien-baudet committed Jan 11, 2021
1 parent dccdc41 commit 93e95b1
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ _This action is a fork of `benc-uk/workflow-dispatch` to add support for waiting
The solution is to manually create a PAT and store it as a secret e.g. `${{ secrets.PERSONAL_TOKEN }}`

### `inputs`
**Optional.** The inputs to pass to the workflow (if any are configured), this must be a JSON encoded string, e.g. `{ "myInput": "foobar" }`
**Optional.** The inputs to pass to the workflow (if any are configured), this must be a JSON encoded string, e.g. `{ "myInput": "foobar" }`.

All values must be strings (even if they are used as booleans or numbers in the triggered workflow). The triggered workflow should use `fromJson` function to get the right type

### `ref`
**Optional.** The Git reference used with the triggered workflow run. The reference can be a branch, tag, or a commit SHA. If omitted the context ref of the triggering workflow is used. If you want to trigger on pull requests and run the target workflow in the context of the pull request branch, set the ref to `${{ github.event.pull_request.head.ref }}`
Expand Down
2 changes: 1 addition & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ inputs:
description: 'GitHub token with repo write access, can NOT use secrets.GITHUB_TOKEN, see readme'
required: true
inputs:
description: 'Inputs to pass to the workflow, must be a JSON string'
description: 'Inputs to pass to the workflow, must be a JSON string. All values must be strings (even if used as boolean or number)'
required: false
ref:
description: 'The reference of the workflow run. The reference can be a branch, tag, or a commit SHA'
Expand Down
31 changes: 28 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5912,7 +5912,7 @@ function run() {
const args = utils_1.getArgs();
const workflowHandler = new workflow_handler_1.WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref);
// Trigger workflow run
workflowHandler.triggerWorkflow(args.inputs);
yield workflowHandler.triggerWorkflow(args.inputs);
core.info(`Workflow triggered 🚀`);
if (args.displayWorkflowUrl) {
const url = yield getFollowUrl(workflowHandler, args.displayWorkflowUrlInterval, args.displayWorkflowUrlTimeout);
Expand Down Expand Up @@ -6147,7 +6147,8 @@ class WorkflowHandler {
debug_1.debug('Workflow Dispatch', dispatchResp);
}
catch (error) {
core.setFailed(error.message);
debug_1.debug('Workflow Dispatch error', error.message);
throw error;
}
});
}
Expand All @@ -6168,6 +6169,29 @@ class WorkflowHandler {
};
}
catch (error) {
debug_1.debug('Workflow Run status error', error);
throw error;
}
});
}
getWorkflowRunArtifacts() {
return __awaiter(this, void 0, void 0, function* () {
try {
const runId = yield this.getWorkflowRunId();
const response = yield this.octokit.actions.getWorkflowRunArtifacts({
owner: this.owner,
repo: this.repo,
run_id: runId
});
debug_1.debug('Workflow Run artifacts', response);
return {
url: response.data.html_url,
status: ofStatus(response.data.status),
conclusion: ofConclusion(response.data.conclusion)
};
}
catch (error) {
debug_1.debug('Workflow Run artifacts error', error);
throw error;
}
});
Expand Down Expand Up @@ -6204,6 +6228,7 @@ class WorkflowHandler {
return this.workflowRunId;
}
catch (error) {
debug_1.debug('Get workflow run id error', error);
throw error;
}
});
Expand Down Expand Up @@ -6234,7 +6259,7 @@ class WorkflowHandler {
return this.workflowId;
}
catch (error) {
// core.setFailed(error.message);
debug_1.debug('List workflows error', error);
throw error;
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "workflow-dispatch-and-wait",
"version": "2.1.0",
"version": "2.1.1",
"description": "Trigger running GitHub Actions workflows and wait for result",
"main": "dist/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function run(): Promise<void> {
const workflowHandler = new WorkflowHandler(args.token, args.workflowRef, args.owner, args.repo, args.ref);

// Trigger workflow run
workflowHandler.triggerWorkflow(args.inputs);
await workflowHandler.triggerWorkflow(args.inputs);
core.info(`Workflow triggered 🚀`);

if (args.displayWorkflowUrl) {
Expand Down
36 changes: 28 additions & 8 deletions src/workflow-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,10 @@ export class WorkflowHandler {
ref: this.ref,
inputs
});

debug('Workflow Dispatch', dispatchResp);

} catch (error) {
core.setFailed(error.message)
debug('Workflow Dispatch error', error.message);
throw error;
}
}

Expand All @@ -83,7 +82,6 @@ export class WorkflowHandler {
repo: this.repo,
run_id: runId
});

debug('Workflow Run status', response);

return {
Expand All @@ -93,6 +91,30 @@ export class WorkflowHandler {
};

} catch (error) {
debug('Workflow Run status error', error);
throw error;
}
}


async getWorkflowRunArtifacts(): Promise<WorkflowRunResult> {
try {
const runId = await this.getWorkflowRunId();
const response = await this.octokit.actions.getWorkflowRunArtifacts({
owner: this.owner,
repo: this.repo,
run_id: runId
});
debug('Workflow Run artifacts', response);

return {
url: response.data.html_url,
status: ofStatus(response.data.status),
conclusion: ofConclusion(response.data.conclusion)
};

} catch (error) {
debug('Workflow Run artifacts error', error);
throw error;
}
}
Expand All @@ -110,12 +132,10 @@ export class WorkflowHandler {
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).valueOf() >= this.triggerDate);

debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r: any) => ({
id: r.id,
name: r.name,
Expand All @@ -132,6 +152,7 @@ export class WorkflowHandler {
this.workflowRunId = runs[0].id as number;
return this.workflowRunId;
} catch (error) {
debug('Get workflow run id error', error);
throw error;
}

Expand All @@ -152,7 +173,6 @@ export class WorkflowHandler {
repo: this.repo
});
const workflows = workflowsResp.data.workflows;

debug(`List Workflows`, workflows);

// Locate workflow either by name or id
Expand All @@ -162,7 +182,7 @@ export class WorkflowHandler {
this.workflowId = workflowFind.id as number;
return this.workflowId;
} catch(error) {
// core.setFailed(error.message);
debug('List workflows error', error);
throw error;
}
}
Expand Down

0 comments on commit 93e95b1

Please sign in to comment.