Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: github comment edit #137

Merged
merged 2 commits into from
Oct 18, 2023
Merged
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
30 changes: 29 additions & 1 deletion pages/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
Platform
} from "../../typings";
import { GITHUB } from "../../utils/constants";
import { replaceImgTags, replaceStrikethroughTags } from "../../utils";
import {
replaceImgTags,
replaceStrikethroughTags,
replaceGithubComment
} from "../../utils";
import {
Issue,
IssueCommentCreatedEvent,
Expand Down Expand Up @@ -284,6 +288,7 @@ export const prepareMarkdownContent = async (
let modifiedMarkdown = await replaceMentions(markdown, platform);
modifiedMarkdown = await replaceStrikethroughTags(modifiedMarkdown);
modifiedMarkdown = await replaceImgTags(modifiedMarkdown);
modifiedMarkdown = await replaceGithubComment(modifiedMarkdown);

if (githubOptions?.anonymous && githubOptions?.sender) {
return `>${modifiedMarkdown}\n\n—[${githubOptions.sender.login} on GitHub](${githubOptions.sender.html_url})`;
Expand Down Expand Up @@ -318,6 +323,29 @@ export const createLinearComment = async (
}
};

export const updateLinearComment = async (
linearCommentId: string,
linear: LinearClient,
linearIssueId: string,
modifiedComment: string,
issueNumber: number
) => {
const comment = await linear.commentUpdate(linearCommentId, {
body: modifiedComment || ""
});

if (!comment.success) {
throw new ApiError(
`Failed to Update comment on Linear issue ${linearIssueId} for GitHub issue ${issueNumber} of id ${linearCommentId}`,
500
);
} else {
console.log(
`Update comment for GitHub issue #${issueNumber} with Id ${linearCommentId}.`
);
}
};

export const createAnonymousUserComment = async (
body: IssueCommentCreatedEvent,
repository: Repository,
Expand Down
10 changes: 10 additions & 0 deletions utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ export const getGitHubFooter = (userName: string): string => {
return `\n\n<!-- From ${sanitizedUsername} on Linear -->`;
};

export const getGithubFooterWithLinearCommentId = (
userName: string,
commentId: string
): string => {
// To avoid exposing a user email if their username is an email address
const sanitizedUsername = userName.split("@")?.[0];

return `\n\n<!-- From ${sanitizedUsername} on Linear. LinearCommentId:${commentId}: -->`;
};

export const getGitHubTokenURL = (): string => {
const scopes = GITHUB.SCOPES.join(",");
const description = GITHUB.TOKEN_NOTE.split(" ").join("%20");
Expand Down
6 changes: 6 additions & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ export const replaceStrikethroughTags = (text: string): string => {
return text?.replace(/(?<!\\)~(?!~)/g, "~~") || text;
};

export const replaceGithubComment = (text: string): string => {
const regex = /<!--(.*?)-->/g;
const stringWithoutComments = text.replace(regex, "");
return stringWithoutComments;
};

export const getSyncFooter = (): string => {
return `From [SyncLinear.com](https://synclinear.com)`;
};
Expand Down
32 changes: 30 additions & 2 deletions utils/webhook/github.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
createAnonymousUserComment,
createLinearComment,
prepareMarkdownContent,
upsertUser
upsertUser,
updateLinearComment
} from "../../pages/api/utils";
import {
IssueCommentCreatedEvent,
Expand All @@ -20,7 +21,8 @@ import {
IssuesLabeledEvent,
IssuesUnassignedEvent,
IssuesUnlabeledEvent,
MilestoneEvent
MilestoneEvent,
IssueCommentEditedEvent
} from "@octokit/webhooks-types";
import {
createLinearCycle,
Expand Down Expand Up @@ -164,6 +166,32 @@ export async function githubWebhookHandler(
})
: null;

if (githubEvent === "issue_comment" && action === "edited") {
if (!syncedIssue) {
const reason = skipReason("comment", issue.number);
return reason;
}
const { comment } = body as IssueCommentEditedEvent;
const regex = /LinearCommentId:(.*?):/;
const match = comment.body.match(regex);
const isLinearCommentIdPresent = match && match[1];

if (isLinearCommentIdPresent) {
const linearCommentId = match[1];
const modifiedComment = await prepareMarkdownContent(
comment.body,
"github"
);
await updateLinearComment(
linearCommentId,
linear,
syncedIssue.linearIssueId,
modifiedComment,
issue.number
);
}
}

if (githubEvent === "issue_comment" && action === "created") {
// Comment created

Expand Down
18 changes: 15 additions & 3 deletions utils/webhook/linear.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import got from "got";
import { getLinearCycle, inviteMember } from "../linear";
import { components } from "@octokit/openapi-types";
import { linearQuery } from "../apollo";
import { createMilestone, getGitHubFooter, setIssueMilestone } from "../github";
import {
createMilestone,
getGitHubFooter,
getGithubFooterWithLinearCommentId,
setIssueMilestone
} from "../github";
import { ApiError, getIssueUpdateError } from "../errors";
import { Issue, User } from "@octokit/webhooks-types";

Expand Down Expand Up @@ -426,7 +431,10 @@ export async function linearWebhookHandler(
comment.body,
"linear"
);
const footer = getGitHubFooter(user.displayName);
const footer = getGithubFooterWithLinearCommentId(
user.displayName,
comment.id
);

const { error: commentError } = await createComment({
repoFullName,
Expand Down Expand Up @@ -935,7 +943,11 @@ export async function linearWebhookHandler(
}

const modifiedBody = await replaceMentions(data.body, "linear");
const footer = getGitHubFooter(data.user?.name);

const footer = getGithubFooterWithLinearCommentId(
data.user?.name,
data.id
);

const { error: commentError } = await createComment({
repoFullName: syncedIssue.GitHubRepo.repoName,
Expand Down