Skip to content

Commit

Permalink
fix: prepare failed when projectPath was string
Browse files Browse the repository at this point in the history
Resolves a bug that lead to fail during build when projectpath was a string instead of an array
  • Loading branch information
Kampfmoehre committed Apr 5, 2022
1 parent 648e8f8 commit f3297b7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 12 deletions.
14 changes: 12 additions & 2 deletions src/prepare.ts
@@ -1,12 +1,18 @@
import execa from "execa";
import execa, { ExecaReturnBase } from "execa";
import { resolve } from "path";
import { Config, Context } from "semantic-release";
import { UserConfig } from "./UserConfig";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const SRError = require("@semantic-release/error");

export const prepare = async (pluginConfig: Config & UserConfig, context: Context): Promise<void> => {
const dotnet = pluginConfig.dotnet || "dotnet";

try {
pluginConfig.projectPath = Array.isArray(pluginConfig.projectPath)
? pluginConfig.projectPath
: [pluginConfig.projectPath];

for (const projectPath of pluginConfig.projectPath as string[]) {
const project = resolve(projectPath);

Expand All @@ -29,6 +35,10 @@ export const prepare = async (pluginConfig: Config & UserConfig, context: Contex
}
} catch (err) {
context.logger.error(`${dotnet} pack failed: ${(err as Error).message}`);
throw err;
throw new SRError(
`${dotnet} pack failed`,
(err as ExecaReturnBase<void>).exitCode,
(err as ExecaReturnBase<void>).command,
);
}
};
26 changes: 22 additions & 4 deletions src/publish.ts
@@ -1,17 +1,20 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import execa from "execa";
import execa, { ExecaReturnBase } from "execa";
import { resolve } from "path";
import { Config, Context } from "semantic-release";
import { UserConfig } from "./UserConfig";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const SemanticReleaseError = require("@semantic-release/error");

export const publish = async (pluginConfig: Config & UserConfig, context: Context): Promise<void> => {
const dotnet = pluginConfig.dotnet || "dotnet";
const registry = pluginConfig.nugetServer ?? "https://api.nuget.org/v3/index.json";
const packagePath = resolve("out");
const baseCliArgs: string[] = ["nuget", "push"];
const token: string = process.env.NUGET_TOKEN!;

try {
const cliArgs = [...baseCliArgs, "-s", registry, "-k", process.env.NUGET_TOKEN!];
const cliArgs = [...baseCliArgs, "-s", registry, "-k", token];

cliArgs.push(`${packagePath}/*.nupkg`);

Expand All @@ -20,7 +23,18 @@ export const publish = async (pluginConfig: Config & UserConfig, context: Contex
await execa(dotnet, cliArgs, { stdio: "inherit" });
} catch (error) {
context.logger.error(`${dotnet} push failed: ${(error as Error).message}`);
throw error;

if (typeof error === "object" && (error as ExecaReturnBase<void>).exitCode) {
const err = error as ExecaReturnBase<void>;
let description = err.command;

// hide token from SR output
if (err.command && err.command.includes(token)) {
description = description.replace(token, "[redacted]");
}

throw new SemanticReleaseError(`publish to registry ${registry} failed`, err.exitCode, description);
}
}

if (pluginConfig.publishToGitLab !== true) {
Expand Down Expand Up @@ -55,6 +69,10 @@ export const publish = async (pluginConfig: Config & UserConfig, context: Contex
await execa(dotnet, cliArgs, { stdio: "inherit" });
} catch (error) {
context.logger.error(`${dotnet} push failed: ${(error as Error).message}`);
throw error;
throw new SemanticReleaseError(
"publish to GitLab failed",
(error as ExecaReturnBase<void>).exitCode,
(error as ExecaReturnBase<void>).command,
);
}
};
6 changes: 3 additions & 3 deletions src/verify.ts
Expand Up @@ -20,15 +20,15 @@ export const verify = async (pluginConfig: Config & UserConfig, _: Context): Pro
}
}

const projects: string[] = Array.isArray(pluginConfig.projectPath)
pluginConfig.projectPath = Array.isArray(pluginConfig.projectPath)
? pluginConfig.projectPath
: [pluginConfig.projectPath];

if (projects.length < 1) {
if (pluginConfig.projectPath.length < 1) {
errors.push(new Error("No project files given"));
}

for (const project of projects) {
for (const project of pluginConfig.projectPath) {
const projectPath = resolve(project ?? "");
try {
const stats = await promises.stat(projectPath);
Expand Down
14 changes: 11 additions & 3 deletions test/prepare.test.ts
Expand Up @@ -94,20 +94,28 @@ describe("prepare", () => {
});

await expect(prepare({ projectPath: "b", includeSymbols: true }, context)).rejects.toThrowError(
"Something went wrong.",
"dotnet pack failed",
);
});

it("should add PackageVersion argument when usePackageVersion is set to true", async () => {
const execaMock = execa as unknown as jest.Mock<ExecaReturnBase<string>, never[]>;
execaMock.mockReturnValue({ exitCode: 0 } as ExecaReturnBase<string>);

await prepare({ projectPath: "b", usePackageVersion: true }, context);
await prepare({ projectPath: "src/SomeProject/SomeProject.csproj", usePackageVersion: true }, context);

expect(execaMock).toHaveBeenCalledTimes(1);
expect(execaMock.mock.calls[0]).toEqual([
"dotnet",
expect.arrayContaining(["pack", resolve("b"), "-c", "Release", "-o", "out", "-p:PackageVersion=1.0.0"]),
expect.arrayContaining([
"pack",
resolve("src/SomeProject/SomeProject.csproj"),
"-c",
"Release",
"-o",
"out",
"-p:PackageVersion=1.0.0",
]),
{ stdio: "inherit" },
]);
});
Expand Down
24 changes: 24 additions & 0 deletions test/publish.test.ts
Expand Up @@ -99,4 +99,28 @@ describe("publish", () => {
{ stdio: "inherit" },
]);
});

it("should report error when pushing fails and mask nuget token", async () => {
execaMock.mockImplementationOnce(() => {
const result: Partial<ExecaReturnBase<void>> = {
command: "dotnet nuget push -s https://api.nuget.org/v3/index.json -k 104E4 out/*.nupkg",
exitCode: 1,
};
throw result;
});

let thrown: any | undefined = undefined;
try {
await publish({ projectPath: ["a/path/to/project"] }, context);
} catch (err) {
thrown = err as Error;
}

expect(thrown).not.toBeUndefined();
expect(thrown.message).toBe("publish to registry https://api.nuget.org/v3/index.json failed");
expect(thrown.code).toBe(1);
expect(thrown.details).toBe("dotnet nuget push -s https://api.nuget.org/v3/index.json -k [redacted] out/*.nupkg");

expect(execaMock).toHaveBeenCalledTimes(1);
});
});

0 comments on commit f3297b7

Please sign in to comment.