Skip to content

Commit

Permalink
Merge pull request #43 from tak-bro/feature/remove-workflow
Browse files Browse the repository at this point in the history
Feature/remove workflow
  • Loading branch information
tak-bro committed Jun 3, 2024
2 parents b81d96e + 4bf6e3e commit cd44451
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 50 deletions.
15 changes: 0 additions & 15 deletions .github/workflows/no-response-issues.yml

This file was deleted.

10 changes: 5 additions & 5 deletions .github/workflows/stale_issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
stale-issue-message: This issue has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the issue from automatically closing.
stale-pr-message: This PR has not received a response in 1 week. If you still think there is a problem, please leave a comment to avoid the PR from automatically closing.
# These labels are required
stale-issue-label: closing-soon
exempt-issue-label: no-autoclose
stale-pr-label: closing-soon
stale-issue-label: stale
exempt-issue-label: 'pinned, security, no-autoclose'
stale-pr-label: stale
exempt-pr-label: pr/needs-review
response-requested-label: response-requested

Expand All @@ -29,8 +29,8 @@ jobs:
closed-for-staleness-label: closed-for-staleness

# Issue timing
days-before-stale: 10
days-before-close: 4
days-before-stale: 7
days-before-close: 2
days-before-ancient: 365

# If you don't want to mark a issue as being ancient based on a
Expand Down
40 changes: 19 additions & 21 deletions src/services/ai/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Observable, of } from 'rxjs';

import { CommitType, ValidConfig } from '../../utils/config.js';
import { StagedDiff } from '../../utils/git.js';
import { extraPrompt, generateDefaultPrompt } from '../../utils/prompt.js';
import { extraPrompt, generateDefaultPrompt, isValidConventionalMessage, isValidGitmojiMessage } from '../../utils/prompt.js';

// NOTE: get AI Type from key names
export const AIType = {
Expand Down Expand Up @@ -67,30 +67,28 @@ export abstract class AIService {
});
};

protected extractCommitMessageFromRawText(type: CommitType, text: string): string {
switch (type) {
case 'conventional':
// NOTE: check loosely for issue that message is not coming out
const regex = new RegExp(/(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?: .*$/);
const match = text.match(regex);
return match ? match[0].replace(/: (\w)/, (_: any, firstLetter: string) => `: ${firstLetter.toLowerCase()}`) : '';
case 'gitmoji':
const gitmojiRegexp = new RegExp(/^\:\w+\: (.*)$/gm);
const gitmojoMatched = text.match(gitmojiRegexp);
return gitmojoMatched ? gitmojoMatched[0] : '';
default:
return text;
}
}

protected sanitizeMessage(generatedText: string, type: CommitType, maxCount: number) {
const messages = generatedText
.split('\n')
.map((message: string) => message.trim().replace(/^\d+\.\s/, ''))
.map((message: string) => message.replace(/`/g, ''))
.map((message: string) => message.replace(/"/g, ''))
.map((message: string) => message.replace(/\*/g, ''))
.map((message: string) => this.extractCommitMessageFromRawText(type, message))
.map((message: string) => message.replace(/[`'"*]/g, ''))
.filter((message: string) => {
switch (type) {
case 'conventional':
return isValidConventionalMessage(message);
case 'gitmoji':
return isValidGitmojiMessage(message);
default:
return true;
}
})
.map(message => {
if (type === 'conventional') {
const regex = /: (\w)/;
return message.replace(regex, (_: any, firstLetter: string) => `: ${firstLetter.toLowerCase()}`);
}
return message;
})
.filter((message: string) => !!message);

if (messages.length > maxCount) {
Expand Down
26 changes: 17 additions & 9 deletions src/utils/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ const MAX_COMMIT_LENGTH = 80;

const commitTypeFormats: Record<CommitType, string> = {
'': '<commit message>',
conventional: `<type>(<optional scope>): <commit message>`,
gitmoji: `:<emoji>: <commit message>`,
conventional: `<type>(<optional scope>): <description>`,
gitmoji: `:<emoji>: <description>`,
};

const exampleCommitByType: Record<CommitType, string> = {
'': '',
conventional: `Example commit message => feat: add new disabled boolean variable to button`,
gitmoji: `Example commit message => :sparkles: Add a generic preset using configuration`,
};

const specifyCommitFormat = (type: CommitType = 'conventional') => {
if (type === '') {
return '';
}
return `The commit message must be in format:\n${commitTypeFormats[type]}`;
return `The commit message must be in format:\n${commitTypeFormats[type]}\n${exampleCommitByType[type]}`;
};

const commitTypes: Record<CommitType, string> = {
Expand Down Expand Up @@ -44,7 +50,7 @@ const commitTypes: Record<CommitType, string> = {
* Conventional Changelog:
* https://github.com/conventional-changelog/conventional-changelog/blob/d0e5d5926c8addba74bc962553dd8bcfba90e228/packages/conventional-changelog-conventionalcommits/writer-opts.js#L182-L193
*/
conventional: `Choose a type from the type-to-description JSON below that best describes the git diff\n${JSON.stringify(
conventional: `Choose a type from the type-to-description JSON below that best describes the git diff.\n${JSON.stringify(
{
docs: 'Documentation only changes',
style: 'Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)',
Expand All @@ -65,7 +71,7 @@ const commitTypes: Record<CommitType, string> = {

export const generateDefaultPrompt = (locale: string, maxLength: number, type: CommitType, additionalPrompts: string = '') =>
[
'You are the expert programmer. You are going to provide a professional git commit message.',
'You are the expert programmer, trained to write commit messages. You are going to provide a professional git commit message.',
'Generate a concise git commit message written in present tense with the given specifications below:',
`Message language: ${locale}`,
`Commit message must be a maximum of ${Math.min(Math.max(maxLength, 0), MAX_COMMIT_LENGTH)} characters.`,
Expand All @@ -77,15 +83,17 @@ export const generateDefaultPrompt = (locale: string, maxLength: number, type: C
.filter(Boolean)
.join('\n');

export const extraPrompt = (generate: number) => `You must generate ${generate} commit messages in numbered list output format.`;
export const extraPrompt = (generate: number) => `THE RESULT MUST BE ${generate} COMMIT MESSAGES AND MUST BE IN NUMBERED LIST FORMAT.`;

export const isValidConventionalMessage = (message: string): boolean => {
const conventionalReg =
/^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\w\-]+\))?(!)?: .{1,80}(\n|\r\n){2}(.*(\n|\r\n)*)*$/;
// TODO: check loosely for issue that message is not coming out
// const conventionalReg =
// /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\w\-]+\))?(!)?: .{1,80}(\n|\r\n){2}(.*(\n|\r\n)*)*$/;
const conventionalReg = /(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?: .*$/;
return conventionalReg.test(message);
};

export const isValidGitmojiMessage = (message: string): boolean => {
const gitmojiCommitMessageRegex = /^\:\w+\: (.*)$/;
const gitmojiCommitMessageRegex = /:\w*:/;
return gitmojiCommitMessageRegex.test(message);
};

0 comments on commit cd44451

Please sign in to comment.