2016-09-02 08:03:32 -04:00
|
|
|
module CycleAnalyticsHelpers
|
2017-03-23 12:37:14 -04:00
|
|
|
def create_commit_referencing_issue(issue, branch_name: generate(:branch))
|
|
|
|
project.repository.add_branch(user, branch_name, 'master')
|
2016-09-20 00:24:42 -04:00
|
|
|
create_commit("Commit for ##{issue.iid}", issue.project, user, branch_name)
|
|
|
|
end
|
|
|
|
|
2016-09-26 00:33:57 -04:00
|
|
|
def create_commit(message, project, user, branch_name, count: 1)
|
2018-01-08 22:51:05 -05:00
|
|
|
repository = project.repository
|
|
|
|
oldrev = repository.commit(branch_name).sha
|
|
|
|
|
|
|
|
if Timecop.frozen? && Gitlab::GitalyClient.feature_enabled?(:operation_user_commit_files)
|
|
|
|
mock_gitaly_multi_action_dates(repository.raw)
|
|
|
|
end
|
|
|
|
|
2016-09-26 00:33:57 -04:00
|
|
|
commit_shas = Array.new(count) do |index|
|
2018-01-08 22:51:05 -05:00
|
|
|
commit_sha = repository.create_file(user, generate(:branch), "content", message: message, branch_name: branch_name)
|
|
|
|
repository.commit(commit_sha)
|
2016-09-26 00:33:57 -04:00
|
|
|
|
|
|
|
commit_sha
|
|
|
|
end
|
2016-09-20 00:24:42 -04:00
|
|
|
|
|
|
|
GitPushService.new(project,
|
|
|
|
user,
|
2016-09-20 01:47:36 -04:00
|
|
|
oldrev: oldrev,
|
2016-09-26 00:33:57 -04:00
|
|
|
newrev: commit_shas.last,
|
2016-09-20 00:24:42 -04:00
|
|
|
ref: 'refs/heads/master').execute
|
2016-09-07 04:59:12 -04:00
|
|
|
end
|
2016-09-02 08:03:32 -04:00
|
|
|
|
2018-02-15 08:23:39 -05:00
|
|
|
def create_cycle(user, project, issue, mr, milestone, pipeline)
|
|
|
|
issue.update(milestone: milestone)
|
|
|
|
pipeline.run
|
|
|
|
|
|
|
|
ci_build = create(:ci_build, pipeline: pipeline, status: :success, author: user)
|
|
|
|
|
2018-02-21 07:13:56 -05:00
|
|
|
merge_merge_requests_closing_issue(user, project, issue)
|
2018-02-15 08:23:39 -05:00
|
|
|
ProcessCommitWorker.new.perform(project.id, user.id, mr.commits.last.to_hash)
|
|
|
|
|
|
|
|
ci_build
|
|
|
|
end
|
|
|
|
|
2018-02-21 07:13:56 -05:00
|
|
|
def create_merge_request_closing_issue(user, project, issue, message: nil, source_branch: nil, commit_message: 'commit message')
|
2016-09-20 01:47:36 -04:00
|
|
|
if !source_branch || project.repository.commit(source_branch).blank?
|
2017-03-23 09:08:39 -04:00
|
|
|
source_branch = generate(:branch)
|
2016-09-20 01:47:36 -04:00
|
|
|
project.repository.add_branch(user, source_branch, 'master')
|
|
|
|
end
|
|
|
|
|
2017-02-15 18:28:29 -05:00
|
|
|
sha = project.repository.create_file(
|
2016-12-08 06:11:52 -05:00
|
|
|
user,
|
2017-03-23 09:08:39 -04:00
|
|
|
generate(:branch),
|
2016-12-08 06:11:52 -05:00
|
|
|
'content',
|
2017-04-24 12:26:23 -04:00
|
|
|
message: commit_message,
|
2017-02-15 18:28:29 -05:00
|
|
|
branch_name: source_branch)
|
2016-09-07 04:59:12 -04:00
|
|
|
project.repository.commit(sha)
|
2016-09-02 08:03:32 -04:00
|
|
|
|
2016-09-07 04:59:12 -04:00
|
|
|
opts = {
|
|
|
|
title: 'Awesome merge_request',
|
|
|
|
description: message || "Fixes #{issue.to_reference}",
|
|
|
|
source_branch: source_branch,
|
|
|
|
target_branch: 'master'
|
|
|
|
}
|
2016-09-02 08:03:32 -04:00
|
|
|
|
2017-08-01 08:38:45 -04:00
|
|
|
mr = MergeRequests::CreateService.new(project, user, opts).execute
|
|
|
|
NewMergeRequestWorker.new.perform(mr, user)
|
|
|
|
mr
|
2016-09-07 04:59:12 -04:00
|
|
|
end
|
2016-09-02 08:03:32 -04:00
|
|
|
|
2018-02-21 07:13:56 -05:00
|
|
|
def merge_merge_requests_closing_issue(user, project, issue)
|
2016-11-01 16:18:51 -04:00
|
|
|
merge_requests = issue.closed_by_merge_requests(user)
|
|
|
|
|
2016-09-07 04:59:12 -04:00
|
|
|
merge_requests.each { |merge_request| MergeRequests::MergeService.new(project, user).execute(merge_request) }
|
|
|
|
end
|
2016-09-02 08:03:32 -04:00
|
|
|
|
2018-02-21 07:13:56 -05:00
|
|
|
def deploy_master(user, project, environment: 'production')
|
2017-06-01 14:46:34 -04:00
|
|
|
dummy_job =
|
|
|
|
case environment
|
|
|
|
when 'production'
|
2018-02-21 07:13:56 -05:00
|
|
|
dummy_production_job(user, project)
|
2017-06-01 14:46:34 -04:00
|
|
|
when 'staging'
|
2018-02-21 07:13:56 -05:00
|
|
|
dummy_staging_job(user, project)
|
2017-06-01 14:46:34 -04:00
|
|
|
else
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
|
|
|
|
CreateDeploymentService.new(dummy_job).execute
|
|
|
|
end
|
|
|
|
|
2018-02-21 07:13:56 -05:00
|
|
|
def dummy_production_job(user, project)
|
|
|
|
new_dummy_job(user, project, 'production')
|
2017-06-01 14:46:34 -04:00
|
|
|
end
|
|
|
|
|
2018-02-21 07:13:56 -05:00
|
|
|
def dummy_staging_job(user, project)
|
|
|
|
new_dummy_job(user, project, 'staging')
|
2017-06-01 14:46:34 -04:00
|
|
|
end
|
|
|
|
|
2018-02-21 07:13:56 -05:00
|
|
|
def dummy_pipeline(project)
|
|
|
|
Ci::Pipeline.new(
|
|
|
|
sha: project.repository.commit('master').sha,
|
|
|
|
ref: 'master',
|
|
|
|
source: :push,
|
|
|
|
project: project,
|
|
|
|
protected: false)
|
2017-06-01 14:46:34 -04:00
|
|
|
end
|
|
|
|
|
2018-02-21 07:13:56 -05:00
|
|
|
def new_dummy_job(user, project, environment)
|
2017-06-02 09:00:56 -04:00
|
|
|
project.environments.find_or_create_by(name: environment)
|
|
|
|
|
2017-06-01 14:46:34 -04:00
|
|
|
Ci::Build.new(
|
|
|
|
project: project,
|
|
|
|
user: user,
|
|
|
|
environment: environment,
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
name: 'dummy',
|
2017-11-06 05:36:11 -05:00
|
|
|
stage: 'dummy',
|
2018-02-21 07:13:56 -05:00
|
|
|
pipeline: dummy_pipeline(project),
|
2017-09-02 03:31:14 -04:00
|
|
|
protected: false)
|
2016-09-02 08:03:32 -04:00
|
|
|
end
|
2018-01-08 22:51:05 -05:00
|
|
|
|
|
|
|
def mock_gitaly_multi_action_dates(raw_repository)
|
|
|
|
allow(raw_repository).to receive(:multi_action).and_wrap_original do |m, *args|
|
|
|
|
new_date = Time.now
|
|
|
|
branch_update = m.call(*args)
|
|
|
|
|
|
|
|
if branch_update.newrev
|
|
|
|
_, opts = args
|
|
|
|
commit = raw_repository.commit(branch_update.newrev).rugged_commit
|
|
|
|
branch_update.newrev = commit.amend(
|
|
|
|
update_ref: "#{Gitlab::Git::BRANCH_REF_PREFIX}#{opts[:branch_name]}",
|
|
|
|
author: commit.author.merge(time: new_date),
|
|
|
|
committer: commit.committer.merge(time: new_date)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
branch_update
|
|
|
|
end
|
|
|
|
end
|
2016-09-02 08:03:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
2016-09-07 04:59:12 -04:00
|
|
|
config.include CycleAnalyticsHelpers
|
2016-09-02 08:03:32 -04:00
|
|
|
end
|