Skip to content

Commit bae2f6f

Browse files
fix(generic-packages): set correct URL for external provider and default storage (#498)
1 parent b2dab9b commit bae2f6f

File tree

2 files changed

+139
-5
lines changed

2 files changed

+139
-5
lines changed

lib/publish.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import getRepoId from "./get-repo-id.js";
1212
import getAssets from "./glob-assets.js";
1313
import { RELEASE_NAME } from "./definitions/constants.js";
1414

15+
const isUrlScheme = (value) => /^(https|http|ftp):\/\//.test(value);
16+
1517
export default async (pluginConfig, context) => {
1618
const {
1719
cwd,
@@ -104,7 +106,7 @@ export default async (pluginConfig, context) => {
104106

105107
const { url } = response.file;
106108

107-
assetsList.push({ label, alt: "release", url, type: "package", filepath, target });
109+
assetsList.push({ label, alt: "release", url, type: "package", filepath });
108110

109111
logger.log("Uploaded file: %s", url);
110112
} else {
@@ -124,7 +126,7 @@ export default async (pluginConfig, context) => {
124126

125127
const { url, alt } = response;
126128

127-
assetsList.push({ label, alt, url, type, filepath, target });
129+
assetsList.push({ label, alt, url, type, filepath });
128130

129131
logger.log("Uploaded file: %s", url);
130132
}
@@ -143,10 +145,10 @@ export default async (pluginConfig, context) => {
143145
description: notes && notes.trim() ? notes : gitTag,
144146
milestones,
145147
assets: {
146-
links: assetsList.map(({ label, alt, url, type, filepath, rawUrl, target }) => {
148+
links: assetsList.map(({ label, alt, url, type, filepath, rawUrl }) => {
147149
return {
148150
name: label || alt,
149-
url: rawUrl || (target === "generic_package" ? url : urlJoin(gitlabUrl, repoId, url)),
151+
url: rawUrl || (isUrlScheme(url) ? url : urlJoin(gitlabUrl, repoId, url)),
150152
link_type: type,
151153
filepath,
152154
};

test/publish.test.js

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test.serial("Publish a release with assets", async (t) => {
8383
t.true(gitlab.isDone());
8484
});
8585

86-
test.serial("Publish a release with generics", async (t) => {
86+
test.serial("Publish a release with generics and relative URL", async (t) => {
8787
const cwd = "test/fixtures/files";
8888
const owner = "test_user";
8989
const repo = "test_repo";
@@ -96,6 +96,138 @@ test.serial("Publish a release with generics", async (t) => {
9696
const generic = { path: "file.css", label: "Style package", target: "generic_package", status: "hidden" };
9797
const assets = [generic];
9898
const encodedLabel = encodeURIComponent(generic.label);
99+
const gitlab = authenticate(env)
100+
.post(`/projects/${encodedRepoId}/releases`, {
101+
tag_name: nextRelease.gitTag,
102+
description: nextRelease.notes,
103+
assets: {
104+
links: [
105+
{
106+
name: "Style package",
107+
url: `https://gitlab.com/${owner}/${repo}${uploaded.file.url}`,
108+
link_type: "package",
109+
},
110+
],
111+
},
112+
})
113+
.reply(200);
114+
const gitlabUpload = authenticate(env)
115+
.put(
116+
`/projects/${encodedRepoId}/packages/generic/release/${encodedGitTag}/${encodedLabel}?status=${generic.status}&select=package_file`,
117+
/\.test\s\{\}/gm
118+
)
119+
.reply(200, uploaded);
120+
121+
const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
122+
123+
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
124+
t.deepEqual(t.context.log.args[0], ["Uploaded file: %s", uploaded.file.url]);
125+
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
126+
t.true(gitlabUpload.isDone());
127+
t.true(gitlab.isDone());
128+
});
129+
130+
test.serial("Publish a release with generics and external storage provider (http)", async (t) => {
131+
const cwd = "test/fixtures/files";
132+
const owner = "test_user";
133+
const repo = "test_repo";
134+
const env = { GITLAB_TOKEN: "gitlab_token" };
135+
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" };
136+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
137+
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
138+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
139+
const uploaded = { file: { url: "http://aws.example.com/bucket/gitlab/file.css" } };
140+
const generic = { path: "file.css", label: "Style package", target: "generic_package", status: "hidden" };
141+
const assets = [generic];
142+
const encodedLabel = encodeURIComponent(generic.label);
143+
const gitlab = authenticate(env)
144+
.post(`/projects/${encodedRepoId}/releases`, {
145+
tag_name: nextRelease.gitTag,
146+
description: nextRelease.notes,
147+
assets: {
148+
links: [
149+
{
150+
name: "Style package",
151+
url: uploaded.file.url,
152+
link_type: "package",
153+
},
154+
],
155+
},
156+
})
157+
.reply(200);
158+
const gitlabUpload = authenticate(env)
159+
.put(
160+
`/projects/${encodedRepoId}/packages/generic/release/${encodedGitTag}/${encodedLabel}?status=${generic.status}&select=package_file`,
161+
/\.test\s\{\}/gm
162+
)
163+
.reply(200, uploaded);
164+
165+
const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
166+
167+
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
168+
t.deepEqual(t.context.log.args[0], ["Uploaded file: %s", uploaded.file.url]);
169+
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
170+
t.true(gitlabUpload.isDone());
171+
t.true(gitlab.isDone());
172+
});
173+
174+
test.serial("Publish a release with generics and external storage provider (https)", async (t) => {
175+
const cwd = "test/fixtures/files";
176+
const owner = "test_user";
177+
const repo = "test_repo";
178+
const env = { GITLAB_TOKEN: "gitlab_token" };
179+
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" };
180+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
181+
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
182+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
183+
const uploaded = { file: { url: "https://aws.example.com/bucket/gitlab/file.css" } };
184+
const generic = { path: "file.css", label: "Style package", target: "generic_package", status: "hidden" };
185+
const assets = [generic];
186+
const encodedLabel = encodeURIComponent(generic.label);
187+
const gitlab = authenticate(env)
188+
.post(`/projects/${encodedRepoId}/releases`, {
189+
tag_name: nextRelease.gitTag,
190+
description: nextRelease.notes,
191+
assets: {
192+
links: [
193+
{
194+
name: "Style package",
195+
url: uploaded.file.url,
196+
link_type: "package",
197+
},
198+
],
199+
},
200+
})
201+
.reply(200);
202+
const gitlabUpload = authenticate(env)
203+
.put(
204+
`/projects/${encodedRepoId}/packages/generic/release/${encodedGitTag}/${encodedLabel}?status=${generic.status}&select=package_file`,
205+
/\.test\s\{\}/gm
206+
)
207+
.reply(200, uploaded);
208+
209+
const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
210+
211+
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
212+
t.deepEqual(t.context.log.args[0], ["Uploaded file: %s", uploaded.file.url]);
213+
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
214+
t.true(gitlabUpload.isDone());
215+
t.true(gitlab.isDone());
216+
});
217+
218+
test.serial("Publish a release with generics and external storage provider (ftp)", async (t) => {
219+
const cwd = "test/fixtures/files";
220+
const owner = "test_user";
221+
const repo = "test_repo";
222+
const env = { GITLAB_TOKEN: "gitlab_token" };
223+
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" };
224+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
225+
const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
226+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
227+
const uploaded = { file: { url: "ftp://drive.example.com/gitlab/file.css" } };
228+
const generic = { path: "file.css", label: "Style package", target: "generic_package", status: "hidden" };
229+
const assets = [generic];
230+
const encodedLabel = encodeURIComponent(generic.label);
99231
const gitlab = authenticate(env)
100232
.post(`/projects/${encodedRepoId}/releases`, {
101233
tag_name: nextRelease.gitTag,

0 commit comments

Comments
 (0)