retag 0.1.3 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e2df15980e560df6495c0565d3b2eef9ea085c34ff1756dc53861e058c31833f
4
- data.tar.gz: 0db02684208f5b8e4b97a6230495025a9033d7b8dfc2ecb3df4294c03b79eec6
3
+ metadata.gz: e71af667189dceff7c2606302321bedfa7defebc54e3c77d73a3d4d2a1d1cfe8
4
+ data.tar.gz: cfdbf324852f22a679609651ed49b1bb7519247d778ace647f056673620aed56
5
5
  SHA512:
6
- metadata.gz: 1d71f1915637a2ca9b256094d49f615cc4f54413338467f82589853f172a4fb41992eb58db19b7bea0ae45743438c0ca80d3409994b2d1450e18ea30473ad762
7
- data.tar.gz: 87ad3d50a71c22574565437d3192333bfe47a006ecdc1d8f26d86f3eba1a5e2a884bf7699176b82f528bd91c07761f629169bd87067619f6e3b373514a061520
6
+ metadata.gz: b324b0efdaa8e56771c8fc2ab5867d67c4a641eab9531a614d1d4d510fb07d4ec9d3e8248a661970f498f8d5291def7ea0c641d478879ff7fdf48d5c632ec47a
7
+ data.tar.gz: 51967abd9fec922eb7de05eaec2a4cbf244d318498c82d8347d15a2df86e51b66c02531f55eb2aa8b61aa5810af5e00e0bdaf907e70735bc184adebccbc14fff
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- retag (0.1.3)
4
+ retag (0.1.6)
5
5
  activesupport (~> 6.0)
6
6
  colorize
7
7
  gitlab
data/bin/retag CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'yaml'
4
+ require 'erb'
4
5
  require 'thor'
5
6
  require 'colorize'
6
7
  require 'retag'
@@ -10,36 +11,67 @@ ENV_PREFIX = 'RETAG'
10
11
  $logger = ActiveSupport::TaggedLogging.new(Retag::Logger.new(STDOUT))
11
12
  $logger.level = :info
12
13
 
13
- class Cli < Thor
14
14
 
15
- def self.exit_on_failure?
16
- true
17
- end
18
-
19
- package_name "Retag is cli to manipulate docker images in handy way.
20
15
 
21
- # Gitlab authorization
22
- Using GITLAB_API_ENDPOINT and GITLAB_API_PRIVATE_TOKEN environment variable used for authorization
16
+ class RepoCommand < Thor
17
+ desc "tag <project> <tag>", "Create tag in Gitlab project"
18
+ long_desc <<-LONGDESC
19
+ LONGDESC
20
+ option :force, default: false,
21
+ required: false,
22
+ type: :boolean,
23
+ desc: "Override Gitlab tag if exists"
23
24
 
24
- #{File.basename(__FILE__)}"
25
+ option :ref, default: ENV.fetch("#{ENV_PREFIX}_TAG_REF", 'master'),
26
+ required: true,
27
+ type: :string,
28
+ desc: "Gtilab refspec from which create tag"
25
29
 
26
- desc 'version', 'Display version'
27
- map %w[-v --version] => :version
30
+ option :message, default: nil,
31
+ type: :string,
32
+ desc: "Tag message"
28
33
 
29
- def version
30
- say Retag::VERSION
34
+ def tag(project, tag)
35
+ opts = options.to_h.dup
36
+ $logger.level = :debug if opts['verbose']
37
+
38
+ begin
39
+ Gitlab.client.delete_tag(project, tag).inspect
40
+ rescue Gitlab::Error::NotFound
41
+ # Success: there is no such tag
42
+ end if opts['force']
43
+
44
+ puts Gitlab.client.create_tag(project, tag, options['ref'], options['message']).inspect
31
45
  end
32
46
 
33
- desc 'tag', "Tag Docker image without downloading"
34
- map %w[--tag] => :tag
47
+ desc "release <project> <tag>", "Create tag in Gitlab project"
48
+ long_desc <<-LONGDESC
49
+ LONGDESC
50
+
51
+ option :ref, default: ENV.fetch("#{ENV_PREFIX}_RELEASE_REF", 'master'),
52
+ required: true,
53
+ type: :string,
54
+ desc: "Gtilab refspec from which create tag"
55
+
56
+ option :description, default: nil,
57
+ type: :string,
58
+ desc: "description Markdown"
59
+ def release(project, tag)
60
+ Gitlab.client.create_project_release(project, name: tag, tag_name: tag, description: description, ref: options['ref'])
61
+ end
62
+ end
35
63
 
64
+ class DockerCommand < Thor
65
+ desc "tag", "Tag Docker image without downloading"
66
+ long_desc <<-LONGDESC
67
+ LONGDESC
36
68
  option :image, default: ENV.fetch("#{ENV_PREFIX}_TAG_IMAGE", nil),
37
69
  required: true,
38
70
  desc: "Env: #{ENV_PREFIX}_TAG_IMAGE. Full image to be tagged. Ex: docker.rnds.pro/aggredator/service-uprid3:latest"
