BigW Consortium Gitlab

cycle_analytics_helpers.rb 2.88 KB
Newer Older
1
module CycleAnalyticsHelpers
Rémy Coutable committed
2 3
  def create_commit_referencing_issue(issue, branch_name: generate(:branch))
    project.repository.add_branch(user, branch_name, 'master')
4 5 6
    create_commit("Commit for ##{issue.iid}", issue.project, user, branch_name)
  end

7
  def create_commit(message, project, user, branch_name, count: 1)
8
    oldrev = project.repository.commit(branch_name).sha
9
    commit_shas = Array.new(count) do |index|
10
      commit_sha = project.repository.create_file(user, generate(:branch), "content", message: message, branch_name: branch_name)
11 12 13 14
      project.repository.commit(commit_sha)

      commit_sha
    end
15 16 17

    GitPushService.new(project,
                       user,
18
                       oldrev: oldrev,
19
                       newrev: commit_shas.last,
20
                       ref: 'refs/heads/master').execute
21
  end
22

23
  def create_merge_request_closing_issue(issue, message: nil, source_branch: nil, commit_message: 'commit message')
24
    if !source_branch || project.repository.commit(source_branch).blank?
25
      source_branch = generate(:branch)
26 27 28
      project.repository.add_branch(user, source_branch, 'master')
    end

29
    sha = project.repository.create_file(
30
      user,
31
      generate(:branch),
32
      'content',
33
      message: commit_message,
34
      branch_name: source_branch)
35
    project.repository.commit(sha)
36

37 38 39 40 41 42
    opts = {
      title: 'Awesome merge_request',
      description: message || "Fixes #{issue.to_reference}",
      source_branch: source_branch,
      target_branch: 'master'
    }
43

44 45 46
    mr = MergeRequests::CreateService.new(project, user, opts).execute
    NewMergeRequestWorker.new.perform(mr, user)
    mr
47
  end
48

49
  def merge_merge_requests_closing_issue(issue)
50 51
    merge_requests = issue.closed_by_merge_requests(user)

52 53
    merge_requests.each { |merge_request| MergeRequests::MergeService.new(project, user).execute(merge_request) }
  end
54

55
  def deploy_master(environment: 'production')
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    dummy_job =
      case environment
      when 'production'
        dummy_production_job
      when 'staging'
        dummy_staging_job
      else
        raise ArgumentError
      end

    CreateDeploymentService.new(dummy_job).execute
  end

  def dummy_production_job
    @dummy_job ||= new_dummy_job('production')
  end

  def dummy_staging_job
    @dummy_job ||= new_dummy_job('staging')
  end

  def dummy_pipeline
    @dummy_pipeline ||=
79
      Ci::Pipeline.new(
80
        sha: project.repository.commit('master').sha,
81 82
        ref: 'master',
        source: :push,
Shinya Maeda committed
83
        project: project,
Shinya Maeda committed
84
        protected: false)
85 86 87
  end

  def new_dummy_job(environment)
88 89
    project.environments.find_or_create_by(name: environment)

90 91 92 93 94 95 96
    Ci::Build.new(
      project: project,
      user: user,
      environment: environment,
      ref: 'master',
      tag: false,
      name: 'dummy',
Shinya Maeda committed
97
      pipeline: dummy_pipeline,
Shinya Maeda committed
98
      protected: false)
99 100 101 102
  end
end

RSpec.configure do |config|
103
  config.include CycleAnalyticsHelpers
104
end