Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce abort-after-seconds #19

Merged
merged 7 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

## 0.1.0

* Initial release
* Initial release
3 changes: 1 addition & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This a [JavaScript](https://help.github.com/en/articles/about-actions#types-of-actions) action but uses [TypeScript](https://www.typescriptlang.org/docs/home.html) to generate that JavaScript.

You can bootstrap your envrinment with a modern version of npm and by running `npm i` at the root of this repo.
You can bootstrap your environment with a modern version of npm and by running `npm i` at the root of this repo.

## testing

Expand All @@ -15,4 +15,3 @@ Source code can be found under the `src` directory. Running `npm run build` will
## formatting

A minimal attempt at keeping a consistent code style is can be applied by running `npm run fmt`

37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,31 @@ jobs:
- name: Turnstyle
uses: softprops/turnstyle@v1
with:
+ continue-after-seconds: 180
+ continue-after-seconds: 180
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy
run: sleep 30
```

or before aborting the step with `jobs.<job_id>.steps.with.abort-after-seconds`


```diff
name: Main

on: push

jobs:
main:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Turnstyle
uses: softprops/turnstyle@v1
with:
+ abort-after-seconds: 180
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Deploy
Expand All @@ -96,11 +120,12 @@ jobs:

#### inputs

| Name | Type | Description |
|-------------|---------|-----------------------------------------------------------------|
| `continue-after-seconds` | number | Maximum number of seconds to wait before moving forward (unbound by default) |
| `poll-interval-seconds` | number | Number of seconds to wait in between checks for previous run completion (defaults to 60) |
| `same-branch-only` | boolean | Only wait on other runs from the same branch (defaults to true) |
| Name | Type | Description |
|-------------------------|---------|--------------------------------------------------------------------------------------------------------------------------------|
| `continue-after-seconds`| number | Maximum number of seconds to wait before moving forward (unbound by default). Mutually exclusive with abort-after-seconds |
| `abort-after-seconds` | number | Maximum number of seconds to wait before aborting the job (unbound by default). Mutually exclusive with continue-after-seconds |
| `poll-interval-seconds` | number | Number of seconds to wait in between checks for previous run completion (defaults to 60) |
| `same-branch-only` | boolean | Only wait on other runs from the same branch (defaults to true) |

#### outputs

Expand Down
44 changes: 44 additions & 0 deletions __tests__/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,54 @@ describe("input", () => {
workflowName: "test",
runId: 1,
continueAfterSeconds: 10,
abortAfterSeconds: undefined,
pollIntervalSeconds: 5,
sameBranchOnly: false
}
);
});

it("parses config from env with abortAfterSeconds", () => {
assert.deepEqual(
parseInput({
GITHUB_TOKEN: "s3cr3t",
GITHUB_REF: "refs/heads/foo",
GITHUB_REPOSITORY: "softprops/turnstyle",
GITHUB_WORKFLOW: "test",
GITHUB_RUN_ID: "1",
"INPUT_ABORT-AFTER-SECONDS": "10",
"INPUT_POLL-INTERVAL-SECONDS": "5",
"INPUT_SAME-BRANCH-ONLY": "false"
}),
{
githubToken: "s3cr3t",
owner: "softprops",
repo: "turnstyle",
branch: "foo",
workflowName: "test",
runId: 1,
continueAfterSeconds: undefined,
abortAfterSeconds: 10,
pollIntervalSeconds: 5,
sameBranchOnly: false
}
);
});

it("rejects env with continueAfterSeconds and abortAfterSeconds", () => {
assert.throws(() =>
parseInput({
GITHUB_TOKEN: "s3cr3t",
GITHUB_REF: "refs/heads/foo",
GITHUB_REPOSITORY: "softprops/turnstyle",
GITHUB_WORKFLOW: "test",
GITHUB_RUN_ID: "1",
"INPUT_CONTINUE-AFTER-SECONDS": "10",
"INPUT_ABORT-AFTER-SECONDS": "2"
})
);
});