39
71
 
40
72
  option :tag, default: ENV.fetch("#{ENV_PREFIX}_TAG_TAG", nil),
41
73
  required: true,
42
- desc: "Env: #{ENV_PREFIX}_TAG_TAG. New tag."
74
+ desc: "Env: #{ENV_PREFIX}_TAG_TAG. New tag"
43
75
 
44
76
  option :suffix, default: ENV.fetch("#{ENV_PREFIX}_TAG_SUFFIX", nil),
45
77
  required: false,
@@ -51,6 +83,8 @@ class Cli < Thor
51
83
 
52
84
  def tag(*_args)
53
85
  opts = options.to_h.dup
86
+ $logger.level = :debug if opts['verbose']
87
+
54
88
  img, tag = opts['image'].split(':')
55
89
  tag ||= 'latest'
56
90
  tag = opts['suffix'].present? ? "#{tag}-#{opts['suffix']}" : tag
@@ -59,10 +93,14 @@ class Cli < Thor
59
93
  image.retag!(opts['tag'])
60
94
  end
61
95
 
96
+ end
62
97
 
63
- desc 'release', 'Release projects bundler from release.yml manifest'
64
- map %w[--release] => :release
98
+ class ReleaseCommand < Thor
99
+ default_command :release
65
100
 
101
+ desc "release", "Release projects bundler from release.yml manifest"
102
+ long_desc <<-LONGDESC
103
+ LONGDESC
66
104
  option :config, default: ENV.fetch("#{ENV_PREFIX}_RELEASE_CONFIG", './release.yml'),
67
105
  required: true,
68
106
  desc: "Env: #{ENV_PREFIX}_RELEASE_CONFIG. Path to release manifest"
@@ -73,14 +111,17 @@ class Cli < Thor
73
111
 
74
112
  def release(*_args)
75
113
  opts = options.to_h.dup
76
- config = YAML.load_file(File.expand_path(opts['config']))
114
+ $logger.level = :debug if opts['verbose']
115
+
116
+ config = YAML.load(ERB.new(File.read(File.expand_path(opts['config']))).result)
77
117
  config['release'] = opts['name'] || config['release']
78
118
 
79
119
  release = config['release'].to_s
80
- suffix = config['suffix']
120
+ suffix = config['default-suffix']
121
+ branch = config['default-branch']
81
122
 
82
- services = config['services'].map do|(name, cfg)|
83
- Retag::Service.new(name, cfg, release, suffix: cfg.fetch('suffix', suffix)) unless name.start_with?('.')
123
+ services = config['services'].map do|(name, cfg)|
124
+ Retag::Service.new(name, cfg, release, suffix: cfg.fetch('suffix', suffix), branch: cfg.fetch('branch', branch)) unless name.start_with?('.')
84
125
  end.compact
85
126
 
86
127
  tag_message = "Release #{release} at #{DateTime.now.iso8601}"
@@ -120,4 +161,37 @@ class Cli < Thor
120
161
 
121
162
  end
122
163
 
164
+
165
+ class Cli < Thor
166
+
167
+ def self.exit_on_failure?
168
+ true
169
+ end
170
+
171
+ package_name "Retag is cli to manipulate docker images in handy way.
172
+
173
+ # Gitlab authorization
174
+ Using GITLAB_API_ENDPOINT and GITLAB_API_PRIVATE_TOKEN environment variable used for authorization
175
+
176
+ #{File.basename(__FILE__)}"
177
+
178
+ class_option :verbose, type: :boolean
179
+
180
+ desc 'version', 'Display version'
181
+ map %w[-v --version] => :version
182
+
183
+ def version
184
+ say Retag::VERSION
185
+ end
186
+
187
+ desc "repo SUBCOMMAND ...ARGS", "Manage git repository stuffs"
188
+ subcommand "repo", RepoCommand
189
+
190
+ desc "docker SUBCOMMAND ...ARGS", "Manage docker stuffs"
191
+ subcommand "docker", DockerCommand
192
+
193
+ desc "release SUBCOMMAND ...ARGS", "Manage releases"
194
+ subcommand "release", ReleaseCommand
195
+ end
196
+
123
197
  ::Cli.start
data/lib/retag/repo.rb CHANGED
@@ -31,37 +31,27 @@ module Retag
31
31
 
32
32
  def revision
33
33
  @revision ||= begin
34
- rev, _name = cmd!("git ls-remote #{repo} #{branch}", capture: true).split("\n").select{|line| line["heads/#{branch}"] }.first.split
34
+ # "^{}" means the object could be a tag. https://mirrors.edge.kernel.org/pub/software/scm/git/docs/gitrevisions.html#_specifying_revisions
35
+ # Example:
36
+ # git ls-remote git@gir.example.com:repo.git | grep release-latest
37
+ # 174cf2b6685df563e59c5cd0d367996fcf24f0ae refs/tags/release-latest <-- annotated tag itself
38
+ # 47208e276cd277896887417a383e05973278438c refs/tags/release-latest^{} <-- WE NEED: original commit tag is references to
39
+ rev_name_pair = remote_refs(repo, branch+"^{}") # try to find commit tag is references to
40
+ rev_name_pair ||= remote_refs(repo, branch) # try to find simple branch or commit
41
+
42
+ raise "There is no '#{branch}' revision at #{repo}" if rev_name_pair.nil?
43
+
44
+ rev, _name = rev_name_pair.split
35
45
  rev[0..7]
