Skip to content

Commit

Permalink
thjs-63: * danger and commitlint configs into typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
v0ldemar01 committed Dec 14, 2023
1 parent 2864278 commit 3e60daf
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 26 deletions.
10 changes: 3 additions & 7 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,9 @@ rules:

overrides:
- files:
- project.config.cjs
- commitlint.config.cjs
- dangerfile.cjs
parserOptions:
sourceType: commonjs
env:
node: true
- project.config.ts
- commitlint.config.ts
- dangerfile.ts
rules:
'import/no-default-export':
- off
2 changes: 1 addition & 1 deletion .github/workflows/ci-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Lint Commits
run: |
npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose -g ./commitlint.config.cjs
npx commitlint --from ${{ github.event.pull_request.base.sha }} --to ${{ github.event.pull_request.head.sha }} --verbose -g ./commitlint.config.ts
- name: Add Folder Labels
uses: actions/labeler@v4
Expand Down
8 changes: 5 additions & 3 deletions commitlint.config.cjs → commitlint.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { ProjectPrefix } = require('./project.config.cjs');
import { UserConfig } from '@commitlint/types';

import { ProjectPrefix } from './project.config';

const COMMIT_MODIFIERS = ['+', '*', '-'];
const COMMIT_MESSAGE_REGEXP = new RegExp(
Expand All @@ -19,7 +21,7 @@ Examples:
- ${ProjectPrefix.APP}-12: * docker homework
- production: - comments in ui/ux homework`;

const configuration = {
const configuration: UserConfig = {
parserPreset: {
parserOpts: {
headerPattern: COMMIT_MESSAGE_REGEXP,
Expand All @@ -45,4 +47,4 @@ const configuration = {
}
};

module.exports = configuration;
export default configuration;
38 changes: 28 additions & 10 deletions dangerfile.cjs → dangerfile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
const { danger, fail } = require('danger');

const { ProjectPrefix } = require('./project.config.cjs');
import {
danger,
fail,
type GitHubPRDSL as LibraryGitHubDSL,
GitHubMergeRef,
GitHubRepo,
GitHubDSL
} from 'danger';

import { ProjectPrefix } from './project.config';

type GitHubPRDSL = LibraryGitHubDSL & {
head: GitHubMergeRef & {
repo: GitHubRepo & {
has_projects: boolean;
};
};
milestone: Record<string, unknown> | null;
labels: unknown[];
project_id: string | null;
};

const BranchPrefix = {
TASK: 'task',
FIX: 'fix'
};
} as const;

const DangerConfig = {
TITLE: {
Expand Down Expand Up @@ -34,17 +52,17 @@ const DangerConfig = {
}
};

const pr = danger.github.pr;
const { pr } = danger.github as GitHubDSL & Record<'pr', GitHubPRDSL>;

const checkAssignees = () => {
const checkAssignees = (): void => {
const hasAssignees = Boolean(pr.assignee);

if (!hasAssignees) {
fail('This pull request should have at least one assignee.');
}
};

const checkTitle = titlePattern => {
const checkTitle = (titlePattern: RegExp): void => {
const isTitleValid = titlePattern.test(pr.title);

if (!isTitleValid) {
Expand All @@ -56,15 +74,15 @@ const checkTitle = titlePattern => {
}
};

const checkLabels = () => {
const checkLabels = (): void => {
const hasLabels = pr.labels.length > 0;

if (!hasLabels) {
fail('This pull request should have at least one label.');
}
};

const checkBranch = branchPattern => {
const checkBranch = (branchPattern: RegExp): void => {
const isBranchValid = branchPattern.test(pr.head.ref);

if (!isBranchValid) {
Expand All @@ -76,7 +94,7 @@ const checkBranch = branchPattern => {
}
};

const applyDanger = () => {
const applyDanger = (): void => {
if (DangerConfig.TITLE.IS_REQUIRED) {
checkTitle(DangerConfig.TITLE.PATTERN);
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"node": "18.x.x",
"npm": "9.x.x"
},
"type": "module",
"workspaces": [
"client",
"server",
Expand Down Expand Up @@ -48,6 +47,6 @@
},
"simple-git-hooks": {
"pre-commit": "npx lint-staged",
"commit-msg": "npx commitlint --edit $1 -c -g ./commitlint.config.cjs"
"commit-msg": "npx commitlint --edit $1"
}
}
4 changes: 2 additions & 2 deletions project.config.cjs → project.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const ProjectPrefix = {
APP: 'thjs',
ENVIRONMENTS: ['development', 'production']
};
} as const;

module.exports = { ProjectPrefix };
export { ProjectPrefix };
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"exclude": ["commitlint.config.ts"],
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
Expand Down

0 comments on commit 3e60daf

Please sign in to comment.