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

generate-as-if-foss-env.rb « setup « scripts - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38f6494684150c6ec45a5a6aae2078c733471e63 (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
98
99
#!/usr/bin/env ruby
# frozen_string_literal: true

# In spec/scripts/setup/generate_as_if_foss_env_spec.rb we completely stub it
require 'gitlab' unless Object.const_defined?(:Gitlab)
require 'set' # rubocop:disable Lint/RedundantRequireStatement -- Ruby 3.1 and earlier needs this. Drop this line after Ruby 3.2+ is only supported.

class GenerateAsIfFossEnv
  FOSS_JOBS = Set.new(%w[
    build-assets-image
    build-qa-image
    compile-production-assets
    compile-storybook
    compile-test-assets
    eslint
    generate-apollo-graphql-schema
    graphql-schema-dump
    jest
    jest-integration
    qa:internal
    qa:selectors
    static-analysis
  ]).freeze

  def initialize
    @client = Gitlab.client(endpoint: ENV['CI_API_V4_URL'], private_token: '')
    @rspec_jobs = Set.new
    @other_jobs = Set.new
  end

  def variables
    @variables ||= generate_variables
  end

  def display
    variables.each do |key, value|
      puts "#{key}=#{value}"
    end
  end

  private

  attr_reader :client, :rspec_jobs, :other_jobs

  def generate_variables
    scan_jobs

    {
      START_AS_IF_FOSS: 'true',
      RUBY_VERSION: ENV['RUBY_VERSION']
    }.merge(rspec_variables).merge(other_jobs_variables)
  end

  def scan_jobs
    each_job do |job|
      detect_rspec(job) || detect_other_jobs(job)
    end
  end

  def each_job
    client.pipeline_jobs(ENV['CI_PROJECT_ID'], ENV['CI_PIPELINE_ID']).auto_paginate do |job|
      yield(job)
    end
  end

  def detect_rspec(job)
    rspec_type = job.name[%r{^rspec(?:-all)? ([\w\-]+)}, 1]

    return unless rspec_type

    rspec_kind = job.name[%r{pg\d+ ([\w\-]+)(?: \d+/\d+)?$}, 1]
    rspec_jobs << rspec_type
    rspec_jobs << rspec_kind if rspec_kind
  end

  def detect_other_jobs(job)
    other_jobs << job.name if FOSS_JOBS.member?(job.name)
  end

  def rspec_variables
    return {} if rspec_jobs.empty?

    rspec_jobs.inject({ ENABLE_RSPEC: 'true' }) do |result, rspec|
      result.merge("ENABLE_RSPEC_#{job_name_to_variable_name(rspec)}": 'true')
    end
  end

  def other_jobs_variables
    other_jobs.inject({}) do |result, job_name|
      result.merge("ENABLE_#{job_name_to_variable_name(job_name)}": 'true')
    end
  end

  def job_name_to_variable_name(name)
    name.upcase.tr('-: ', '_')
  end
end

GenerateAsIfFossEnv.new.display if $PROGRAM_NAME == __FILE__