Commit c0e69e10 authored by Sanad Liaquat's avatar Sanad Liaquat

Merge branch 'acunskis-github-import-api' into 'master'

E2E: Github project import via API

See merge request gitlab-org/gitlab!65464
parents 02348297 29054aef
...@@ -217,7 +217,7 @@ module QA ...@@ -217,7 +217,7 @@ module QA
autoload :License, 'qa/ee/resource/license' autoload :License, 'qa/ee/resource/license'
autoload :Epic, 'qa/ee/resource/epic' autoload :Epic, 'qa/ee/resource/epic'
autoload :GroupIteration, 'qa/ee/resource/group_iteration' autoload :GroupIteration, 'qa/ee/resource/group_iteration'
autoload :ImportRepoWithCICD, 'qa/ee/resource/import_repo_with_ci_cd' autoload :ImportRepoWithCiCd, 'qa/ee/resource/import_repo_with_ci_cd'
autoload :PipelineSubscriptions, 'qa/ee/resource/pipeline_subscriptions' autoload :PipelineSubscriptions, 'qa/ee/resource/pipeline_subscriptions'
module Board module Board
......
...@@ -3,11 +3,7 @@ ...@@ -3,11 +3,7 @@
module QA module QA
module EE module EE
module Resource module Resource
class ImportRepoWithCICD < QA::Resource::ProjectImportedFromGithub class ImportRepoWithCiCd < QA::Resource::ProjectImportedFromGithub
def fabricate!
super
end
def go_to_import_page def go_to_import_page
QA::Page::Project::New.perform(&:click_cicd_for_external_repo) QA::Page::Project::New.perform(&:click_cicd_for_external_repo)
end end
......
...@@ -37,8 +37,16 @@ module QA ...@@ -37,8 +37,16 @@ module QA
choose_test_namespace(full_path) choose_test_namespace(full_path)
set_path(full_path, name) set_path(full_path, name)
import_project(full_path) import_project(full_path)
wait_for_success wait_for_success
go_to_project(name) end
# TODO: refactor to use 'go to project' button instead of generic main menu
def go_to_project(name)
Page::Main::Menu.perform(&:go_to_projects)
Page::Dashboard::Projects.perform do |dashboard|
dashboard.go_to_project(name)
end
end end
private private
...@@ -84,13 +92,6 @@ module QA ...@@ -84,13 +92,6 @@ module QA
end end
end end
def go_to_project(name)
Page::Main::Menu.perform(&:go_to_projects)
Page::Dashboard::Projects.perform do |dashboard|
dashboard.go_to_project(name)
end
end
def already_imported(full_path) def already_imported(full_path)
within_repo_path(full_path) do within_repo_path(full_path) do
has_element?(:project_path_content) && has_element?(:go_to_project_button) has_element?(:project_path_content) && has_element?(:go_to_project_button)
......
...@@ -9,21 +9,23 @@ module QA ...@@ -9,21 +9,23 @@ module QA
include Members include Members
include Visibility include Visibility
attr_accessor :repository_storage # requires admin access attr_accessor :repository_storage, # requires admin access
attr_writer :initialize_with_readme, :initialize_with_readme,
:auto_devops_enabled, :auto_devops_enabled,
:github_personal_access_token, :github_personal_access_token,
:github_repository_path :github_repository_path
attribute :id attributes :id,
attribute :name :name,
attribute :add_name_uuid :add_name_uuid,
attribute :description :description,
attribute :standalone :standalone,
attribute :runners_token :runners_token,
attribute :visibility :visibility,
attribute :template_name :template_name,
attribute :import :import,
:import_status,
:import_error
attribute :group do attribute :group do
Group.fabricate! Group.fabricate!
......
# frozen_string_literal: true # frozen_string_literal: true
require 'securerandom' require 'github_api'
module QA module QA
module Resource module Resource
class ProjectImportedFromGithub < Resource::Project class ProjectImportedFromGithub < Resource::Project
attribute :github_repo_id do
github_repository_path.split('/').yield_self do |path|
github_client.repos.get(user: path[0], repo: path[1]).id
end
end
def fabricate! def fabricate!
self.import = true self.import = true
...@@ -16,10 +22,44 @@ module QA ...@@ -16,10 +22,44 @@ module QA
end end
Page::Project::Import::Github.perform do |import_page| Page::Project::Import::Github.perform do |import_page|
import_page.add_personal_access_token(@github_personal_access_token) import_page.add_personal_access_token(github_personal_access_token)
import_page.import!(@github_repository_path, @name) import_page.import!(github_repository_path, name)
import_page.go_to_project(name)
end end
end end
def fabricate_via_api!
super
rescue ResourceURLMissingError
"#{Runtime::Scenario.gitlab_address}/#{group.full_path}/#{name}"
end
def api_post_path
'/import/github'
end
def api_post_body
{
repo_id: github_repo_id,
new_name: name,
target_namespace: group.full_path,
personal_access_token: github_personal_access_token,
ci_cd_only: false
}
end
def transform_api_resource(api_resource)
api_resource
end
private
# Github client
#
# @return [Github::Client]
def github_client
@github_client ||= Github.new(oauth_token: github_personal_access_token)
end
end end
end end
end end
# frozen_string_literal: true
module QA
RSpec.describe 'Manage', :github, :requires_admin do
describe 'Project import' do
let!(:api_client) { Runtime::API::Client.as_admin }
let!(:group) { Resource::Group.fabricate_via_api! { |resource| resource.api_client = api_client } }
let!(:user) do
Resource::User.fabricate_via_api! do |resource|
resource.api_client = api_client
resource.hard_delete_on_api_removal = true
end
end
let(:imported_project) do
Resource::ProjectImportedFromGithub.fabricate_via_api! do |project|
project.name = 'imported-project'
project.group = group
project.github_personal_access_token = Runtime::Env.github_access_token
project.github_repository_path = 'gitlab-qa-github/test-project'
project.api_client = api_client
end
end
before do
group.add_member(user, Resource::Members::AccessLevel::MAINTAINER)
end
after do
user.remove_via_api!
end
it 'imports Github repo via api', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/1858' do
imported_project # import the project
expect { imported_project.reload!.import_status }.to eventually_eq('finished').within(duration: 90)
aggregate_failures do
verify_repository_import
verify_commits_import
verify_labels_import
verify_issues_import
verify_milestones_import
verify_wikis_import
verify_merge_requests_import
end
end
def verify_repository_import
expect(imported_project.api_response).to include(
description: 'A new repo for test',
import_error: nil
)
end
def verify_commits_import
expect(imported_project.commits.length).to eq(20)
end
def verify_labels_import
labels = imported_project.labels.map { |label| label.slice(:name, :color) }
expect(labels).to include(
{ name: 'bug', color: '#d73a4a' },
{ name: 'custom new label', color: '#fc8f91' },
{ name: 'documentation', color: '#0075ca' },
{ name: 'duplicate', color: '#cfd3d7' },
{ name: 'enhancement', color: '#a2eeef' },
{ name: 'good first issue', color: '#7057ff' },
{ name: 'help wanted', color: '#008672' },
{ name: 'invalid', color: '#e4e669' },
{ name: 'question', color: '#d876e3' },
{ name: 'wontfix', color: '#ffffff' }
)
end
def verify_issues_import
issues = imported_project.issues
expect(issues.length).to eq(1)
expect(issues.first).to include(
title: 'This is a sample issue',
description: "*Created by: gitlab-qa-github*\n\nThis is a sample first comment",
labels: ['custom new label', 'good first issue', 'help wanted'],
user_notes_count: 1
)
end
def verify_milestones_import
milestones = imported_project.milestones
expect(milestones.length).to eq(1)
expect(milestones.first).to include(title: 'v1.0', description: nil, state: 'active')
end
def verify_wikis_import
wikis = imported_project.wikis
expect(wikis.length).to eq(1)
expect(wikis.first).to include(title: 'Home', format: 'markdown')
end
def verify_merge_requests_import
merge_requests = imported_project.merge_requests
merge_request = Resource::MergeRequest.init do |mr|
mr.project = imported_project
mr.iid = merge_requests.first[:iid]
mr.api_client = api_client
end.reload!
mr_comments = merge_request.comments.map { |comment| comment[:body] } # rubocop:disable Rails/Pluck
expect(merge_requests.length).to eq(1)
expect(merge_request.api_resource).to include(
title: 'Improve readme',
state: 'opened',
target_branch: 'main',
source_branch: 'improve-readme',
labels: %w[bug documentation],
description: <<~DSC.strip
*Created by: gitlab-qa-github*\n\nThis improves the README file a bit.\r\n\r\nTODO:\r\n\r\n \r\n\r\n- [ ] Do foo\r\n- [ ] Make bar\r\n - [ ] Think about baz
DSC
)
expect(mr_comments).to eq(
[
"*Created by: gitlab-qa-github*\n\n[PR comment by @sliaquat] Nice work! ",
"*Created by: gitlab-qa-github*\n\n[Single diff comment] Nice addition",
"*Created by: gitlab-qa-github*\n\n[Single diff comment] Good riddance"
]
)
end
end
end
end
...@@ -18,7 +18,6 @@ module QA ...@@ -18,7 +18,6 @@ module QA
project.group = group project.group = group
project.github_personal_access_token = Runtime::Env.github_access_token project.github_personal_access_token = Runtime::Env.github_access_token
project.github_repository_path = 'gitlab-qa-github/test-project' project.github_repository_path = 'gitlab-qa-github/test-project'
project.api_client = api_client
end end
end end
...@@ -33,102 +32,13 @@ module QA ...@@ -33,102 +32,13 @@ module QA
it 'imports a GitHub repo', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/1762' do it 'imports a GitHub repo', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/1762' do
Flow::Login.sign_in(as: user) Flow::Login.sign_in(as: user)
imported_project.reload! # import the project and reload all fields imported_project # import the project
aggregate_failures do Page::Project::Show.perform do |project|
verify_repository_import expect(project).to have_content(imported_project.name)
verify_commits_import expect(project).to have_content('This test project is used for automated GitHub import by GitLab QA.')
verify_labels_import
verify_issues_import
verify_milestones_import
verify_wikis_import
verify_merge_requests_import
end end
end end
def verify_repository_import
expect(imported_project.api_response).to include(
description: 'A new repo for test',
import_status: 'finished',
import_error: nil
)
end
def verify_commits_import
expect(imported_project.commits.length).to eq(20)
end
def verify_labels_import
labels = imported_project.labels.map { |label| label.slice(:name, :color) }
expect(labels).to include(
{ name: 'bug', color: '#d73a4a' },
{ name: 'custom new label', color: '#fc8f91' },
{ name: 'documentation', color: '#0075ca' },
{ name: 'duplicate', color: '#cfd3d7' },
{ name: 'enhancement', color: '#a2eeef' },
{ name: 'good first issue', color: '#7057ff' },
{ name: 'help wanted', color: '#008672' },
{ name: 'invalid', color: '#e4e669' },
{ name: 'question', color: '#d876e3' },
{ name: 'wontfix', color: '#ffffff' }
)
end
def verify_issues_import
issues = imported_project.issues
expect(issues.length).to eq(1)
expect(issues.first).to include(
title: 'This is a sample issue',
description: "*Created by: gitlab-qa-github*\n\nThis is a sample first comment",
labels: ['custom new label', 'good first issue', 'help wanted'],
user_notes_count: 1
)
end
def verify_milestones_import
milestones = imported_project.milestones
expect(milestones.length).to eq(1)
expect(milestones.first).to include(title: 'v1.0', description: nil, state: 'active')
end
def verify_wikis_import
wikis = imported_project.wikis
expect(wikis.length).to eq(1)
expect(wikis.first).to include(title: 'Home', format: 'markdown')
end
def verify_merge_requests_import
merge_requests = imported_project.merge_requests
merge_request = Resource::MergeRequest.init do |mr|
mr.project = imported_project
mr.iid = merge_requests.first[:iid]
mr.api_client = api_client
end.reload!
mr_comments = merge_request.comments.map { |comment| comment[:body] } # rubocop:disable Rails/Pluck
expect(merge_requests.length).to eq(1)
expect(merge_request.api_resource).to include(
title: 'Improve readme',
state: 'opened',
target_branch: 'main',
source_branch: 'improve-readme',
labels: %w[bug documentation],
description: <<~DSC.strip
*Created by: gitlab-qa-github*\n\nThis improves the README file a bit.\r\n\r\nTODO:\r\n\r\n \r\n\r\n- [ ] Do foo\r\n- [ ] Make bar\r\n - [ ] Think about baz
DSC
)
expect(mr_comments).to eq(
[
"*Created by: gitlab-qa-github*\n\n[PR comment by @sliaquat] Nice work! ",
"*Created by: gitlab-qa-github*\n\n[Single diff comment] Nice addition",
"*Created by: gitlab-qa-github*\n\n[Single diff comment] Good riddance"
]
)
end
end end
end end
end end
...@@ -27,7 +27,7 @@ module QA ...@@ -27,7 +27,7 @@ module QA
end end
let(:import_project) do let(:import_project) do
EE::Resource::ImportRepoWithCICD.fabricate_via_browser_ui! do |project| EE::Resource::ImportRepoWithCiCd.fabricate_via_browser_ui! do |project|
project.import = true project.import = true
project.name = project_name project.name = project_name
project.github_personal_access_token = github_data[:access_token] project.github_personal_access_token = github_data[:access_token]
......
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