- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1
fix(comment): resolve inaccurate comment checks preventing merge #102
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package github | ||
|  | ||
| import ( | ||
| "sort" | ||
| "strings" | ||
|  | ||
| pkgtesting "github.com/AlaudaDevops/pkg/testing" | ||
| "github.com/AlaudaDevops/toolbox/pr-cli/pkg/git" | ||
| "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|  | ||
| type reviewFixture struct { | ||
| Description string `json:"description" yaml:"description"` | ||
| PRSender string `json:"pr_sender" yaml:"pr_sender"` | ||
| Reviews []reviewFixtureRow `json:"reviews" yaml:"reviews"` | ||
| } | ||
|  | ||
| type reviewFixtureRow struct { | ||
| User string `json:"user" yaml:"user"` | ||
| State string `json:"state" yaml:"state"` | ||
| SubmittedAt string `json:"submitted_at" yaml:"submitted_at"` | ||
| } | ||
|  | ||
| type expectedApprovalsFixture struct { | ||
| Description string `json:"description" yaml:"description"` | ||
| ApprovedUsers []string `json:"approved_users" yaml:"approved_users"` | ||
| } | ||
|  | ||
| var _ = ginkgo.Describe("GitHub review approvals", func() { | ||
| ginkgo.Describe("effective approval aggregation", func() { | ||
| type testCase struct { | ||
| description string | ||
| reviewsFile string | ||
| expectedFile string | ||
| } | ||
|  | ||
| ginkgo.DescribeTable("collects LGTM users from reviews", | ||
| func(tc testCase) { | ||
| reviewData := reviewFixture{} | ||
| pkgtesting.MustLoadYaml(tc.reviewsFile, &reviewData) | ||
|  | ||
| expected := expectedApprovalsFixture{} | ||
| pkgtesting.MustLoadYaml(tc.expectedFile, &expected) | ||
|  | ||
| client := &Client{ | ||
| Logger: logrus.New(), | ||
| prSender: reviewData.PRSender, | ||
| } | ||
|  | ||
| reviews := make([]git.Review, len(reviewData.Reviews)) | ||
| for i := range reviewData.Reviews { | ||
| row := reviewData.Reviews[i] | ||
| reviews[i] = git.Review{ | ||
| User: git.User{ | ||
| Login: row.User, | ||
| }, | ||
| State: row.State, | ||
| SubmittedAt: row.SubmittedAt, | ||
| } | ||
| } | ||
|  | ||
| latestReviews := client.findLatestActionableReviews(reviews) | ||
| approvedReviews := client.findEffectiveApprovals(reviews) | ||
|  | ||
| lgtmUsers := make(map[string]string) | ||
| client.addApprovedReviews(lgtmUsers, latestReviews, approvedReviews) | ||
|  | ||
| collectedUsers := make([]string, 0, len(lgtmUsers)) | ||
| for user := range lgtmUsers { | ||
| collectedUsers = append(collectedUsers, user) | ||
| } | ||
| sort.Strings(collectedUsers) | ||
|  | ||
| expectedUsers := make([]string, len(expected.ApprovedUsers)) | ||
| for i, user := range expected.ApprovedUsers { | ||
| expectedUsers[i] = strings.ToLower(user) | ||
| } | ||
| sort.Strings(expectedUsers) | ||
|  | ||
| Expect(collectedUsers).To(Equal(expectedUsers), "test case: %s", tc.description) | ||
| }, | ||
| ginkgo.Entry("retains approval after comment", testCase{ | ||
| description: "should retain approval when the latest review is only a comment", | ||
| reviewsFile: "testdata/review_effective_approvals/retain_after_comment/reviews.yaml", | ||
| expectedFile: "testdata/review_effective_approvals/retain_after_comment/expected.yaml", | ||
| }), | ||
| ginkgo.Entry("removes approval after changes requested", testCase{ | ||
| description: "should remove approval when changes are requested after approval", | ||
| reviewsFile: "testdata/review_effective_approvals/revoked_by_changes/reviews.yaml", | ||
| expectedFile: "testdata/review_effective_approvals/revoked_by_changes/expected.yaml", | ||
| }), | ||
| ginkgo.Entry("restores approval when re-approved after changes", testCase{ | ||
| description: "should restore approval when a new approval follows requested changes", | ||
| reviewsFile: "testdata/review_effective_approvals/reapprove_after_changes/reviews.yaml", | ||
| expectedFile: "testdata/review_effective_approvals/reapprove_after_changes/expected.yaml", | ||
| }), | ||
| ginkgo.Entry("removes approval after dismissal", testCase{ | ||
| description: "should remove approval when the latest review is dismissed", | ||
| reviewsFile: "testdata/review_effective_approvals/revoked_by_dismissed/reviews.yaml", | ||
| expectedFile: "testdata/review_effective_approvals/revoked_by_dismissed/expected.yaml", | ||
| }), | ||
| ) | ||
| }) | ||
| }) | 
        
          
          
            4 changes: 4 additions & 0 deletions
          
          4 
        
  ...latforms/github/testdata/review_effective_approvals/reapprove_after_changes/expected.yaml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| description: approval is valid again after the second approval | ||
| approved_users: | ||
|  | ||
| - reviewer-b | 
        
          
          
            15 changes: 15 additions & 0 deletions
          
          15 
        
  ...platforms/github/testdata/review_effective_approvals/reapprove_after_changes/reviews.yaml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| description: restore approval when a new approval is submitted after requested changes | ||
| pr_sender: author | ||
| reviews: | ||
|  | ||
| - user: reviewer-b | ||
| state: APPROVED | ||
| submitted_at: 2025-10-24T08:00:00Z | ||
|  | ||
| - user: reviewer-b | ||
| state: CHANGES_REQUESTED | ||
| submitted_at: 2025-10-24T08:10:00Z | ||
|  | ||
| - user: reviewer-b | ||
| state: APPROVED | ||
| submitted_at: 2025-10-24T08:20:00Z | 
        
          
          
            4 changes: 4 additions & 0 deletions
          
          4 
        
  ...g/platforms/github/testdata/review_effective_approvals/retain_after_comment/expected.yaml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| description: approver should remain valid after a plain comment | ||
| approved_users: | ||
|  | ||
| - kycheng | 
        
          
          
            11 changes: 11 additions & 0 deletions
          
          11 
        
  ...kg/platforms/github/testdata/review_effective_approvals/retain_after_comment/reviews.yaml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| description: retain approval when only a comment follows the approval | ||
| pr_sender: author | ||
| reviews: | ||
|  | ||
| - user: kycheng | ||
| state: APPROVED | ||
| submitted_at: 2025-10-24T09:16:07Z | ||
|  | ||
| - user: kycheng | ||
| state: COMMENTED | ||
| submitted_at: 2025-10-24T09:20:38Z | 
        
          
          
            2 changes: 2 additions & 0 deletions
          
          2 
        
  ...pkg/platforms/github/testdata/review_effective_approvals/revoked_by_changes/expected.yaml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| description: no approvers remain after changes are requested | ||
| approved_users: [] | 
        
          
          
            15 changes: 15 additions & 0 deletions
          
          15 
        
  .../pkg/platforms/github/testdata/review_effective_approvals/revoked_by_changes/reviews.yaml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| description: remove approval when changes are requested after the approval | ||
| pr_sender: author | ||
| reviews: | ||
|  | ||
| - user: reviewer-a | ||
| state: APPROVED | ||
| submitted_at: 2025-10-24T09:00:00Z | ||
|  | ||
| - user: reviewer-a | ||
| state: COMMENTED | ||
| submitted_at: 2025-10-24T09:05:00Z | ||
|  | ||
| - user: reviewer-a | ||
| state: CHANGES_REQUESTED | ||
| submitted_at: 2025-10-24T09:10:00Z | 
        
          
          
            2 changes: 2 additions & 0 deletions
          
          2 
        
  ...g/platforms/github/testdata/review_effective_approvals/revoked_by_dismissed/expected.yaml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| description: the dismissal should remove the approver | ||
| approved_users: [] | 
        
          
          
            11 changes: 11 additions & 0 deletions
          
          11 
        
  ...kg/platforms/github/testdata/review_effective_approvals/revoked_by_dismissed/reviews.yaml
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| description: remove approval when it is dismissed after submission | ||
| pr_sender: author | ||
| reviews: | ||
|  | ||
| - user: reviewer-c | ||
| state: APPROVED | ||
| submitted_at: 2025-10-24T07:00:00Z | ||
|  | ||
| - user: reviewer-c | ||
| state: DISMISSED | ||
| submitted_at: 2025-10-24T07:05:00Z | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
之前就是这里的问题,kycheng 在第一个 APPROVED 后,有发送了一个 COMMENTED 消息,导致了误判。