Skip to content

Commit

Permalink
chore(effect): using Data.TaggedClass across the board (#36)
Browse files Browse the repository at this point in the history
* chore(efefct): using Data.TaggedClass across the board

* fix: test
  • Loading branch information
JJayet authored Mar 1, 2024
1 parent b02c060 commit 0137089
Show file tree
Hide file tree
Showing 25 changed files with 59 additions and 87 deletions.
12 changes: 0 additions & 12 deletions src/errors/base.error.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/errors/commit-messages-extraction.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from './base.error';
import { Data } from 'effect';

export class CommitMessagesExtractionError extends BaseError {
readonly _tag = 'CommitMessagesExtraction';
}
export class CommitMessagesExtractionError extends Data.TaggedError(
'CommitMessagesExtraction',
) {}
8 changes: 4 additions & 4 deletions src/errors/github-actions-exec.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from './base.error';
import { Data } from 'effect';

export class GithubActionsExecError extends BaseError {
readonly _tag = 'GithubActionsExec';
}
export class GithubActionsExecError extends Data.TaggedError(
'GithubActionsExec',
)<{ message: string }> {}
6 changes: 2 additions & 4 deletions src/errors/invalid-keywords.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { BaseError } from './base.error';
import { Data } from 'effect';

export class InvalidKeywordsError extends BaseError {
readonly _tag = 'InvalidKeywords';
}
export class InvalidKeywordsError extends Data.TaggedError('InvalidKeywords') {}
8 changes: 4 additions & 4 deletions src/errors/no-github-event.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from './base.error';
import { Data } from 'effect';

export class NoGithubEventError extends BaseError {
readonly _tag = 'NoGithubEvent';
}
export class NoGithubEventError extends Data.TaggedError('NoGithubEvent')<{
message: string;
}> {}
8 changes: 4 additions & 4 deletions src/errors/no-version-bump-requested.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from './base.error';
import { Data } from 'effect';

export class NoVersionBumpRequestedError extends BaseError {
readonly _tag = 'NoVersionBumpRequested';
}
export class NoVersionBumpRequestedError extends Data.TaggedError(
'NoVersionBumpRequested',
) {}
8 changes: 4 additions & 4 deletions src/errors/not-running-on-default-branch.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from './base.error';
import { Data } from 'effect';

export class NotRunningOnDefaultBranchError extends BaseError {
readonly _tag = 'NotRunningOnDefaultBranch';
}
export class NotRunningOnDefaultBranchError extends Data.TaggedError(
'NotRunningOnDefaultBranch',
) {}
8 changes: 4 additions & 4 deletions src/errors/unknown-current-branch.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from './base.error';
import { Data } from 'effect';

export class UnknownCurrentBranchError extends BaseError {
readonly _tag = 'UnknownCurrentBranch';
}
export class UnknownCurrentBranchError extends Data.TaggedError(
'UnknownCurrentBranch',
) {}
8 changes: 4 additions & 4 deletions src/errors/unknown-default-branch.error.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BaseError } from './base.error';
import { Data } from 'effect';

