Commit 11f878fb authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch '346609-correlates-jh-branch' into 'master'

Resolve "Correlates the JH branch for `add-jh-folder` job"

See merge request gitlab-org/gitlab!75667
parents 3a876c09 ba6a4137
......@@ -151,12 +151,15 @@ detect-previous-failed-tests:
add-jh-folder:
extends: .setup:rules:add-jh-folder
image: ${GITLAB_DEPENDENCY_PROXY}alpine:edge
image: ${GITLAB_DEPENDENCY_PROXY}ruby:2.7
stage: prepare
before_script:
- apk add --no-cache --update curl bash
- source ./scripts/utils.sh
- install_gitlab_gem
script:
- curl --location -o "jh-folder.tar.gz" "https://gitlab.com/gitlab-jh/gitlab/-/archive/main-jh/gitlab-main-jh.tar.gz?path=jh"
- JH_BRANCH=$(./scripts/setup/find-jh-branch.rb)
- 'echo "JH_BRANCH: ${JH_BRANCH}"'
- curl --location -o "jh-folder.tar.gz" "https://gitlab.com/gitlab-jh/gitlab/-/archive/${JH_BRANCH}/gitlab-${JH_BRANCH}.tar.gz?path=jh"
- tar -xf "jh-folder.tar.gz"
- mv gitlab-main-jh-jh/jh/ ./
- cp Gemfile.lock jh/
......
......@@ -222,6 +222,13 @@ The `* as-if-jh` jobs are run in addition to the regular EE-context jobs. The `j
The intent is to ensure that a change doesn't introduce a failure after the `gitlab-org/gitlab` project is synced to
the `gitlab-jh/gitlab` project.
### Corresponding JH branch
You can create a corresponding JH branch on the `gitlab-jh/gitlab` project by
appending `-jh` to the branch name. If a corresponding JH branch is found,
`* as-if-jh` jobs grab the `jh` folder from the respective branch,
rather than from the default branch.
## `undercover` RSpec test
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/74859) in GitLab 14.6.
......
#!/usr/bin/env ruby
# frozen_string_literal: true
# In spec/scripts/setup/find_jh_branch_spec.rb we completely stub it
require 'gitlab' unless Object.const_defined?(:Gitlab)
require_relative '../api/default_options'
class FindJhBranch
JH_DEFAULT_BRANCH = 'main-jh'
JH_PROJECT_PATH = 'gitlab-jh/gitlab'
BranchNotFound = Class.new(RuntimeError)
def run
return JH_DEFAULT_BRANCH unless merge_request?
jh_merge_request_ref_name ||
default_branch_merge_request_ref_name ||
stable_branch_merge_request_ref_name ||
default_branch_for_non_stable
end
private
def merge_request?
!!merge_request_id
end
def jh_merge_request_ref_name
branch_exist?(JH_PROJECT_PATH, jh_ref_name) && jh_ref_name
end
def default_branch_merge_request_ref_name
target_default_branch? && JH_DEFAULT_BRANCH
end
def stable_branch_merge_request_ref_name
target_stable_branch? && begin
jh_stable_branch_name = merge_request.target_branch.sub(/\-ee\z/, '-jh')
branch_exist?(JH_PROJECT_PATH, jh_stable_branch_name) &&
jh_stable_branch_name
end
end
def default_branch_for_non_stable
if target_stable_branch?
raise(BranchNotFound, "Cannot find a suitable JH branch")
else
JH_DEFAULT_BRANCH
end
end
def branch_exist?(project_path, branch_name)
!!gitlab.branch(project_path, branch_name)
rescue Gitlab::Error::NotFound
false
end
def target_default_branch?
merge_request.target_branch == default_branch
end
def target_stable_branch?
merge_request.target_branch.match?(/\A(?:\d+\-)+\d+\-stable\-ee\z/)
end
def ref_name
ENV['CI_COMMIT_REF_NAME']
end
def default_branch
ENV['CI_DEFAULT_BRANCH']
end
def merge_request_id
ENV['CI_MERGE_REQUEST_IID']
end
def jh_ref_name
"#{ref_name}-jh"
end
def project_id
API::DEFAULT_OPTIONS[:project]
end
def merge_request
@merge_request ||= gitlab.merge_request(project_id, merge_request_id)
end
def gitlab
@gitlab ||= Gitlab.client(
endpoint: API::DEFAULT_OPTIONS[:endpoint],
private_token: API::DEFAULT_OPTIONS[:api_token] || ''
)
end
end
if $0 == __FILE__
puts FindJhBranch.new.run
end
# frozen_string_literal: true
require 'fast_spec_helper'
# NOTE: Under the context of fast_spec_helper, when we `require 'gitlab'`
# we do not load the Gitlab client, but our own Gitlab module.
# Keep this in mind and just stub anything which might touch it!
require_relative '../../../scripts/setup/find-jh-branch'
RSpec.describe FindJhBranch do
subject { described_class.new }
describe '#run' do
context 'when it is not a merge request' do
before do
expect(subject).to receive(:merge_request?).and_return(false)
end
it 'returns JH_DEFAULT_BRANCH' do
expect(subject.run).to eq(described_class::JH_DEFAULT_BRANCH)
end
end
context 'when it is a merge request' do
let(:branch_name) { 'branch-name' }
let(:jh_branch_name) { 'branch-name-jh' }
let(:default_branch) { 'main' }
let(:merge_request) { double(target_branch: target_branch) }
let(:target_branch) { default_branch }
before do
expect(subject).to receive(:merge_request?).and_return(true)
expect(subject)
.to receive(:branch_exist?)
.with(described_class::JH_PROJECT_PATH, jh_branch_name)
.and_return(jh_branch_exist)
allow(subject).to receive(:ref_name).and_return(branch_name)
allow(subject).to receive(:default_branch).and_return(default_branch)
allow(subject).to receive(:merge_request).and_return(merge_request)
end
context 'when there is a corresponding JH branch' do
let(:jh_branch_exist) { true }
it 'returns the corresponding JH branch name' do
expect(subject.run).to eq(jh_branch_name)
end
end
context 'when there is no corresponding JH branch' do
let(:jh_branch_exist) { false }
it 'returns the default JH branch' do
expect(subject.run).to eq(described_class::JH_DEFAULT_BRANCH)
end
context 'when it is targeting a default branch' do
let(:target_branch) { '14-6-stable-ee' }
let(:jh_stable_branch_name) { '14-6-stable-jh' }
before do
expect(subject)
.to receive(:branch_exist?)
.with(described_class::JH_PROJECT_PATH, jh_stable_branch_name)
.and_return(jh_stable_branch_exist)
end
context 'when there is a corresponding JH stable branch' do
let(:jh_stable_branch_exist) { true }
it 'returns the corresponding JH stable branch' do
expect(subject.run).to eq(jh_stable_branch_name)
end
end
context 'when there is no corresponding JH stable branch' do
let(:jh_stable_branch_exist) { false }
it "raises #{described_class::BranchNotFound}" do
expect { subject.run }.to raise_error(described_class::BranchNotFound)
end
end
end
context 'when it is not targeting the default branch' do
let(:target_branch) { default_branch.swapcase }
it 'returns the default JH branch' do
expect(subject.run).to eq(described_class::JH_DEFAULT_BRANCH)
end
end
end
end
end
end
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