Skip to content

Commit

Permalink
fix: #777 check for existing NuGet source before adding
Browse files Browse the repository at this point in the history
Adds a check if a NuGet source with the GitLab url exists before adding a new one.

This fixes #777
  • Loading branch information
Kampfmoehre committed Jan 10, 2024
1 parent 5c51d3d commit e3047bb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
43 changes: 25 additions & 18 deletions src/publish.mts
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,31 @@ export const publish = async (pluginConfig: Config & UserConfig, context: Publis
}

const url = `${process.env.CI_SERVER_URL!}/api/v4/projects/${projectId}/packages/nuget/index.json`;
context.logger.log(`Adding GitLab as NuGet source ${url}`);
await execa(
dotnet,
[
"nuget",
"add",
"source",
url,
"--name",
"gitlab",
"--username",
gitlabUser,
"--password",
gitlabToken,
"--store-password-in-clear-text",
],
{ stdio: "inherit" },
);

// Check if there is already a NuGet source with the GitLab url before adding it
const { stdout } = await execa(dotnet, ["nuget", "list", "source", "--format", "short"]);
if (stdout?.includes(url) === true) {
context.logger.log(`GitLab NuGet source ${url} already exists, skip adding`);
} else {
context.logger.log(`Adding GitLab as NuGet source ${url}`);
await execa(
dotnet,
[
"nuget",
"add",
"source",
url,
"--name",
"gitlab",
"--username",
gitlabUser,
"--password",
gitlabToken,
"--store-password-in-clear-text",
],
{ stdio: "inherit" },
);
}

const cliArgs = [...baseCliArgs, "--source", "gitlab", `${packagePath}/*.nupkg`];

Expand Down
13 changes: 10 additions & 3 deletions test/publish.test.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterEach, beforeAll, describe, expect, it, jest } from "@jest/globals";
import type { ExecaChildProcess, ExecaError, execa } from "execa";
import type { ExecaChildProcess, ExecaError, ExecaReturnBase, execa } from "execa";
import { PublishContext } from "semantic-release";
import { publishFailed } from "../src/Helper.mjs";
import type { publish as publishType } from "../src/publish.mjs";
Expand Down Expand Up @@ -234,6 +234,13 @@ describe("publish", () => {
});

it("should use gitlabRegistryProjectId over CI_PROJECT_ID if set", async () => {
execaMock.mockImplementationOnce(() => {
return {
stdout:
"dotnet nuget push -s https://gitlab.com/api/v4/projects/12345/packages/nuget/index.json -k 104E4 out/*.nupkg",
exitCode: 0,
} as Partial<ExecaReturnBase<string>> as never;
});
execaMock.mockImplementationOnce(() => {
return {
command:
Expand All @@ -255,8 +262,8 @@ describe("publish", () => {
context,
);

expect(execaMock).toHaveBeenCalledTimes(2);
expect(execaMock.mock.calls[0]).toEqual([
expect(execaMock).toHaveBeenCalledTimes(3);
expect(execaMock.mock.calls[1]).toEqual([
"dotnet",
[
"nuget",
Expand Down

0 comments on commit e3047bb

Please sign in to comment.