Skip to content

Commit cc47465

Browse files
authored
feat: Actually use user provided options (#15)
Use user provided options when bundler is bundling.
1 parent 676e582 commit cc47465

File tree

13 files changed

+239
-52
lines changed

13 files changed

+239
-52
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { bundleAnalysisPluginFactory } from "../bundleAnalysisPluginFactory";
2+
3+
describe("bundleAnalysisPluginFactory", () => {
4+
it("returns a build start function", () => {
5+
const plugin = bundleAnalysisPluginFactory({
6+
userOptions: {},
7+
bundleAnalysisUploadPlugin: () => ({
8+
version: "1",
9+
name: "plugin-name",
10+
pluginVersion: "1.0.0",
11+
}),
12+
});
13+
14+
expect(plugin.buildStart).toEqual(expect.any(Function));
15+
});
16+
17+
it("returns a build end function", () => {
18+
const plugin = bundleAnalysisPluginFactory({
19+
userOptions: {},
20+
bundleAnalysisUploadPlugin: () => ({
21+
version: "1",
22+
name: "plugin-name",
23+
pluginVersion: "1.0.0",
24+
}),
25+
});
26+
27+
expect(plugin.buildEnd).toEqual(expect.any(Function));
28+
});
29+
30+
it("returns a write bundle function", () => {
31+
const plugin = bundleAnalysisPluginFactory({
32+
userOptions: {},
33+
bundleAnalysisUploadPlugin: () => ({
34+
version: "1",
35+
name: "plugin-name",
36+
pluginVersion: "1.0.0",
37+
}),
38+
});
39+
40+
expect(plugin.writeBundle).toEqual(expect.any(Function));
41+
});
42+
});

packages/bundler-plugin-core/src/bundle-analysis/bundleAnalysisPluginFactory.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,25 @@ export const bundleAnalysisPluginFactory = ({
5252
const inputs: ProviderUtilInputs = { envs, args };
5353
const provider = await detectProvider(inputs);
5454

55-
let sendStats = true;
5655
let url = "";
5756
try {
5857
url = await getPreSignedURL({
59-
apiURL: "http://localhost:3000",
60-
globalUploadToken: "123",
58+
apiURL: userOptions?.apiUrl ?? "https://api.codecov.io",
59+
globalUploadToken: userOptions?.globalUploadToken,
60+
repoToken: userOptions?.repoToken,
6161
serviceParams: provider,
62+
retryCount: userOptions?.retryCount,
6263
});
6364
} catch (error) {
64-
sendStats = false;
65+
return;
6566
}
6667

6768
try {
68-
if (sendStats) {
69-
await uploadStats({
70-
preSignedUrl: url,
71-
message: JSON.stringify(output),
72-
});
73-
}
69+
await uploadStats({
70+
preSignedUrl: url,
71+
message: JSON.stringify(output),
72+
retryCount: userOptions?.retryCount,
73+
});
7474
} catch {}
7575
},
7676
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class InvalidSlugError extends Error {
2+
constructor(msg: string) {
3+
super(msg);
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class NoCommitShaError extends Error {
2+
constructor(msg: string) {
3+
super(msg);
4+
}
5+
}

packages/bundler-plugin-core/src/types.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,13 @@ export interface Options {
5757
*/
5858
globalUploadToken?: string;
5959

60-
/**
61-
* The name of the repository to upload the bundle analysis information to.
62-
*
63-
* `globalUploadToken` and `repoName` must be set if this is not set.
64-
*/
65-
repoName?: string;
66-
6760
/**
6861
* The upload token to use for uploading the bundle analysis information.
6962
*
7063
* Mutually exclusive to using `globalUploadToken` and `repoName`.
7164
*/
7265
repoToken?: string;
7366

74-
/**
75-
* The commit hash to use for uploading the bundle analysis information.
76-
*
77-
* Defaults package.json name field.
78-
*/
79-
namespace?: string;
80-
81-
// TODO: Update the default value here
8267
/**
8368
* The api url used to fetch the upload url.
8469
*

packages/bundler-plugin-core/src/utils/__tests__/getPreSignedURL.test.ts

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getPreSignedURL } from "../getPreSignedURL.ts";
55
import { FailedFetchError } from "../../errors/FailedFetchError.ts";
66
import { NoUploadTokenError } from "../../errors/NoUploadTokenError.ts";
77
import { UploadLimitReachedError } from "../../errors/UploadLimitReachedError.ts";
8+
import { NoCommitShaError } from "../../errors/NoCommitShaError.ts";
89

910
const server = setupServer();
1011

@@ -56,6 +57,25 @@ describe("getPreSignedURL", () => {
5657

5758
describe("successful request", () => {
5859
describe("when the initial response is successful", () => {
60+
describe('"globalUploadToken" is provided and "repoToken" is', () => {
61+
it("returns the pre-signed URL", async () => {
62+
setup({
63+
data: { url: "http://example.com" },
64+
});
65+
66+
const url = await getPreSignedURL({
67+
apiURL: "http://localhost",
68+
globalUploadToken: "super-cool-token",
69+
repoToken: "super-repo-token",
70+
serviceParams: {
71+
commit: "123",
72+
},
73+
});
74+
75+
expect(url).toEqual("http://example.com");
76+
});
77+
});
78+
5979
describe('"globalUploadToken" is provided and "repoToken" is not', () => {
6080
it("returns the pre-signed URL", async () => {
6181
setup({
@@ -95,17 +115,16 @@ describe("getPreSignedURL", () => {
95115
});
96116

97117
describe("unsuccessful request", () => {
98-
describe("return body does not match schema", () => {
118+
describe("no upload token found", () => {
99119
it("throws an error", async () => {
100120
const { consoleSpy } = setup({
101-
data: { randomValue: "random" },
121+
data: { url: "http://example.com" },
102122
});
103123

104124
let error;
105125
try {
106126
await getPreSignedURL({
107127
apiURL: "http://localhost",
108-
globalUploadToken: "cool-upload-token",
109128
serviceParams: {
110129
commit: "123",
111130
},
@@ -115,11 +134,16 @@ describe("getPreSignedURL", () => {
115134
}
116135

117136
expect(consoleSpy).toHaveBeenCalled();
118-
expect(error).toBeInstanceOf(FailedFetchError);
137+
// for some reason, this test fails even tho it's the same values
138+
// Expected: "No upload token found"
139+
// Received: "No upload token found"
140+
// Number of calls: 1
141+
// expect(consoleSpy).toHaveBeenCalledWith("No upload token found");
142+
expect(error).toBeInstanceOf(NoUploadTokenError);
119143
});
120144
});
121145

122-
describe("no upload token found", () => {
146+
describe("no commit sha found", () => {
123147
it("throws an error", async () => {
124148
const { consoleSpy } = setup({
125149
data: { url: "http://example.com" },
@@ -129,6 +153,29 @@ describe("getPreSignedURL", () => {
129153
try {
130154
await getPreSignedURL({
131155
apiURL: "http://localhost",
156+
globalUploadToken: "global-upload-token",
157+
serviceParams: {},
158+
});
159+
} catch (e) {
160+
error = e;
161+
}
162+
163+
expect(consoleSpy).toHaveBeenCalled();
164+
expect(error).toBeInstanceOf(NoCommitShaError);
165+
});
166+
});
167+
168+
describe("return body does not match schema", () => {
169+
it("throws an error", async () => {
170+
const { consoleSpy } = setup({
171+
data: { randomValue: "random" },
172+
});
173+
174+
let error;
175+
try {
176+
await getPreSignedURL({
177+
apiURL: "http://localhost",
178+
globalUploadToken: "cool-upload-token",
132179
serviceParams: {
133180
commit: "123",
134181
},
@@ -138,12 +185,7 @@ describe("getPreSignedURL", () => {
138185
}
139186

140187
expect(consoleSpy).toHaveBeenCalled();
141-
// for some reason, this test fails even tho it's the same values
142-
// Expected: "No upload token found"
143-
// Received: "No upload token found"
144-
// Number of calls: 1
145-
// expect(consoleSpy).toHaveBeenCalledWith("No upload token found");
146-
expect(error).toBeInstanceOf(NoUploadTokenError);
188+
expect(error).toBeInstanceOf(FailedFetchError);
147189
});
148190
});
149191

packages/bundler-plugin-core/src/utils/__tests__/preProcessBody.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1+
import { InvalidSlugError } from "../../errors/InvalidSlugError";
12
import { preProcessBody } from "../preProcessBody";
23

34
describe("preProcessBody", () => {
5+
let consoleSpy: jest.SpyInstance;
6+
7+
beforeEach(() => {
8+
consoleSpy = jest.spyOn(console, "log").mockImplementation(() => null);
9+
});
10+
11+
afterEach(() => {
12+
consoleSpy.mockReset();
13+
});
14+
415
describe("there is a valid value", () => {
516
it("does not change the `string`", () => {
617
const body = {
@@ -11,6 +22,39 @@ describe("preProcessBody", () => {
1122

1223
expect(result).toEqual({ key: "value" });
1324
});
25+
26+
describe('the key is "slug"', () => {
27+
describe("value is not an empty string", () => {
28+
it('encodes the "slug" value', () => {
29+
const body = {
30+
slug: "codecov/engineering/applications-team/gazebo",
31+
};
32+
33+
const result = preProcessBody(body);
34+
35+
expect(result).toEqual({
36+
slug: "codecov:::engineering:::applications-team::::gazebo",
37+
});
38+
});
39+
});
40+
41+
describe("value is an empty string", () => {
42+
it('throws an "InvalidSlugError"', () => {
43+
const body = {
44+
slug: "",
45+
};
46+
47+
let error;
48+
try {
49+
preProcessBody(body);
50+
} catch (e) {
51+
error = e;
52+
}
53+
54+
expect(error).toBeInstanceOf(InvalidSlugError);
55+
});
56+
});
57+
});
1458
});
1559

1660
describe("there is an empty string", () => {

packages/bundler-plugin-core/src/utils/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ export const SPAWN_PROCESS_BUFFER_SIZE = 1_048_576 * 100; // 100 MiB
22

33
export const DEFAULT_RETRY_COUNT = 3;
44
export const DEFAULT_RETRY_DELAY = 1000;
5+
6+
export const OWNER_SLUG_JOIN = ":::";
7+
export const REPO_SLUG_JOIN = "::::";

packages/bundler-plugin-core/src/utils/getPreSignedURL.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DEFAULT_RETRY_COUNT } from "./constants.ts";
88
import { fetchWithRetry } from "./fetchWithRetry.ts";
99
import { green, red, yellow } from "./logging.ts";
1010
import { preProcessBody } from "./preProcessBody.ts";
11+
import { NoCommitShaError } from "../errors/NoCommitShaError.ts";
1112

1213
interface GetPreSignedURLArgs {
1314
apiURL: string;
@@ -34,10 +35,14 @@ export const getPreSignedURL = async ({
3435
throw new NoUploadTokenError("No upload token found");
3536
}
3637

37-
const commitSha = serviceParams?.commit ?? "";
38-
const url = `${apiURL}/upload/service/commits/${commitSha}/bundle_analysis`;
38+
const commitSha = serviceParams?.commit;
39+
40+
if (!commitSha) {
41+
red("No commit found");
42+
throw new NoCommitShaError("No commit found");
43+
}
3944

40-
const sentServiceParams = preProcessBody({ token, ...serviceParams });
45+
const url = `${apiURL}/upload/service/commits/${commitSha}/bundle_analysis`;
4146

4247
let response: Response;
4348
try {
@@ -51,7 +56,7 @@ export const getPreSignedURL = async ({
5156
"Content-Type": "application/json",
5257
Authorization: `token ${token}`,
5358
},
54-
body: JSON.stringify(sentServiceParams),
59+
body: JSON.stringify(preProcessBody(serviceParams)),
5560
},
5661
});
5762
} catch (e) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import childprocess from "child_process";
22

33
export function isProgramInstalled(programName: string): boolean {
4-
return !childprocess.spawnSync(programName).error;
4+
return !childprocess?.spawnSync(programName)?.error;
55
}

0 commit comments

Comments
 (0)