Skip to content

Commit f1d3257

Browse files
authored
Merge pull request #800 from semantic-release/fix/retry-limit
2 parents 00a3f5e + e8c8f15 commit f1d3257

7 files changed

+61
-10
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

+15-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,23 @@ export default async (pluginConfig, context) => {
1414
errors,
1515
logger,
1616
} = context;
17-
const { gitlabToken, gitlabUrl, gitlabApiUrl, failComment, failTitle, failCommentCondition, labels, assignee } =
18-
resolveConfig(pluginConfig, context);
17+
const {
18+
gitlabToken,
19+
gitlabUrl,
20+
gitlabApiUrl,
21+
failComment,
22+
failTitle,
23+
failCommentCondition,
24+
labels,
25+
assignee,
26+
retryLimit,
27+
} = resolveConfig(pluginConfig, context);
1928
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2029
const encodedRepoId = encodeURIComponent(repoId);
21-
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
30+
const apiOptions = {
31+
headers: { "PRIVATE-TOKEN": gitlabToken },
32+
retry: { limit: retryLimit },
33+
};
2234

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

lib/publish.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ 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(
26+
pluginConfig,
27+
context
28+
);
2629
const assetsList = [];
2730
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2831
const encodedRepoId = encodeURIComponent(repoId);
@@ -46,6 +49,7 @@ export default async (pluginConfig, context) => {
4649
},
4750
],
4851
},
52+
retry: { limit: retryLimit },
4953
};
5054

5155
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

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ export default async (pluginConfig, context) => {
1515
commits,
1616
releases,
1717
} = context;
18-
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, successCommentCondition, proxy } = resolveConfig(
19-
pluginConfig,
20-
context
21-
);
18+
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, successCommentCondition, proxy, retryLimit } =
19+
resolveConfig(pluginConfig, context);
2220
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
2321
const encodedRepoId = encodeURIComponent(repoId);
24-
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
22+
const apiOptions = {
23+
headers: { "PRIVATE-TOKEN": gitlabToken },
24+
retry: { limit: retryLimit },
25+
};
2526

2627
if (successComment === false) {
2728
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

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

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

0 commit comments

Comments
 (0)