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

fix: increase retry limit and make it configurable #800

Merged
merged 2 commits into from
Dec 4, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ If you need to bypass the proxy for some hosts, configure the `NO_PROXY` environ
| `failCommentCondition` | Use this as condition, when to comment on or create an issues in case of failures. See [failCommentCondition](#failCommentCondition). | - |
| `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` |
| `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. | - |
| `retryLimit` | The maximum number of retries for failing HTTP requests. | `3` |

#### assets

Expand Down
18 changes: 15 additions & 3 deletions lib/fail.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ export default async (pluginConfig, context) => {
errors,
logger,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, failComment, failTitle, failCommentCondition, labels, assignee } =
resolveConfig(pluginConfig, context);
const {
gitlabToken,
gitlabUrl,
gitlabApiUrl,
failComment,
failTitle,
failCommentCondition,
labels,
assignee,
retryLimit,
} = resolveConfig(pluginConfig, context);
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
const apiOptions = {
headers: { "PRIVATE-TOKEN": gitlabToken },
retry: { limit: retryLimit },
};

if (failComment === false || failTitle === false) {
logger.log("Skip issue creation.");
Expand Down
6 changes: 5 additions & 1 deletion lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export default async (pluginConfig, context) => {
nextRelease: { gitTag, gitHead, notes, version },
logger,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy } = resolveConfig(pluginConfig, context);
const { gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy, retryLimit } = resolveConfig(
pluginConfig,
context
);
const assetsList = [];
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
Expand All @@ -46,6 +49,7 @@ export default async (pluginConfig, context) => {
},
],
},
retry: { limit: retryLimit },
};

debug("repoId: %o", repoId);
Expand Down
3 changes: 3 additions & 0 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default (
failCommentCondition,
labels,
assignee,
retryLimit,
},
{
envCi: { service } = {},
Expand All @@ -34,6 +35,7 @@ export default (
},
}
) => {
const DEFAULT_RETRY_LIMIT = 3;
const userGitlabApiPathPrefix = isNil(gitlabApiPathPrefix)
? isNil(GL_PREFIX)
? GITLAB_PREFIX
Expand Down Expand Up @@ -64,6 +66,7 @@ export default (
failCommentCondition,
labels: isNil(labels) ? "semantic-release" : labels === false ? false : labels,
assignee,
retryLimit: retryLimit ?? DEFAULT_RETRY_LIMIT,
};
};

Expand Down
11 changes: 6 additions & 5 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ export default async (pluginConfig, context) => {
commits,
releases,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, successCommentCondition, proxy } = resolveConfig(
pluginConfig,
context
);
const { gitlabToken, gitlabUrl, gitlabApiUrl, successComment, successCommentCondition, proxy, retryLimit } =
resolveConfig(pluginConfig, context);
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };
const apiOptions = {
headers: { "PRIVATE-TOKEN": gitlabToken },
retry: { limit: retryLimit },
};

if (successComment === false) {
logger.log("Skip commenting on issues and pull requests.");
Expand Down
5 changes: 4 additions & 1 deletion test/resolve-config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const defaultOptions = {
labels: "semantic-release",
assignee: undefined,
proxy: {},
retryLimit: 3,
};

test("Returns user config", (t) => {
Expand All @@ -27,10 +28,11 @@ test("Returns user config", (t) => {
const postComments = true;
const proxy = {};
const labels = false;
const retryLimit = 42;

t.deepEqual(
resolveConfig(
{ gitlabUrl, gitlabApiPathPrefix, assets, postComments, labels },
{ gitlabUrl, gitlabApiPathPrefix, assets, postComments, labels, retryLimit },
{ env: { GITLAB_TOKEN: gitlabToken } }
),
{
Expand All @@ -40,6 +42,7 @@ test("Returns user config", (t) => {
gitlabApiUrl: urlJoin(gitlabUrl, gitlabApiPathPrefix),
assets,
labels: false,
retryLimit,
}
);

Expand Down
27 changes: 27 additions & 0 deletions test/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,30 @@ test.serial("Does not post comments when successCommentCondition is set to false

t.true(gitlab.isDone());
});

test.serial("Retries requests when rate limited", async (t) => {
const owner = "test_user";
const repo = "test_repo";
const env = { GITLAB_TOKEN: "gitlab_token" };
const pluginConfig = {};
const nextRelease = { version: "1.0.0" };
const releases = [{ name: RELEASE_NAME, url: "https://gitlab.com/test_user/test_repo/-/releases/v1.0.0" }];
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
const commits = [{ hash: "abcdef" }];
const retryLimit = 3;
const gitlab = authenticate(env)
.get(`/projects/${encodedRepoId}/repository/commits/abcdef/merge_requests`)
.times(retryLimit)
.reply(429)
.get(`/projects/${encodedRepoId}/repository/commits/abcdef/merge_requests`)
.reply(200, [{ project_id: 100, iid: 1, state: "merged" }])
.get(`/projects/100/merge_requests/1/closes_issues`)
.reply(200, [])
.post(`/projects/100/merge_requests/1/notes`)
.reply(200);

await success(pluginConfig, { env, options, nextRelease, logger: t.context.logger, commits, releases, retryLimit });

t.true(gitlab.isDone());
});
Loading