Class: Gitlab::Fastlane::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/fastlane/cli.rb

Overview

Command-line interface for gitlab-fastlane

Constant Summary collapse

GITLAB_CI_LOCAL_MISSING =
"Error: gitlab-ci-local is not installed.\nInstall it with: npm install -g gitlab-ci-local\nOr visit: https://github.com/firecow/gitlab-ci-local\n"
HELP_MESSAGE =
"gitlab-fastlane - Run GitLab CI pipelines locally\n\nUsage:\n  gitlab-fastlane run [options]    Run CI pipeline locally\n  gitlab-fastlane version          Show version\n  gitlab-fastlane help             Show this help\n\nRun options:\n  --enable-artifacts               Enable artifact creation (disabled by default)\n\nGlobal options:\n  -h, --help                       Show this help\n  -v, --version                    Show version\n"

Class Method Summary collapse

Class Method Details

.add_validation_tagObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gitlab/fastlane/cli.rb', line 101

def self.add_validation_tag
  tag = "\n\nGitlab Fastlane passed: #{ci_stages * "; "}"
  body = `git log -1 --pretty=%b`.strip

  return if body.include? tag

  subject = `git log -1 --pretty=%s`.strip
  body += tag

  Tempfile.create("commit_msg") do |file|
    file.write("#{subject}\n\n#{body}")
    file.flush

    system("git", "commit", "--amend", "--file", file.path)
  end
end

.ci_stagesObject



118
119
120
# File 'lib/gitlab/fastlane/cli.rb', line 118

def self.ci_stages
  Psych.load_file(".gitlab-ci.yml")["stages"] || []
end

.git_status_clean?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gitlab/fastlane/cli.rb', line 75

def self.git_status_clean?
  status = `git status --porcelain`.strip

  return true if status.empty?

  puts "Warning: You have unstaged/untracked changes:"
  puts status
  puts "\nDo you want to continue? (y/N)"

  response = $stdin.gets.chomp.downcase
  response == "y"
end

.gitlab_ci_local_installed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/gitlab/fastlane/cli.rb', line 71

def self.gitlab_ci_local_installed?
  !`which gitlab-ci-local 2>/dev/null`.strip.empty?
end

.run_command(args = []) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gitlab/fastlane/cli.rb', line 48

def self.run_command(args = [])
  return unless git_status_clean?

  unless gitlab_ci_local_installed?
    puts GITLAB_CI_LOCAL_MISSING
    return
  end

  enable_artifacts = args.include?("--enable-artifacts")
  gitlab_ci_args = enable_artifacts ? "--artifacts-to-source" : "--no-artifacts-to-source"

  puts "Running GitLab CI locally..."
  success = system("gitlab-ci-local #{gitlab_ci_args}")

  if success
    add_validation_tag
    return
  end

  puts "\nPipeline failed. Fix the issues and try again."
  exit 1
end

.show_helpObject



97
98
99
# File 'lib/gitlab/fastlane/cli.rb', line 97

def self.show_help
  puts HELP_MESSAGE
end

.start(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/fastlane/cli.rb', line 32

def self.start(args)
  command = args.first || "help"

  case command
  when "run"
    run_args = args[1..]
    run_command(run_args)
  when "version", "-v", "--version"
    version_command
  when "help", "-h", "--help"
    show_help
  else
    unknown_command(command)
  end
end

.unknown_command(command) ⇒ Object



92
93
94
95
# File 'lib/gitlab/fastlane/cli.rb', line 92

def self.unknown_command(command)
  puts "Unknown command: #{command}"
  show_help
end

.version_commandObject



88
89
90
# File 'lib/gitlab/fastlane/cli.rb', line 88

def self.version_command
  puts "gitlab-fastlane #{VERSION}"
end