Repository: semantic-release/gitlab
Pull request #630 (2023-11-07 10:39:17)

feat: templating of `path`
Runs Lodash templating on the asset path.

fgreinacher: Thanks @mattyclarkson! The change looks good! Please update the relevant [readme chapter](https://github.com/semantic-release/gitlab#assets) and add [a test case](https://github.com/semantic-release/gitlab/blob/master/test/publish.test.js) that verifies the templating works and we can get this in 🙇 
mattyclarkson: > Thanks @mattyclarkson! The change looks good! Please update the relevant [readme chapter](https://github.com/semantic-release/gitlab#assets) and add [a test case](https://github.com/semantic-release/gitlab/blob/master/test/publish.test.js) that verifies the templating works and we can get this in 🙇

Done ✔️ Hope it is enough test coverage!
mattyclarkson: Ooo, will fix up the test failures. Classic "worked on my machine" 😆 

Patch:
diff --git a/README.md b/README.md
index 208eb7f..9bc1a6a 100644
--- a/README.md
+++ b/README.md
@@ -101,7 +101,7 @@ Can be a [glob](https://github.com/isaacs/node-glob#glob-primer) or and `Array`
 
 | Property   | Description                                                                                                                                                                                                                                                                                                                        | Default                              |
 | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------ |
-| `path`     | **Required**, unless `url` is set. A [glob](https://github.com/isaacs/node-glob#glob-primer) to identify the files to upload.                                                                                                                                                                                                      | -                                    |
+| `path`     | **Required**, unless `url` is set. A [glob](https://github.com/isaacs/node-glob#glob-primer) to identify the files to upload. Supports [Lodash templating](https://lodash.com/docs#template).                                                                                                                                      | -                                    |
 | `url`      | Alternative to setting `path` this provides the ability to add links to releases, e.g. URLs to container images. Supports [Lodash templating](https://lodash.com/docs#template).                                                                                                                                                   | -                                    |
 | `label`    | Short description of the file displayed on the GitLab release. Ignored if `path` matches more than one file. Supports [Lodash templating](https://lodash.com/docs#template).                                                                                                                                                       | File name extracted from the `path`. |
 | `type`     | Asset type displayed on the GitLab release. Can be `runbook`, `package`, `image` and `other` (see official documents on [release assets](https://docs.gitlab.com/ee/user/project/releases/#release-assets)). Supports [Lodash templating](https://lodash.com/docs#template).                                                       | `other`                              |
diff --git a/lib/publish.js b/lib/publish.js
index b880ba4..ef691bf 100644
--- a/lib/publish.js
+++ b/lib/publish.js
@@ -48,7 +48,7 @@ export default async (pluginConfig, context) => {
 
     await Promise.all(
       allAssets.map(async (asset) => {
-        const { path } = isPlainObject(asset) ? asset : { path: asset };
+        const path = template((isPlainObject(asset) ? asset : { path: asset }).path)(context);
         const _url = asset.url ? template(asset.url)(context) : undefined;
         const label = asset.label ? template(asset.label)(context) : undefined;
         const type = asset.type ? template(asset.type)(context) : undefined;


Test patch:
diff --git a/test/publish.test.js b/test/publish.test.js
index ce4e714..78403ad 100644
--- a/test/publish.test.js
+++ b/test/publish.test.js
@@ -45,6 +45,45 @@ test.serial("Publish a release", async (t) => {
   t.true(gitlab.isDone());
 });
 
+test.serial("Publish a release with templated path", async (t) => {
+  const cwd = "test/fixtures/files";
+  const owner = "test_user";
+  const repo = "test_repo";
+  const env = { GITLAB_TOKEN: "gitlab_token", FIXTURE: "upload" };
+  const nextRelease = { gitHead: "123", gitTag: "v1.0.0", notes: "Test release note body" };
+  const options = { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` };
+  const encodedRepoId = encodeURIComponent(`${owner}/${repo}`);
+  const encodedGitTag = encodeURIComponent(nextRelease.gitTag);
+  const generic = { path: "${env.FIXTURE}.txt", filepath: "/upload.txt" };
+  const assets = [generic];
+  const uploaded = { url: "/uploads/upload.txt", alt: "upload.txt" };
+  const gitlab = authenticate(env)
+    .post(`/projects/${encodedRepoId}/releases`, {
+      tag_name: nextRelease.gitTag,
+      description: nextRelease.notes,
+      assets: {
+        links: [
+          {
+            name: "upload.txt",
+            url: `https://gitlab.com/${owner}/${repo}${uploaded.url}`,
+            filepath: "/upload.txt",
+          },
+        ],
+      },
+    })
+    .reply(200);
+  const gitlabUpload = authenticate(env)
+    .post(`/projects/${encodedRepoId}/uploads`, /Content-Disposition/g)
+    .reply(200, uploaded);
+
+  const result = await publish({ assets }, { env, cwd, options, nextRelease, logger: t.context.logger });
+
+  t.is(result.url, `https://gitlab.com/${owner}/${repo}/-/releases/${encodedGitTag}`);
+  t.deepEqual(t.context.log.args[0], ["Uploaded file: %s", uploaded.url]);
+  t.deepEqual(t.context.log.args[1], ["Published GitLab release: %s", nextRelease.gitTag]);
+  t.true(gitlab.isDone());
+});
+
 test.serial("Publish a release with assets", async (t) => {
   const cwd = "test/fixtures/files";
   const owner = "test_user";