Repository: semantic-release/gitlab (js)

Issue:
Generic package from registry can't be extracted
Hi everyone 👋 

I'm using GitLab registry to push an asset with the format `gzip compressed data` (.tar.gz) but when I download the uploaded package, I can't decompress it.

Follow my `.releaserc.yml`

```yaml
branches:
  - "main"
plugins:
  - "@semantic-release/commit-analyzer"
  - "@semantic-release/release-notes-generator"
  - - "@semantic-release/exec"
    - verifyConditionsCmd: 'tar -czf package.tar.gz --exclude=".git*" --exclude="__pycache__" aws/'
  - - "@semantic-release/gitlab"
    - assets:
        - path: "package.tar.gz"
          filepath: "/package.tar.gz"
          label: "package.tar.gz"
          target: generic_package
          status: default
tagFormat: ${version}
```

If we do a `cat` to the package file, we will see weird data on top and at the end of the file.

```bash
❯ cat package.tar.gz
----------------------------636039438515691214353669
Content-Disposition: form-data; name="file"; filename="aws.tar.gz"
Content-Type: application/gzip

<REDACTED BINARY DATA>
----------------------------636039438515691214353669--
```

If we remove the first 4 files from the file with the command `sed -e '1,4d' < package.tar.gz > package2.tar.gz`, we will decompress it.

If we compress the file manually, upload it, and download it after we can decompress it since the data are been untouched.

Can someone give any insight?

Pull request:
fix(generic-package): fix issues when uploading multiple assets
This should fix #490 (WIP). Working on it I found some issues which were not recognized yet:
- wrong README example
- missing proxy
- invalid URL if `gitlapApiUrl` is set

Patch:
diff --git a/README.md b/README.md
index fa26ae6..208eb7f 100644
--- a/README.md
+++ b/README.md
@@ -35,7 +35,7 @@ The plugin can be configured in the [**semantic-release** configuration file](ht
         "gitlabUrl": "https://custom.gitlab.com",
         "assets": [
           { "path": "dist/asset.min.css", "label": "CSS distribution" },
-          { "path": "dist/asset.min.js", "label": "JS distribution", "type": "generic_package" },
+          { "path": "dist/asset.min.js", "label": "JS distribution", "target": "generic_package" },
           { "path": "dist/asset.min.js", "label": "v${nextRelease.version}.js" },
           { "url": "https://gitlab.com/gitlab-org/gitlab/-/blob/master/README.md" }
         ]
diff --git a/lib/publish.js b/lib/publish.js
index b081dfa..56c6902 100644
--- a/lib/publish.js
+++ b/lib/publish.js
@@ -1,4 +1,4 @@
-import { createReadStream } from "fs";
+import { createReadStream, readFileSync } from "fs";
 import pathlib from "path";
 import fs from "fs-extra";
 import { isPlainObject, template } from "lodash-es";
@@ -80,10 +80,6 @@ export default async (pluginConfig, context) => {
           debug("file target: %o", target);
           debug("file status: %o", status);
 
-          // Uploaded assets to the project
-          const form = new FormData();
-          form.append("file", createReadStream(file));
-
           let uploadEndpoint;
           let response;
 
@@ -100,7 +96,7 @@ export default async (pluginConfig, context) => {
             debug("PUT-ing the file %s to %s", file, uploadEndpoint);
 
             try {
-              response = await got.put(uploadEndpoint, { ...apiOptions, body: form }).json();
+              response = await got.put(uploadEndpoint, { ...apiOptions, ...proxy, body: readFileSync(file) }).json();
             } catch (error) {
               logger.error("An error occurred while uploading %s to the GitLab generics package API:\n%O", file, error);
               throw error;
@@ -108,7 +104,7 @@ export default async (pluginConfig, context) => {
 
             const { url } = response.file;
 
-            assetsList.push({ label, alt: "release", url, type: "package", filepath });
+            assetsList.push({ label, alt: "release", url, type: "package", filepath, target });
 
             logger.log("Uploaded file: %s", url);
           } else {
@@ -118,6 +114,8 @@ export default async (pluginConfig, context) => {
             debug("POST-ing the file %s to %s", file, uploadEndpoint);
 
             try {
+              const form = new FormData();
+              form.append("file", createReadStream(file));
               response = await got.post(uploadEndpoint, { ...apiOptions, ...proxy, body: form }).json();
             } catch (error) {
               logger.error("An error occurred while uploading %s to the GitLab project uploads API:\n%O", file, error);
@@ -126,7 +124,7 @@ export default async (pluginConfig, context) => {
 
             const { url, alt } = response;
 
-            assetsList.push({ label, alt, url, type, filepath });
+            assetsList.push({ label, alt, url, type, filepath, target });
 
             logger.log("Uploaded file: %s", url);
           }
@@ -145,10 +143,10 @@ export default async (pluginConfig, context) => {
     description: notes && notes.trim() ? notes : gitTag,
     milestones,
     assets: {
-      links: assetsList.map(({ label, alt, url, type, filepath, rawUrl }) => {
+      links: assetsList.map(({ label, alt, url, type, filepath, rawUrl, target }) => {
         return {
           name: label || alt,
-          url: rawUrl || urlJoin(gitlabUrl, repoId, url),
+          url: rawUrl || (target === "generic_package" ? url : urlJoin(gitlabUrl, repoId, url)),
           link_type: type,
           filepath,
         };


Test patch:
diff --git a/test/publish.test.js b/test/publish.test.js
index 4e15ebe..40371fe 100644
--- a/test/publish.test.js
+++ b/test/publish.test.js
@@ -104,7 +104,7 @@ test.serial("Publish a release with generics", async (t) => {
         links: [
           {
             name: "Style package",
-            url: `https://gitlab.com/${owner}/${repo}${uploaded.file.url}`,
+            url: uploaded.file.url,
             link_type: "package",
           },
         ],
@@ -114,7 +114,7 @@ test.serial("Publish a release with generics", async (t) => {
   const gitlabUpload = authenticate(env)
     .put(
       `/projects/${encodedRepoId}/packages/generic/release/${encodedGitTag}/${encodedLabel}?status=${generic.status}&select=package_file`,
-      /filename="file.css"/gm
+      /\.test\s\{\}/gm
     )
     .reply(200, uploaded);