- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 6.2k
 
Add GitHub API compatibility for workflow runs filtering #34894
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
          
     Open
      
      
            bdruth
  wants to merge
  10
  commits into
  go-gitea:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
bdruth:feature/enhanced-workflow-runs-api
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +563
        
        
          −17
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      18546ed
              
                Add GitHub API compatibility for workflow runs filtering
              
              
                bdruth aeaa2c1
              
                Update Swagger documentation for workflow runs API
              
              
                bdruth 722ac05
              
                run `make fmt`
              
              
                bdruth 695496c
              
                Apply Go modernization fixes - use strings.CutPrefix instead of HasPr…
              
              
                bdruth 1b9b410
              
                Remove non-functional CheckSuiteID parameter support
              
              
                bdruth 0a17b7a
              
                Merge branch 'main' into feature/enhanced-workflow-runs-api
              
              
                bdruth ac87911
              
                Merge branch 'main' into feature/enhanced-workflow-runs-api
              
              
                bdruth 6b074cb
              
                refactor: extract buildRunOptions for better test coverage
              
              
                bdruth 3e0c740
              
                Merge branch 'feature/enhanced-workflow-runs-api' of github.com:bdrut…
              
              
                bdruth aff4e64
              
                Merge branch 'main' into feature/enhanced-workflow-runs-api
              
              
                bdruth 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| 
          
            
          
           | 
    @@ -5,6 +5,7 @@ package actions | |
| 
     | 