export class UnknownDefaultBranchError extends BaseError {
readonly _tag = 'UnknownDefaultBranch';
}
export class UnknownDefaultBranchError extends Data.TaggedError(
'UnknownDefaultBranch',
) {}
2 changes: 1 addition & 1 deletion src/github-actions/exec/push-tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { GithubActionsExecError } from '../../errors/github-actions-exec.error';
export const pushTags = Effect.withSpan(__filename)(
Effect.tryPromise({
try: () => exec('git push', ['--tags']),
catch: (e) => new GithubActionsExecError(e),
catch: (e) => new GithubActionsExecError({ message: (e as Error).message }),
}),
);
2 changes: 1 addition & 1 deletion src/github-actions/exec/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { GithubActionsExecError } from '../../errors/github-actions-exec.error';
export const push = Effect.withSpan(__filename)(
Effect.tryPromise({
try: () => exec('git push'),
catch: (e) => new GithubActionsExecError(e),
catch: (e) => new GithubActionsExecError({ message: (e as Error).message }),
}),
);
3 changes: 2 additions & 1 deletion src/github-actions/exec/set-git-user-email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const setGitUserEmail = (email: string) =>
Effect.withSpan(__filename)(
Effect.tryPromise({
try: () => exec('git config', ['--global', 'user.email', email]),
catch: (e) => new GithubActionsExecError(e),
catch: (e) =>
new GithubActionsExecError({ message: (e as Error).message }),
}),
);
3 changes: 2 additions & 1 deletion src/github-actions/exec/set-git-user-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const setGitUserName = (userName: string) =>
Effect.withSpan(__filename)(
Effect.tryPromise({
try: () => exec('git config', ['--global', 'user.name', userName]),
catch: (e) => new GithubActionsExecError(e),
catch: (e) =>
new GithubActionsExecError({ message: (e as Error).message }),
}),
);
3 changes: 2 additions & 1 deletion src/github-actions/exec/set-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const setVersion = (bumpType: BumpType) =>
Effect.withSpan(__filename)(
Effect.tryPromise({
try: () => exec('npm version', [bumpType, '--force']),
catch: (e) => new GithubActionsExecError(e),
catch: (e) =>
new GithubActionsExecError({ message: (e as Error).message }),
}),
);
22 changes: 6 additions & 16 deletions src/github/event/get-github-event-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('getGithubEventData function', () => {
await expect(() =>
Effect.runPromise(getGithubEventData),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"message":"\\"[object Object]\\" is not valid JSON","_tag":"NoGithubEvent"}]`,
`[NoGithubEvent: "[object Object]" is not valid JSON]`,
);
});

Expand All @@ -42,9 +42,7 @@ describe('getGithubEventData function', () => {

await expect(() =>
Effect.runPromise(getGithubEventData),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"_tag":"UnknownDefaultBranch"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[UnknownDefaultBranch]`);
});

it('should send an error message if repository infos are missing', async () => {
Expand All @@ -61,9 +59,7 @@ describe('getGithubEventData function', () => {

await expect(() =>
Effect.runPromise(getGithubEventData),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"_tag":"UnknownDefaultBranch"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[UnknownDefaultBranch]`);
});

it('should send an error message if the current branch cannot be defined', async () => {
Expand All @@ -82,9 +78,7 @@ describe('getGithubEventData function', () => {

await expect(() =>
Effect.runPromise(getGithubEventData),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"_tag":"UnknownCurrentBranch"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[UnknownCurrentBranch]`);
});

it('should send an error message when commit messages are missing', async () => {
Expand All @@ -99,9 +93,7 @@ describe('getGithubEventData function', () => {

await expect(() =>
Effect.runPromise(getGithubEventData),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"_tag":"CommitMessagesExtraction"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[CommitMessagesExtraction]`);
});

it('should return relevant data', async () => {
Expand Down Expand Up @@ -238,8 +230,6 @@ describe('getGithubEventData function', () => {

await expect(() =>
Effect.runPromise(getGithubEventData),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"_tag":"NotRunningOnDefaultBranch"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[NotRunningOnDefaultBranch]`);
});
});
2 changes: 1 addition & 1 deletion src/github/event/read-github-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export const readGithubEvent = Effect.withSpan(__filename)(
}),
) as GithubEvent,
),
Effect.catchAll((e) => Effect.fail(new NoGithubEventError(e))),
Effect.catchAll((e) => new NoGithubEventError({ message: e.message })),
),
);
2 changes: 1 addition & 1 deletion src/github/extract-commits-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export const extractCommitsMessages = (event: GithubEvent) =>
return event.commits.map((el) => el.message);
}

return yield* _(Effect.fail(new CommitMessagesExtractionError()));
return yield* _(new CommitMessagesExtractionError());
}),
);
2 changes: 1 addition & 1 deletion src/github/fail-if-no-default-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const failIfNoDefaultBranch = (event?: GithubEvent) =>
Effect.gen(function* (_) {
const defaultBranch = event?.repository?.default_branch;
if (!defaultBranch || defaultBranch.length === 0) {
yield* _(Effect.fail(new UnknownDefaultBranchError()));
yield* _(new UnknownDefaultBranchError());
}
}),
);
2 changes: 1 addition & 1 deletion src/github/fail-if-not-current-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const failIfNotRunningOnCurrentBranch = (event: GithubEvent) =>

