Skip to content

Commit 1f00454

Browse files
authored
feat: Add in Provider Utils to Grab Env Vars (#9)
Add in some util functions that grab provider env values that will be sent in the post request, alongside tests for the first time.
1 parent 8588ef3 commit 1f00454

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+8050
-56
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"eslint-plugin-isaacscript": "^3.5.6",
2828
"eslint-plugin-prettier": "^5.0.1",
2929
"prettier": "^3.0.3",
30-
"typescript": "^5.2.2"
30+
"typescript": "^5.3.2"
3131
},
3232
"volta": {
3333
"node": "20.9.0"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { type Config } from "jest";
2+
3+
// eslint-disable-next-line @typescript-eslint/no-var-requires
4+
const packageJson = require("./package.json") as { version: string };
5+
6+
const config: Config = {
7+
testEnvironment: "node",
8+
collectCoverageFrom: [
9+
"src/**/*.ts",
10+
"src/**/*.tsx",
11+
"!**/node_modules/**",
12+
"!test/helpers.ts",
13+
],
14+
moduleNameMapper: {
15+
"^@/(.*)$": "<rootDir>/src/$1",
16+
"^@test-utils/(.*)$": "<rootDir>/test-utils/$1",
17+
},
18+
transform: {
19+
"^.+\\.(t|j)sx?$": [
20+
"@swc/jest",
21+
{
22+
jsc: {
23+
transform: {
24+
optimizer: {
25+
globals: {
26+
vars: {
27+
__PACKAGE_VERSION__: packageJson.version,
28+
},
29+
},
30+
},
31+
},
32+
},
33+
},
34+
],
35+
},
36+
};
37+
38+
export default config;

packages/bundler-plugin-core/package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,30 @@
2929
"lint": "eslint . --ext .ts,.tsx",
3030
"lint:fix": "pnpm lint --fix",
3131
"format": "prettier '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-path ../.gitignore --ignore-unknown --no-error-on-unmatched-pattern --write",
32-
"format:check": "prettier '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-path ../.gitignore --ignore-unknown --no-error-on-unmatched-pattern --check"
32+
"format:check": "prettier '**/*.{cjs,mjs,ts,tsx,md,json}' --ignore-path ../.gitignore --ignore-unknown --no-error-on-unmatched-pattern --check",
33+
"test:unit": "jest",
34+
"test:unit:ci": "jest --coverage"
3335
},
3436
"keywords": [],
3537
"author": "",
3638
"license": "Apache-2.0",
3739
"dependencies": {
3840
"chalk": "4.1.2",
3941
"semver": "^7.5.4",
42+
"testdouble": "^3.20.1",
43+
"testdouble-jest": "^2.0.0",
4044
"unplugin": "^1.5.0",
4145
"zod": "^3.22.4"
4246
},
4347
"devDependencies": {
48+
"@swc/core": "^1.3.99",
49+
"@swc/jest": "^0.2.29",
50+
"@types/jest": "^29.5.10",
4451
"@types/node": "^20.8.6",
4552
"@types/semver": "^7.5.5",
53+
"jest": "^29.7.0",
54+
"msw": "^2.0.8",
55+
"ts-node": "^10.9.1",
4656
"typescript": "^5.0.4",
4757
"unbuild": "^2.0.0"
4858
},

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
type Module,
1010
type Options,
1111
type Output,
12-
} from "./types.js";
12+
} from "./types.ts";
1313

14-
import { jsonSchema } from "./schemas.js";
15-
import { red } from "./utils/logging.js";
14+
import { jsonSchema } from "./schemas.ts";
15+
import { red } from "./utils/logging.ts";
1616

1717
const NODE_VERSION_RANGE = ">=18.18.0";
1818

