Skip to content

Commit

Permalink
Support closed and merged pull/merge requests in OBS workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Dany Marcoux committed Sep 7, 2021
1 parent 52c4ae9 commit 00f8301
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/api/app/models/scm_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def updated_pull_request?
(gitlab_merge_request? && @payload[:action] == 'update')
end

def closed_merged_pull_request?
(github_pull_request? && @payload[:action] == 'closed') ||
(gitlab_merge_request? && ['close', 'merge'].include?(@payload[:action]))
end

private

def github_pull_request?
Expand Down
4 changes: 2 additions & 2 deletions src/api/app/validators/scm_webhook_event_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class ScmWebhookEventValidator < ActiveModel::Validator
ALLOWED_GITHUB_EVENTS = ['pull_request'].freeze
ALLOWED_GITLAB_EVENTS = ['Merge Request Hook'].freeze

ALLOWED_PULL_REQUEST_ACTIONS = ['opened', 'synchronize'].freeze
ALLOWED_MERGE_REQUEST_ACTIONS = ['open', 'update'].freeze
ALLOWED_PULL_REQUEST_ACTIONS = ['closed', 'opened', 'synchronize'].freeze
ALLOWED_MERGE_REQUEST_ACTIONS = ['close', 'merge', 'open', 'update'].freeze

def validate(record)
@record = record
Expand Down
52 changes: 52 additions & 0 deletions src/api/spec/models/scm_webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,56 @@
it { is_expected.to be true }
end
end

describe '#closed_merged_pull_request?' do
subject { described_class.new(payload: payload).closed_merged_pull_request? }

context 'for an unsupported SCM' do
let(:payload) { { scm: 'GitHoob', event: 'pull_request', action: 'closed' } }

it { is_expected.to be false }
end

context 'for an unsupported event from GitHub' do
let(:payload) { { scm: 'github', event: 'something', action: 'closed' } }

it { is_expected.to be false }
end

context 'for an unsupported action from GitHub' do
let(:payload) { { scm: 'github', event: 'pull_request', action: 'something' } }

it { is_expected.to be false }
end

context 'for a closed/merged pull request from GitHub' do
let(:payload) { { scm: 'github', event: 'pull_request', action: 'closed' } }

it { is_expected.to be true }
end

context 'for an unsupported event from GitLab' do
let(:payload) { { scm: 'gitlab', event: 'something', action: 'close' } }

it { is_expected.to be false }
end

context 'for an unsupported action from GitLab' do
let(:payload) { { scm: 'gitlab', event: 'Merge Request Hook', action: 'something' } }

it { is_expected.to be false }
end

context 'for a closed merge request from GitLab' do
let(:payload) { { scm: 'gitlab', event: 'Merge Request Hook', action: 'close' } }

it { is_expected.to be true }
end

context 'for a merged merge request from GitLab' do
let(:payload) { { scm: 'gitlab', event: 'Merge Request Hook', action: 'merge' } }

it { is_expected.to be true }
end
end
end
21 changes: 21 additions & 0 deletions src/api/spec/validators/scm_webhook_event_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@

it { is_expected.to be_valid }
end

context 'for a pull request which was closed/merged' do
let(:event) { 'pull_request' }
let(:action) { 'closed' }

it { is_expected.to be_valid }
end
end

context 'when the SCM is GitLab' do
Expand Down Expand Up @@ -105,6 +112,20 @@

it { is_expected.to be_valid }
end

context 'for a merge request which was closed' do
let(:event) { 'Merge Request Hook' }
let(:action) { 'close' }

it { is_expected.to be_valid }
end

context 'for a merge request which was merged' do
let(:event) { 'Merge Request Hook' }
let(:action) { 'merge' }

it { is_expected.to be_valid }
end
end
end
end

0 comments on commit 00f8301

Please sign in to comment.