Skip to content

Commit be690ca

Browse files
author
Ludovic TOURMAN
committed
Split function
1 parent 1fda9fc commit be690ca

File tree

3 files changed

+56
-33
lines changed

3 files changed

+56
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ For details of the `workflow_dispatch` even see [this blog post introducing this
2121
2222
### `token`
2323

24-
> **Required.** A GitHub access token (PAT) with write access to the repo in question.
24+
> **Required.** A GitHub access token (PAT) with write access to the repo in question.
2525
>
2626
> **NOTE.** The automatically provided token e.g. `${{ secrets.GITHUB_TOKEN }}` can not be used, GitHub prevents this token from being able to fire the `workflow_dispatch` and `repository_dispatch` event. [The reasons are explained in the docs](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token).
2727
> The solution is to manually create a PAT and store it as a secret e.g. `${{ secrets.PERSONAL_TOKEN }}`

dist/index.js

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10089,11 +10089,8 @@ class WorkflowHandler {
1008910089
}
1009010090
});
1009110091
}
10092-
getWorkflowRunId() {
10092+
findAllWorkflowRuns() {
1009310093
return __awaiter(this, void 0, void 0, function* () {
10094-
if (this.workflowRunId) {
10095-
return this.workflowRunId;
10096-
}
1009710094
try {
1009810095
const workflowId = yield this.getWorkflowId();
1009910096
const response = yield this.octokit.rest.actions.listWorkflowRuns({
@@ -10104,24 +10101,30 @@ class WorkflowHandler {
1010410101
created: `>=${new Date(this.triggerDate).toISOString()}`
1010510102
});
1010610103
(0, debug_1.debug)('List Workflow Runs', response);
10107-
let runs = response.data.workflow_runs;
10104+
return response.data.workflow_runs;
10105+
}
10106+
catch (error) {
10107+
(0, debug_1.debug)('Fin all workflow runs error', error);
10108+
throw new Error(`Failed to list workflow runs. Cause: ${error}`);
10109+
}
10110+
});
10111+
}
10112+
getWorkflowRunId() {
10113+
return __awaiter(this, void 0, void 0, function* () {
10114+
if (this.workflowRunId) {
10115+
return this.workflowRunId;
10116+
}
10117+
try {
10118+
let runs = yield this.findAllWorkflowRuns();
1010810119
if (this.runName) {
10109-
runs = response.data.workflow_runs
10110-
.filter((r) => r.name == this.runName);
10120+
runs = runs.filter((r) => r.name == this.runName);
1011110121
}
1011210122
if (runs.length == 0) {
1011310123
throw new Error('Run not found');
1011410124
}
1011510125
if (runs.length > 1) {
1011610126
core.warning(`Found ${runs.length} runs. Using the last one.`);
10117-
(0, debug_1.debug)(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r) => ({
10118-
id: r.id,
10119-
name: r.name,
10120-
created_at: r.created_at,
10121-
triggerDate: new Date(this.triggerDate).toISOString(),
10122-
created_at_ts: new Date(r.created_at).valueOf(),
10123-
triggerDateTs: this.triggerDate
10124-
})));
10127+
yield this.debugFoundWorkflowRuns(runs);
1012510128
}
1012610129
this.workflowRunId = runs[0].id;
1012710130
return this.workflowRunId;
@@ -10166,6 +10169,16 @@ class WorkflowHandler {
1016610169
isFilename(workflowRef) {
1016710170
return /.+\.ya?ml$/.test(workflowRef);
1016810171
}
10172+
debugFoundWorkflowRuns(runs) {
10173+
(0, debug_1.debug)(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r) => ({
10174+
id: r.id,
10175+
name: r.name,
10176+
created_at: r.created_at,
10177+
triggerDate: new Date(this.triggerDate).toISOString(),
10178+
created_at_ts: new Date(r.created_at).valueOf(),
10179+
triggerDateTs: this.triggerDate
10180+
})));
10181+
}
1016910182
}
1017010183
exports.WorkflowHandler = WorkflowHandler;
1017110184

src/workflow-handler.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,9 @@ export class WorkflowHandler {
124124
}
125125
}
126126

127-
128-
async getWorkflowRunId(): Promise<number> {
129-
if (this.workflowRunId) {
130-
return this.workflowRunId;
131-
}
127+
private async findAllWorkflowRuns() {
132128
try {
133129
const workflowId = await this.getWorkflowId();
134-
135130
const response = await this.octokit.rest.actions.listWorkflowRuns({
136131
owner: this.owner,
137132
repo: this.repo,
@@ -142,10 +137,21 @@ export class WorkflowHandler {
142137

143138
debug('List Workflow Runs', response);
144139

145-
let runs = response.data.workflow_runs;
140+
return response.data.workflow_runs
141+
} catch (error) {
142+
debug('Fin all workflow runs error', error);
143+
throw new Error(`Failed to list workflow runs. Cause: ${error}`)
144+
}
145+
}
146+
147+
async getWorkflowRunId(): Promise<number> {
148+
if (this.workflowRunId) {
149+
return this.workflowRunId;
150+
}
151+
try {
152+
let runs = await this.findAllWorkflowRuns();
146153
if (this.runName) {
147-
runs = response.data.workflow_runs
148-
.filter((r: any) => r.name == this.runName)
154+
runs = runs.filter((r: any) => r.name == this.runName)
149155
}
150156

151157
if (runs.length == 0) {
@@ -154,14 +160,7 @@ export class WorkflowHandler {
154160

155161
if (runs.length > 1) {
156162
core.warning(`Found ${runs.length} runs. Using the last one.`);
157-
debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r: any) => ({
158-
id: r.id,
159-
name: r.name,
160-
created_at: r.created_at,
161-
triggerDate: new Date(this.triggerDate).toISOString(),
162-
created_at_ts: new Date(r.created_at).valueOf(),
163-
triggerDateTs: this.triggerDate
164-
})));
163+
await this.debugFoundWorkflowRuns(runs);
165164
}
166165

167166
this.workflowRunId = runs[0].id as number;
@@ -206,5 +205,16 @@ export class WorkflowHandler {
206205
return /.+\.ya?ml$/.test(workflowRef);
207206
}
208207

208+
private debugFoundWorkflowRuns(runs: any){
209+
debug(`Filtered Workflow Runs (after trigger date: ${new Date(this.triggerDate).toISOString()})`, runs.map((r: any) => ({
210+
id: r.id,
211+
name: r.name,
212+
created_at: r.created_at,
213+
triggerDate: new Date(this.triggerDate).toISOString(),
214+
created_at_ts: new Date(r.created_at).valueOf(),
215+
triggerDateTs: this.triggerDate
216+
})));
217+
}
218+
209219
}
210220

0 commit comments

Comments
 (0)