Skip to content

Commit 7644bc2

Browse files
Copilotfgreinacher
andcommitted
fix: allow templating asset glob patterns
Co-authored-by: fgreinacher <[email protected]>
1 parent 0d8cf55 commit 7644bc2

File tree

3 files changed

+90
-25
lines changed

3 files changed

+90
-25
lines changed

lib/publish.js

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,49 @@ export default async (pluginConfig, context) => {
5555
debug("milestones: %o", milestones);
5656

5757
if (assets && assets.length > 0) {
58+
const templatedAssets = assets.map((asset) => {
59+
if (isPlainObject(asset)) {
60+
const templatedAsset = { ...asset };
61+
if (asset.path) {
62+
templatedAsset.path = Array.isArray(asset.path)
63+
? asset.path.map((pattern) => template(pattern)(context))
64+
: template(asset.path)(context);
65+
}
66+
templatedAsset.url = asset.url ? template(asset.url)(context) : asset.url;
67+
templatedAsset.label = asset.label ? template(asset.label)(context) : asset.label;
68+
templatedAsset.type = asset.type ? template(asset.type)(context) : asset.type;
69+
templatedAsset.filepath = asset.filepath ? template(asset.filepath)(context) : asset.filepath;
70+
templatedAsset.target = asset.target ? template(asset.target)(context) : asset.target;
71+
templatedAsset.status = asset.status ? template(asset.status)(context) : asset.status;
72+
templatedAsset.packageName = asset.packageName ? template(asset.packageName)(context) : asset.packageName;
73+
return templatedAsset;
74+
} else if (Array.isArray(asset)) {
75+
// Handle array of glob patterns
76+
return asset.map((pattern) => template(pattern)(context));
77+
} else {
78+
// String asset path
79+
return template(asset)(context);
80+
}
81+
});
82+
5883
// Skip glob if url is provided
59-
const urlAssets = assets.filter((asset) => asset.url);
84+
const urlAssets = templatedAssets.filter((asset) => asset.url);
6085
debug("url assets: %o", urlAssets);
6186
const globbedAssets = await getAssets(
6287
context,
63-
assets.filter((asset) => !asset.url)
88+
templatedAssets.filter((asset) => !asset.url)
6489
);
6590
debug("globbed assets: %o", globbedAssets);
6691
const allAssets = [...urlAssets, ...globbedAssets];
6792
debug("all assets: %o", allAssets);
6893

6994
await Promise.all(
7095
allAssets.map(async (asset) => {
71-
const path = template((isPlainObject(asset) ? asset : { path: asset }).path)(context);
72-
const _url = asset.url ? template(asset.url)(context) : undefined;
73-
const label = asset.label ? template(asset.label)(context) : undefined;
74-
const type = asset.type ? template(asset.type)(context) : undefined;
75-
const filepath = asset.filepath ? template(asset.filepath)(context) : undefined;
76-
const target = asset.target ? template(asset.target)(context) : undefined;
77-
const status = asset.status ? template(asset.status)(context) : undefined;
78-
const packageName = asset.packageName ? template(asset.packageName)(context) : "release";
79-
80-
if (_url) {
81-
assetsList.push({ label, rawUrl: _url, type, filepath });
82-
debug("use link from release setting: %s", _url);
96+
const path = isPlainObject(asset) ? asset.path : asset;
97+
98+
if (asset.url) {
99+
assetsList.push({ label: asset.label, rawUrl: asset.url, type: asset.type, filepath: asset.filepath });
100+
debug("use link from release setting: %s", asset.url);
83101
} else {
84102
const file = pathlib.resolve(cwd, path);
85103

@@ -98,18 +116,19 @@ export default async (pluginConfig, context) => {
98116
}
99117

100118
debug("file path: %o", path);
101-
debug("file label: %o", label);
102-
debug("file type: %o", type);
103-
debug("file filepath: %o", filepath);
104-
debug("file target: %o", target);
105-
debug("file status: %o", status);
106-
debug("package name: %o", packageName);
119+
debug("file label: %o", asset.label);
120+
debug("file type: %o", asset.type);
121+
debug("file filepath: %o", asset.filepath);
122+
debug("file target: %o", asset.target);
123+
debug("file status: %o", asset.status);
124+
debug("package name: %o", asset.packageName);
107125

108126
let uploadEndpoint;
109127
let response;
110128

111-
if (target === "generic_package") {
112-
const finalLabel = label ?? pathlib.basename(file);
129+
if (asset.target === "generic_package") {
130+
const finalLabel = asset.label ?? pathlib.basename(file);
131+
const packageName = asset.packageName ?? "release";
113132
// Upload generic packages
114133
const encodedVersion = encodeURIComponent(version);
115134
const encodedPackageName = encodeURIComponent(packageName);
@@ -118,7 +137,7 @@ export default async (pluginConfig, context) => {
118137
uploadEndpoint = urlJoin(
119138
projectApiUrl,
120139
`packages/generic/${encodedPackageName}/${encodedVersion}/${encodedLabel}?${
121-
status ? `status=${status}&` : ""
140+
asset.status ? `status=${asset.status}&` : ""
122141
}select=package_file`
123142
);
124143

@@ -137,7 +156,7 @@ export default async (pluginConfig, context) => {
137156
`packages/generic/${encodedPackageName}/${encodedVersion}/${encodedLabel}`
138157
);
139158

140-
assetsList.push({ label: finalLabel, alt: packageName, url, type: "package", filepath });
159+
assetsList.push({ label: finalLabel, alt: packageName, url, type: "package", filepath: asset.filepath });
141160

142161
logger.log("Uploaded file: %s (%s)", url, response.file.url);
143162
} else {
@@ -158,7 +177,7 @@ export default async (pluginConfig, context) => {
158177
const { alt, full_path } = response;
159178
const url = urlJoin(gitlabUrl, full_path);
160179

161-
assetsList.push({ label, alt, url, type, filepath });
180+
assetsList.push({ label: asset.label, alt, url, type: asset.type, filepath: asset.filepath });
162181

163182
logger.log("Uploaded file: %s", url);
164183
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Test file content for release with version in filename.

test/publish.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,48 @@ test.serial("Publish a release with error response", async (t) => {
823823
t.is(error.message, `Response code 499 (Something went wrong)`);
824824
t.true(gitlab.isDone());
825825
});
826+
827+
test.serial("Publish a release with templated wildcard path", async (t) => {
828+
const cwd = "test/fixtures";
829+
const owner = "test_user";
830+
const repo = "test_repo";
831+
const env = { GITLAB_TOKEN: "gitlab_token" };
832+
const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body", version: "1.0.0" };
833+
const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
834+
const encodedProjectPath = encodeURIComponent(`${owner}/${repo}`);
835+
const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
836+
837+
const assets = ["versioned/upload_v${nextRelease.version}.txt"];
838+
839+
const uploaded = {
840+
url: "/uploads/upload_v1.0.0.txt",
841+
alt: "upload_v1.0.0.txt",
842+
full_path: "/-/project/4/66dbcd21ec5d24ed6ea225176098d52b/upload_v1.0.0.txt",
843+
};
844+
845+
const gitlab = authenticate(env)
846+
.post(`/projects/${encodedProjectPath}/releases`, {
847+
tag_name: nextRelease.gitTag,
848+
description: nextRelease.notes,
849+
assets: {
850+
links: [
851+
{
852+
name: "upload_v1.0.0.txt",
853+
url: `https://gitlab.com${uploaded.full_path}`,
854+
},
855+
],
856+
},
857+
})
858+
.reply(200);
859+
const gitlabUpload = authenticate(env)
860+
.post(`/projects/${encodedProjectPath}/uploads`, /Content-Disposition.*upload_v1\.0\.0\.txt/g)
861+
.reply(200, uploaded);
862+
863+
const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
864+
865+
t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
866+
t.deepEqual(t.context.log.args[0], ["Uploaded file: %s", `https://gitlab.com${uploaded.full_path}`]);
867+
t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
868+
t.true(gitlab.isDone());
869+
t.true(gitlabUpload.isDone());
870+
});

0 commit comments

Comments
 (0)