2020-01-01 13:55:28 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Gitlab::LegacyGithubImport::PullRequestFormatter do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:client) { double }
|
|
|
|
let(:project) { create(:project, :repository) }
|
2016-08-24 12:49:21 +05:30
|
|
|
let(:source_sha) { create(:commit, project: project).id }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:target_commit) { create(:commit, project: project, git_commit: RepoHelpers.another_sample_commit) }
|
|
|
|
let(:target_sha) { target_commit.id }
|
|
|
|
let(:target_short_sha) { target_commit.id.to_s[0..7] }
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:repository) { double(id: 1, fork: false) }
|
2016-01-29 22:53:50 +05:30
|
|
|
let(:source_repo) { repository }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:source_branch) { double(ref: 'branch-merged', repo: source_repo, sha: source_sha) }
|
|
|
|
let(:forked_source_repo) { double(id: 2, fork: true, name: 'otherproject', full_name: 'company/otherproject') }
|
2016-01-29 22:53:50 +05:30
|
|
|
let(:target_repo) { repository }
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:target_branch) { double(ref: 'master', repo: target_repo, sha: target_sha, user: octocat) }
|
|
|
|
let(:removed_branch) { double(ref: 'removed-branch', repo: source_repo, sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b', user: octocat) }
|
|
|
|
let(:forked_branch) { double(ref: 'master', repo: forked_source_repo, sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b', user: octocat) }
|
|
|
|
let(:branch_deleted_repo) { double(ref: 'master', repo: nil, sha: '2e5d3239642f9161dcbbc4b70a211a68e5e45e2b', user: octocat) }
|
|
|
|
let(:octocat) { double(id: 123456, login: 'octocat', email: 'octocat@example.com') }
|
2016-01-14 18:37:52 +05:30
|
|
|
let(:created_at) { DateTime.strptime('2011-01-26T19:01:12Z') }
|
|
|
|
let(:updated_at) { DateTime.strptime('2011-01-27T19:01:12Z') }
|
|
|
|
let(:base_data) do
|
|
|
|
{
|
|
|
|
number: 1347,
|
2016-06-02 11:05:42 +05:30
|
|
|
milestone: nil,
|
2016-01-14 18:37:52 +05:30
|
|
|
state: 'open',
|
|
|
|
title: 'New feature',
|
|
|
|
body: 'Please pull these awesome changes',
|
|
|
|
head: source_branch,
|
|
|
|
base: target_branch,
|
|
|
|
assignee: nil,
|
|
|
|
user: octocat,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at,
|
|
|
|
closed_at: nil,
|
2016-09-13 17:45:13 +05:30
|
|
|
merged_at: nil,
|
|
|
|
url: 'https://api.github.com/repos/octocat/Hello-World/pulls/1347'
|
2016-01-14 18:37:52 +05:30
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
subject(:pull_request) { described_class.new(project, raw_data, client) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
before do
|
|
|
|
allow(client).to receive(:user).and_return(octocat)
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
shared_examples 'Gitlab::LegacyGithubImport::PullRequestFormatter#attributes' do
|
2016-01-14 18:37:52 +05:30
|
|
|
context 'when pull request is open' do
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:raw_data) { double(base_data.merge(state: 'open')) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
it 'returns formatted attributes' do
|
|
|
|
expected = {
|
2016-06-02 11:05:42 +05:30
|
|
|
iid: 1347,
|
2016-01-14 18:37:52 +05:30
|
|
|
title: 'New feature',
|
|
|
|
description: "*Created by: octocat*\n\nPlease pull these awesome changes",
|
|
|
|
source_project: project,
|
2017-08-17 22:00:37 +05:30
|
|
|
source_branch: 'branch-merged',
|
2016-08-24 12:49:21 +05:30
|
|
|
source_branch_sha: source_sha,
|
2016-01-14 18:37:52 +05:30
|
|
|
target_project: project,
|
|
|
|
target_branch: 'master',
|
2016-08-24 12:49:21 +05:30
|
|
|
target_branch_sha: target_sha,
|
2016-01-14 18:37:52 +05:30
|
|
|
state: 'opened',
|
2016-06-02 11:05:42 +05:30
|
|
|
milestone: nil,
|
2016-01-14 18:37:52 +05:30
|
|
|
author_id: project.creator_id,
|
|
|
|
assignee_id: nil,
|
|
|
|
created_at: created_at,
|
2017-08-17 22:00:37 +05:30
|
|
|
updated_at: updated_at,
|
|
|
|
imported: true
|
2016-01-14 18:37:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
expect(pull_request.attributes).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pull request is closed' do
|
2016-09-29 09:46:39 +05:30
|
|
|
let(:raw_data) { double(base_data.merge(state: 'closed')) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
it 'returns formatted attributes' do
|
|
|
|
expected = {
|
2016-06-02 11:05:42 +05:30
|
|
|
iid: 1347,
|
2016-01-14 18:37:52 +05:30
|
|
|
title: 'New feature',
|
|
|
|
description: "*Created by: octocat*\n\nPlease pull these awesome changes",
|
|
|
|
source_project: project,
|
2017-08-17 22:00:37 +05:30
|
|
|
source_branch: 'branch-merged',
|
2016-08-24 12:49:21 +05:30
|
|
|
source_branch_sha: source_sha,
|
2016-01-14 18:37:52 +05:30
|
|
|
target_project: project,
|
|
|
|
target_branch: 'master',
|
2016-08-24 12:49:21 +05:30
|
|
|
target_branch_sha: target_sha,
|
2016-01-14 18:37:52 +05:30
|
|
|
state: 'closed',
|
2016-06-02 11:05:42 +05:30
|
|
|
milestone: nil,
|
2016-01-14 18:37:52 +05:30
|
|
|
author_id: project.creator_id,
|
|
|
|
assignee_id: nil,
|
|
|
|
created_at: created_at,
|
2017-08-17 22:00:37 +05:30
|
|
|
updated_at: updated_at,
|
|
|
|
imported: true
|
2016-01-14 18:37:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
expect(pull_request.attributes).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when pull request is merged' do
|
|
|
|
let(:merged_at) { DateTime.strptime('2011-01-28T13:01:12Z') }
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:raw_data) { double(base_data.merge(state: 'closed', merged_at: merged_at)) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
it 'returns formatted attributes' do
|
|
|
|
expected = {
|
2016-06-02 11:05:42 +05:30
|
|
|
iid: 1347,
|
2016-01-14 18:37:52 +05:30
|
|
|
title: 'New feature',
|
|
|
|
description: "*Created by: octocat*\n\nPlease pull these awesome changes",
|
|
|
|
source_project: project,
|
2017-08-17 22:00:37 +05:30
|
|
|
source_branch: 'branch-merged',
|
2016-08-24 12:49:21 +05:30
|
|
|
source_branch_sha: source_sha,
|
2016-01-14 18:37:52 +05:30
|
|
|
target_project: project,
|
|
|
|
target_branch: 'master',
|
2016-08-24 12:49:21 +05:30
|
|
|
target_branch_sha: target_sha,
|
2016-01-14 18:37:52 +05:30
|
|
|
state: 'merged',
|
2016-06-02 11:05:42 +05:30
|
|
|
milestone: nil,
|
2016-01-14 18:37:52 +05:30
|
|
|
author_id: project.creator_id,
|
|
|
|
assignee_id: nil,
|
|
|
|
created_at: created_at,
|
2017-08-17 22:00:37 +05:30
|
|
|
updated_at: updated_at,
|
|
|
|
imported: true
|
2016-01-14 18:37:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
expect(pull_request.attributes).to eq(expected)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is assigned to someone' do
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:raw_data) { double(base_data.merge(assignee: octocat)) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
it 'returns nil as assignee_id when is not a GitLab user' do
|
|
|
|
expect(pull_request.attributes.fetch(:assignee_id)).to be_nil
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns GitLab user id associated with GitHub id as assignee_id' do
|
2016-01-14 18:37:52 +05:30
|
|
|
gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')
|
|
|
|
|
|
|
|
expect(pull_request.attributes.fetch(:assignee_id)).to eq gl_user.id
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'returns GitLab user id associated with GitHub email as assignee_id' do
|
|
|
|
gl_user = create(:user, email: octocat.email)
|
|
|
|
|
|
|
|
expect(pull_request.attributes.fetch(:assignee_id)).to eq gl_user.id
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when author is a GitLab user' do
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:raw_data) { double(base_data.merge(user: octocat)) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns project creator_id as author_id when is not a GitLab user' do
|
2016-01-14 18:37:52 +05:30
|
|
|
expect(pull_request.attributes.fetch(:author_id)).to eq project.creator_id
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns GitLab user id associated with GitHub id as author_id' do
|
2016-01-14 18:37:52 +05:30
|
|
|
gl_user = create(:omniauth_user, extern_uid: octocat.id, provider: 'github')
|
|
|
|
|
|
|
|
expect(pull_request.attributes.fetch(:author_id)).to eq gl_user.id
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'returns GitLab user id associated with GitHub email as author_id' do
|
|
|
|
gl_user = create(:user, email: octocat.email)
|
|
|
|
|
|
|
|
expect(pull_request.attributes.fetch(:author_id)).to eq gl_user.id
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
it 'returns description without created at tag line' do
|
|
|
|
create(:omniauth_user, extern_uid: octocat.id, provider: 'github')
|
|
|
|
|
|
|
|
expect(pull_request.attributes.fetch(:description)).to eq('Please pull these awesome changes')
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
context 'when it has a milestone' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:milestone) { double(id: 42, number: 42) }
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:raw_data) { double(base_data.merge(milestone: milestone)) }
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
it 'returns nil when milestone does not exist' do
|
|
|
|
expect(pull_request.attributes.fetch(:milestone)).to be_nil
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
it 'returns milestone when it exists' do
|
2017-08-17 22:00:37 +05:30
|
|
|
milestone = create(:milestone, project: project, iid: 42)
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
expect(pull_request.attributes.fetch(:milestone)).to eq milestone
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
shared_examples 'Gitlab::LegacyGithubImport::PullRequestFormatter#number' do
|
2017-08-17 22:00:37 +05:30
|
|
|
let(:raw_data) { double(base_data) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
it 'returns pull request number' do
|
|
|
|
expect(pull_request.number).to eq 1347
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
shared_examples 'Gitlab::LegacyGithubImport::PullRequestFormatter#source_branch_name' do
|
2016-09-13 17:45:13 +05:30
|
|
|
context 'when source branch exists' do
|
|
|
|
let(:raw_data) { double(base_data) }
|
|
|
|
|
|
|
|
it 'returns branch ref' do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(pull_request.source_branch_name).to eq 'branch-merged'
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source branch does not exist' do
|
|
|
|
let(:raw_data) { double(base_data.merge(head: removed_branch)) }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'prefixes branch name with gh-:short_sha/:number/:user pattern to avoid collision' do
|
|
|
|
expect(pull_request.source_branch_name).to eq "gh-#{target_short_sha}/1347/octocat/removed-branch"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source branch is from a fork' do
|
|
|
|
let(:raw_data) { double(base_data.merge(head: forked_branch)) }
|
|
|
|
|
|
|
|
it 'prefixes branch name with gh-:short_sha/:number/:user pattern to avoid collision' do
|
|
|
|
expect(pull_request.source_branch_name).to eq "gh-#{target_short_sha}/1347/octocat/master"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source branch is from a deleted fork' do
|
|
|
|
let(:raw_data) { double(base_data.merge(head: branch_deleted_repo)) }
|
|
|
|
|
|
|
|
it 'prefixes branch name with gh-:short_sha/:number/:user pattern to avoid collision' do
|
|
|
|
expect(pull_request.source_branch_name).to eq "gh-#{target_short_sha}/1347/octocat/master"
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
shared_examples 'Gitlab::LegacyGithubImport::PullRequestFormatter#target_branch_name' do
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when target branch exists' do
|
2016-09-13 17:45:13 +05:30
|
|
|
let(:raw_data) { double(base_data) }
|
|
|
|
|
|
|
|
it 'returns branch ref' do
|
|
|
|
expect(pull_request.target_branch_name).to eq 'master'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when target branch does not exist' do
|
|
|
|
let(:raw_data) { double(base_data.merge(base: removed_branch)) }
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
it 'prefixes branch name with gh-:short_sha/:number/:user pattern to avoid collision' do
|
|
|
|
expect(pull_request.target_branch_name).to eq 'gl-2e5d3239/1347/octocat/removed-branch'
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
context 'when importing a GitHub project' do
|
2018-03-17 18:26:18 +05:30
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::PullRequestFormatter#attributes'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::PullRequestFormatter#number'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::PullRequestFormatter#source_branch_name'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::PullRequestFormatter#target_branch_name'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when importing a Gitea project' do
|
|
|
|
before do
|
2021-04-29 21:17:54 +05:30
|
|
|
project.update!(import_type: 'gitea')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::PullRequestFormatter#attributes'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::PullRequestFormatter#number'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::PullRequestFormatter#source_branch_name'
|
|
|
|
it_behaves_like 'Gitlab::LegacyGithubImport::PullRequestFormatter#target_branch_name'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
2016-01-14 18:37:52 +05:30
|
|
|
describe '#valid?' do
|
2016-06-02 11:05:42 +05:30
|
|
|
context 'when source, and target repos are not a fork' do
|
|
|
|
let(:raw_data) { double(base_data) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(pull_request.valid?).to eq true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
context 'when source repo is a fork' do
|
|
|
|
let(:source_repo) { double(id: 2) }
|
|
|
|
let(:raw_data) { double(base_data) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it 'returns true' do
|
|
|
|
expect(pull_request.valid?).to eq true
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
context 'when target repo is a fork' do
|
|
|
|
let(:target_repo) { double(id: 2) }
|
|
|
|
let(:raw_data) { double(base_data) }
|
2016-01-14 18:37:52 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
it 'returns true' do
|
|
|
|
expect(pull_request.valid?).to eq true
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#cross_project?' do
|
|
|
|
context 'when source and target repositories are different' do
|
|
|
|
let(:raw_data) { double(base_data.merge(head: forked_branch)) }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(pull_request.cross_project?).to eq true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source repository does not exist anymore' do
|
|
|
|
let(:raw_data) { double(base_data.merge(head: branch_deleted_repo)) }
|
|
|
|
|
|
|
|
it 'returns true' do
|
|
|
|
expect(pull_request.cross_project?).to eq true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when source and target repositories are the same' do
|
|
|
|
let(:raw_data) { double(base_data.merge(head: source_branch)) }
|
|
|
|
|
|
|
|
it 'returns false' do
|
|
|
|
expect(pull_request.cross_project?).to eq false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#source_branch_exists?' do
|
|
|
|
let(:raw_data) { double(base_data.merge(head: forked_branch)) }
|
|
|
|
|
|
|
|
it 'returns false when is a cross_project' do
|
|
|
|
expect(pull_request.source_branch_exists?).to eq false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
describe '#url' do
|
|
|
|
let(:raw_data) { double(base_data) }
|
|
|
|
|
|
|
|
it 'return raw url' do
|
|
|
|
expect(pull_request.url).to eq 'https://api.github.com/repos/octocat/Hello-World/pulls/1347'
|
|
|
|
end
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
describe '#opened?' do
|
|
|
|
let(:raw_data) { double(base_data.merge(state: 'open')) }
|
|
|
|
|
|
|
|
it 'returns true when state is "open"' do
|
|
|
|
expect(pull_request.opened?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2016-01-14 18:37:52 +05:30
|
|
|
end
|