Skip to content
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

Parse workflow's branch filters as strings #17427

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/api/app/models/workflow.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Workflow
class Workflow # rubocop:disable Metrics/ClassLength
include ActiveModel::Model
include WorkflowInstrumentation # for run_callbacks
include WorkflowVersionMatcher
Expand Down 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
@@ -0,0 +1,27 @@
{
"action": "opened",
"number": 1,
"pull_request": {
"number": 1,
"html_url": "http://github.com/something",
"base": {
"ref": "16.0",
"repo": {
"full_name": "openSUSE/repo123"
}
},
"head": {
"sha": "123456789"
}
},
"repository": {
"name": "hello_world",
"html_url": "https://github.com",
"owner": {
"login": "iggy"
}
},
"sender": {
"url": "https://api.github.com"
}
}
13 changes: 13 additions & 0 deletions src/api/spec/models/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@
subject.call
expect(subject.steps.first).to have_received(:call)
end

context 'the branches filter contains a number' do
let(:yaml) do
{ 'steps' => [{ 'branch_package' => { 'source_project' => 'test-project', 'source_package' => 'test-package' } }],
'filters' => { 'branches' => { 'only' => [16.0, 'develop'] } } }
end
let(:request_payload) { file_fixture('request_payload_github_pull_request_opened_branch_number.json').read }

it 'the workflow runs' do
subject.call
expect(subject.steps.first).to have_received(:call)
end
end
end

context 'when the webhook event is not supported by the branches filter' do
Expand Down