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

add options to set max attempts and intervals #1873

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ Please refer to the [release page](https://github.com/actions/checkout/releases/
# running from unless specified. Example URLs are https://github.com or
# https://my-ghes-server.example.com
github-server-url: ''

# Maximum number of attempts
# Default: 3
max-attempts: ''

# Minimum number of seconds to wait before retrying
# Default: 10
min-retry-interval: ''

# Maximum number of seconds to wait before retrying
# Default: 20
max-retry-interval: ''
```
<!-- end usage -->

Expand Down
24 changes: 24 additions & 0 deletions __test__/retry-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,28 @@ describe('retry-helper tests', () => {
expect(info[2]).toBe('some error 2')
expect(info[3]).toMatch(/Waiting .+ seconds before trying again/)
})

it('should be able to set max attempts', async () => {
let attempts = 0
let error: Error = null as unknown as Error
try {
retryHelper.config(5, 1, 5)
await retryHelper.execute(() => {
throw new Error(`some error ${++attempts}`)
})
} catch (err) {
error = err as Error
}
expect(error.message).toBe('some error 5')
expect(attempts).toBe(5)
expect(info).toHaveLength(6)
expect(info[0]).toBe('some error 1')
expect(info[1]).toMatch(/Waiting .+ seconds before trying again/)
expect(info[2]).toBe('some error 2')
expect(info[3]).toMatch(/Waiting .+ seconds before trying again/)
expect(info[4]).toBe('some error 3')
expect(info[5]).toMatch(/Waiting .+ seconds before trying again/)
expect(info[6]).toBe('some error 4')
expect(info[7]).toMatch(/Waiting .+ seconds before trying again/)
})
})
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ inputs:
github-server-url:
description: The base URL for the GitHub instance that you are trying to clone from, will use environment defaults to fetch from the same instance that the workflow is running from unless specified. Example URLs are https://github.com or https://my-ghes-server.example.com
required: false
max-attempts:
description: Maximum number of attempts
default: 3
min-retry-interval:
description: Minimum number of seconds to wait before retrying
default: 10
max-retry-interval:
description: Maximum number of seconds to wait before retrying
default: 20
runs:
using: node20
main: dist/index.js
Expand Down
14 changes: 12 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,10 @@ function getInputs() {
// Determine the GitHub URL that the repository is being hosted from
result.githubServerUrl = core.getInput('github-server-url');
core.debug(`GitHub Host URL = ${result.githubServerUrl}`);
// Retry
result.maxAttempts = parseInt(core.getInput('max-attempts') || '3');
result.minRetryInterval = parseInt(core.getInput('min-retry-interval') || '10');
result.maxRetryInterval = parseInt(core.getInput('max-retry-interval') || '20');
return result;
});
}
Expand Down Expand Up @@ -1887,11 +1891,13 @@ const gitSourceProvider = __importStar(__nccwpck_require__(9210));
const inputHelper = __importStar(__nccwpck_require__(5480));
const path = __importStar(__nccwpck_require__(1017));
const stateHelper = __importStar(__nccwpck_require__(4866));
const retryHelper = __importStar(__nccwpck_require__(2155));
function run() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
try {
const sourceSettings = yield inputHelper.getInputs();
retryHelper.config(sourceSettings.maxAttempts, sourceSettings.minRetryInterval, sourceSettings.maxRetryInterval);
try {
// Register problem matcher
coreCommand.issueCommand('add-matcher', {}, path.join(__dirname, 'problem-matcher.json'));
Expand Down Expand Up @@ -2265,7 +2271,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.execute = exports.RetryHelper = void 0;
exports.config = exports.execute = exports.RetryHelper = void 0;
const core = __importStar(__nccwpck_require__(2186));
const defaultMaxAttempts = 3;
const defaultMinSeconds = 10;
Expand Down Expand Up @@ -2311,13 +2317,17 @@ class RetryHelper {
}
}
exports.RetryHelper = RetryHelper;
let retryHelper = new RetryHelper();
function execute(action) {
return __awaiter(this, void 0, void 0, function* () {
const retryHelper = new RetryHelper();
return yield retryHelper.execute(action);
});
}
exports.execute = execute;
function config(maxAttempts, minSeconds, maxSeconds) {
retryHelper = new RetryHelper(maxAttempts, minSeconds, maxSeconds);
}
exports.config = config;


/***/ }),
Expand Down
15 changes: 15 additions & 0 deletions src/git-source-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,19 @@ export interface IGitSourceSettings {
* User override on the GitHub Server/Host URL that hosts the repository to be cloned
*/
githubServerUrl: string | undefined

/**
* Retry helper max attempts
*/
maxAttempts: number

/**
* Retry helper min interval seconds
*/
minRetryInterval: number

/**
* Retry helper max interval seconds
*/
maxRetryInterval: number
}
5 changes: 5 additions & 0 deletions src/input-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,10 @@ export async function getInputs(): Promise<IGitSourceSettings> {
result.githubServerUrl = core.getInput('github-server-url')
core.debug(`GitHub Host URL = ${result.githubServerUrl}`)

// Retry
result.maxAttempts = parseInt(core.getInput('max-attempts') || '3')
result.minRetryInterval = parseInt(core.getInput('min-retry-interval') || '10')
result.maxRetryInterval = parseInt(core.getInput('max-retry-interval') || '20')

return result
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import * as gitSourceProvider from './git-source-provider'
import * as inputHelper from './input-helper'
import * as path from 'path'
import * as stateHelper from './state-helper'
import * as retryHelper from './retry-helper'

async function run(): Promise<void> {
try {
const sourceSettings = await inputHelper.getInputs()
retryHelper.config(sourceSettings.maxAttempts, sourceSettings.minRetryInterval, sourceSettings.maxRetryInterval)

try {
// Register problem matcher
Expand Down
7 changes: 6 additions & 1 deletion src/retry-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export class RetryHelper {
}
}

let retryHelper = new RetryHelper()

export async function execute<T>(action: () => Promise<T>): Promise<T> {
const retryHelper = new RetryHelper()
return await retryHelper.execute(action)
}

export function config(maxAttempts: number, minSeconds: number, maxSeconds: number): void {
retryHelper = new RetryHelper(maxAttempts, minSeconds, maxSeconds)
}