||
| import ( | ||
| "context" | ||
| "time" | ||
| 
     | 
||
| "code.gitea.io/gitea/models/db" | ||
| repo_model "code.gitea.io/gitea/models/repo" | ||
| 
          
            
          
           | 
    @@ -64,15 +65,18 @@ func (runs RunList) LoadRepos(ctx context.Context) error { | |
| 
     | 
||
| type FindRunOptions struct { | ||
| db.ListOptions | ||
| RepoID int64 | ||
| OwnerID int64 | ||
| WorkflowID string | ||
| Ref string // the commit/tag/… that caused this workflow | ||
| TriggerUserID int64 | ||
| TriggerEvent webhook_module.HookEventType | ||
| Approved bool // not util.OptionalBool, it works only when it's true | ||
| Status []Status | ||
| CommitSHA string | ||
| RepoID int64 | ||
| OwnerID int64 | ||
| WorkflowID string | ||
| Ref string // the commit/tag/... that caused this workflow | ||
| TriggerUserID int64 | ||
| TriggerEvent webhook_module.HookEventType | ||
| Approved bool // not util.OptionalBool, it works only when it's true | ||
| Status []Status | ||
| CommitSHA string | ||
| CreatedAfter time.Time | ||
| CreatedBefore time.Time | ||
| ExcludePullRequests bool | ||
| } | ||
| 
     | 
||
| func (opts FindRunOptions) ToConds() builder.Cond { | ||
| 
          
            
          
           | 
    @@ -101,6 +105,15 @@ func (opts FindRunOptions) ToConds() builder.Cond { | |
| if opts.CommitSHA != "" { | ||
| cond = cond.And(builder.Eq{"`action_run`.commit_sha": opts.CommitSHA}) | ||
| } | ||
| if !opts.CreatedAfter.IsZero() { | ||
| cond = cond.And(builder.Gte{"`action_run`.created": opts.CreatedAfter}) | ||
| } | ||
| if !opts.CreatedBefore.IsZero() { | ||
| cond = cond.And(builder.Lte{"`action_run`.created": opts.CreatedBefore}) | ||
| 
         There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same as above.  | 
||
| } | ||
| if opts.ExcludePullRequests { | ||
| cond = cond.And(builder.Neq{"`action_run`.trigger_event": webhook_module.HookEventPullRequest}) | ||
| } | ||
| return cond | ||
| } | ||
| 
     | 
||
| 
          
            
          
           | 
    ||
  
    
      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,88 @@ | ||
| // Copyright 2025 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
| 
     | 
||
| package actions | ||
| 
     | 
||
| import ( | ||
| "testing" | ||
| "time" | ||
| 
     | 
||
| "code.gitea.io/gitea/modules/webhook" | ||
| 
     | 
||
| "github.com/stretchr/testify/assert" | ||
| "xorm.io/builder" | ||
| ) | ||
| 
     | 
||
| func TestFindRunOptions_ToConds_ExcludePullRequests(t *testing.T) { | ||
| // Test when ExcludePullRequests is true | ||
| opts := FindRunOptions{ | ||
| ExcludePullRequests: true, | ||
| } | ||
| cond := opts.ToConds() | ||
| 
     | 
||
| // Convert the condition to SQL for assertion | ||
| sql, args, err := builder.ToSQL(cond) | ||
| assert.NoError(t, err) | ||
| // The condition should contain the trigger_event not equal to pull_request | ||
| assert.Contains(t, sql, "`action_run`.trigger_event<>") | ||
| assert.Contains(t, args, webhook.HookEventPullRequest) | ||
| } | ||
| 
     | 
||
| func TestFindRunOptions_ToConds_CreatedDateRange(t *testing.T) { | ||
| // Test when CreatedAfter and CreatedBefore are set | ||
| startDate := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) | ||
| endDate := time.Date(2023, 12, 31, 23, 59, 59, 0, time.UTC) | ||
| 
     | 
||
| opts := FindRunOptions{ | ||
| CreatedAfter: startDate, | ||
| CreatedBefore: endDate, | ||
| } | ||
| cond := opts.ToConds() | ||
| 
     | 
||
| // Convert the condition to SQL for assertion | ||
| sql, args, err := builder.ToSQL(cond) | ||
| assert.NoError(t, err) | ||
| // The condition should contain created >= startDate and created <= endDate | ||
| assert.Contains(t, sql, "`action_run`.created>=") | ||
| assert.Contains(t, sql, "`action_run`.created<=") | ||
| assert.Contains(t, args, startDate) | ||
| assert.Contains(t, args, endDate) | ||
| } | ||
| 
     | 
||
| func TestFindRunOptions_ToConds_CreatedAfterOnly(t *testing.T) { | ||
| // Test when only CreatedAfter is set | ||
| startDate := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC) | ||
| 
     | 
||
| opts := FindRunOptions{ | ||
| CreatedAfter: startDate, | ||
| } | ||
| cond := opts.ToConds() | ||
| 
     | 
||
| // Convert the condition to SQL for assertion | ||
| sql, args, err := builder.ToSQL(cond) | ||
| assert.NoError(t, err) | ||
| // The condition should contain created >= startDate | ||
| assert.Contains(t, sql, "`action_run`.created>=") | ||
| assert.Contains(t, args, startDate) | ||
| // But should not contain created <= endDate | ||
| assert.NotContains(t, sql, "`action_run`.created<=") | ||
| } | ||
| 
     | 
||
| func TestFindRunOptions_ToConds_CreatedBeforeOnly(t *testing.T) { | ||
| // Test when only CreatedBefore is set | ||
| endDate := time.Date(2023, 12, 31, 23, 59, 59, 0, time.UTC) | ||
| 
     | 
||
| opts := FindRunOptions{ | ||
| CreatedBefore: endDate, | ||
| } | ||
| cond := opts.ToConds() | ||
| 
     | 
||
| // Convert the condition to SQL for assertion | ||
| sql, args, err := builder.ToSQL(cond) | ||
| assert.NoError(t, err) | ||
| // The condition should contain created <= endDate | ||
| assert.Contains(t, sql, "`action_run`.created<=") | ||
| assert.Contains(t, args, endDate) | ||
| // But should not contain created >= startDate | ||
| assert.NotContains(t, sql, "`action_run`.created>=") | ||
| } | 
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  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.
I don't think this will work. It should be
opts.CreatedAfter.Unix().