Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use channel as package name for generic_package upload #607

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import resolveConfig from "./resolve-config.js";
import getRepoId from "./get-repo-id.js";
import getAssets from "./glob-assets.js";
import { RELEASE_NAME } from "./definitions/constants.js";
import semver from "semver";

const isUrlScheme = (value) => /^(https|http|ftp):\/\//.test(value);

export default async (pluginConfig, context) => {
const {
cwd,
options: { repositoryUrl },
nextRelease: { gitTag, gitHead, notes, version },
nextRelease: { gitTag, gitHead, notes, version, channel },
logger,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, assets, milestones, proxy } = resolveConfig(pluginConfig, context);
const assetsList = [];
const repoId = getRepoId(context, gitlabUrl, repositoryUrl);
const encodedRepoId = encodeURIComponent(repoId);
const encodedGitTag = encodeURIComponent(gitTag);
const encodedVersion = encodeURIComponent(version);
const apiOptions = { headers: { "PRIVATE-TOKEN": gitlabToken } };

debug("repoId: %o", repoId);
Expand Down Expand Up @@ -88,11 +88,14 @@ export default async (pluginConfig, context) => {

if (target === "generic_package") {
// Upload generic packages
const encodedLabel = encodeURIComponent(label);
const { major, minor, patch } = semver.parse(version);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand that correct, this renders it impossible to use prereleases (alpha, beta).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this does not look correct. GitLab allows much more:

The package version. The following regex validates this: \A(.?[\w+-]+.?)+\z. You can test your version strings on Rubular.

const encodedVersion = `${major}.${minor}.${patch}`;
const encodedLabel = encodeURIComponent(label ?? version);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the fallback to the version is confusing. The version will then be the file name and package version

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice this too.

Copy link
Contributor

@fgreinacher fgreinacher Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asset.label can be templated, so that should be solvable via configuration, see https://github.com/semantic-release/gitlab#usage, and we don't need to change the core logic.

const encodedChannel = encodeURIComponent(channel ?? "release");
// https://docs.gitlab.com/ee/user/packages/generic_packages/#publish-a-package-file
uploadEndpoint = urlJoin(
gitlabApiUrl,
`/projects/${encodedRepoId}/packages/generic/release/${encodedVersion}/${encodedLabel}?${
`/projects/${encodedRepoId}/packages/generic/${encodedChannel}/${encodedVersion}/${encodedLabel}?${
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the Gitlab documentation this changed part is the package name.
I would suggest, if we change this, to either use the package name or allow to provide a name for assets.
IMO the semantic release channel is not the correct way to go.

Copy link
Author

@phuocnb phuocnb Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my Package Registry on a private gitlab repo.

The repo home page

Screenshot from 2023-09-29 09-01-55

The channel released assets

Screenshot from 2023-09-29 09-02-46

Screenshot from 2023-09-29 09-06-13

The release detail

Screenshot from 2023-09-29 09-25-48

You may notice that it is organized much better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does look clean and well organized, yes.
My concerns are regarding users trying to use package names instead of channels.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.
It's better add it as an optional configuration.
Old users will not be forced to change their existing projects.

Copy link
Contributor

@fgreinacher fgreinacher Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the structure too, but I would not enforce it to all users.

Maybe let's just a configuration option for asset.packageName and make that templatable. Then you could configure it something like this:

{
  "branches": ["main"],
  "plugins": [
    [
      "@semantic-release/gitlab",
      {
        "assets": [
          {
            "path": "dist/asset.min.js",
            "label": "JS distribution",
            "target": "generic_package",
            "packageName": "${nextRelease.channel}"
          }
        ]
      }
    ]
  ]
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fgreinacher thank you for the suggestion.
I'm implementing it that way and will push new code soon.

status ? `status=${status}&` : ""
}select=package_file`
);
Expand All @@ -109,7 +112,7 @@ export default async (pluginConfig, context) => {
// https://docs.gitlab.com/ee/user/packages/generic_packages/#download-package-file
const url = urlJoin(
gitlabApiUrl,
`/projects/${encodedRepoId}/packages/generic/release/${encodedVersion}/${encodedLabel}`
`/projects/${encodedRepoId}/packages/generic/${encodedChannel}/${encodedVersion}/${encodedLabel}`
);

assetsList.push({ label, alt: "release", url, type: "package", filepath });
Expand Down
131 changes: 112 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"hpagent": "^1.0.0",
"lodash-es": "^4.17.21",
"parse-url": "^8.0.0",
"semver": "^7.3.2",
"url-join": "^4.0.0"
},
"devDependencies": {
Expand Down