it("parses config from env with defaults", () => {
assert.deepEqual(
parseInput({
Expand All @@ -46,6 +88,7 @@ describe("input", () => {
workflowName: "test",
runId: 1,
continueAfterSeconds: undefined,
abortAfterSeconds: undefined,
pollIntervalSeconds: 60,
sameBranchOnly: true
}
Expand All @@ -70,6 +113,7 @@ describe("input", () => {
workflowName: "test",
runId: 1,
continueAfterSeconds: undefined,
abortAfterSeconds: undefined,
pollIntervalSeconds: 60,
sameBranchOnly: true
}
Expand Down
38 changes: 38 additions & 0 deletions __tests__/wait.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe("wait", () => {
input = {
branch: "master",
continueAfterSeconds: undefined,
abortAfterSeconds: undefined,
pollIntervalSeconds: 1,
githubToken: "fake-token",
owner: "org",
Expand Down Expand Up @@ -61,6 +62,43 @@ describe("wait", () => {
]);
});

it("will abort after a prescribed number of seconds", async () => {
input.abortAfterSeconds = 1;
const inProgressRun = {
id: 1,
status: "in_progress",
html_url: ""
};
const githubClient = {
runs: async (
owner: string,
repo: string,
branch: string | undefined,
workflowId: number
) => Promise.resolve([inProgressRun]),
workflows: async (owner: string, repo: string) =>
Promise.resolve([workflow])
};

const messages: Array<string> = [];
const waiter = new Waiter(
workflow.id,
githubClient,
input,
(message: string) => {
messages.push(message);
}
);
await assert.rejects(waiter.wait(), {
name: "Error",
message: "Aborted after waiting 1 seconds"
});
assert.deepEqual(messages, [
"✋Awaiting run ...",
"🛑Exceeded wait seconds. Aborting..."
]);
});

it("will return when a run is completed", async () => {
const run: Run = {
id: 1,
Expand Down
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ inputs:
poll-interval-seconds:
description: "Number of seconds to wait in between checks for previous run completion (defaults to 60)"
continue-after-seconds:
description: "Maximum number of seconds to wait before moving forward (unbound by default)"
description: "Maximum number of seconds to wait before moving forward (unbound by default). Mutually exclusive with abort-after-seconds"
abort-after-seconds:
description: "Maximum number of seconds to wait before failing the step (unbound by default). Mutually exclusive with continue-after-seconds"
same-branch-only:
description: "Only wait on other runs from the same branch (defaults to true)"
branding:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Input {
runId: number;
pollIntervalSeconds: number;
continueAfterSeconds: number | undefined;
abortAfterSeconds: number | undefined;
sameBranchOnly: boolean;
}

Expand All @@ -23,6 +24,14 @@ export const parseInput = (env: Record<string, string | undefined>): Input => {
const continueAfterSeconds = env["INPUT_CONTINUE-AFTER-SECONDS"]
? parseInt(env["INPUT_CONTINUE-AFTER-SECONDS"], 10)
: undefined;
const abortAfterSeconds = env["INPUT_ABORT-AFTER-SECONDS"]
? parseInt(env["INPUT_ABORT-AFTER-SECONDS"], 10)
: undefined;
if (continueAfterSeconds !== undefined && abortAfterSeconds !== undefined) {
throw new Error(
"Only one of continue-after-seconds and abort-after-seconds may be defined"
);
}
const sameBranchOnly =
env["INPUT_SAME-BRANCH-ONLY"] === "true" ||
env["INPUT_SAME-BRANCH-ONLY"] === undefined; // true if not specified
Expand All @@ -35,6 +44,7 @@ export const parseInput = (env: Record<string, string | undefined>): Input => {
runId,
pollIntervalSeconds,
continueAfterSeconds,
abortAfterSeconds,
sameBranchOnly
};
};
8 changes: 8 additions & 0 deletions src/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export class Waiter implements Wait {
return secondsSoFar || 0;
}

if (
this.input.abortAfterSeconds &&
(secondsSoFar || 0) >= this.input.abortAfterSeconds
) {
this.info(`🛑Exceeded wait seconds. Aborting...`);
throw new Error(`Aborted after waiting ${secondsSoFar} seconds`);
}

const runs = await this.githubClient.runs(
this.input.owner,
this.input.repo,
Expand Down