Skip to content

Commit e3047bb

Browse files
committed
fix: #777 check for existing NuGet source before adding
Adds a check if a NuGet source with the GitLab url exists before adding a new one. This fixes #777
1 parent 5c51d3d commit e3047bb

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

src/publish.mts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,31 @@ export const publish = async (pluginConfig: Config & UserConfig, context: Publis
6262
}
6363

6464
const url = `${process.env.CI_SERVER_URL!}/api/v4/projects/${projectId}/packages/nuget/index.json`;
65-
context.logger.log(`Adding GitLab as NuGet source ${url}`);
66-
await execa(
67-
dotnet,
68-
[
69-
"nuget",
70-
"add",
71-
"source",
72-
url,
73-
"--name",
74-
"gitlab",
75-
"--username",
76-
gitlabUser,
77-
"--password",
78-
gitlabToken,
79-
"--store-password-in-clear-text",
80-
],
81-
{ stdio: "inherit" },
82-
);
65+
66+
// Check if there is already a NuGet source with the GitLab url before adding it
67+
const { stdout } = await execa(dotnet, ["nuget", "list", "source", "--format", "short"]);
68+
if (stdout?.includes(url) === true) {
69+
context.logger.log(`GitLab NuGet source ${url} already exists, skip adding`);
70+
} else {
71+
context.logger.log(`Adding GitLab as NuGet source ${url}`);
72+
await execa(
73+
dotnet,
74+
[
75+
"nuget",
76+
"add",
77+
"source",
78+
url,
79+
"--name",
80+
"gitlab",
81+
"--username",
82+
gitlabUser,
83+
"--password",
84+
gitlabToken,
85+
"--store-password-in-clear-text",
86+
],
87+
{ stdio: "inherit" },
88+
);
89+
}
8390

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

test/publish.test.mts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, beforeAll, describe, expect, it, jest } from "@jest/globals";
2-
import type { ExecaChildProcess, ExecaError, execa } from "execa";
2+
import type { ExecaChildProcess, ExecaError, ExecaReturnBase, execa } from "execa";
33
import { PublishContext } from "semantic-release";
44
import { publishFailed } from "../src/Helper.mjs";
55
import type { publish as publishType } from "../src/publish.mjs";
@@ -234,6 +234,13 @@ describe("publish", () => {
234234
});
235235

236236
it("should use gitlabRegistryProjectId over CI_PROJECT_ID if set", async () => {
237+
execaMock.mockImplementationOnce(() => {
238+
return {
239+
stdout:
240+
"dotnet nuget push -s https://gitlab.com/api/v4/projects/12345/packages/nuget/index.json -k 104E4 out/*.nupkg",
241+
exitCode: 0,
242+
} as Partial<ExecaReturnBase<string>> as never;
243+
});
237244
execaMock.mockImplementationOnce(() => {
238245
return {
239246
command:
@@ -255,8 +262,8 @@ describe("publish", () => {
255262
context,
256263
);
257264

258-
expect(execaMock).toHaveBeenCalledTimes(2);
259-
expect(execaMock.mock.calls[0]).toEqual([
265+
expect(execaMock).toHaveBeenCalledTimes(3);
266+
expect(execaMock.mock.calls[1]).toEqual([
260267
"dotnet",
261268
[
262269
"nuget",

0 commit comments

Comments
 (0)