Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add "changes requested" status to GitHub PR oneboxes (#36531)

Régis Hanolc2e1cfc904fa
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 53c9c939659..8a1cd7342a8 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -5590,6 +5590,7 @@ en:
draft: "Draft Pull Request"
open: "Open Pull Request"
approved: "Approved Pull Request"
+ changes_requested: "Changes Requested"
merged: "Merged Pull Request"
closed: "Closed Pull Request"
diff --git a/lib/onebox/engine/github_pull_request_onebox.rb b/lib/onebox/engine/github_pull_request_onebox.rb
index 5d7a4be21e3..ae77c9abce7 100644
--- a/lib/onebox/engine/github_pull_request_onebox.rb
+++ b/lib/onebox/engine/github_pull_request_onebox.rb
@@ -131,8 +131,10 @@ module Onebox
return "draft" if pr_data["draft"]
reviews_data = load_json(url + "/reviews")
+ review_states = latest_review_states(reviews_data)
- return "approved" if reviews_approved?(reviews_data)
+ return "changes_requested" if review_states.include?("CHANGES_REQUESTED")
+ return "approved" if review_states.include?("APPROVED")
"open"
rescue StandardError => e
@@ -140,19 +142,14 @@ module Onebox
nil
end
- def reviews_approved?(reviews)
- return false if reviews.blank?
+ def latest_review_states(reviews)
+ return [] if reviews.blank?
- states =
- reviews
- .reject { |r| r.dig("user", "id").nil? || %w[PENDING COMMENTED].include?(r["state"]) }
- .group_by { |r| r.dig("user", "id") }
- .transform_values { |rs| rs.max_by { |r| r["submitted_at"] }["state"] }
- .values
-
- return false if states.empty?
-
- states.all? { |s| %w[APPROVED DISMISSED].include?(s) } && states.include?("APPROVED")
+ reviews
+ .reject { |r| r.dig("user", "id").nil? || %w[PENDING COMMENTED].include?(r["state"]) }
+ .group_by { |r| r.dig("user", "id") }
+ .transform_values { |rs| rs.max_by { |r| r["submitted_at"] }["state"] }
+ .values
end
end
end
diff --git a/plugins/discourse-github/assets/stylesheets/common/github-pr-status.scss b/plugins/discourse-github/assets/stylesheets/common/github-pr-status.scss
index 3fd673f1896..874da33b7b9 100644
--- a/plugins/discourse-github/assets/stylesheets/common/github-pr-status.scss
+++ b/plugins/discourse-github/assets/stylesheets/common/github-pr-status.scss
@@ -1,22 +1,15 @@
.onebox.githubpullrequest .github-row {
- // Draft PR icon (dashed outline)
--gh-icon-draft: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.25 1A2.25 2.25 0 0 1 4 5.372v5.256a2.251 2.251 0 1 1-1.5 0V5.372A2.251 2.251 0 0 1 3.25 1Zm9.5 14a2.25 2.25 0 1 1 0-4.5 2.25 2.25 0 0 1 0 4.5ZM2.5 3.25a.75.75 0 1 0 1.5 0 .75.75 0 0 0-1.5 0ZM3.25 12a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm9.5 0a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5ZM14 4.25a.75.75 0 0 1-.75.75h-2a.75.75 0 0 1 0-1.5h2a.75.75 0 0 1 .75.75Zm-.75 3.75a.75.75 0 0 0 0-1.5h-2a.75.75 0 0 0 0 1.5Z'/%3E%3C/svg%3E");
-
- // Open PR icon
--gh-icon-open: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M1.5 3.25a2.25 2.25 0 1 1 3 2.122v5.256a2.251 2.251 0 1 1-1.5 0V5.372A2.25 2.25 0 0 1 1.5 3.25Zm5.677-.177L9.573.677A.25.25 0 0 1 10 .854V2.5h1A2.5 2.5 0 0 1 13.5 5v5.628a2.251 2.251 0 1 1-1.5 0V5a1 1 0 0 0-1-1h-1v1.646a.25.25 0 0 1-.427.177L7.177 3.427a.25.25 0 0 1 0-.354ZM3.75 2.5a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm0 9.5a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm8.25.75a.75.75 0 1 0 1.5 0 .75.75 0 0 0-1.5 0Z'/%3E%3C/svg%3E");
-
- // Merged PR icon
--gh-icon-merged: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M5.45 5.154A4.25 4.25 0 0 0 9.25 7.5h1.378a2.251 2.251 0 1 1 0 1.5H9.25A5.734 5.734 0 0 1 5 7.123v3.505a2.25 2.25 0 1 1-1.5 0V5.372a2.25 2.25 0 1 1 1.95-.218ZM4.25 13.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5Zm8.5-4.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5ZM5 3.25a.75.75 0 1 0 0 .005V3.25Z'/%3E%3C/svg%3E");
-
- // Closed PR icon
--gh-icon-closed: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.25 1A2.25 2.25 0 0 1 4 5.372v5.256a2.251 2.251 0 1 1-1.5 0V5.372A2.251 2.251 0 0 1 3.25 1Zm9.5 5.5a.75.75 0 0 1 .75.75v3.378a2.251 2.251 0 1 1-1.5 0V7.25a.75.75 0 0 1 .75-.75Zm-2.03-5.273a.75.75 0 0 1 1.06 0l.97.97.97-.97a.748.748 0 0 1 1.265.332.75.75 0 0 1-.205.729l-.97.97.97.97a.751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018l-.97-.97-.97.97a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734l.97-.97-.97-.97a.75.75 0 0 1 0-1.06ZM2.5 3.25a.75.75 0 1 0 1.5 0 .75.75 0 0 0-1.5 0ZM3.25 12a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm9.5 0a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Z'/%3E%3C/svg%3E");
-
- // Approved check-circle icon
--gh-icon-approved: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M8 16A8 8 0 1 1 8 0a8 8 0 0 1 0 16Zm3.78-9.72a.751.751 0 0 0-.018-1.042.751.751 0 0 0-1.042-.018L6.75 9.19 5.28 7.72a.751.751 0 0 0-1.042.018.751.751 0 0 0-.018 1.042l2 2a.75.75 0 0 0 1.06 0Z'/%3E%3C/svg%3E");
+ --gh-icon-changes-requested: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M8 16A8 8 0 1 1 8 0a8 8 0 0 1 0 16ZM4.5 7.25a.75.75 0 0 0 0 1.5h7a.75.75 0 0 0 0-1.5h-7Z'/%3E%3C/svg%3E");
&.--gh-status-draft,
&.--gh-status-open,
&.--gh-status-approved,
+ &.--gh-status-changes_requested,
&.--gh-status-merged,
&.--gh-status-closed {
.github-icon-container {
@@ -56,6 +49,11 @@
background-color: #3fb950;
}
+ &.--gh-status-changes_requested .github-icon-container::before {
+ mask-image: var(--gh-icon-changes-requested);
+ background-color: #f79939;
+ }
+
&.--gh-status-merged .github-icon-container::before {
mask-image: var(--gh-icon-merged);
background-color: #a371f7;
diff --git a/plugins/discourse-github/spec/lib/github_pr_onebox_status_spec.rb b/plugins/discourse-github/spec/lib/github_pr_onebox_status_spec.rb
index e89014c9d00..f70e829743d 100644
--- a/plugins/discourse-github/spec/lib/github_pr_onebox_status_spec.rb
+++ b/plugins/discourse-github/spec/lib/github_pr_onebox_status_spec.rb
@@ -113,12 +113,61 @@ RSpec.describe Onebox::Engine::GithubPullRequestOnebox do
stub_request(:get, reviews_api_uri).to_return(status: 200, body: MultiJson.dump(reviews))
end
- it "shows as open status (not approved)" do
- expect(onebox_html).to include("--gh-status-open")
+ it "includes changes_requested status" do
+ expect(onebox_html).to include("--gh-status-changes_requested")
+ end
+ end
+
+ context "when PR has both approval and changes requested from different reviewers" do
+ before do
+ stub_request(:get, api_uri).to_return(status: 200, body: MultiJson.dump(open_pr_response))
+ reviews = [
+ { "user" => { "id" => 1 }, "state" => "APPROVED", "submitted_at" => Time.now.iso8601 },
+ {
+ "user" => {
+ "id" => 2,
+ },
+ "state" => "CHANGES_REQUESTED",
+ "submitted_at" => Time.now.iso8601,
+ },
+ ]
+ stub_request(:get, reviews_api_uri).to_return(status: 200, body: MultiJson.dump(reviews))
+ end
+
+ it "shows changes_requested (takes priority over approved)" do
+ expect(onebox_html).to include("--gh-status-changes_requested")
expect(onebox_html).not_to include("--gh-status-approved")
end
end
+ context "when reviewer first requests changes then approves" do
+ before do
+ stub_request(:get, api_uri).to_return(status: 200, body: MultiJson.dump(open_pr_response))
+ reviews = [
+ {
+ "user" => {
+ "id" => 1,
+ },
+ "state" => "CHANGES_REQUESTED",
+ "submitted_at" => "2024-01-01T00:00:00Z",
+ },
+ {
+ "user" => {
+ "id" => 1,
+ },
+ "state" => "APPROVED",
+ "submitted_at" => "2024-01-02T00:00:00Z",
+ },
+ ]
+ stub_request(:get, reviews_api_uri).to_return(status: 200, body: MultiJson.dump(reviews))
+ end
+
+ it "shows approved (latest review wins)" do
+ expect(onebox_html).to include("--gh-status-approved")
+ expect(onebox_html).not_to include("--gh-status-changes_requested")
+ end
+ end
+
context "when reviews API fails" do
before do
stub_request(:get, api_uri).to_return(status: 200, body: MultiJson.dump(open_pr_response))