const isDefaultBranch = currentBranch === defaultBranch;
if (!isDefaultBranch) {
yield* _(Effect.fail(new NotRunningOnDefaultBranchError()));
yield* _(new NotRunningOnDefaultBranchError());
}
}),
);
2 changes: 1 addition & 1 deletion src/github/get-current-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export const getCurrentBranch = (event: GithubEvent) =>
return event.workflow_run.head_branch;
}

return yield* _(Effect.fail(new UnknownCurrentBranchError()));
return yield* _(new UnknownCurrentBranchError());
}),
);
6 changes: 2 additions & 4 deletions src/semver/get-bump-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { runPromise } from '../effects/run-promise';
import { getBumpType } from './get-bump-type';

describe('getBumpType function', () => {
it("should return 'none'", async () => {
it('should throw if no bump type match', async () => {
const messages = ['yolo', 'bro', 'cool'];
const keywords = {
major: ['oh,bah'],
Expand All @@ -17,9 +17,7 @@ describe('getBumpType function', () => {

await expect(() =>
Effect.runPromise(getBumpType([messages, keywords])),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"_tag":"NoVersionBumpRequested"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[NoVersionBumpRequested]`);
});

it("should return 'major'", async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/semver/get-bump-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { NoVersionBumpRequestedError } from '../errors/no-version-bump-requested

import { Keywords } from './get-keywords';

export type BumpType = 'major' | 'minor' | 'patch' | 'none';
export type BumpType = 'major' | 'minor' | 'patch';

const isBumpRequestedFor = (keywords: string[], messages: string[]) =>
messages.some((mes) => keywords.some((key) => mes.startsWith(key)));

export const getBumpType = ([messages, keywords]: [
messages: string[],
keywords: Keywords,
]): Effect.Effect<BumpType, NoVersionBumpRequestedError> =>
]) =>
Effect.withSpan(__filename)(
Effect.gen(function* (_) {
const isMajorBump = isBumpRequestedFor(keywords.major, messages);
Expand All @@ -34,6 +34,6 @@ export const getBumpType = ([messages, keywords]: [
return 'patch';
}

return yield* _(Effect.fail(new NoVersionBumpRequestedError()));
return yield* _(new NoVersionBumpRequestedError());
}),
);
4 changes: 1 addition & 3 deletions src/semver/get-keywords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ describe('getKeywords function', () => {

await expect(() =>
Effect.runPromise(getKeywords),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"_tag":"InvalidKeywords"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[InvalidKeywords]`);

expect(error).toHaveBeenCalledTimes(3);
expect(error).toHaveBeenNthCalledWith(
Expand Down
3 changes: 2 additions & 1 deletion src/semver/get-keywords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ export const getKeywords = Effect.withSpan(__filename)(
const areKeywordsInvalid = shouldDefaultToPatch
? keywords.slice(0, -1).some((el) => isEmpty(el))
: keywords.some((el) => isEmpty(el));

if (areKeywordsInvalid) {
return yield* _(Effect.fail(new InvalidKeywordsError()));
return yield* _(new InvalidKeywordsError());
}

return {
Expand Down
8 changes: 2 additions & 6 deletions src/workflow/action-workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ describe('actionWorkflow function', () => {

await expect(() =>
Effect.runPromise(actionWorkflow),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"message":"${errorMessage}","_tag":"NoGithubEvent"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[NoGithubEvent: Oh no]`);

expect(setFailed).toHaveBeenCalledTimes(1);
expect(setFailed).toHaveBeenCalledWith(
Expand Down Expand Up @@ -92,9 +90,7 @@ describe('actionWorkflow function', () => {

await expect(() =>
Effect.runPromise(actionWorkflow),
).rejects.toThrowErrorMatchingInlineSnapshot(
`[Error: {"_tag":"InvalidKeywords"}]`,
);
).rejects.toThrowErrorMatchingInlineSnapshot(`[InvalidKeywords]`);

expect(setFailed).toHaveBeenCalledTimes(1);
expect(setFailed).toHaveBeenCalledWith('❌ Invalid keywords provided.');
Expand Down

0 comments on commit 0137089

Please sign in to comment.