Skip to content

Commit 2db57cc

Browse files
authored
feat: Enable true tokenless for bundler plugins (#189)
1 parent b17f870 commit 2db57cc

File tree

4 files changed

+28
-60
lines changed

4 files changed

+28
-60
lines changed

.changeset/big-news-bake.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@codecov/bundler-plugin-core": minor
3+
"@codecov/bundle-analyzer": minor
4+
"@codecov/nextjs-webpack-plugin": minor
5+
"@codecov/nuxt-plugin": minor
6+
"@codecov/remix-vite-plugin": minor
7+
"@codecov/rollup-plugin": minor
8+
"@codecov/solidstart-plugin": minor
9+
"@codecov/sveltekit-plugin": minor
10+
"@codecov/vite-plugin": minor
11+
"@codecov/webpack-plugin": minor
12+
---
13+
14+
Remove the org branch requirement for tokenless uploads

packages/bundler-plugin-core/src/errors/NoUploadTokenError.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

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

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import childProcess from "child_process";
1616
import { SPAWN_PROCESS_BUFFER_SIZE } from "../constants.ts";
1717
import { getPreSignedURL } from "../getPreSignedURL.ts";
1818
import { FailedFetchError } from "../../errors/FailedFetchError.ts";
19-
import { NoUploadTokenError } from "../../errors/NoUploadTokenError.ts";
2019
import { UploadLimitReachedError } from "../../errors/UploadLimitReachedError.ts";
2120
import { UndefinedGitServiceError } from "../../errors/UndefinedGitServiceError.ts";
2221
import { BadOIDCServiceError } from "src/errors/BadOIDCServiceError.ts";
@@ -160,7 +159,7 @@ describe("getPreSignedURL", () => {
160159
retryCount: 0,
161160
serviceParams: {
162161
commit: "123",
163-
branch: "owner:branch",
162+
branch: "any-branch-format",
164163
},
165164
});
166165

@@ -185,7 +184,7 @@ describe("getPreSignedURL", () => {
185184
gitService: "github_enterprise",
186185
serviceParams: {
187186
commit: "123",
188-
branch: "owner:branch",
187+
branch: "any-branch-format",
189188
},
190189
});
191190

@@ -228,37 +227,6 @@ describe("getPreSignedURL", () => {
228227

229228
describe("unsuccessful request", () => {
230229
describe("no upload token present", () => {
231-
describe("branch is in incorrect format", () => {
232-
it("throws an error", async () => {
233-
const { consoleSpy } = setup({
234-
data: { url: "http://example.com" },
235-
});
236-
237-
let error;
238-
try {
239-
await getPreSignedURL({
240-
apiUrl: "http://localhost",
241-
retryCount: 0,
242-
gitService: "github",
243-
serviceParams: {
244-
commit: "123",
245-
branch: "main",
246-
},
247-
});
248-
} catch (e) {
249-
error = e;
250-
}
251-
252-
expect(consoleSpy).toHaveBeenCalled();
253-
// for some reason, this test fails even tho it's the same values
254-
// Expected: "No upload token found"
255-
// Received: "No upload token found"
256-
// Number of calls: 1
257-
// expect(consoleSpy).toHaveBeenCalledWith("No upload token found");
258-
expect(error).toBeInstanceOf(NoUploadTokenError);
259-
});
260-
});
261-
262230
describe("git service is not found", () => {
263231
beforeEach(() => {
264232
td.when(

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { type ProviderServiceParams } from "../types.ts";
66
import { fetchWithRetry } from "./fetchWithRetry.ts";
77
import { green, red } from "./logging.ts";
88
import { preProcessBody } from "./preProcessBody.ts";
9-
import { NoUploadTokenError } from "../errors/NoUploadTokenError.ts";
109
import { findGitService } from "./findGitService.ts";
1110
import { UndefinedGitServiceError } from "../errors/UndefinedGitServiceError.ts";
1211
import { FailedOIDCFetchError } from "../errors/FailedOIDCFetchError.ts";
@@ -45,24 +44,7 @@ export const getPreSignedURL = async ({
4544
});
4645

4746
const requestBody: RequestBody = serviceParams;
48-
/**
49-
* We currently require the branch to be in the format `owner:branch` to identify that it is a
50-
* proper tokenless upload.
51-
* See: https://github.com/codecov/codecov-api/pull/741
52-
*/
53-
if (!uploadToken && serviceParams.branch?.includes(":")) {
54-
if (gitService) {
55-
requestBody.git_service = gitService;
56-
} else {
57-
const foundGitService = findGitService();
58-
if (!foundGitService || foundGitService === "") {
59-
red("Failed to find git service for tokenless upload");
60-
throw new UndefinedGitServiceError("No upload token provided");
61-
}
62-
63-
requestBody.git_service = foundGitService;
64-
}
65-
} else if (oidc?.useGitHubOIDC && Core) {
47+
if (oidc?.useGitHubOIDC && Core) {
6648
if (serviceParams?.service !== "github-actions") {
6749
red("OIDC is only supported for GitHub Actions");
6850
throw new BadOIDCServiceError(
@@ -89,8 +71,17 @@ export const getPreSignedURL = async ({
8971
} else if (uploadToken) {
9072
headers.set("Authorization", `token ${uploadToken}`);
9173
} else {
92-
red("No upload token provided");
93-
throw new NoUploadTokenError("No upload token provided");
74+
if (gitService) {
75+
requestBody.git_service = gitService;
76+
} else {
77+
const foundGitService = findGitService();
78+
if (!foundGitService || foundGitService === "") {
79+
red("Failed to find git service for tokenless upload");
80+
throw new UndefinedGitServiceError("No upload token provided");
81+
}
82+
83+
requestBody.git_service = foundGitService;
84+
}
9485
}
9586

9687
let response: Response;

0 commit comments

Comments
 (0)