Skip to content

Commit

Permalink
Merge pull request #76 from LironEr/feat/sub-project
Browse files Browse the repository at this point in the history
feat: sub project
  • Loading branch information
LironEr authored Dec 17, 2021
2 parents ff79dc5 + 4376793 commit bafa5fc
Show file tree
Hide file tree
Showing 31 changed files with 353 additions and 126 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ Add `bundlemon` property to your `package.json`

BundleMon config can be placed in other places like: `.bundlemonrc`, `.bundlemonrc.json`, `bundlemon.config.js` exporting a JS object, more forms can be found [here](https://github.com/davidtheclark/cosmiconfig)

| Name | Description | Type | Default |
| ------------------ | -------------------------------------------------------------------------------------------------------------------- | ---------------------------------- | --------------- |
| baseDir | Relative/absolute path to the directory | `string` | `process.cwd()` |
| files | [Files config](./docs/types.md#File) | `FileConfig[]` **required** | - |
| groups | Sum all file sizes matching the pattern, rules applies to the sum of the files [Groups config](./docs/types.md#File) | `FileConfig[]` | - |
| defaultCompression | Use compression before calculating file size | `"none"` \| `"gzip"` \| `"brotli"` | `"gzip"` |
| reportOutput | [Output options](./docs/output.md) | `(string \| [string, object])[]` | [] |
| verbose | Print more details | `boolean` | `false` |
| Name | Description | Type | Default |
| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------- | --------------- |
| baseDir | Relative/absolute path to the directory | `string` | `process.cwd()` |
| files | [Files config](./docs/types.md#File) | `FileConfig[]` **required** | - |
| groups | Sum all file sizes matching the pattern, rules applies to the sum of the files [Groups config](./docs/types.md#File) | `FileConfig[]` | - |
| defaultCompression | Use compression before calculating file size | `"none"` \| `"gzip"` \| `"brotli"` | `"gzip"` |
| reportOutput | [Output options](./docs/output.md) | `(string \| [string, object])[]` | [] |
| verbose | Print more details | `boolean` | `false` |
| subProject | By setting sub project you can use the same project id for multiple projects. This can be useful for monorepos. Can be set/overwrite with `BUNDLEMON_SUB_PROJECT` env var | `string` | `undefined` |

## CLI usage

Expand Down
2 changes: 2 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ services:
env_file:
- ./service/dev.env
- ./service/.env
environment:
- MONGO_URL=mongodb://mongo:27017
depends_on:
- mongo
ports:
Expand Down
2 changes: 1 addition & 1 deletion packages/bundlemon-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bundlemon-utils",
"version": "0.3.2",
"version": "0.3.3",
"description": "",
"keywords": [],
"author": "Liron Er",
Expand Down
35 changes: 35 additions & 0 deletions packages/bundlemon-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,35 @@ export type FileStatusObject =
export type FileDetailsDiff = FileDetails & { diff: DiffFromBase } & FileStatusObject;

export interface CommitRecordPayload {
/**
* @minLength 1
* @maxLength 100
* @pattern ^[A-Za-z0-9_\-. ]*$
*/
subProject?: string;
files: FileDetails[];
groups: FileDetails[];
/**
* @minLength 1
* @maxLength 100
*/
branch: string;
/**
* @minLength 1
* @maxLength 100
* @pattern ^[A-Za-z0-9]*$
*/
commitSha: string;
/**
* @minLength 1
* @maxLength 100
*/
baseBranch?: string;
/**
* @minLength 1
* @maxLength 10
* @pattern ^[0-9]*$
*/
prNumber?: string;
}

Expand Down Expand Up @@ -57,6 +81,7 @@ export interface CreateCommitRecordResponse extends BaseCommitRecordResponse {
}

export interface ReportMetadata {
subProject?: string;
linkToReport?: string;
record?: CommitRecord;
baseRecord?: CommitRecord;
Expand All @@ -82,3 +107,13 @@ export interface CreateProjectResponse {
projectId: string;
apiKey: string;
}

export type OutputResult = 'success' | 'failure' | 'skipped';

export interface OutputResponse {
result: OutputResult;
message: string;
metadata?: Record<string, unknown>;
}
export type GithubOutputTypes = 'checkRun' | 'commitStatus' | 'prComment';
export type GithubOutputResponse = Partial<Record<GithubOutputTypes, OutputResponse>>;
38 changes: 0 additions & 38 deletions packages/bundlemon-utils/src/v1/types.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/bundlemon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"dependencies": {
"axios": "^0.21.1",
"brotli-size": "^4.0.0",
"bundlemon-utils": "^0.3.2",
"bundlemon-utils": "^0.3.3",
"bytes": "^3.1.0",
"chalk": "^4.1.1",
"commander": "^8.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
"projectId": "BUNDLEMON_PROJECT_ID",
"remoteFlag": "BUNDLEMON_REMOTE",
"serviceURL": "BUNDLEMON_SERVICE_URL",
"subProject": "BUNDLEMON_SUB_PROJECT",
},
"serviceUrl": "https://api.bundlemon.dev",
}
Expand Down
1 change: 1 addition & 0 deletions packages/bundlemon/src/common/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum EnvVar {
projectId = 'BUNDLEMON_PROJECT_ID',
projectApiKey = 'BUNDLEMON_PROJECT_APIKEY',
serviceURL = 'BUNDLEMON_SERVICE_URL',
subProject = 'BUNDLEMON_SUB_PROJECT',
}

export const serviceUrl = process.env[EnvVar.serviceURL] || 'https://api.bundlemon.dev';
Expand Down
2 changes: 1 addition & 1 deletion packages/bundlemon/src/main/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export async function initializer(config: Config): Promise<NormalizedConfig | un
try {
await initOutputs(normalizedConfig);
} catch (err) {
logger.error(err.message);
logger.error((err as Error).message);
return undefined;
}

Expand Down
22 changes: 4 additions & 18 deletions packages/bundlemon/src/main/outputs/outputs/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import { AxiosError } from 'axios';
import { owner, repo } from '../../utils/ci';
import { createLogger } from '../../../common/logger';
import { validateYup } from '../../utils/validationUtils';
import type { Output } from '../types';
import { serviceClient } from '../../../common/service';

import type { GithubOutputResponse, GithubOutputTypes } from 'bundlemon-utils';
import type { Output } from '../types';

const NAME = 'github';

const logger = createLogger(`${NAME} output`);

interface GithubOutputOptions {
checkRun: boolean;
commitStatus: boolean;
prComment: boolean;
}
type GithubOutputOptions = Record<GithubOutputTypes, boolean>;

function validateOptions(options: unknown): GithubOutputOptions | undefined {
const schema: yup.SchemaOf<GithubOutputOptions, GithubOutputOptions> = yup
Expand Down Expand Up @@ -121,15 +119,3 @@ const output: Output = {
};

export default output;

// TODO: move to utils package

type OutputResult = 'success' | 'failure' | 'skipped';

interface OutputResponse {
result: OutputResult;
message: string;
metadata?: Record<string, unknown>;
}
type GithubOutputTypes = 'checkRun' | 'commitStatus' | 'prComment';
type GithubOutputResponse = Partial<Record<GithubOutputTypes, OutputResponse>>;
4 changes: 3 additions & 1 deletion packages/bundlemon/src/main/report/generateReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { NormalizedConfig } from '../types';
export async function generateReport(config: NormalizedConfig, input: DiffReportInput): Promise<Report | undefined> {
logger.info('Start generating report');

const subProject = config.subProject;
let record: CommitRecord | undefined;
let baseRecord: CommitRecord | undefined;
let linkToReport: string | undefined;
Expand All @@ -21,6 +22,7 @@ export async function generateReport(config: NormalizedConfig, input: DiffReport
const result = await createCommitRecord(
config.projectId,
{
subProject,
...gitVars,
...input,
},
Expand All @@ -46,6 +48,6 @@ export async function generateReport(config: NormalizedConfig, input: DiffReport

return {
...diffReport,
metadata: { linkToReport, record, baseRecord },
metadata: { subProject, linkToReport, record, baseRecord },
};
}
4 changes: 3 additions & 1 deletion packages/bundlemon/src/main/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface NormalizedFileConfig extends Omit<FileConfig, 'maxSize' | 'comp
}

export interface Config {
subProject?: string;
baseDir?: string;
files: FileConfig[];
groups?: FileConfig[];
Expand All @@ -21,7 +22,8 @@ export interface Config {
reportOutput?: (string | [string, unknown])[];
}

export interface BaseNormalizedConfig extends Omit<Required<Config>, 'files' | 'groups'> {
export interface BaseNormalizedConfig extends Omit<Required<Config>, 'files' | 'groups' | 'subProject'> {
subProject?: string;
files: NormalizedFileConfig[];
groups: NormalizedFileConfig[];
remote: boolean;
Expand Down
13 changes: 13 additions & 0 deletions packages/bundlemon/src/main/utils/configUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ function getConfigSchema() {
.object()
.required()
.shape({
subProject: yup
.string()
.optional()
.min(1, "subProject cant be an empty string, set undefined or dont set it if you don't need it")
.max(100)
.matches(/^[A-Za-z0-9_\-. ]*$/),
baseDir: yup.string().optional(),
verbose: yup.boolean().optional(),
defaultCompression: yup.mixed<Compression>().optional().oneOf(Object.values(Compression)),
Expand All @@ -74,6 +80,7 @@ export function validateConfig(config: Config): NormalizedConfig | undefined {
}

const {
subProject,
baseDir = process.cwd(),
files,
groups = [],
Expand All @@ -86,6 +93,7 @@ export function validateConfig(config: Config): NormalizedConfig | undefined {
const isRemote = ciVars.ci && process.env[EnvVar.remoteFlag] !== 'false';

const baseNormalizedConfig: Omit<BaseNormalizedConfig, 'remote'> = {
subProject,
baseDir: path.resolve(baseDir),
verbose: false,
defaultCompression,
Expand All @@ -95,6 +103,11 @@ export function validateConfig(config: Config): NormalizedConfig | undefined {
...restConfig,
};

if (process.env[EnvVar.subProject]) {
logger.debug('overwrite sub project from env var');
baseNormalizedConfig.subProject = process.env[EnvVar.subProject];
}

if (!isRemote) {
return { ...baseNormalizedConfig, remote: false };
}
Expand Down
2 changes: 1 addition & 1 deletion service/docs/self-hosted.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ MONGO_DB_PASSWORD=password
### Create indexes

```js
db.commitRecords.createIndex({ projectId: 1, branch: 1, creationDate: -1 });
db.commitRecords.createIndex({ projectId: 1, subProject: 1, branch: 1, creationDate: -1 });

// TTL index - remove commit records on PRs after 30 days
db.commitRecords.createIndex(
Expand Down
2 changes: 1 addition & 1 deletion service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@octokit/auth-app": "^3.4.0",
"@octokit/rest": "^18.5.3",
"aws-lambda-fastify": "^1.4.4",
"bundlemon-utils": "^0.3.2",
"bundlemon-utils": "^0.3.3",
"bytes": "^3.1.0",
"env-var": "^7.0.1",
"fastify": "^3.19.1",
Expand Down
Loading

0 comments on commit bafa5fc

Please sign in to comment.