@@ -49,6 +49,7 @@ export function codecovUnpluginFactory({
4949
bundleAnalysisUploadPlugin({
5050
output,
5151
statsFileName,
52+
uploaderOverrides: userOptions?.uploaderOverrides,
5253
});
5354

5455
plugins.push({

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ export interface Output {
4747
export interface BundleAnalysisUploadPluginArgs {
4848
output: Output;
4949
statsFileName?: string;
50+
uploaderOverrides?: UploadOverrides;
5051
}
5152

52-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
5353
export interface Options {
5454
statsFileName?: string;
5555
enableBundleAnalysis?: boolean;
56+
uploaderOverrides?: UploadOverrides;
5657
}
5758

5859
export type BundleAnalysisUploadPlugin = (
@@ -61,3 +62,54 @@ export type BundleAnalysisUploadPlugin = (
6162
pluginVersion: string;
6263
version: string;
6364
};
65+
66+
export interface UploadOverrides {
67+
/** Specify the branch manually. */
68+
branch?: string;
69+
/** Specify the build number manually. */
70+
build?: string;
71+
/** The commit SHA of the parent for which you are uploading coverage. */
72+
parent?: string;
73+
/** Specify the pull request number manually. */
74+
pr?: string;
75+
/** Specify the commit SHA manually. */
76+
sha?: string;
77+
/** Specify the slug manually. */
78+
slug?: string;
79+
/** Specify the tag manually. */
80+
tag?: string;
81+
/** Change the upload host (Enterprise use). */
82+
url?: string;
83+
}
84+
85+
export type ProviderEnvs = NodeJS.Dict<string>;
86+
87+
export interface ProviderUtilInputs {
88+
envs: ProviderEnvs;
89+
args: Options["uploaderOverrides"];
90+
}
91+
92+
export interface ProviderUtil {
93+
detect: (arg0: ProviderEnvs) => boolean;
94+
getServiceName: () => string;
95+
getServiceParams: (
96+
arg0: ProviderUtilInputs,
97+
) => Promise<ProviderServiceParams>;
98+
getEnvVarNames: () => string[];
99+
}
100+
101+
export interface ProviderServiceParams {
102+
branch: string;
103+
build: string;
104+
buildURL: string;
105+
commit: string;
106+
job: string;
107+
pr: string;
108+
service: string;
109+
slug: string;
110+
name?: string;
111+
tag?: string;
112+
parent?: string;
113+
project?: string;
114+
server_uri?: string;
115+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const SPAWN_PROCESS_BUFFER_SIZE = 1_048_576 * 100; // 100 MiB
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { runExternalProgram } from "./runExternalProgram.ts";
2+
3+
export function parseSlug(slug: unknown): string {
4+
// origin https://github.com/torvalds/linux.git (fetch)
5+
// git@github.com: codecov / uploader.git
6+
if (typeof slug !== "string") {
7+
return "";
8+
}
9+
10+
if (slug.match(/^(ssh|https?):/)) {
11+
// Type is http(s) or ssh
12+
const phaseOne = slug.split("//")[1]?.replace(".git", "") ?? "";
13+
const phaseTwo = phaseOne?.split("/") ?? "";
14+
const cleanSlug =
15+
phaseTwo.length > 2 ? `${phaseTwo[1]}/${phaseTwo[2]}` : "";
16+
return cleanSlug;
17+
}
18+
19+
if (slug.match("@")) {
20+
// Type is git
21+
const cleanSlug = slug.split(":")[1]?.replace(".git", "");
22+
return cleanSlug ?? "";
23+
}
24+
25+
throw new Error(`Unable to parse slug URL: ${slug}`);
26+
}
27+
28+
export function parseSlugFromRemoteAddr(remoteAddr?: string): string {
29+
const addr =
30+
remoteAddr ??
31+
runExternalProgram("git", ["config", "--get", "remote.origin.url"]) ??
32+
"";
33+
if (!addr) {
34+
return "";
35+
}
36+
37+
const slug = parseSlug(addr);
38+
if (slug === "/") {
39+
return "";
40+
}
41+
return slug;
42+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import childprocess from "child_process";
2+
3+
export function isProgramInstalled(programName: string): boolean {
4+
return !childprocess.spawnSync(programName).error;
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function setSlug(
2+
slugArg: string | undefined,
3+
orgEnv: string | undefined,
4+
repoEnv: string | undefined,
5+
) {
6+
if (typeof slugArg !== "undefined" && slugArg !== "") {
7+
return slugArg;
8+
}
9+
10+
if (
11+
typeof orgEnv !== "undefined" &&
12+
typeof repoEnv !== "undefined" &&
13+
orgEnv !== "" &&
14+
repoEnv !== ""
15+
) {
16+
return `${orgEnv}/${repoEnv}`;
17+
}
18+
19+
return "";
20+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import {
2+
type ProviderEnvs,
3+
type ProviderServiceParams,
4+
type ProviderUtilInputs,
5+
} from "@/types.ts";
6+
7+
export function detect(envs: ProviderEnvs): boolean {
8+
return (
9+
(envs?.CI === "true" || envs?.CI === "True") &&
10+
(envs?.APPVEYOR === "true" || envs?.APPVEYOR === "True")
11+
);
12+
}
13+
14+
function _getBuild(inputs: ProviderUtilInputs) {
15+
const { args, envs } = inputs;
16+
return args?.build ?? envs?.APPVEYOR_JOB_ID ?? "";
17+
}
18+
19+
function _getBuildURL(inputs: ProviderUtilInputs) {
20+
const { envs } = inputs;
21+
if (
22+
envs?.APPVEYOR_URL &&
23+
envs?.APPVEYOR_REPO_NAME &&
24+
envs?.APPVEYOR_BUILD_ID &&
25+
envs?.APPVEYOR_JOB_ID
26+
) {
27+
return `${envs?.APPVEYOR_URL}/project/${envs?.APPVEYOR_REPO_NAME}/builds/${envs?.APPVEYOR_BUILD_ID}/job/${envs?.APPVEYOR_JOB_ID}`;
28+
}
29+
return "";
30+
}
31+
32+
function _getBranch(inputs: ProviderUtilInputs) {
33+
const { args, envs } = inputs;
34+
if (args?.branch && args?.branch !== "") {
35+
return args?.branch;
36+
}
37+
38+
return envs?.APPVEYOR_REPO_BRANCH ?? "";
39+
}
40+
41+
function _getJob(envs: ProviderEnvs) {
42+
if (
43+
envs?.APPVEYOR_ACCOUNT_NAME &&
44+
envs?.APPVEYOR_PROJECT_SLUG &&
45+
envs?.APPVEYOR_BUILD_VERSION
46+
) {
47+
return `${envs?.APPVEYOR_ACCOUNT_NAME}/${envs?.APPVEYOR_PROJECT_SLUG}/${envs?.APPVEYOR_BUILD_VERSION}`;
48+
}
49+
return "";
50+
}
51+
52+
function _getPR(inputs: ProviderUtilInputs): string {
53+
const { args, envs } = inputs;
54+
return args?.pr ?? envs?.APPVEYOR_PULL_REQUEST_NUMBER ?? "";
55+
}
56+
57+
function _getService() {
58+
return "appveyor";
59+
}
60+
61+
export function getServiceName(): string {
62+
return "Appveyor CI";
63+
}
64+
65+
function _getSHA(inputs: ProviderUtilInputs) {
66+
const { args, envs } = inputs;
67+
return (
68+
args?.sha ??
69+
envs?.APPVEYOR_PULL_REQUEST_HEAD_COMMIT ??
70+
envs?.APPVEYOR_REPO_COMMIT ??
71+
""
72+
);
73+
}
74+
75+
function _getSlug(inputs: ProviderUtilInputs) {
76+
const { args, envs } = inputs;
77+
return args?.slug ?? envs?.APPVEYOR_REPO_NAME ?? "";
78+
}
79+
80+
// eslint-disable-next-line @typescript-eslint/require-await
81+
export async function getServiceParams(
82+
inputs: ProviderUtilInputs,
83+
): Promise<ProviderServiceParams> {
84+
return {
85+
branch: _getBranch(inputs),
86+
build: _getBuild(inputs),
87+
buildURL: _getBuildURL(inputs),
88+
commit: _getSHA(inputs),
89+
job: _getJob(inputs.envs),
90+
pr: _getPR(inputs),
91+
service: _getService(),
92+
slug: _getSlug(inputs),
93+
};
94+
}
95+
96+
export function getEnvVarNames(): string[] {
97+
return [
98+
"APPVEYOR",
99+
"APPVEYOR_ACCOUNT_NAME",
100+
"APPVEYOR_BUILD_ID",
101+
"APPVEYOR_BUILD_VERSION",
102+
"APPVEYOR_JOB_ID",
103+
"APPVEYOR_PROJECT_SLUG",
104+
"APPVEYOR_PULL_REQUEST_HEAD_COMMIT",
105+
"APPVEYOR_PULL_REQUEST_NUMBER",
106+
"APPVEYOR_REPO_BRANCH",
107+
"APPVEYOR_REPO_COMMIT",
108+
"APPVEYOR_REPO_NAME",
109+
"APPVEYOR_URL",
110+
"CI",
111+
];
112+
}

0 commit comments

Comments
 (0)