Skip to content

Commit

Permalink
Merge pull request #18 from planningcenter/wm/fix-comment-clobbering
Browse files Browse the repository at this point in the history
fix: address issue where +1 / +2 label prematurely gets removed
  • Loading branch information
wassimk authored Jun 10, 2024
2 parents 788ea8f + 0947890 commit e66ee5f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @zhubert
* @zhubert @wassimk
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ Sample config, place in `.github/workflows/plusone.yml`
name: Plus one

on:
pull_request:
types: [review_request_removed, review_requested]
pull_request_review:
types: [submitted, dismissed]
types: [dismissed, submitted]

jobs:
lint:
Expand Down
5 changes: 3 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: "Plus one"
description: A GitHub action which labels a PR with +1 or +2 according to reviews
runs:
using: node16
main: dist/index.js
using: node20
main: dist/index.js
34 changes: 29 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13639,61 +13639,85 @@ const core = __nccwpck_require__(2186);
const github = __nccwpck_require__(5438);
const { Octokit } = __nccwpck_require__(1231);

async function run () {
async function run() {
const octokit = new Octokit;
const pull_number = github.context.payload.pull_request.number
const owner = github.context.payload.repository.owner.login
const repo = github.context.payload.repository.name

// Gather requested reviewers
let requestedReviewers = []
const reviewRequests = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers', {
owner,
repo,
pull_number
})

reviewRequests.data.users.forEach(rr => requestedReviewers.push(rr.login))
console.log(`requestedReviewers: ${JSON.stringify(requestedReviewers, undefined, 2)}`)

const prReviews = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
owner,
repo,
pull_number
})

console.log(`prReviews: ${JSON.stringify(prReviews, undefined, 2)}`)
// Gather non-COMMENT PR reviews
let reviews = []
prReviews.data.reverse().forEach(review => {
let filteredPrReviews = prReviews.data.filter(review => review.state !== "COMMENTED");
console.log(`filteredPrReviews: ${JSON.stringify(filteredPrReviews, undefined, 2)}`)

// Gather most recent review from each reviewer
// Exclude reviewer if they are in the requested reviewers list
filteredPrReviews.reverse().forEach(review => {
const reviewer = review.user.login
if (!reviews.find(r => r.reviewer === reviewer)) {
reviews.push({reviewer: reviewer, state: review.state})
if (!requestedReviewers.includes(reviewer)) {
reviews.push({ reviewer: reviewer, state: review.state })
}
}
});

console.log(`reviews: ${JSON.stringify(reviews, undefined, 2)}`)

// Count PR approvals
const approvedCount = reviews.filter(r => r.state === "APPROVED").length
console.log(`There are ${approvedCount} approvals.`)

// Get the current labels, excluding the +1 and +2 labels
let { data: issue } = await octokit.request('GET /repos/{owner}/{repo}/issues/{pull_number}', {
owner,
repo,
pull_number,
})
})

const labels = issue["labels"].map(l => l["name"])
let filteredLabels = labels.filter((l) => !["+1", "+2"].includes(l))
console.log(`Filtered labels: ${JSON.stringify(filteredLabels, undefined, 2)}`)

// Add the new +1 or +2 label, if applicable
if (approvedCount >= 2) {
filteredLabels.push("+2")
} else if (approvedCount == 1) {
filteredLabels.push("+1")
}
console.log(`We are setting these labels ${JSON.stringify(filteredLabels, undefined, 2)}`)

// Set the labels
await octokit.request('PUT /repos/{owner}/{repo}/issues/{issue_number}/labels', {
owner,
repo,
issue_number: pull_number,
labels: filteredLabels,
})
}

try {
run()
} catch (error) {
core.setFailed(error.message);
}

})();

module.exports = __webpack_exports__;
Expand Down
35 changes: 29 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,81 @@ const core = require('@actions/core');
const github = require('@actions/github');
const { Octokit } = require("@octokit/action");

async function run () {
async function run() {
const octokit = new Octokit;
const pull_number = github.context.payload.pull_request.number
const owner = github.context.payload.repository.owner.login
const repo = github.context.payload.repository.name

// Gather requested reviewers
let requestedReviewers = []
const reviewRequests = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers', {
owner,
repo,
pull_number
})

reviewRequests.data.users.forEach(rr => requestedReviewers.push(rr.login))
console.log(`requestedReviewers: ${JSON.stringify(requestedReviewers, undefined, 2)}`)

const prReviews = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
owner,
repo,
pull_number
})

console.log(`prReviews: ${JSON.stringify(prReviews, undefined, 2)}`)
// Gather non-COMMENT PR reviews
let reviews = []
prReviews.data.reverse().forEach(review => {
let filteredPrReviews = prReviews.data.filter(review => review.state !== "COMMENTED");
console.log(`filteredPrReviews: ${JSON.stringify(filteredPrReviews, undefined, 2)}`)

// Gather most recent review from each reviewer
// Exclude reviewer if they are in the requested reviewers list
filteredPrReviews.reverse().forEach(review => {
const reviewer = review.user.login
if (!reviews.find(r => r.reviewer === reviewer)) {
reviews.push({reviewer: reviewer, state: review.state})
if (!requestedReviewers.includes(reviewer)) {
reviews.push({ reviewer: reviewer, state: review.state })
}
}
});

console.log(`reviews: ${JSON.stringify(reviews, undefined, 2)}`)

// Count PR approvals
const approvedCount = reviews.filter(r => r.state === "APPROVED").length
console.log(`There are ${approvedCount} approvals.`)

// Get the current labels, excluding the +1 and +2 labels
let { data: issue } = await octokit.request('GET /repos/{owner}/{repo}/issues/{pull_number}', {
owner,
repo,
pull_number,
})
})

const labels = issue["labels"].map(l => l["name"])
let filteredLabels = labels.filter((l) => !["+1", "+2"].includes(l))
console.log(`Filtered labels: ${JSON.stringify(filteredLabels, undefined, 2)}`)

// Add the new +1 or +2 label, if applicable
if (approvedCount >= 2) {
filteredLabels.push("+2")
} else if (approvedCount == 1) {
filteredLabels.push("+1")
}
console.log(`We are setting these labels ${JSON.stringify(filteredLabels, undefined, 2)}`)

// Set the labels
await octokit.request('PUT /repos/{owner}/{repo}/issues/{issue_number}/labels', {
owner,
repo,
issue_number: pull_number,
labels: filteredLabels,
})
}

try {
run()
} catch (error) {
core.setFailed(error.message);
}
}

0 comments on commit e66ee5f

Please sign in to comment.