Skip to content

Commit

Permalink
Merge pull request #11585 from dmarcoux/scm-closed-merged-reopened-pu…
Browse files Browse the repository at this point in the history
…ll-requests

Support closed, merged and reopened pull/merge requests in OBS workflows
  • Loading branch information
krauselukas committed Sep 7, 2021
2 parents 2ed7c4e + ec7307a commit 60a7a30
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 2 deletions.
10 changes: 10 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,16 @@ 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

def reopened_pull_request?
(github_pull_request? && @payload[:action] == 'reopened') ||
(gitlab_merge_request? && @payload[:action] == 'reopen')
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', 'reopened', 'synchronize'].freeze
ALLOWED_MERGE_REQUEST_ACTIONS = ['close', 'merge', 'open', 'reopen', 'update'].freeze

def validate(record)
@record = record
Expand Down
98 changes: 98 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,102 @@
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

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

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

it { is_expected.to be false }
end

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

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 reopened pull request from GitHub' do
let(:payload) { { scm: 'github', event: 'pull_request', action: 'reopened' } }

it { is_expected.to be true }
end

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

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 reopened merge request from GitLab' do
let(:payload) { { scm: 'gitlab', event: 'Merge Request Hook', action: 'reopen' } }

it { is_expected.to be true }
end
end
end
35 changes: 35 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,20 @@

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

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

it { is_expected.to be_valid }
end
end

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

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

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

it { is_expected.to be_valid }
end
end
end
end

0 comments on commit 60a7a30

Please sign in to comment.