Class: GitlabMrRelease::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/gitlab_mr_release/cli.rb

Constant Summary collapse

GITLAB_ENV_FILES =
%w(.env.gitlab ~/.env.gitlab)
DEFAULT_TITLE_TEMPLATE =
"Release <%= Time.now %> <%= source_branch %> -> <%= target_branch %>"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



13
14
15
# File 'lib/gitlab_mr_release/cli.rb', line 13

def self.source_root
  "#{__dir__}/../templates"
end

Instance Method Details

#createObject



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
# File 'lib/gitlab_mr_release/cli.rb', line 33

def create
  Dotenv.load(*GITLAB_ENV_FILES)

  assert_env("GITLAB_API_ENDPOINT")
  assert_env("GITLAB_API_PRIVATE_TOKEN")
  assert_env("GITLAB_PROJECT_NAME")

  assert_option_or_env(:source, "DEFAULT_SOURCE_BRANCH")
  assert_option_or_env(:target, "DEFAULT_TARGET_BRANCH")

  template = File.read(template_file)

  project = GitlabMrRelease::Project.new(
    api_endpoint:  ENV["GITLAB_API_ENDPOINT"],
    private_token: ENV["GITLAB_API_PRIVATE_TOKEN"],
    project_name:  ENV["GITLAB_PROJECT_NAME"],
  )

  unless project.api_version >= 4
    raise InvalidApiVersionError, "'#{ENV["GITLAB_API_ENDPOINT"]}' seems not to be GitLab API v4+. gitlab_mr_release requires GitLab API v4+"
  end

  mr = project.create_merge_request(
    source_branch: source_branch,
    target_branch: target_branch,
    labels:        labels,
    title:         generate_title,
    template:      template,
  )

  mr_url = "#{project.web_url}/merge_requests/#{mr.iid}"

  message = <<-EOS
MergeRequest is created

[Title] #{mr.title}

[Description]
#{mr.description}

[Url] #{mr_url}
  EOS

  puts message
end

#initObject



23
24
25
26
# File 'lib/gitlab_mr_release/cli.rb', line 23

def init
  copy_file ".env.gitlab", ".env.gitlab"
  copy_file "gitlab_mr_release.md.erb", "gitlab_mr_release.md.erb"
end

#versionObject



18
19
20
# File 'lib/gitlab_mr_release/cli.rb', line 18

def version
  puts GitlabMrRelease::VERSION
end