Skip to content

Commit 0a141c9

Browse files
committed
fix: increase retry limit and make it configurable
1 parent d82152e commit 0a141c9

File tree

7 files changed

+48
-6
lines changed

7 files changed

+48
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ If you need to bypass the proxy for some hosts, configure the `NO_PROXY` environ
9595
| `failCommentCondition` | Use this as condition, when to comment on or create an issues in case of failures. See [failCommentCondition](#failCommentCondition). | - |
9696
| `labels` | The [labels](https://docs.gitlab.com/ee/user/project/labels.html#labels) to add to the issue created when a release fails. Set to `false` to not add any label. Labels should be comma-separated as described in the [official docs](https://docs.gitlab.com/ee/api/issues.html#new-issue), e.g. `"semantic-release,bot"`. | `semantic-release` |
9797
| `assignee` | The [assignee](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#assignee) to add to the issue created when a release fails. | - |
98+
| `retryLimit` | The maximum number of retries for failing HTTP requests. | `3` |
9899

99100
#### assets
100101

lib/fail.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ export default async (pluginConfig, context) => {
1414
errors,
1515
logger,
1616
} = context;
17-
const { gitlabToken, gitlabUrl, gitlabApiUrl, failComment, failTitle, failCommentCondition, labels, assignee } =
17+
const { gitlabToken, gitlabUrl, gitlabApiUrl, failComment, failTitle, failCommentCondition, labels, assignee, retryLimit } =
1818
resolveConfig(pluginConfig, context);
1919
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2020
const encodedRepoId = encodeURIComponent(repoId);
21-
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
21+
const apiOptions = {
22+
headers: { "PRIVATE-TOKEN": gitlabToken },
23+
retry: { limit: retryLimit }
24+
};
2225

2326
if (failComment === false || failTitle === false) {
2427
logger.log("Skip issue creation.");

lib/publish.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default async (pluginConfig, context) => {
2222
nextRelease: { gitTag, gitHead, notes, version },
2323
logger,
2424
} = context;
25-
const { gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy } = resolveConfig(pluginConfig, context);
25+
const { gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy, retryLimit } = resolveConfig(pluginConfig, context);
2626
const assetsList = [];
2727
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2828
const encodedRepoId = encodeURIComponent(repoId);
@@ -46,6 +46,7 @@ export default async (pluginConfig, context) => {
4646
},
4747
],
4848
},
49+
retry: { limit: retryLimit }
4950
};
5051

5152
debug("repoId: %o", repoId);

lib/resolve-config.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default (
1515
failCommentCondition,
1616
labels,
1717
assignee,
18+
retryLimit,
1819
},
1920
{
2021
envCi: { service } = {},
@@ -34,6 +35,7 @@ export default (
3435
},
3536
}
3637
) => {
38+
const DEFAULT_RETRY_LIMIT = 3
3739
const userGitlabApiPathPrefix = isNil(gitlabApiPathPrefix)
3840
? isNil(GL_PREFIX)
3941
? GITLAB_PREFIX
@@ -64,6 +66,7 @@ export default (
6466
failCommentCondition,
6567
labels: isNil(labels) ? "semantic-release" : labels === false ? false : labels,
6668
assignee,
69+
retryLimit: retryLimit ?? DEFAULT_RETRY_LIMIT
6770
};
6871
};
6972

lib/success.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ export default async (pluginConfig, context) => {
1515
commits,
1616
releases,
1717
} = context;
18-
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, successCommentCondition, proxy } = resolveConfig(
18+
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, successCommentCondition, proxy, retryLimit } = resolveConfig(
1919
pluginConfig,
2020
context
2121
);
2222
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2323
const encodedRepoId = encodeURIComponent(repoId);
24-
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
24+
const apiOptions = {
25+
headers: { "PRIVATE-TOKEN": gitlabToken },
26+
retry: { limit: retryLimit }
27+
};
2528

2629
if (successComment === false) {
2730
logger.log("Skip commenting on issues and pull requests.");

test/resolve-config.test.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const defaultOptions = {
1717
labels: "semantic-release",
1818
assignee: undefined,
1919
proxy: {},
20+
retryLimit: 3
2021
};
2122

2223
test("Returns user config", (t) => {
@@ -27,10 +28,11 @@ test("Returns user config", (t) => {
2728
const postComments = true;
2829
const proxy = {};
2930
const labels = false;
31+
const retryLimit = 42
3032

3133
t.deepEqual(
3234
resolveConfig(
33-
{ gitlabUrl, gitlabApiPathPrefix, assets, postComments, labels },
35+
{ gitlabUrl, gitlabApiPathPrefix, assets, postComments, labels, retryLimit },
3436
{ env: { GITLAB_TOKEN: gitlabToken } }
3537
),
3638
{
@@ -40,6 +42,7 @@ test("Returns user config", (t) => {
4042
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
4143
assets,
4244
labels: false,
45+
retryLimit
4346
}
4447
);
4548

test/success.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,31 @@ test.serial("Does not post comments when successCommentCondition is set to false
305305

306306
t.true(gitlab.isDone());
307307
});
308+
309+
310+
test.serial("Retries requests when rate limited", async (t) => {
311+
const owner = "test_user";
312+
const repo = "test_repo";
313+
const env = { GITLAB_TOKEN: "gitlab_token" };
314+
const pluginConfig = {};
315+
const nextRelease = { version: "1.0.0" };
316+
const releases = [{ name: RELEASE_NAME, url: "https://gitlab.com/test_user/test_repo/-/releases/v1.0.0" }];
317+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
318+
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
319+
const commits = [{ hash: "abcdef" }];
320+
const retryLimit = 3
321+
const gitlab = authenticate(env)
322+
.get(`/projects/${encodedRepoId}/repository/commits/abcdef/merge_requests`)
323+
.times(retryLimit)
324+
.reply(429)
325+
.get(`/projects/${encodedRepoId}/repository/commits/abcdef/merge_requests`)
326+
.reply(200, [{ project_id: 100, iid: 1, state: "merged" }])
327+
.get(`/projects/100/merge_requests/1/closes_issues`)
328+
.reply(200, [])
329+
.post(`/projects/100/merge_requests/1/notes`)
330+
.reply(200)
331+
332+
await success(pluginConfig, { env, options, nextRelease, logger: t.context.logger, commits, releases, retryLimit });
333+
334+
t.true(gitlab.isDone());
335+
});

0 commit comments

Comments
 (0)