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
18 changes: 0 additions & 18 deletions .eslintrc.js

This file was deleted.

26 changes: 26 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"parser": "@typescript-eslint/parser",
"env": {
"node": true,
"jest": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/strict"
],
"parserOptions": {
"project": "./tsconfig.json",
"ecmaVersion": 2015
},
"rules": {
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/explicit-module-boundary-types": "error",
"@typescript-eslint/strict-boolean-expressions": "error",
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/explicit-function-return-type": "error",
"no-nested-ternary": "error",
"no-use-before-define": "off",
"no-restricted-syntax": "off"
}
}
11 changes: 11 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"preset": "ts-jest",
"testEnvironment": "node",
"transform": {
"^.+\\.ts$": "ts-jest"
},
"transformIgnorePatterns": [
"/node_modules/"
],
"moduleFileExtensions": ["ts", "js", "json", "node"]
}
80 changes: 0 additions & 80 deletions lib/comment.js

This file was deleted.

114 changes: 114 additions & 0 deletions lib/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import * as Mustache from 'mustache';
import {MinimatchOptions} from 'minimatch/dist/esm';
import { Snippet } from './snippets';
import { TemplateVariables } from "./config";

export type Comment = {
id: number;
url: string;
created_at: string;
body?: string;
body_text?: string;
body_html?: string;
}

export interface CommentObject extends Map<string, unknown>{
get(key: 'header' | 'footer' | 'onCreate' | 'onUpdate'): string;
get(key: 'snippets'): Snippet[],
get(key: 'on-create' | 'on-update' | 'globOptions'): Map<string, unknown>[];
get(key: 'globOptions'): MinimatchOptions | undefined;
}

function commentMetadata(snippetIds: string[]): string {
return `<!-- pr-commenter-metadata: ${snippetIds.join(',')} -->`;
}

function extractCommentMetadata(commentBody: string): string[] | null {
// snippet id regex plus a comma
const regex = /<!-- pr-commenter-metadata: ([A-Za-z0-9\-_,]*) -->/;
const match = regex.exec(commentBody);

if (match) {
return match[1].split(',').map((s: string) => s.trim()).filter((s) => s !== '');
}
return null;
}

function assembleCommentBody(
snippetIds: string[],
commentConfig: CommentObject,
templateVariables: TemplateVariables = {}): string {
let strings = [
commentConfig.get('header'),
...(commentConfig.get('snippets')).map(snippet => {
if (snippetIds.includes(snippet.get('id'))) {
return snippet.get('body');
}
return null;
}),
commentConfig.get('footer'),
commentMetadata(snippetIds),
];

strings = strings.filter((s) => Boolean(s));

const rawCommentBody = strings.join('\n\n');

return Mustache.render(rawCommentBody, templateVariables);
}

function newCommentDifferentThanPreviousComment(previousComment: Comment, snippetIds: string[]): boolean {
const previousSnippetIds = (previousComment.body != null) ? extractCommentMetadata(previousComment.body) : null;

return previousSnippetIds !== null && previousSnippetIds.join(',') !== snippetIds.join(',');
}

function newCommentWouldHaveContent(snippetIds: string[]): boolean {
return snippetIds.length > 0;
}

function shouldPostNewComment(
previousComment: Comment | null,
snippetIds: string[],
commentConfig: CommentObject
): boolean {
const isNotEmpty = newCommentWouldHaveContent(snippetIds);
const isCreating = !previousComment && commentConfig.get('onCreate') === 'create';
const isUpdating = !!previousComment
&& commentConfig.get('onUpdate') === 'recreate'
&& newCommentDifferentThanPreviousComment(previousComment, snippetIds);

return isNotEmpty && (isCreating || isUpdating);
}

function shouldDeletePreviousComment(
previousComment: Comment | null,
snippetIds: string[],
commentConfig: CommentObject
): boolean {
return !!previousComment && (
shouldPostNewComment(previousComment, snippetIds, commentConfig)
|| (!newCommentWouldHaveContent(snippetIds) && commentConfig.get('onUpdate') !== 'nothing')
);
}

function shouldEditPreviousComment(
previousComment: Comment | null,
snippetIds: string[],
commentConfig: CommentObject
): boolean {
return newCommentWouldHaveContent(snippetIds) && (
!!previousComment
&& commentConfig.get('onUpdate') === 'edit'
&& newCommentDifferentThanPreviousComment(previousComment, snippetIds)
);
}

export {
commentMetadata,
assembleCommentBody,
extractCommentMetadata,
shouldPostNewComment,
shouldDeletePreviousComment,
shouldEditPreviousComment,
};
Loading