diff --git a/src/class/prsReview.ts b/src/class/prsReview.ts index da984fa..f3b2c46 100644 --- a/src/class/prsReview.ts +++ b/src/class/prsReview.ts @@ -6,7 +6,6 @@ import Context from "../index"; * @exports */ export default class PullRequestReview { - /** * @private * @type Context<"pull_request_review.submitted"> @@ -23,6 +22,48 @@ export default class PullRequestReview { /** * @private + * @param {string} body + * @param {string[]} labels + * @param {string[]} [removeLabel] + * @async + * @returns {Promise} + */ + private async createReview(body: string, labels: string[], removeLabel?: string[]): Promise { + await this.context.octokit.pulls.createReview({ + repo: this.context.payload.repository.name, + owner: this.context.payload.repository.owner.login, + pull_number: this.context.payload.pull_request.number, + body: body, + event: "COMMENT" + }); + + await this.context.octokit.issues.addLabels( + this.context.issue({ + labels: labels + }) + ); + + if (removeLabel) { + removeLabel.forEach((label) => { + new Promise((resolve, reject) => { + this.context.octokit.issues.removeLabel( + this.context.issue({ + name: label + }) + ).then(() => { + resolve(); + }).catch((err) => { + reject(err); + }); + }).catch((err) => { + console.error(err); + }); + }); + } + } + + /** + * @public * @async * @returns {Promise} */ @@ -30,230 +71,58 @@ export default class PullRequestReview { if (this.context.payload.sender.login == this.context.payload.repository.owner.login) { // Owner if (this.context.payload.review.state == "approved") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `@${this.context.payload.pull_request.user.login} your pull request has been approved by @${this.context.payload.review.user.login}, please type \`Ready to merge\` for merging`, - event: "COMMENT" - }); + const reviewMessage = `@${this.context.payload.pull_request.user.login} your pull request has been approved by @${this.context.payload.review.user.login}, please type \`Ready to merge\` for merging`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Requested Changes")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Requested Changes" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Approved"], ["Requested Changes", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Approved"], ["Pending"]); } } else if (this.context.payload.review.state == "changes_requested") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `Pull request has requested changes by @${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I'm merging this PR, thanks!`, - event: "COMMENT" - }); + const reviewMessage = `Pull request has requested changes by @${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I'm merging this PR, thanks!`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Approved")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Approved" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Requested Changes"], ["Approved", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Requested Changes"], ["Pending"]); } } } else if (this.context.payload.pull_request.author_association == "MEMBER" || this.context.payload.pull_request.author_association == "COLLABORATOR") { if (this.context.payload.review.state == "approved") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `@${this.context.payload.pull_request.user.login} your pull request has been approved by \`[MAINTAINER]\`@${this.context.payload.review.user.login}, please type \`Ready to merge\` for merging`, - event: "COMMENT" - }); + const reviewMessage = `@${this.context.payload.pull_request.user.login} your pull request has been approved by \`[MAINTAINER]\`@${this.context.payload.review.user.login}, please type \`Ready to merge\` for merging`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Requested Changes")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Requested Changes" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Approved"], ["Requested Changes", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Approved"], ["Pending"]); } } else if (this.context.payload.review.state == "changes_requested") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `Pull request has requested changes by @${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I'm merging this PR, thanks!`, - event: "COMMENT" - }); + const reviewMessage = `Pull request has requested changes by @${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I'm merging this PR, thanks!`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Approved")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Approved" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Requested Changes"], ["Approved", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Requested Changes"], ["Pending"]); } } } else { // Others Approved if (this.context.payload.review.state == "approved") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `@${this.context.payload.pull_request.user.login} your pull request has been approved by @${this.context.payload.review.user.login}, even though please wait for the \`MAINTAINERS\`/\`CODEOWNERS\` to review`, - event: "COMMENT" - }); + const reviewMessage = `@${this.context.payload.pull_request.user.login} your pull request has been approved by @${this.context.payload.review.user.login}, even though please wait for the \`MAINTAINERS\`/\`CODEOWNERS\` to review`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Requested Changes")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Requested Changes" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Others Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Others Approved"], ["Requested Changes", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Others Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Others Approved"], ["Pending"]); } } else if (this.context.payload.review.state == "changes_requested") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `Pull request has requested changes by @${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I'm merging this PR, thanks!`, - event: "COMMENT" - }); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + const reviewMessage = `Pull request has requested changes by @${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I'm merging this PR, thanks!`; + if (this.context.payload.pull_request.labels.find((a) => a.name == "Others Approved")) { + await this.createReview(reviewMessage, ["Requested Changes"], ["Others Approved", "Pending"]); + } else { + await this.createReview(reviewMessage, ["Requested Changes"], ["Pending"]); + } } } } /** - * @private + * @public * @async * @returns {Promise} */ @@ -261,226 +130,53 @@ export default class PullRequestReview { if (this.context.payload.sender.login == this.context.payload.repository.owner.login) { // Owner if (this.context.payload.review.state == "approved") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `@${this.context.payload.pull_request.user.login} Pull request has been approved by \`[OWNER]\`@${this.context.payload.review.user.login}, please type \`Merge\` for merging @${this.context.payload.review.user.login}`, - event: "COMMENT" - }); + const reviewMessage = `@${this.context.payload.pull_request.user.login} Pull request has been approved by \`[OWNER]\`@${this.context.payload.review.user.login}, please type \`Merge\` for merging @${this.context.payload.review.user.login}`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Requested Changes")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Requested Changes" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Approved"], ["Requested Changes", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Approved"], ["Pending"]); } } else if (this.context.payload.review.state == "changes_requested") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `@${this.context.payload.pull_request.user.login} your pull request has requested changes by \`[OWNER]\`@${this.context.payload.review.user.login}. Please address their comments before I'm merging this PR, thanks!`, - event: "COMMENT" - }); + const reviewMessage = `@${this.context.payload.pull_request.user.login} your pull request has requested changes by \`[OWNER]\`@${this.context.payload.review.user.login}. Please address their comments before I'm merging this PR, thanks!`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Approved")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Approved" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Requested Changes"], ["Approved", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Requested Changes"], ["Pending"]); } } } else if (this.context.payload.pull_request.author_association == "MEMBER" || this.context.payload.pull_request.author_association == "COLLABORATOR") { if (this.context.payload.review.state == "approved") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `@${this.context.payload.pull_request.user.login} Pull request has been approved by \`[MAINTAINER]\`@${this.context.payload.review.user.login}, please type \`Merge\` for merging @${this.context.payload.review.user.login}`, - event: "COMMENT" - }); + const reviewMessage = `@${this.context.payload.pull_request.user.login} Pull request has been approved by \`[MAINTAINER]\`@${this.context.payload.review.user.login}, please type \`Merge\` for merging @${this.context.payload.review.user.login}`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Requested Changes")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Requested Changes" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Approved"], ["Requested Changes", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Approved"], ["Pending"]); } } else if (this.context.payload.review.state == "changes_requested") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `Pull request has requested changes by \`[MAINTAINER]\`@${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I"m merging this PR, thanks!`, - event: "COMMENT" - }); + const reviewMessage = `Pull request has requested changes by \`[MAINTAINER]\`@${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I"m merging this PR, thanks!`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Approved")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Approved" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Requested Changes"], ["Approved", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Requested Changes"], ["Pending"]); } } } else { // Others Approved if (this.context.payload.review.state == "approved") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `@${this.context.payload.pull_request.user.login} your pull request has been approved by @${this.context.payload.review.user.login}, even though please wait for the \`MAINTAINERS\`/\`CODEOWNERS\` to review`, - event: "COMMENT" - }); + const reviewMessage = `@${this.context.payload.pull_request.user.login} your pull request has been approved by @${this.context.payload.review.user.login}, even though please wait for the \`MAINTAINERS\`/\`CODEOWNERS\` to review`; if (this.context.payload.pull_request.labels.find((a) => a.name == "Requested Changes")) { - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Requested Changes" - }) - ); - console.log("Label removed"); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Others Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Others Approved"], ["Requested Changes", "Pending"]); } else { - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Others Approved"] - }) - ); - console.log("PRs Approved"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + await this.createReview(reviewMessage, ["Others Approved"], ["Pending"]); } } else if (this.context.payload.review.state == "changes_requested") { - await this.context.octokit.pulls.createReview({ - repo: this.context.payload.repository.name, - owner: this.context.payload.repository.owner.login, - pull_number: this.context.payload.pull_request.number, - body: `Pull request has requested changes by @${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I'm merging this PR, thanks!`, - event: "COMMENT" - }); - await this.context.octokit.issues.addLabels( - this.context.issue({ - labels: ["Requested Changes"] - }) - ); - console.log("PRs Requested Changes"); - await this.context.octokit.issues.removeLabel( - this.context.issue({ - name: "Pending" - }) - ); + const reviewMessage = `Pull request has requested changes by @${this.context.payload.review.user.login}. PING! @${this.context.payload.pull_request.user.login} Please address their comments before I'm merging this PR, thanks!`; + if (this.context.payload.pull_request.labels.find((a) => a.name == "Others Approved")) { + await this.createReview(reviewMessage, ["Requested Changes"], ["Others Approved", "Pending"]); + } else { + await this.createReview(reviewMessage, ["Requested Changes"], ["Pending"]); + } } } } - }