2016-09-29 09:46:39 +05:30
|
|
|
module CycleAnalyticsHelpers
|
2017-08-17 22:00:37 +05:30
|
|
|
def create_commit_referencing_issue(issue, branch_name: generate(:branch))
|
2016-09-29 09:46:39 +05:30
|
|
|
project.repository.add_branch(user, branch_name, 'master')
|
|
|
|
create_commit("Commit for ##{issue.iid}", issue.project, user, branch_name)
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def create_commit(message, project, user, branch_name, count: 1, commit_time: nil, skip_push_handler: false)
|
2018-03-17 18:26:18 +05:30
|
|
|
repository = project.repository
|
2018-11-08 19:23:39 +05:30
|
|
|
oldrev = repository.commit(branch_name)&.sha || Gitlab::Git::BLANK_SHA
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
if Timecop.frozen? && Gitlab::GitalyClient.feature_enabled?(:operation_user_commit_files)
|
2018-11-08 19:23:39 +05:30
|
|
|
mock_gitaly_multi_action_dates(repository.raw, commit_time)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2016-10-01 15:18:49 +05:30
|
|
|
commit_shas = Array.new(count) do |index|
|
2018-03-17 18:26:18 +05:30
|
|
|
commit_sha = repository.create_file(user, generate(:branch), "content", message: message, branch_name: branch_name)
|
|
|
|
repository.commit(commit_sha)
|
2016-10-01 15:18:49 +05:30
|
|
|
|
|
|
|
commit_sha
|
|
|
|
end
|
2016-09-29 09:46:39 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
return if skip_push_handler
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
GitPushService.new(project,
|
|
|
|
user,
|
|
|
|
oldrev: oldrev,
|
2016-10-01 15:18:49 +05:30
|
|
|
newrev: commit_shas.last,
|
2016-09-29 09:46:39 +05:30
|
|
|
ref: 'refs/heads/master').execute
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
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)
|
|
|
|
|
|
|
|
merge_merge_requests_closing_issue(user, project, issue)
|
|
|
|
ProcessCommitWorker.new.perform(project.id, user.id, mr.commits.last.to_hash)
|
|
|
|
|
|
|
|
ci_build
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_merge_request_closing_issue(user, project, issue, message: nil, source_branch: nil, commit_message: 'commit message')
|
2016-09-29 09:46:39 +05:30
|
|
|
if !source_branch || project.repository.commit(source_branch).blank?
|
2017-08-17 22:00:37 +05:30
|
|
|
source_branch = generate(:branch)
|
2016-09-29 09:46:39 +05:30
|
|
|
project.repository.add_branch(user, source_branch, 'master')
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
# Cycle analytic specs often test with frozen times, which causes metrics to be
|
|
|
|
# pinned to the current time. For example, in the plan stage, we assume that an issue
|
|
|
|
# milestone has been created before any code has been written. We add a second
|
|
|
|
# to ensure that the plan time is positive.
|
|
|
|
create_commit(commit_message, project, user, source_branch, commit_time: Time.now + 1.second, skip_push_handler: true)
|
2016-09-29 09:46:39 +05:30
|
|
|
|
|
|
|
opts = {
|
|
|
|
title: 'Awesome merge_request',
|
|
|
|
description: message || "Fixes #{issue.to_reference}",
|
|
|
|
source_branch: source_branch,
|
|
|
|
target_branch: 'master'
|
|
|
|
}
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
mr = MergeRequests::CreateService.new(project, user, opts).execute
|
|
|
|
NewMergeRequestWorker.new.perform(mr, user)
|
|
|
|
mr
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def merge_merge_requests_closing_issue(user, project, issue)
|
2018-11-20 20:47:30 +05:30
|
|
|
merge_requests = Issues::ReferencedMergeRequestsService
|
|
|
|
.new(project, user)
|
|
|
|
.closed_by_merge_requests(issue)
|
2016-11-24 13:41:30 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
merge_requests.each { |merge_request| MergeRequests::MergeService.new(project, user).execute(merge_request) }
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def deploy_master(user, project, environment: 'production')
|
2017-09-10 17:25:29 +05:30
|
|
|
dummy_job =
|
|
|
|
case environment
|
|
|
|
when 'production'
|
2018-03-27 19:54:05 +05:30
|
|
|
dummy_production_job(user, project)
|
2017-09-10 17:25:29 +05:30
|
|
|
when 'staging'
|
2018-03-27 19:54:05 +05:30
|
|
|
dummy_staging_job(user, project)
|
2017-09-10 17:25:29 +05:30
|
|
|
else
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
|
|
|
|
CreateDeploymentService.new(dummy_job).execute
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def dummy_production_job(user, project)
|
|
|
|
new_dummy_job(user, project, 'production')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def dummy_staging_job(user, project)
|
|
|
|
new_dummy_job(user, project, 'staging')
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def dummy_pipeline(project)
|
|
|
|
Ci::Pipeline.new(
|
|
|
|
sha: project.repository.commit('master').sha,
|
|
|
|
ref: 'master',
|
|
|
|
source: :push,
|
|
|
|
project: project,
|
|
|
|
protected: false)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def new_dummy_job(user, project, environment)
|
2017-09-10 17:25:29 +05:30
|
|
|
project.environments.find_or_create_by(name: environment)
|
|
|
|
|
|
|
|
Ci::Build.new(
|
|
|
|
project: project,
|
|
|
|
user: user,
|
|
|
|
environment: environment,
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
name: 'dummy',
|
2018-03-17 18:26:18 +05:30
|
|
|
stage: 'dummy',
|
2018-03-27 19:54:05 +05:30
|
|
|
pipeline: dummy_pipeline(project),
|
2018-03-17 18:26:18 +05:30
|
|
|
protected: false)
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def mock_gitaly_multi_action_dates(raw_repository, commit_time)
|
2018-03-17 18:26:18 +05:30
|
|
|
allow(raw_repository).to receive(:multi_action).and_wrap_original do |m, *args|
|
2018-11-08 19:23:39 +05:30
|
|
|
new_date = commit_time || Time.now
|
2018-03-17 18:26:18 +05:30
|
|
|
branch_update = m.call(*args)
|
|
|
|
|
|
|
|
if branch_update.newrev
|
|
|
|
_, opts = args
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
commit = Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
2018-11-18 11:00:15 +05:30
|
|
|
rugged = raw_repository.rugged
|
|
|
|
rugged.rev_parse(branch_update.newrev)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
end
|