2019-07-25 05:27:42 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-02 12:03:32 +00:00
|
|
|
module CycleAnalyticsHelpers
|
2018-10-02 03:21:46 +00:00
|
|
|
include GitHelpers
|
|
|
|
|
2017-03-23 16:37:14 +00:00
|
|
|
def create_commit_referencing_issue(issue, branch_name: generate(:branch))
|
|
|
|
project.repository.add_branch(user, branch_name, 'master')
|
2016-09-20 04:24:42 +00:00
|
|
|
create_commit("Commit for ##{issue.iid}", issue.project, user, branch_name)
|
|
|
|
end
|
|
|
|
|
2018-06-02 07:28:57 +00:00
|
|
|
def create_commit(message, project, user, branch_name, count: 1, commit_time: nil, skip_push_handler: false)
|
2018-01-09 03:51:05 +00:00
|
|
|
repository = project.repository
|
2018-06-02 07:28:57 +00:00
|
|
|
oldrev = repository.commit(branch_name)&.sha || Gitlab::Git::BLANK_SHA
|
2018-01-09 03:51:05 +00:00
|
|
|
|
2019-06-12 14:20:01 +00:00
|
|
|
if Timecop.frozen?
|
2018-10-02 03:21:46 +00:00
|
|
|
mock_gitaly_multi_action_dates(repository, commit_time)
|
2018-01-09 03:51:05 +00:00
|
|
|
end
|
|
|
|
|
2016-09-26 04:33:57 +00:00
|
|
|
commit_shas = Array.new(count) do |index|
|
2018-01-09 03:51:05 +00:00
|
|
|
commit_sha = repository.create_file(user, generate(:branch), "content", message: message, branch_name: branch_name)
|
|
|
|
repository.commit(commit_sha)
|
2016-09-26 04:33:57 +00:00
|
|
|
|
|
|
|
commit_sha
|
|
|
|
end
|
2016-09-20 04:24:42 +00:00
|
|
|
|
2018-06-02 07:28:57 +00:00
|
|
|
return if skip_push_handler
|
|
|
|
|
2019-10-03 15:07:07 +00:00
|
|
|
Git::BranchPushService.new(
|
|
|
|
project,
|
|
|
|
user,
|
|
|
|
change: {
|
|
|
|
oldrev: oldrev,
|
|
|
|
newrev: commit_shas.last,
|
|
|
|
ref: 'refs/heads/master'
|
|
|
|
}
|
|
|
|
).execute
|
2016-09-07 08:59:12 +00:00
|
|
|
end
|
2016-09-02 12:03:32 +00:00
|
|
|
|
2018-02-15 13:23:39 +00:00
|
|
|
def create_cycle(user, project, issue, mr, milestone, pipeline)
|
2020-08-13 18:10:36 +00:00
|
|
|
issue.update!(milestone: milestone)
|
2018-02-15 13:23:39 +00:00
|
|
|
pipeline.run
|
|
|
|
|
|
|
|
ci_build = create(:ci_build, pipeline: pipeline, status: :success, author: user)
|
|
|
|
|
2018-02-21 12:13:56 +00:00
|
|
|
merge_merge_requests_closing_issue(user, project, issue)
|
2018-02-15 13:23:39 +00:00
|
|
|
ProcessCommitWorker.new.perform(project.id, user.id, mr.commits.last.to_hash)
|
|
|
|
|
|
|
|
ci_build
|
|
|
|
end
|
|
|
|
|
2018-02-21 12:13:56 +00:00
|
|
|
def create_merge_request_closing_issue(user, project, issue, message: nil, source_branch: nil, commit_message: 'commit message')
|
2016-09-20 05:47:36 +00:00
|
|
|
if !source_branch || project.repository.commit(source_branch).blank?
|
2017-03-23 13:08:39 +00:00
|
|
|
source_branch = generate(:branch)
|
2016-09-20 05:47:36 +00:00
|
|
|
project.repository.add_branch(user, source_branch, 'master')
|
|
|
|
end
|
|
|
|
|
2018-06-02 07:28:57 +00:00
|
|
|
# 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-02 12:03:32 +00:00
|
|
|
|
2016-09-07 08:59:12 +00:00
|
|
|
opts = {
|
|
|
|
title: 'Awesome merge_request',
|
|
|
|
description: message || "Fixes #{issue.to_reference}",
|
|
|
|
source_branch: source_branch,
|
|
|
|
target_branch: 'master'
|
|
|
|
}
|
2016-09-02 12:03:32 +00:00
|
|
|
|
2017-08-01 12:38:45 +00:00
|
|
|
mr = MergeRequests::CreateService.new(project, user, opts).execute
|
|
|
|
NewMergeRequestWorker.new.perform(mr, user)
|
|
|
|
mr
|
2016-09-07 08:59:12 +00:00
|
|
|
end
|
2016-09-02 12:03:32 +00:00
|
|
|
|
2018-02-21 12:13:56 +00:00
|
|
|
def merge_merge_requests_closing_issue(user, project, issue)
|
2018-08-16 10:07:43 +00:00
|
|
|
merge_requests = Issues::ReferencedMergeRequestsService
|
|
|
|
.new(project, user)
|
|
|
|
.closed_by_merge_requests(issue)
|
2016-11-01 20:18:51 +00:00
|
|
|
|
2019-11-15 15:06:12 +00:00
|
|
|
merge_requests.each { |merge_request| MergeRequests::MergeService.new(project, user, sha: merge_request.diff_head_sha).execute(merge_request) }
|
2016-09-07 08:59:12 +00:00
|
|
|
end
|
2016-09-02 12:03:32 +00:00
|
|
|
|
2018-02-21 12:13:56 +00:00
|
|
|
def deploy_master(user, project, environment: 'production')
|
2017-06-01 18:46:34 +00:00
|
|
|
dummy_job =
|
|
|
|
case environment
|
|
|
|
when 'production'
|
2018-02-21 12:13:56 +00:00
|
|
|
dummy_production_job(user, project)
|
2017-06-01 18:46:34 +00:00
|
|
|
when 'staging'
|
2018-02-21 12:13:56 +00:00
|
|
|
dummy_staging_job(user, project)
|
2017-06-01 18:46:34 +00:00
|
|
|
else
|
|
|
|
raise ArgumentError
|
|
|
|
end
|
|
|
|
|
2018-11-05 00:37:40 +00:00
|
|
|
dummy_job.success! # State machine automatically update associated deployment/environment record
|
2017-06-01 18:46:34 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 12:13:56 +00:00
|
|
|
def dummy_production_job(user, project)
|
|
|
|
new_dummy_job(user, project, 'production')
|
2017-06-01 18:46:34 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 12:13:56 +00:00
|
|
|
def dummy_staging_job(user, project)
|
|
|
|
new_dummy_job(user, project, 'staging')
|
2017-06-01 18:46:34 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 12:13:56 +00:00
|
|
|
def dummy_pipeline(project)
|
2018-11-05 00:37:40 +00:00
|
|
|
create(:ci_pipeline,
|
2018-02-21 12:13:56 +00:00
|
|
|
sha: project.repository.commit('master').sha,
|
|
|
|
ref: 'master',
|
|
|
|
source: :push,
|
|
|
|
project: project,
|
|
|
|
protected: false)
|
2017-06-01 18:46:34 +00:00
|
|
|
end
|
|
|
|
|
2018-02-21 12:13:56 +00:00
|
|
|
def new_dummy_job(user, project, environment)
|
2018-11-05 00:37:40 +00:00
|
|
|
create(:ci_build,
|
2019-10-15 12:06:06 +00:00
|
|
|
:with_deployment,
|
2017-06-01 18:46:34 +00:00
|
|
|
project: project,
|
|
|
|
user: user,
|
|
|
|
environment: environment,
|
|
|
|
ref: 'master',
|
|
|
|
tag: false,
|
|
|
|
name: 'dummy',
|
2017-11-06 10:36:11 +00:00
|
|
|
stage: 'dummy',
|
2018-02-21 12:13:56 +00:00
|
|
|
pipeline: dummy_pipeline(project),
|
2017-09-02 07:31:14 +00:00
|
|
|
protected: false)
|
2016-09-02 12:03:32 +00:00
|
|
|
end
|
2018-01-09 03:51:05 +00:00
|
|
|
|
2018-10-02 03:21:46 +00:00
|
|
|
def mock_gitaly_multi_action_dates(repository, commit_time)
|
|
|
|
allow(repository.raw).to receive(:multi_action).and_wrap_original do |m, *args|
|
2018-06-02 07:28:57 +00:00
|
|
|
new_date = commit_time || Time.now
|
2018-01-09 03:51:05 +00:00
|
|
|
branch_update = m.call(*args)
|
|
|
|
|
|
|
|
if branch_update.newrev
|
|
|
|
_, opts = args
|
2018-06-05 15:51:14 +00:00
|
|
|
|
2018-10-02 03:21:46 +00:00
|
|
|
commit = rugged_repo(repository).rev_parse(branch_update.newrev)
|
2018-06-05 15:51:14 +00:00
|
|
|
|
2018-01-09 03:51:05 +00:00
|
|
|
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 12:03:32 +00:00
|
|
|
end
|