Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@
"@rollup/plugin-commonjs": "^28.0.2",
"@rollup/plugin-node-resolve": "^16.0.0",
"@types/express": "^5.0.0",
"@types/jest": "^30.0.0",
"@types/js-yaml": "^4.0.9",
"@types/mocha": "^10.0.10",
"@types/node": "^22.13.4",
"@types/nodemailer": "^6.4.17",
"@types/nodemailer-express-handlebars": "^4.0.5",
Expand Down
1 change: 0 additions & 1 deletion src/configs/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export const appConfig = {
drGitApiKeyName: 'DR_GIT_API_KEY',
configFileNames: ['.drgit.yml', '.drgit.yaml'],
metadata: {
// Don't change these strings on production
walkthrough: '[drgit]: walkthrough',
diagram: '[drgit]: diagram',
summary: '[drgit]: summary'
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/app.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class AppHandler {
new UserErrorListener().listen();

if (envs.isDevelopment) {
await this.services.cacheService.flush();
await this.services.cacheService.flushByPattern('user/*');

systemLogger.info('Cache has been flushed');
}
Expand All @@ -103,6 +103,7 @@ export class AppHandler {
config = await this.services.configService.getConfig(context, apiKey);

await this.services.appService.init(context);
if (!apiKey || !config) return;
await callback(apiKey, config);
} catch (error) {
if ('repository' in context.payload) {
Expand Down
12 changes: 12 additions & 0 deletions src/services/cache.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ export class CacheService<Key extends string = CacheKey> {
});
}

async flushByPattern(pattern: string): Promise<number> {
return this.wrapError('flush cache by pattern', undefined, async () => {
const keys = await this.client.keys(pattern);

if (keys.length === 0) {
return 0;
}

return this.client.del(...keys);
});
}

async close(): Promise<boolean> {
return this.wrapError('close connection', undefined, async () => {
const result = await this.client.quit();
Expand Down
2 changes: 1 addition & 1 deletion src/services/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class GitHubService {
pull_number
});

const filePromises = files.data.map(async (file) => {
const filePromises = files.data.map(async (file: RestEndpointMethodTypes['pulls']['listFiles']['response']['data'][number]) => {
const content = await context.octokit.repos.getContent({
owner,
repo,
Expand Down
8 changes: 4 additions & 4 deletions src/services/queue.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import pQueue, { Options, QueueAddOptions } from 'p-queue';
import PriorityQueue from 'p-queue/dist/priority-queue';
import pQueue from 'p-queue';
import pRetry, { Options as pRetryOption } from 'p-retry';

type TaskFunction<T> = () => Promise<T>;
type PQueueOptions = ConstructorParameters<typeof pQueue>[0];

export class QueueService {
private static instance: QueueService;
private queue: pQueue;
private defaultRetryOptions: pRetryOption;

private constructor(queueOptions: Options<PriorityQueue, QueueAddOptions>, retryOptions: pRetryOption) {
private constructor(queueOptions: PQueueOptions, retryOptions: pRetryOption) {
this.queue = new pQueue(queueOptions);
this.defaultRetryOptions = retryOptions;
}

public static getInstance(
queueOptions: Options<PriorityQueue, QueueAddOptions> = { concurrency: 5, interval: 3000 },
queueOptions: PQueueOptions = { concurrency: 5, interval: 3000 },
retryOptions: pRetryOption = { minTimeout: 1000, maxTimeout: 5000, retries: 3, factor: 2 }
): QueueService {
if (!QueueService.instance) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/chat-config.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ChatConfig {
} else if (isInlineComment(context.payload) || isPullRequestComment(context.payload)) {
return config.chat['stop-on']['pull-requests'].closed;
} else if (isIssue(context.payload) || isIssueComment(context.payload)) {
return config.chat['stop-on'].issues.locked;
return config.chat['stop-on'].issues.closed;
} else {
throw new SystemError('Unhandled case');
}
Expand Down
61 changes: 33 additions & 28 deletions src/utils/github.util.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
import { DiffLine } from '~common/types';

const hunkRegex = /@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@([\s\S]*?)(?=@@|$)/g;

export const extractDiffsFromPatch = (patch: string): DiffLine[] => {
const diffLines = [];
const diffLines: DiffLine[] = [];
const hunkRegex = /@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@([\s\S]*?)(?=@@|$)/g;

let match;
while ((match = hunkRegex.exec(patch)) !== null) {
const allCleanedLines = match[3].split('\n').flatMap((line) => (!!line.trim() ? line.trim() : []));
const firstAdditionIndex = allCleanedLines.findIndex((line) => line.startsWith('+'));
const totalLinesBeforeFirstAddition = allCleanedLines.filter(
(line, lineIndex) => /^[^-+]/.test(line) && lineIndex < firstAdditionIndex
).length;
const firstDeletionIndex = allCleanedLines.findIndex((line) => line.startsWith('-'));
const totalLinesAfterLastAddition = allCleanedLines.filter(
(line, lineIndex) => /^[^-+]/.test(line) && lineIndex < firstDeletionIndex
).length;

const start = parseInt(match[1], 10) + totalLinesBeforeFirstAddition;
const lineCount = match[2] ? parseInt(match[2], 10) : 1;
const end = start - totalLinesBeforeFirstAddition + lineCount - 1 - totalLinesAfterLastAddition;

const addition = allCleanedLines
.filter((line) => line.startsWith('+'))
.map((line) => line.slice(1))
.join('\n');

const deletion = allCleanedLines
.filter((line) => line.startsWith('-'))
.map((line) => line.slice(1))
.join('\n');

diffLines.push({ start, end, addition, deletion });
let lineNum = parseInt(match[1], 10);
let startLine: number | null = null;
let endLine: number | null = null;
const addedLines: string[] = [];
const deletedLines: string[] = [];

const lines = match[2].split('\n');

for (const line of lines) {
if (line.startsWith('+')) {
if (startLine === null) startLine = lineNum;
endLine = lineNum;
addedLines.push(line.slice(1));
lineNum++;
} else if (line.startsWith('-')) {
deletedLines.push(line.slice(1));
// Deleted lines do not advance the new-file line counter
} else if (line.trim() !== '') {
// Context line — counts in both old and new file
lineNum++;
}
}

if (startLine !== null && endLine !== null) {
diffLines.push({
start: startLine,
end: endLine,
addition: addedLines.join('\n'),
deletion: deletedLines.join('\n')
});
}
}

return diffLines;
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/chat-config.util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('ChatConfig', () => {
},
'stop-on': {
discussions: { locked: true, closed: false },
issues: { locked: false, draft: true },
issues: { locked: false, draft: true, closed: true },
'pull-requests': { locked: false, draft: false, merged: true, closed: false }
},
allow: {
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('ChatConfig', () => {

expect(ChatConfig.getChatStopOnClosed(contextDiscussion, config)).toBe(false);
expect(ChatConfig.getChatStopOnClosed(contextInlineComment, config)).toBe(false);
expect(ChatConfig.getChatStopOnClosed(contextIssue, config)).toBe(false);
expect(ChatConfig.getChatStopOnClosed(contextIssue, config)).toBe(true); // fixed: reads .closed not .locked

expect(ChatConfig.getChatAllow(contextDiscussion, config)).toBe(true);
expect(ChatConfig.getChatAllow(contextInlineComment, config)).toBe(true);
Expand Down
14 changes: 10 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"ignoreDeprecations": true,
"compilerOptions": {
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "Node",
"moduleResolution": "Bundler",
"incremental": true,
"strict": true,
"sourceMap": true,
Expand All @@ -26,10 +27,15 @@
"declaration": false,
"outDir": "dist",
"rootDir": "src",
"typeRoots": ["types", "./node_modules/@types"],
"typeRoots": [
"types",
"./node_modules/@types"
],
"baseUrl": "./",
"paths": {
"~*": ["./src/*"]
"~*": [
"./src/*"
]
}
},
"exclude": [
Expand All @@ -40,5 +46,5 @@
"include": [
"src",
"types"
]
],
}
Loading