Commit 9ace872d authored by Fabio Pitino's avatar Fabio Pitino

Merge branch 'alberts-add-variable-ci_merge_request_approved' into 'master'

Add CI variable CI_MERGE_REQUEST_APPROVED [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!64677
parents 1ff79116 dff9601f
......@@ -130,6 +130,7 @@ These variables are available when:
| Variable | GitLab | Runner | Description |
|----------------------------------------|--------|--------|-------------|
| `CI_MERGE_REQUEST_APPROVED` | 14.1 | all | Approval status of the merge request. `true` when [merge request approvals](../../user/project/merge_requests/approvals/index.md) is available and the merge request has been approved. |
| `CI_MERGE_REQUEST_ASSIGNEES` | 11.9 | all | Comma-separated list of usernames of assignees for the merge request. |
| `CI_MERGE_REQUEST_ID` | 11.6 | all | The instance-level ID of the merge request. This is a unique ID across all projects on GitLab. |
| `CI_MERGE_REQUEST_IID` | 11.6 | all | The project-level IID (internal ID) of the merge request. This ID is unique for the current project. |
......
......@@ -110,6 +110,11 @@ module EE
end
end
override :predefined_variables
def predefined_variables
super.concat(merge_request_approval_variables)
end
override :mergeable?
def mergeable?(skip_ci_check: false, skip_discussions_check: false)
return false unless approved?
......@@ -298,5 +303,15 @@ module EE
ApprovalWrappedRule.wrap(self, rule).approved?
end
end
def merge_request_approval_variables
return unless approval_feature_available?
strong_memoize(:merge_request_approval_variables) do
::Gitlab::Ci::Variables::Collection.new.tap do |variables|
variables.append(key: 'CI_MERGE_REQUEST_APPROVED', value: approved?.to_s) if approved?
end
end
end
end
end
......@@ -1062,6 +1062,49 @@ RSpec.describe MergeRequest do
end
end
describe '#predefined_variables' do
context 'when merge request has approver feature' do
before do
stub_licensed_features(merge_request_approvers: true)
end
context 'without any rules' do
it 'includes variable CI_MERGE_REQUEST_APPROVED=true' do
expect(merge_request.predefined_variables.to_hash).to include('CI_MERGE_REQUEST_APPROVED' => 'true')
end
end
context 'with a rule' do
let(:approver) { create(:user) }
let!(:rule) { create(:approval_merge_request_rule, merge_request: merge_request, approvals_required: 1, users: [approver]) }
context 'that has been approved' do
it 'includes variable CI_MERGE_REQUEST_APPROVED=true' do
create(:approval, merge_request: merge_request, user: approver)
expect(merge_request.predefined_variables.to_hash).to include('CI_MERGE_REQUEST_APPROVED' => 'true')
end
end
context 'that has not been approved' do
it 'does not include variable CI_MERGE_REQUEST_APPROVED' do
expect(merge_request.predefined_variables.to_hash.keys).not_to include('CI_MERGE_REQUEST_APPROVED')
end
end
end
end
context 'when merge request does not have approver feature' do
before do
stub_licensed_features(merge_request_approvers: false)
end
it 'does not include variable CI_MERGE_REQUEST_APPROVED' do
expect(merge_request.predefined_variables.to_hash.keys).not_to include('CI_MERGE_REQUEST_APPROVED')
end
end
end
describe '#mergeable?' do
subject { merge_request.mergeable? }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment