Skip to content

Commit

Permalink
Parse workflow's branch filters as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardoj committed Feb 24, 2025
1 parent 9e23ea6 commit fa3e2b0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/api/app/models/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,23 @@ def event_matches_event_filter?
def branch_matches_branches_filter?
return true unless supported_filters.key?(:branches)

branches_only = filters[:branches].fetch(:only, [])
branches_ignore = filters[:branches].fetch(:ignore, [])
return true if branch_matches_branches_only_filter?

return true if branches_only.present? && branches_only.include?(workflow_run.target_branch)
return true if branches_ignore.present? && branches_ignore.exclude?(workflow_run.target_branch)
return true if branch_matches_branches_ignore_filter?

false
end

def branch_matches_branches_only_filter?
branches_only = filters[:branches].fetch(:only, []).map(&:to_s)
branches_only.present? && branches_only.include?(workflow_run.target_branch)
end

def branch_matches_branches_ignore_filter?
branches_ignore = filters[:branches].fetch(:ignore, []).map(&:to_s)
branches_ignore.present? && branches_ignore.exclude?(workflow_run.target_branch)
end

# rubocop:disable Metrics/CyclomaticComplexity
# Execute only if labeled or unlabeled
def label_matches_labels_filter?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"number": 1,
"html_url": "http://github.com/something",
"base": {
"ref": "master",
"ref": "16.0",
"repo": {
"full_name": "openSUSE/repo123"
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/spec/models/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
context 'when the webhook event is against one of the branches in the branches/only filters' do
let(:yaml) do
{ 'steps' => [{ 'branch_package' => { 'source_project' => 'test-project', 'source_package' => 'test-package' } }],
'filters' => { 'branches' => { 'only' => %w[master develop] } } }
'filters' => { 'branches' => { 'only' => [16.0, 'develop'] } } }
end
let(:request_payload) { file_fixture('request_payload_github_pull_request_opened.json').read }
let!(:workflow_run) { create(:workflow_run, scm_vendor: 'github', hook_event: 'pull_request', hook_action: 'opened', token: token, request_payload: request_payload) }
Expand Down

0 comments on commit fa3e2b0

Please sign in to comment.