Skip to content

Commit af29586

Browse files
committed
refactor: introduce projectApiUrl abstraction
1 parent a1d8d9f commit af29586

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

lib/fail.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default async (pluginConfig, context) => {
2727
} = resolveConfig(pluginConfig, context);
2828
const projectPath = getProjectPath(context, gitlabUrl, repositoryUrl);
2929
const encodedProjectPath = encodeURIComponent(projectPath);
30+
const projectApiUrl = urlJoin(gitlabApiUrl, `/projects/${encodedProjectPath}`);
3031
const apiOptions = {
3132
headers: { "PRIVATE-TOKEN": gitlabToken },
3233
retry: { limit: retryLimit },
@@ -42,7 +43,7 @@ Using 'false' for 'failComment' or 'failTitle' is deprecated and will be removed
4243
const encodedFailTitle = encodeURIComponent(failTitle);
4344
const description = failComment ? template(failComment)({ branch, errors }) : getFailComment(branch, errors);
4445

45-
const issuesEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedProjectPath}/issues`);
46+
const issuesEndpoint = urlJoin(projectApiUrl, `issues`);
4647
const openFailTitleIssueEndpoint = urlJoin(issuesEndpoint, `?state=opened&search=${encodedFailTitle}`);
4748

4849
const openFailTitleIssues = await got(openFailTitleIssueEndpoint, { ...apiOptions }).json();

lib/publish.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default async (pluginConfig, context) => {
2929
const assetsList = [];
3030
const projectPath = getProjectPath(context, gitlabUrl, repositoryUrl);
3131
const encodedProjectPath = encodeURIComponent(projectPath);
32+
const projectApiUrl = urlJoin(gitlabApiUrl, `/projects/${encodedProjectPath}`);
3233
const encodedGitTag = encodeURIComponent(gitTag);
3334
const encodedVersion = encodeURIComponent(version);
3435
const apiOptions = {
@@ -114,8 +115,8 @@ export default async (pluginConfig, context) => {
114115
const encodedLabel = encodeURIComponent(label);
115116
// https://docs.gitlab.com/ee/user/packages/generic_packages/#publish-a-package-file
116117
uploadEndpoint = urlJoin(
117-
gitlabApiUrl,
118-
`/projects/${encodedProjectPath}/packages/generic/release/${encodedVersion}/${encodedLabel}?${
118+
projectApiUrl,
119+
`packages/generic/release/${encodedVersion}/${encodedLabel}?${
119120
status ? `status=${status}&` : ""
120121
}select=package_file`
121122
);
@@ -131,16 +132,16 @@ export default async (pluginConfig, context) => {
131132

132133
// https://docs.gitlab.com/ee/user/packages/generic_packages/#download-package-file
133134
const url = urlJoin(
134-
gitlabApiUrl,
135-
`/projects/${encodedProjectPath}/packages/generic/release/${encodedVersion}/${encodedLabel}`
135+
projectApiUrl,
136+
`packages/generic/release/${encodedVersion}/${encodedLabel}`
136137
);
137138

138139
assetsList.push({ label, alt: "release", url, type: "package", filepath });
139140

140141
logger.log("Uploaded file: %s (%s)", url, response.file.url);
141142
} else {
142143
// Handle normal assets
143-
uploadEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedProjectPath}/uploads`);
144+
uploadEndpoint = urlJoin(projectApiUrl, "uploads");
144145

145146
debug("POST-ing the file %s to %s", file, uploadEndpoint);
146147

@@ -167,7 +168,7 @@ export default async (pluginConfig, context) => {
167168

168169
debug("Create a release for git tag %o with commit %o", gitTag, gitHead);
169170

170-
const createReleaseEndpoint = urlJoin(gitlabApiUrl, `/projects/${encodedProjectPath}/releases`);
171+
const createReleaseEndpoint = urlJoin(projectApiUrl, "releases");
171172

172173
const json = {
173174
/* eslint-disable camelcase */

lib/success.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default async (pluginConfig, context) => {
1919
resolveConfig(pluginConfig, context);
2020
const projectPath = getProjectPath(context, gitlabUrl, repositoryUrl);
2121
const encodedProjectPath = encodeURIComponent(projectPath);
22+
const projectApiUrl = urlJoin(gitlabApiUrl, `/projects/${encodedProjectPath}`);
2223
const apiOptions = {
2324
headers: { "PRIVATE-TOKEN": gitlabToken },
2425
retry: { limit: retryLimit },
@@ -78,8 +79,8 @@ Using 'false' for 'successComment' is deprecated and will be removed in a future
7879

7980
const getRelatedMergeRequests = async (commitHash) => {
8081
const relatedMergeRequestsEndpoint = urlJoin(
81-
gitlabApiUrl,
82-
`/projects/${encodedProjectPath}/repository/commits/${commitHash}/merge_requests`
82+
projectApiUrl,
83+
`repository/commits/${commitHash}/merge_requests`
8384
);
8485
debug("Getting MRs from %s", relatedMergeRequestsEndpoint);
8586
const relatedMergeRequests = await got

0 commit comments

Comments
 (0)