36
46
  end
37
47
  end
38
48
 
39
- end
40
- end
41
-
42
-
49
+ private
43
50
 
51
+ def remote_refs repo, branch
52
+ cmd!("git ls-remote #{repo} #{branch}", capture: true).split("\n").select{|line| line.strip.end_with?("/#{branch}") }.first
53
+ end
44
54
 
45
- # ==> /var/log/gitlab/gitlab-rails/api_json.log <==
46
- # {"time":"2022-01-21T10:48:07.308Z","severity":"INFO",
47
- # "duration_s":0.20214,"db_duration_s":0.00607,"view_duration_s":0.19607,"status":201,
48
- # "method":"POST","path":"/api/v4/projects/aggredator%2Fservices%2Fuprid3/repository/tags",
49
- # "params":[{"key":"tag_name","value":"testtag3"},{"key":"ref","value":"master"}],
50
- # "host":"br.rnds.pro","remote_ip":"213.138.65.74, 172.22.20.31, 213.138.65.74","ua":"curl/7.79.1",
51
- # "route":"/api/:version/projects/:id/repository/tags","user_id":38,"username":"bot",
52
- # "queue_duration_s":0.008366,"gitaly_calls":1,"gitaly_duration_s":0.16823,"redis_calls":6,
53
- # "redis_duration_s":0.004274,"redis_read_bytes":113,"redis_write_bytes":521,"redis_cache_calls":6,
54
- # "redis_cache_duration_s":0.004274,"redis_cache_read_bytes":113,"redis_cache_write_bytes":521,"db_count":10,
55
- # "db_write_count":0,"db_cached_count":0,"cpu_s":0.034309,"mem_objects":15111,"mem_bytes":920832,"mem_mallocs":3159,
56
- # "mem_total_bytes":1525272,"correlation_id":"01FSY40ANS3TD0CJJTDCQXGQ1N","meta.user":"bot","meta.project":"aggredator/services/uprid3",
57
- # "meta.root_namespace":"aggredator","meta.caller_id":"POST /api/:version/projects/:id/repository/tags",
58
- # "meta.remote_ip":"213.138.65.74","meta.feature_category":"source_code_management","meta.client_id":
59
- # "user/38","content_length":"0"}
60
-
61
- # ==> /var/log/gitlab/gitlab-rails/api_json.log <==
62
- # {"time":"2022-01-21T10:49:36.954Z","severity":"INFO",
63
- # "duration_s":0.01892,"db_duration_s":0.0036,"view_duration_s":0.01532,"status":400,
64
- # "method":"POST","path":"/api/v4/projects/aggredator%2Fservices%2Fuprid3/repository/tags",
65
- # "params":[{"key":"tag_name","value":"44444444"},{"key":"ref","value":"509a3719"},{"key":"message","value":{"message":"hello"}},{"key":"release_description","value":""}],"host":"br.rnds.pro","remote_ip":"213.138.65.74, 172.22.20.31, 213.138.65.74","ua":"Gitlab Ruby Gem 4.18.0","route":"/api/:version/projects/:id/repository/tags","user_id":38,"username":"bot","queue_duration_s":0.007279,"db_count":6,"db_write_count":0,"db_cached_count":0,"cpu_s":0.022143,"mem_objects":11483,"mem_bytes":783064,"mem_mallocs":2634,"mem_total_bytes":1242384,"correlation_id":"01FSY432CZMEFNDJQ88HMNW0E8","meta.user":"bot","meta.project":"aggredator/services/uprid3","meta.root_namespace":"aggredator","meta.caller_id":"POST /api/:version/projects/:id/repository/tags","meta.remote_ip":"213.138.65.74","meta.feature_category":"source_code_management","meta.client_id":"user/38","content_length":"78"}
66
-
55
+ end
56
+ end
67
57
 
data/lib/retag/service.rb CHANGED
@@ -9,10 +9,10 @@ module Retag
9
9
 
10
10
  delegate :revision, :branch, to: :repo
11
11
 
12
- def initialize(name, config, release, suffix: )
12
+ def initialize(name, config, release, branch:, suffix:)
13
13
  @name = name
14
14
  @config = config
15
- @repo = Retag::Repo.new(config['repo'], config.fetch('branch', 'master'))
15
+ @repo = Retag::Repo.new(config['repo'], branch)
16
16
  @release = release
17
17
  @suffix = suffix
18
18
  end
data/lib/retag/version.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Retag
4
4
 
5
- VERSION = '0.1.3'
5
+ VERSION = '0.1.7'
6
6
 
7
7
  end
8
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: retag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samoilenko Yuri