Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20150601043231_migrate_jobs_to_yaml.rb « migrate « ci « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1fa4cf74dd8d5d063904f8bb1f8e09e782775c7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Migration tested on MySQL and PostgreSQL.
# Can be performed online without errors.
# This migration will loop through all projects and jobs, so it can take some time.

class MigrateJobsToYaml < ActiveRecord::Migration
  def up
    select_all("SELECT * FROM projects").each do |project|
      config = {}

      concatenate_expression = if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
                                 "string_agg(tags.name, ',')"
                               else
                                 "GROUP_CONCAT(tags.name SEPARATOR ',')"
                               end

      sql = "SELECT j.*, #{concatenate_expression} tags
        FROM jobs j
          LEFT JOIN taggings tgs ON j.id = tgs.taggable_id AND tgs.taggable_type = 'Job'
          LEFT JOIN tags ON tgs.tag_id = tags.id
        WHERE project_id = #{project['id']}
          AND active = true
          AND job_type = 'parallel'
        GROUP BY j.id"

      # skip_refs migrate
      skip_refs = []

      if project["skip_refs"].present?
        skip_refs = project["skip_refs"].split(",").map(&:strip).select{|ref| ref =~  /^[\w-]*\Z/ }
      end


      # Create Jobs
      select_all(sql).each do |job|
        config[job["name"].to_s] = {
          script: job["commands"] && job["commands"].split("\n").map(&:strip),
          tags: job["tags"] && job["tags"].split(",").map(&:strip)
        }

        except = build_except_param(parse_boolean_value(job["build_branches"]), parse_boolean_value(job["build_tags"]))
        except = except + skip_refs

        if except.any?
          config[job["name"].to_s][:except] = except
        end
      end

      # Create Deploy Jobs
      select_all(sql.sub("parallel", 'deploy')).each do |job|
        config[job["name"].to_s] = {
          script: job["commands"] && job["commands"].split("\n").map(&:strip),
          type: "deploy",
          tags: job["tags"] && job["tags"].split(",").map(&:strip)
        }

        if job["refs"].present?
          config[job["name"].to_s][:only] = job["refs"].split(",").map(&:strip)
        else
          except = build_except_param(parse_boolean_value(job["build_branches"]), parse_boolean_value(job["build_tags"]))
          except = except + skip_refs

          if except.any?
            config[job["name"].to_s][:except] = except
          end
        end
      end

      yaml_config = YAML.dump(config.deep_stringify_keys)

      yaml_config.sub!("---", "# This file is generated by GitLab CI")

      execute("UPDATE projects SET generated_yaml_config = '#{quote_string(yaml_config)}' WHERE projects.id = #{project["id"]}")
    end
  end

  def down

  end

  private

  def parse_boolean_value(value)
    [ true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON' ].include?(value)
  end

  def build_except_param(branches, tags)
    unless branches
      return ["branches"]
    end

    unless tags
      return ["tags"]
    end

    []
  end
end