Skip to content

Commit

Permalink
Remove reaction approval (#77)
Browse files Browse the repository at this point in the history
* Remove reaction approval

* More cleanup
  • Loading branch information
philip-gai authored Apr 15, 2022
1 parent 9ee67ca commit 5e58b37
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 47 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ Hello beautiful world! 🌎
1. Receive feedback from your teammates
1. Push updates
1. If `announcement-drafter` bot asks, make sure to authenticate so it can post as the author and not as itself
1. Approve the discussion to be created by reacting (not replying) with a 🚀
1. Merge the pull request
1. `announcement-drafter` will create the discussion
1. `announcement-drafter` will reply to the comment with a link to the newly created discussion
Expand Down
52 changes: 18 additions & 34 deletions web-app/src/eventHandlers/pullRequestEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ interface PullInfo {

export class PullRequestEventHandler {
private readonly errorIcon = "⛔️";
private readonly approverPrefix = "To approve, @";
private readonly approvalReaction = {
icon: "🚀",
label: "rocket",
};

private _tokenService: TokenService;
private _configService: ConfigService;
Expand Down Expand Up @@ -113,10 +108,7 @@ export class PullRequestEventHandler {
throw new Error("Markdown is missing a repo or team to post the discussion to");
}

let commentBody =
`⚠️ ${appLinkMarkdown} will create a discussion using this file once this PR is merged ⚠️\n\n` +
"**IMPORTANT**:\n\n" +
`- ${this.approverPrefix}${authorLogin} must react (not reply) to this comment with a ${this.approvalReaction.icon}\n`;
let commentBody = `⚠️ ${appLinkMarkdown} will create a discussion using this file once this PR is merged ⚠️\n\n` + "**IMPORTANT**:\n\n";

const userRefreshToken = await this._tokenService.getRefreshToken({
userLogin: authorLogin,
Expand Down Expand Up @@ -214,7 +206,7 @@ export class PullRequestEventHandler {
return;
}

// 1. (Shortcut) Look for comments made by the app and which files they were made on
// Get pull request comments
const app = await appGitHubService.getAuthenticatedApp();
const appLogin = `${app.slug}[bot]`;
const postFooter = `\n\n<hr /><em>This discussion was created using <a href='${app.html_url}'>${app.name}</a>.</em>\n`;
Expand All @@ -223,7 +215,7 @@ export class PullRequestEventHandler {
...pullInfo,
});

// Our app will skip it if our comment has been edited (for security)
// Get comments made by the bot
const botComments = pullRequestComments.filter(
(comment) => comment.user.login === appLogin && !comment.in_reply_to_id && comment.path && !comment.body.includes(this.errorIcon)
);
Expand All @@ -237,30 +229,22 @@ export class PullRequestEventHandler {
const authorLogin = payload.pull_request.user.login;
const authorToken = await this._tokenService.refreshUserToken(authorLogin);

// 2. Check for the approval reaction made by the author
// For each file to post
for (const fileToPostComment of botComments) {
const reactions = await appGitHubService.getPullRequestCommentReaction({
...pullInfo,
comment_id: fileToPostComment.id,
});
const authorApprovalReaction = reactions.find((reaction) => reaction.content === this.approvalReaction.label && reaction.user?.login === authorLogin);
if (authorApprovalReaction) {
logger.info("Found an approval!");
// 3. Create the discussions based off of the file
const filepath = fileToPostComment.path;
try {
await this.createDiscussion(appGitHubService, logger, appConfig, {
filepath: filepath,
pullInfo: pullInfo,
userToken: authorToken,
dryRun: false,
pullRequestCommentId: fileToPostComment.id,
postFooter: postFooter,
});
} catch (err) {
const errorMessage = HelperService.getErrorMessage(err);
logger.error(errorMessage);
}
// Create the discussion!
const filepath = fileToPostComment.path;
try {
await this.createDiscussion(appGitHubService, logger, appConfig, {
filepath: filepath,
pullInfo: pullInfo,
userToken: authorToken,
dryRun: false,
pullRequestCommentId: fileToPostComment.id,
postFooter: postFooter,
});
} catch (err) {
const errorMessage = HelperService.getErrorMessage(err);
logger.error(errorMessage);
}
}
// Delete the token to avoid conflicts with prod
Expand Down
13 changes: 1 addition & 12 deletions web-app/src/services/githubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ProbotOctokit } from "probot";
import { DeprecatedLogger } from "probot/lib/types";
import { AppConfig } from "../models/appConfig";
import { Content } from "../models/fileContent";
import { GitHubApp, PullRequestComment, PullRequestCommentReaction, TeamDiscussion } from "../models/githubModels";
import { GitHubApp, PullRequestComment, TeamDiscussion } from "../models/githubModels";

export type OctokitPlus = Octokit & RestEndpointMethods & Api & PaginateInterface & API;

Expand Down Expand Up @@ -196,17 +196,6 @@ export class GitHubService {
return comments.data;
}

public async getPullRequestCommentReaction(options: { owner: string; repo: string; comment_id: number }): Promise<PullRequestCommentReaction[]> {
this._logger.info(`Getting pull request comment reactions...`);
this._logger.debug(`Options: ${JSON.stringify(options)}`);
const commentReactions = await this._octokit.reactions.listForPullRequestReviewComment({
...options,
per_page: 100,
});
this._logger.info("Done.");
return commentReactions.data as PullRequestCommentReaction[];
}

// https://docs.github.com/en/graphql/guides/using-the-graphql-api-for-discussions#creatediscussion
public async createRepoDiscussion(options: {
repoNodeId: string;
Expand Down

0 comments on commit 5e58b37

Please sign in to comment.