renuo-cli 4.30.0 → 5.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e403749b46f9d289d45f218209831c68a8c624840f4df20e1315127d4ba2bbac
4
- data.tar.gz: 85cd1b725f516ab96fa5da67355c9268d8397eeb3b7db4017263b3583558f4d8
3
+ metadata.gz: 60777c11e01d643cc6ef4c0c8854fc1fa68ba8df183594351b006b6299ceef9e
4
+ data.tar.gz: d1e6dc528d8931203e70d02d44e4d380be2f158cbf1a96b647a63c6cb6c3986f
5
5
  SHA512:
6
- metadata.gz: 193344bdebb9952145a2c8e948e04b8ad85f8d04d0d642c8c0f00cd1173639454c7306a7c80665de5bcc68c7e78a5de74f4406012de7e4d61cda5d0a9a173211
7
- data.tar.gz: c6bd00ecd6bedcde704ab346dcfa3cdd0affae38a0413c457636c575c561b12432a9a06c1422de2673b0ff0a40bb8c7e03f66626321d7c756eb0e8ca5927ae0a
6
+ metadata.gz: 28bacf587a042f3b55763b27ff83c2dc3cc0b7ae2921c33e21f5ad627bf51a3a52065cc71b614bd57f1ef1757567bcb6a062d5ce27df866472345fa7b4ef291c
7
+ data.tar.gz: 11ff874aa94522fc89d761d6a595a9abe5f25805ce2c3cbed552508dc52d0077b692ebaa838522b5de6c3e0b551fe587fe02e9b07258d4c7dfb3b13646531c75
@@ -33,6 +33,7 @@ class Renuo::Cli::Commands::ConfigureSemaphore
33
33
 
34
34
  create_semaphore_notification
35
35
  create_semaphore_secrets
36
+ create_nctl_service_account_secret
36
37
  create_semaphore_deployment_targets
37
38
  end
38
39
 
@@ -52,6 +53,42 @@ class Renuo::Cli::Commands::ConfigureSemaphore
52
53
  system("sem create secret -p #{project_name} #{project_name}")
53
54
  end
54
55
 
56
+ DEPLOIO_ORGANIZATION = "renuo"
57
+
58
+ def deploio_project
59
+ "#{DEPLOIO_ORGANIZATION}-#{project_name}"
60
+ end
61
+
62
+ def service_account_name
63
+ "#{project_name}-semaphore"
64
+ end
65
+
66
+ def nctl_secret_name
67
+ "nctl-#{project_name}"
68
+ end
69
+
70
+ def create_nctl_service_account_secret
71
+ if system(nctl_create_service_account_command)
72
+ system(sem_create_nctl_secret_command)
73
+ else
74
+ warn("Could not create the API service account. Does the Deploio project #{deploio_project} exist? " \
75
+ "Create it with `renuo create-deploio-app`, then run:\n\n " \
76
+ "#{nctl_create_service_account_command}\n #{sem_create_nctl_secret_command}")
77
+ end
78
+ end
79
+
80
+ def nctl_create_service_account_command
81
+ "nctl create asa #{service_account_name} --project #{deploio_project} --wait"
82
+ end
83
+
84
+ def sem_create_nctl_secret_command
85
+ get_asa = "nctl get asa #{service_account_name} --project #{deploio_project}"
86
+ "sem create secret #{nctl_secret_name} -p #{project_name} " \
87
+ "-e NCTL_API_CLIENT_ID=\"$(#{get_asa} --print-client-id)\" " \
88
+ "-e NCTL_API_CLIENT_SECRET=\"$(#{get_asa} --print-client-secret)\" " \
89
+ "-e NCTL_ORGANIZATION=#{DEPLOIO_ORGANIZATION}"
90
+ end
91
+
55
92
  def create_semaphore_deployment_targets
56
93
  %w[main develop].each do |branch|
57
94
  system("sem create dt #{branch} -p #{project_name} --object-rule branch,exact,#{branch}")
@@ -175,7 +175,7 @@ class Renuo::Cli::Commands::CreateDeploioApp # rubocop:disable Metrics/ClassLeng
175
175
  --basic-auth=#{environment == "develop"} \\
176
176
  --build-env=SECRET_KEY_BASE="$(rails secret)" \\
177
177
  --language=ruby \\
178
- --buildpack-stack=heroku \\
178
+ --buildpack-type=heroku \\
179
179
  --size=mini \\
180
180
  --replicas=1
181
181
  OUTPUT
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../helpers/review_app_notification"
4
+
5
+ module Renuo::Cli::Commands::Notify
6
+ module Slack
7
+ class ReviewAppDeploy
8
+ include ReviewAppNotification
9
+ include Renuo::Cli::Services
10
+
11
+ command "notify:slack:review-app-deploy" do |c|
12
+ c.syntax = "renuo notify:slack:review-app-deploy [options]"
13
+ c.summary = "Posts the branch's review app URL (with its basic auth credentials) and pull request to Slack."
14
+ c.description = "#{c.summary} The Slack incoming webhook is read from SLACK_WEBHOOK_URL, the GitHub " \
15
+ "repository from SEMAPHORE_GIT_REPO_SLUG and bundler's GitHub credentials (a token or " \
16
+ "user:token) from BUNDLE_GITHUB__COM to link the branch's open pull request. On Semaphore, " \
17
+ "it links the workflow (SEMAPHORE_ORGANIZATION_URL, SEMAPHORE_WORKFLOW_ID) offering the " \
18
+ "review app promotions."
19
+ ReviewAppNotification.define_options(c)
20
+ c.example "renuo notify:slack:review-app-deploy -p renuo-acme -b feature/42-migrate-to-deploio " \
21
+ "--channel '#project-acme-notifications'",
22
+ "Announces the branch's review app in the project's Slack channel"
23
+ c.action { |_args, options| new(options).run }
24
+ end
25
+
26
+ COLOR = "#141d50"
27
+
28
+ private
29
+
30
+ def message_lines
31
+ credentials = basic_auth_credentials
32
+ pull_request = fetch_pull_request
33
+
34
+ [
35
+ "Review app is ready: #{app_link(credentials)}",
36
+ pull_request && pull_request_line(pull_request),
37
+ branch_line
38
+ ].compact
39
+ end
40
+
41
+ def pull_request_line(pull_request)
42
+ "Pull request: <#{pull_request["html_url"]}|##{pull_request["number"]} #{pull_request["title"]}>"
43
+ end
44
+
45
+ def app_link(credentials)
46
+ url = app_url
47
+ url.userinfo = userinfo(credentials) if credentials
48
+ "<#{url}|#{url.host}>"
49
+ end
50
+
51
+ def userinfo(credentials)
52
+ credentials.values_at("username", "password").map { |value| URI.encode_www_form_component(value) }.join(":")
53
+ end
54
+
55
+ def app_url
56
+ url = Deploio.fetch_resource("app", @app_name, @project).to_h.dig("status", "atProvider", "defaultURLs", 0)
57
+ URI(url || abort("#{@app_name} has no URL assigned yet"))
58
+ end
59
+
60
+ # Deploio assigns basic auth credentials per app, so the review app's differ from the origin's.
61
+ def basic_auth_credentials
62
+ stdout, = system!("nctl get app #{@app_name} -p #{@project} -o json --basic-auth-credentials", quiet: true)
63
+ Array(JSON.parse(stdout)).first&.dig("basicauth")
64
+ end
65
+
66
+ def fetch_pull_request
67
+ credentials = ENV.fetch("BUNDLE_GITHUB__COM", "")
68
+ return if credentials.empty?
69
+
70
+ response = Net::HTTP.start("api.github.com", 443, use_ssl: true) do |http|
71
+ http.request(pull_request_request(credentials))
72
+ end
73
+ return JSON.parse(response.body).first if response.is_a?(Net::HTTPSuccess)
74
+
75
+ warn "GitHub pull request lookup failed: #{response.code}"
76
+ nil
77
+ end
78
+
79
+ def pull_request_request(credentials)
80
+ query = URI.encode_www_form(state: "open", head: "#{@repo.split("/").first}:#{@branch}")
81
+ request = Net::HTTP::Get.new("/repos/#{@repo}/pulls?#{query}", "Accept" => "application/vnd.github+json")
82
+ user, token = credentials.split(":", 2)
83
+ request.basic_auth(user, token || "x-oauth-basic")
84
+ request
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../../../helpers/review_app_notification"
4
+
5
+ module Renuo::Cli::Commands::Notify
6
+ module Slack
7
+ class ReviewAppPurge
8
+ include ReviewAppNotification
9
+
10
+ command "notify:slack:review-app-purge" do |c|
11
+ c.syntax = "renuo notify:slack:review-app-purge [options]"
12
+ c.summary = "Announces on Slack that the branch's review app was purged."
13
+ c.description = "#{c.summary} The Slack incoming webhook is read from SLACK_WEBHOOK_URL and the GitHub " \
14
+ "repository from SEMAPHORE_GIT_REPO_SLUG. On Semaphore, it links the workflow " \
15
+ "(SEMAPHORE_ORGANIZATION_URL, SEMAPHORE_WORKFLOW_ID) offering the review app promotions."
16
+ ReviewAppNotification.define_options(c)
17
+ c.example "renuo notify:slack:review-app-purge -p renuo-acme -b feature/42-migrate-to-deploio " \
18
+ "--channel '#project-acme-notifications'",
19
+ "Announces that the branch's review app was purged"
20
+ c.action { |_args, options| new(options).run }
21
+ end
22
+
23
+ COLOR = "#9e9e9e"
24
+
25
+ private
26
+
27
+ def message_lines
28
+ ["Review app #{@app_name} was purged", branch_line]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -13,8 +13,9 @@ These are the steps of command execution in the happy path:
13
13
  1. Create or update `renuo review-app:deploy`
14
14
  1. _Intentional gap for configuration. Consider this the wiggle-room for LLMs_
15
15
  1. Start the paused app `renuo review-app:start`
16
- 1. Announce it on Slack `renuo review-app:notify`
16
+ 1. Announce it on Slack `renuo notify:slack:review-app-deploy`
17
17
  1. Cleanup `renuo review-app:purge`
18
+ 1. Announce the cleanup on Slack `renuo notify:slack:review-app-purge`
18
19
 
19
20
  ## Design Choices
20
21
 
@@ -28,6 +29,9 @@ These are the steps of command execution in the happy path:
28
29
  * All `nctl` commands are routed through `system!` and are logged in a command trail for auditing.
29
30
  * Configuration of system tools like `psql` is intentionally out of scope.
30
31
  But CI templating can give you some pointers.
31
- * `review-app:notify` posts the review app URL (carrying its Deploio-assigned basic auth credentials) and the
32
- branch's open pull request to a Slack incoming webhook (`SLACK_WEBHOOK_URL`). The pull request lookup uses
32
+ * `notify:slack:review-app-deploy` posts the review app URL (carrying its Deploio-assigned basic auth credentials) and
33
+ the branch's open pull request to a Slack incoming webhook (`SLACK_WEBHOOK_URL`). The pull request lookup uses
33
34
  bundler's `BUNDLE_GITHUB__COM` (a token or `user:token`) and never fails the pipeline when GitHub refuses it.
35
+ Incoming webhooks can't edit messages, so `notify:slack:review-app-purge` posts a separate one once purged.
36
+ On Semaphore, both messages link the branch's workflow offering the promotions. It's a link, not a button:
37
+ buttons posted by an incoming webhook show a warning unless they come from an interactive Slack app.
@@ -16,7 +16,7 @@ module Renuo::Cli::Commands::ReviewApp
16
16
  c.description = c.summary
17
17
  c.option "-p", "--project <name>", String, "The name of the Deploio project"
18
18
  c.option "--origin-pattern <pattern>", String, "Name pattern identifying the origin app to copy from"
19
- c.option "--slack-channel <name>", String, "The Slack channel notified once a review app is ready"
19
+ c.option "--slack-channel <name>", String, "The Slack channel notified when a review app is ready or purged"
20
20
  c.action { |_args, options| new(options).run }
21
21
  end
22
22
 
@@ -25,7 +25,8 @@ module Renuo::Cli::Commands::ReviewApp
25
25
  write_or_warn DEPLOY_PIPELINE_TARGET_FILE,
26
26
  render(DEPLOY_PIPELINE_TEMPLATE_FILE,
27
27
  { project:, origin_pattern:, secret_name:, slack_channel: })
28
- write_or_warn PURGE_PIPELINE_TARGET_FILE, render(PURGE_PIPELINE_TEMPLATE_FILE, { project:, secret_name: })
28
+ write_or_warn PURGE_PIPELINE_TARGET_FILE,
29
+ render(PURGE_PIPELINE_TEMPLATE_FILE, { project:, secret_name:, slack_channel: })
29
30
  print_instructions
30
31
  end
31
32
 
@@ -75,7 +76,8 @@ module Renuo::Cli::Commands::ReviewApp
75
76
  pipeline_file: review-app-purge.yml
76
77
 
77
78
  Both buttons appear on every branch workflow in Semaphore. Deploy and purge the review app
78
- of a branch by clicking the promotion on that branch's workflow.
79
+ of a branch by clicking the promotion on that branch's workflow. The Slack message announcing
80
+ a review app links that workflow, so it can be purged from there.
79
81
  INSTRUCTIONS
80
82
  end
81
83
 
@@ -7,6 +7,10 @@ module ReviewAppHelper
7
7
  MAX_NUMBER_LENGTH = 10
8
8
  NAME_PATTERN = /\Areview-app-(?:\d{1,#{MAX_NUMBER_LENGTH}}-)?\h{#{HASH_LENGTH}}\z/
9
9
  MIN_PATTERN_LENGTH = 3
10
+ # Deploio keeps modifying an app for a while after a change, so nctl updates right after it can conflict.
11
+ RECONCILIATION_RETRY_DELAYS = [1, 4, 16].freeze
12
+
13
+ class FailedAttempt < StandardError; end
10
14
 
11
15
  def review_app_name(branch)
12
16
  number = branch[/\d+/]&.slice(0, MAX_NUMBER_LENGTH)
@@ -26,12 +30,12 @@ module ReviewAppHelper
26
30
  abort "'#{command}' printed no nctl resources:\n#{stdout}"
27
31
  end
28
32
 
29
- def system!(command, env = {}, quiet: false, abort_on_failure: true)
33
+ def system!(command, env = {}, quiet: false, abort_on_failure: true, retry_after: [])
30
34
  CmdTrail.record(command, env)
31
- stdout, stderr, status = Open3.capture3(env, command)
35
+ stdout, stderr, status = capture_retrying(command, env, retry_after)
32
36
  puts stdout unless quiet || stdout.empty?
33
37
  if !status.success? && abort_on_failure
34
- abort "'#{command.split.first(2).join(" ")}' failed: #{stderr.presence || stdout}"
38
+ abort "'#{command_name(command)}' failed: #{stderr.presence || stdout}"
35
39
  end
36
40
  [stdout, stderr, status]
37
41
  end
@@ -43,4 +47,28 @@ module ReviewAppHelper
43
47
  "AWS_DEFAULT_REGION" => "us-east-1"
44
48
  }
45
49
  end
50
+
51
+ private
52
+
53
+ def capture_retrying(command, env, retry_after)
54
+ delays = retry_after.dup
55
+ begin
56
+ stdout, stderr, status = Open3.capture3(env, command)
57
+ raise FailedAttempt if !status.success? && delays.any?
58
+
59
+ [stdout, stderr, status]
60
+ rescue FailedAttempt
61
+ await_retry(command, delays.shift, stderr.presence || stdout)
62
+ retry
63
+ end
64
+ end
65
+
66
+ def await_retry(command, delay, reason)
67
+ puts "'#{command_name(command)}' failed, retrying in #{delay}s: #{reason}"
68
+ Kernel.sleep(delay)
69
+ end
70
+
71
+ def command_name(command)
72
+ command.split.first(2).join(" ")
73
+ end
46
74
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "review_app_helper"
4
+ require_relative "safe_review_app_name_contract"
5
+ require_relative "../services/review_app_slack_webhook"
6
+
7
+ module ReviewAppNotification
8
+ include ReviewAppHelper
9
+ include Contracts::Core
10
+
11
+ def self.define_options(command)
12
+ command.option "-p", "--project <name>", String, "The name of the Deploio project"
13
+ command.option "-b", "--branch <name>", String, "The Git branch"
14
+ command.option "--channel <name>", String, "The Slack channel to post to"
15
+ command.option "--repo <owner/name>", String, "The GitHub repository, defaults to SEMAPHORE_GIT_REPO_SLUG"
16
+ end
17
+
18
+ def run
19
+ @slack.post([*message_lines, workflow_line].compact.join("\n"), color: self.class::COLOR)
20
+ CmdTrail::Print.call
21
+ puts "\nnotified #{@channel}"
22
+ end
23
+
24
+ private
25
+
26
+ def initialize(options)
27
+ @project = options.project || abort("missing project name")
28
+ @branch = options.branch || abort("missing branch name")
29
+ @channel = options.channel || abort("missing Slack channel")
30
+ @repo = options.repo || ENV.fetch("SEMAPHORE_GIT_REPO_SLUG", nil).presence ||
31
+ abort("missing GitHub repository, set SEMAPHORE_GIT_REPO_SLUG or pass --repo")
32
+ @slack = Renuo::Cli::Services::ReviewAppSlackWebhook.new(@channel)
33
+ derive_app_name(@branch)
34
+ end
35
+
36
+ Contract String => SafeReviewAppNameContract
37
+ def derive_app_name(branch)
38
+ @app_name = review_app_name(branch)
39
+ end
40
+
41
+ def branch_line
42
+ "Branch: <https://github.com/#{@repo}/tree/#{@branch}|#{@branch}>"
43
+ end
44
+
45
+ # The deploy and purge promotions both run within the branch's workflow, which offers the other one.
46
+ # A link, because a button posted by an incoming webhook shows a warning without an interactive Slack app.
47
+ def workflow_line
48
+ organization_url, workflow_id = ENV.values_at("SEMAPHORE_ORGANIZATION_URL", "SEMAPHORE_WORKFLOW_ID")
49
+ return if organization_url.blank? || workflow_id.blank?
50
+
51
+ "Workflow: <#{organization_url}/workflows/#{workflow_id}|Open on Semaphore>"
52
+ end
53
+ end
@@ -28,14 +28,13 @@ class Renuo::Cli::Services::Deploio
28
28
  private
29
29
 
30
30
  def reconfigure_copy
31
- Kernel.sleep(1) # Deploio usually needs a moment to finish reconciling the fresh copy
32
31
  flags = [delete_flag("--delete-env", dropped_keys("config", "env", kind: "runtime")),
33
32
  delete_flag("--delete-build-env", dropped_keys("buildEnv", kind: "build"))].compact
34
33
  dropping = flags.empty? ? "" : " and dropping the copied env variables"
35
34
  puts "pointing #{target_app_name} at branch #{branch}#{dropping}..."
36
35
  system! ["nctl update app #{target_app_name} -p #{project}",
37
36
  "--git-revision #{Shellwords.escape(branch)} --skip-repo-access-check",
38
- *flags].join(" ")
37
+ *flags].join(" "), retry_after: RECONCILIATION_RETRY_DELAYS
39
38
  end
40
39
 
41
40
  def delete_flag(flag, keys)
@@ -13,7 +13,7 @@ class Renuo::Cli::Services::Deploio
13
13
  def call
14
14
  puts "starting app #{target_app_name}..."
15
15
  system! "nctl update app #{target_app_name} -p #{project} --git-revision #{Shellwords.escape(branch)} " \
16
- "--skip-repo-access-check --no-pause"
16
+ "--skip-repo-access-check --no-pause", retry_after: RECONCILIATION_RETRY_DELAYS
17
17
  end
18
18
  end
19
19
  end
@@ -19,7 +19,8 @@ class Renuo::Cli::Services::Deploio
19
19
  puts "updating env of app #{target_app_name}..."
20
20
  system! ["nctl update app #{target_app_name} -p #{project}",
21
21
  env_flag("--env", env.except(*SENSITIVE_ENV_KEYS)),
22
- env_flag("--sensitive-env", env.slice(*SENSITIVE_ENV_KEYS))].compact.join(" ")
22
+ env_flag("--sensitive-env", env.slice(*SENSITIVE_ENV_KEYS))].compact.join(" "),
23
+ retry_after: RECONCILIATION_RETRY_DELAYS
23
24
  end
24
25
 
25
26
  private
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Renuo::Cli::Services::ReviewAppSlackWebhook
4
+ SENDER = { username: "Deploio Review Apps", icon_url: "https://deploio.ch/meta/apple-touch-icon.png" }.freeze
5
+
6
+ def initialize(channel)
7
+ @channel = channel
8
+ @url = ENV.fetch("SLACK_WEBHOOK_URL", nil).presence || abort("SLACK_WEBHOOK_URL is not set")
9
+ end
10
+
11
+ # The color bar on the side comes from the (legacy) attachment, which an incoming webhook still renders.
12
+ def post(text, color:)
13
+ attachment = { color: color, text: text, mrkdwn_in: ["text"] }
14
+ payload = SENDER.merge(channel: @channel, attachments: [attachment]).to_json
15
+ response = Net::HTTP.post(URI(@url), payload, "Content-Type" => "application/json")
16
+ return if response.is_a?(Net::HTTPSuccess)
17
+
18
+ abort "Slack refused the notification: #{response.code} #{response.body}"
19
+ end
20
+ end
@@ -43,4 +43,4 @@ blocks:
43
43
  # - renuo review-app:deploy --project <%= project %> --branch $SEMAPHORE_GIT_WORKING_BRANCH --origin-pattern <%= origin_pattern %> --keep-envs=build,sensitive-build,runtime,sensitive-runtime --start
44
44
  - renuo review-app:start --project <%= project %> --branch $SEMAPHORE_GIT_WORKING_BRANCH
45
45
  - renuo ci check-deploio-status $(renuo review-app:name --branch $SEMAPHORE_GIT_WORKING_BRANCH) --project <%= project %>
46
- - renuo review-app:notify --project <%= project %> --branch $SEMAPHORE_GIT_WORKING_BRANCH --channel '<%= slack_channel %>'
46
+ - renuo notify:slack:review-app-deploy --project <%= project %> --branch $SEMAPHORE_GIT_WORKING_BRANCH --channel '<%= slack_channel %>'
@@ -19,3 +19,4 @@ blocks:
19
19
  - gem i -q renuo-cli
20
20
  - renuo ci setup
21
21
  - renuo review-app:purge --project <%= project %> --branch $SEMAPHORE_GIT_WORKING_BRANCH
22
+ - renuo notify:slack:review-app-purge --project <%= project %> --branch $SEMAPHORE_GIT_WORKING_BRANCH --channel '<%= slack_channel %>'
@@ -13,7 +13,7 @@ blocks:
13
13
  task:
14
14
  secrets:
15
15
  - name: <%= project_name %>
16
- - name: nctl
16
+ - name: <%= nctl_secret_name %>
17
17
  jobs:
18
18
  - name: deploying
19
19
  commands:
@@ -21,6 +21,7 @@ global_job_config:
21
21
  prologue:
22
22
  commands:
23
23
  - checkout --use-cache
24
+ - sem-version ruby $(cat .ruby-version)
24
25
  - source .semaphore/bin/cache_restore rails
25
26
  - bundle config set deployment 'true'
26
27
  - bundle config set path 'vendor/bundle'
@@ -3,7 +3,7 @@
3
3
  # simplecov:disable
4
4
  module Renuo
5
5
  class Cli
6
- VERSION = "4.30.0"
6
+ VERSION = "5.0.1"
7
7
  NAME = "renuo-cli"
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,189 +1,203 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: renuo-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.30.0
4
+ version: 5.0.1
5
5
  platform: ruby
6
6
  authors:
7
- - Renuo AG
7
+ - Renuo AG
8
8
  bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: activesupport
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - ">="
17
- - !ruby/object:Gem::Version
18
- version: '7.1'
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- version: '7.1'
26
- - !ruby/object:Gem::Dependency
27
- name: colorize
28
- requirement: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - "~>"
31
- - !ruby/object:Gem::Version
32
- version: '1.1'
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '1.1'
40
- - !ruby/object:Gem::Dependency
41
- name: commander
42
- requirement: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - "~>"
45
- - !ruby/object:Gem::Version
46
- version: '5.0'
47
- type: :runtime
48
- prerelease: false
49
- version_requirements: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '5.0'
54
- - !ruby/object:Gem::Dependency
55
- name: contracts
56
- requirement: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '0.17'
61
- type: :runtime
62
- prerelease: false
63
- version_requirements: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '0.17'
68
- - !ruby/object:Gem::Dependency
69
- name: service_actor
70
- requirement: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '3.9'
75
- type: :runtime
76
- prerelease: false
77
- version_requirements: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '3.9'
82
- description: The Renuo CLI automates some commonly used workflows by providing a command
83
- line interface.
12
+ - !ruby/object:Gem::Dependency
13
+ name: activesupport
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: "7.1"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: "7.1"
28
+ - !ruby/object:Gem::Dependency
29
+ name: colorize
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ -
33
+ - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: "1.1"
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ -
41
+ - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: "1.1"
44
+ - !ruby/object:Gem::Dependency
45
+ name: commander
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ -
49
+ - ~>
50
+ - !ruby/object:Gem::Version
51
+ version: "5.0"
52
+ type: :runtime
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ -
57
+ - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: "5.0"
60
+ - !ruby/object:Gem::Dependency
61
+ name: contracts
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ -
65
+ - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: "0.17"
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ -
73
+ - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: "0.17"
76
+ - !ruby/object:Gem::Dependency
77
+ name: service_actor
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ -
81
+ - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: "3.9"
84
+ type: :runtime
85
+ prerelease: false
86
+ version_requirements: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ -
89
+ - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: "3.9"
92
+ description: The Renuo CLI automates some commonly used workflows by providing a command line interface.
84
93
  email:
85
- - info@renuo.ch
94
+ - info@renuo.ch
86
95
  executables:
87
- - renuo
96
+ - renuo
88
97
  extensions: []
89
98
  extra_rdoc_files: []
90
99
  files:
91
- - LICENSE.txt
92
- - README.md
93
- - exe/renuo
94
- - lib/renuo/cli.rb
95
- - lib/renuo/cli/commands/ci/check_deploio_status.rb
96
- - lib/renuo/cli/commands/ci/setup.rb
97
- - lib/renuo/cli/commands/ci/update_deploio_app.rb
98
- - lib/renuo/cli/commands/commit_leaderboard_stage.rb
99
- - lib/renuo/cli/commands/commit_leaderboard_sync.rb
100
- - lib/renuo/cli/commands/configure_semaphore.rb
101
- - lib/renuo/cli/commands/configure_sentry.rb
102
- - lib/renuo/cli/commands/create_aws_project.rb
103
- - lib/renuo/cli/commands/create_deploio_app.rb
104
- - lib/renuo/cli/commands/create_deploio_object_storage.rb
105
- - lib/renuo/cli/commands/create_heroku_app.rb
106
- - lib/renuo/cli/commands/create_new_logins.rb
107
- - lib/renuo/cli/commands/create_nine_cloudvm.rb
108
- - lib/renuo/cli/commands/create_pr.rb
109
- - lib/renuo/cli/commands/create_slidev_presentation.rb
110
- - lib/renuo/cli/commands/debug.rb
111
- - lib/renuo/cli/commands/debug/all.rb
112
- - lib/renuo/cli/commands/display_name.rb
113
- - lib/renuo/cli/commands/fetch_emails.rb
114
- - lib/renuo/cli/commands/fetch_secrets.rb
115
- - lib/renuo/cli/commands/generate_password.rb
116
- - lib/renuo/cli/commands/github_replace.rb
117
- - lib/renuo/cli/commands/heroku_users.rb
118
- - lib/renuo/cli/commands/list_large_git_files.rb
119
- - lib/renuo/cli/commands/release.rb
120
- - lib/renuo/cli/commands/review_app/README.md
121
- - lib/renuo/cli/commands/review_app/deploy.rb
122
- - lib/renuo/cli/commands/review_app/name.rb
123
- - lib/renuo/cli/commands/review_app/notify.rb
124
- - lib/renuo/cli/commands/review_app/purge.rb
125
- - lib/renuo/cli/commands/review_app/setup_semaphoreci.rb
126
- - lib/renuo/cli/commands/review_app/start.rb
127
- - lib/renuo/cli/commands/setup_autocompletion.rb
128
- - lib/renuo/cli/commands/setup_uptimerobot.rb
129
- - lib/renuo/cli/commands/upgrade_laptop.rb
130
- - lib/renuo/cli/credentials.yml.enc
131
- - lib/renuo/cli/helpers/actor_contracts.rb
132
- - lib/renuo/cli/helpers/cmd_trail.rb
133
- - lib/renuo/cli/helpers/command_helper.rb
134
- - lib/renuo/cli/helpers/commander_introspector.rb
135
- - lib/renuo/cli/helpers/credentials_helper.rb
136
- - lib/renuo/cli/helpers/environments.rb
137
- - lib/renuo/cli/helpers/renuo_version.rb
138
- - lib/renuo/cli/helpers/review_app_helper.rb
139
- - lib/renuo/cli/helpers/safe_review_app_name_contract.rb
140
- - lib/renuo/cli/helpers/zsh_completion_script.rb
141
- - lib/renuo/cli/services/cache.rb
142
- - lib/renuo/cli/services/cloudfront_config_service.rb
143
- - lib/renuo/cli/services/deploio.rb
144
- - lib/renuo/cli/services/deploio/copy_app.rb
145
- - lib/renuo/cli/services/deploio/copy_database_data.rb
146
- - lib/renuo/cli/services/deploio/copy_s3_data.rb
147
- - lib/renuo/cli/services/deploio/create_database.rb
148
- - lib/renuo/cli/services/deploio/create_kvs.rb
149
- - lib/renuo/cli/services/deploio/create_s3.rb
150
- - lib/renuo/cli/services/deploio/inventorize_origin_resources.rb
151
- - lib/renuo/cli/services/deploio/start_target_app.rb
152
- - lib/renuo/cli/services/deploio/update_target_app_env.rb
153
- - lib/renuo/cli/services/heroku.rb
154
- - lib/renuo/cli/services/hetzner.rb
155
- - lib/renuo/cli/services/renuo_cli_config.rb
156
- - lib/renuo/cli/templates/semaphore/bin/cache_restore.erb
157
- - lib/renuo/cli/templates/semaphore/bin/cache_store.erb
158
- - lib/renuo/cli/templates/semaphore/notification.yml.erb
159
- - lib/renuo/cli/templates/semaphore/review-app-deploy.yml.erb
160
- - lib/renuo/cli/templates/semaphore/review-app-purge.yml.erb
161
- - lib/renuo/cli/templates/semaphore/semaphore-deploy.yml.erb
162
- - lib/renuo/cli/templates/semaphore/semaphore.yml.erb
163
- - lib/renuo/cli/templates/slidev/README.md.erb
164
- - lib/renuo/cli/templates/slidev/package.json.erb
165
- - lib/renuo/cli/templates/zsh_completion.erb
166
- - lib/renuo/cli/version.rb
167
- homepage: https://github.com/renuo/renuo-cli
100
+ - LICENSE.txt
101
+ - README.md
102
+ - exe/renuo
103
+ - lib/renuo/cli.rb
104
+ - lib/renuo/cli/commands/ci/check_deploio_status.rb
105
+ - lib/renuo/cli/commands/ci/setup.rb
106
+ - lib/renuo/cli/commands/ci/update_deploio_app.rb
107
+ - lib/renuo/cli/commands/commit_leaderboard_stage.rb
108
+ - lib/renuo/cli/commands/commit_leaderboard_sync.rb
109
+ - lib/renuo/cli/commands/configure_semaphore.rb
110
+ - lib/renuo/cli/commands/configure_sentry.rb
111
+ - lib/renuo/cli/commands/create_aws_project.rb
112
+ - lib/renuo/cli/commands/create_deploio_app.rb
113
+ - lib/renuo/cli/commands/create_deploio_object_storage.rb
114
+ - lib/renuo/cli/commands/create_heroku_app.rb
115
+ - lib/renuo/cli/commands/create_new_logins.rb
116
+ - lib/renuo/cli/commands/create_nine_cloudvm.rb
117
+ - lib/renuo/cli/commands/create_pr.rb
118
+ - lib/renuo/cli/commands/create_slidev_presentation.rb
119
+ - lib/renuo/cli/commands/debug.rb
120
+ - lib/renuo/cli/commands/debug/all.rb
121
+ - lib/renuo/cli/commands/display_name.rb
122
+ - lib/renuo/cli/commands/fetch_emails.rb
123
+ - lib/renuo/cli/commands/fetch_secrets.rb
124
+ - lib/renuo/cli/commands/generate_password.rb
125
+ - lib/renuo/cli/commands/github_replace.rb
126
+ - lib/renuo/cli/commands/heroku_users.rb
127
+ - lib/renuo/cli/commands/list_large_git_files.rb
128
+ - lib/renuo/cli/commands/notify/slack/review_app_deploy.rb
129
+ - lib/renuo/cli/commands/notify/slack/review_app_purge.rb
130
+ - lib/renuo/cli/commands/release.rb
131
+ - lib/renuo/cli/commands/review_app/README.md
132
+ - lib/renuo/cli/commands/review_app/deploy.rb
133
+ - lib/renuo/cli/commands/review_app/name.rb
134
+ - lib/renuo/cli/commands/review_app/purge.rb
135
+ - lib/renuo/cli/commands/review_app/setup_semaphoreci.rb
136
+ - lib/renuo/cli/commands/review_app/start.rb
137
+ - lib/renuo/cli/commands/setup_autocompletion.rb
138
+ - lib/renuo/cli/commands/setup_uptimerobot.rb
139
+ - lib/renuo/cli/commands/upgrade_laptop.rb
140
+ - lib/renuo/cli/credentials.yml.enc
141
+ - lib/renuo/cli/helpers/actor_contracts.rb
142
+ - lib/renuo/cli/helpers/cmd_trail.rb
143
+ - lib/renuo/cli/helpers/command_helper.rb
144
+ - lib/renuo/cli/helpers/commander_introspector.rb
145
+ - lib/renuo/cli/helpers/credentials_helper.rb
146
+ - lib/renuo/cli/helpers/environments.rb
147
+ - lib/renuo/cli/helpers/renuo_version.rb
148
+ - lib/renuo/cli/helpers/review_app_helper.rb
149
+ - lib/renuo/cli/helpers/review_app_notification.rb
150
+ - lib/renuo/cli/helpers/safe_review_app_name_contract.rb
151
+ - lib/renuo/cli/helpers/zsh_completion_script.rb
152
+ - lib/renuo/cli/services/cache.rb
153
+ - lib/renuo/cli/services/cloudfront_config_service.rb
154
+ - lib/renuo/cli/services/deploio.rb
155
+ - lib/renuo/cli/services/deploio/copy_app.rb
156
+ - lib/renuo/cli/services/deploio/copy_database_data.rb
157
+ - lib/renuo/cli/services/deploio/copy_s3_data.rb
158
+ - lib/renuo/cli/services/deploio/create_database.rb
159
+ - lib/renuo/cli/services/deploio/create_kvs.rb
160
+ - lib/renuo/cli/services/deploio/create_s3.rb
161
+ - lib/renuo/cli/services/deploio/inventorize_origin_resources.rb
162
+ - lib/renuo/cli/services/deploio/start_target_app.rb
163
+ - lib/renuo/cli/services/deploio/update_target_app_env.rb
164
+ - lib/renuo/cli/services/heroku.rb
165
+ - lib/renuo/cli/services/hetzner.rb
166
+ - lib/renuo/cli/services/renuo_cli_config.rb
167
+ - lib/renuo/cli/services/review_app_slack_webhook.rb
168
+ - lib/renuo/cli/templates/semaphore/bin/cache_restore.erb
169
+ - lib/renuo/cli/templates/semaphore/bin/cache_store.erb
170
+ - lib/renuo/cli/templates/semaphore/notification.yml.erb
171
+ - lib/renuo/cli/templates/semaphore/review-app-deploy.yml.erb
172
+ - lib/renuo/cli/templates/semaphore/review-app-purge.yml.erb
173
+ - lib/renuo/cli/templates/semaphore/semaphore-deploy.yml.erb
174
+ - lib/renuo/cli/templates/semaphore/semaphore.yml.erb
175
+ - lib/renuo/cli/templates/slidev/README.md.erb
176
+ - lib/renuo/cli/templates/slidev/package.json.erb
177
+ - lib/renuo/cli/templates/zsh_completion.erb
178
+ - lib/renuo/cli/version.rb
179
+ homepage: "https://github.com/renuo/renuo-cli"
168
180
  licenses:
169
- - MIT
181
+ - MIT
170
182
  metadata:
171
- rubygems_mfa_required: 'true'
183
+ rubygems_mfa_required: "true"
172
184
  rdoc_options: []
173
185
  require_paths:
174
- - lib
186
+ - lib
175
187
  required_ruby_version: !ruby/object:Gem::Requirement
176
188
  requirements:
177
- - - ">="
178
- - !ruby/object:Gem::Version
179
- version: 3.2.0
189
+ -
190
+ - ">="
191
+ - !ruby/object:Gem::Version
192
+ version: 3.2.0
180
193
  required_rubygems_version: !ruby/object:Gem::Requirement
181
194
  requirements:
182
- - - ">="
183
- - !ruby/object:Gem::Version
184
- version: '0'
195
+ -
196
+ - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: "0"
185
199
  requirements: []
186
- rubygems_version: 4.0.23
200
+ rubygems_version: 4.1.0
187
201
  specification_version: 4
188
202
  summary: The Renuo CLI automates some common workflows.
189
203
  test_files: []
@@ -1,120 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "../../helpers/safe_review_app_name_contract"
4
-
5
- module Renuo::Cli::Commands::ReviewApp
6
- class Notify
7
- include ReviewAppHelper
8
- include Renuo::Cli::Services
9
- include Contracts::Core
10
-
11
- command "review-app:notify" do |c|
12
- c.syntax = "renuo review-app:notify [options]"
13
- c.summary = "Posts the branch's review app URL (with its basic auth credentials) and pull request to Slack."
14
- c.description = "#{c.summary} The Slack incoming webhook is read from SLACK_WEBHOOK_URL, the GitHub " \
15
- "repository from SEMAPHORE_GIT_REPO_SLUG and bundler's GitHub credentials (a token or " \
16
- "user:token) from BUNDLE_GITHUB__COM to link the branch's open pull request."
17
- c.option "-p", "--project <name>", String, "The name of the Deploio project"
18
- c.option "-b", "--branch <name>", String, "The Git branch"
19
- c.option "--channel <name>", String, "The Slack channel to post to"
20
- c.option "--repo <owner/name>", String, "The GitHub repository, defaults to SEMAPHORE_GIT_REPO_SLUG"
21
- c.example "renuo review-app:notify -p renuo-acme -b feature/42-migrate-to-deploio " \
22
- "--channel '#project-acme-notifications'",
23
- "Announces the branch's review app in the project's Slack channel"
24
- c.action { |_args, options| new(options).run }
25
- end
26
-
27
- def run
28
- post_to_slack(message_lines.join("\n"))
29
- CmdTrail::Print.call
30
- puts "\nnotified #{@channel}"
31
- end
32
-
33
- private
34
-
35
- def initialize(options)
36
- @project = options.project || abort("missing project name")
37
- @branch = options.branch || abort("missing branch name")
38
- @channel = options.channel || abort("missing Slack channel")
39
- @repo = options.repo || ENV.fetch("SEMAPHORE_GIT_REPO_SLUG", nil).presence ||
40
- abort("missing GitHub repository, set SEMAPHORE_GIT_REPO_SLUG or pass --repo")
41
- @webhook_url = ENV.fetch("SLACK_WEBHOOK_URL", nil).presence || abort("SLACK_WEBHOOK_URL is not set")
42
- derive_app_name(@branch)
43
- end
44
-
45
- Contract String => SafeReviewAppNameContract
46
- def derive_app_name(branch)
47
- @app_name = review_app_name(branch)
48
- end
49
-
50
- def message_lines
51
- credentials = basic_auth_credentials
52
- pull_request = fetch_pull_request
53
-
54
- [
55
- "Review app is ready: #{app_link(credentials)}",
56
- pull_request && pull_request_line(pull_request),
57
- "Branch: <https://github.com/#{@repo}/tree/#{@branch}|#{@branch}>"
58
- ].compact
59
- end
60
-
61
- def pull_request_line(pull_request)
62
- "Pull request: <#{pull_request["html_url"]}|##{pull_request["number"]} #{pull_request["title"]}>"
63
- end
64
-
65
- def app_link(credentials)
66
- url = app_url
67
- url.userinfo = userinfo(credentials) if credentials
68
- "<#{url}|#{url.host}>"
69
- end
70
-
71
- def userinfo(credentials)
72
- credentials.values_at("username", "password").map { |value| URI.encode_www_form_component(value) }.join(":")
73
- end
74
-
75
- def app_url
76
- url = Deploio.fetch_resource("app", @app_name, @project).to_h.dig("status", "atProvider", "defaultURLs", 0)
77
- URI(url || abort("#{@app_name} has no URL assigned yet"))
78
- end
79
-
80
- # Deploio assigns basic auth credentials per app, so the review app's differ from the origin's.
81
- def basic_auth_credentials
82
- stdout, = system!("nctl get app #{@app_name} -p #{@project} -o json --basic-auth-credentials", quiet: true)
83
- Array(JSON.parse(stdout)).first&.dig("basicauth")
84
- end
85
-
86
- def fetch_pull_request
87
- credentials = ENV.fetch("BUNDLE_GITHUB__COM", "")
88
- return if credentials.empty?
89
-
90
- response = Net::HTTP.start("api.github.com", 443, use_ssl: true) do |http|
91
- http.request(pull_request_request(credentials))
92
- end
93
- return JSON.parse(response.body).first if response.is_a?(Net::HTTPSuccess)
94
-
95
- warn "GitHub pull request lookup failed: #{response.code}"
96
- nil
97
- end
98
-
99
- def pull_request_request(credentials)
100
- query = URI.encode_www_form(state: "open", head: "#{@repo.split("/").first}:#{@branch}")
101
- request = Net::HTTP::Get.new("/repos/#{@repo}/pulls?#{query}", "Accept" => "application/vnd.github+json")
102
- user, token = credentials.split(":", 2)
103
- request.basic_auth(user, token || "x-oauth-basic")
104
- request
105
- end
106
-
107
- SENDER = { username: "Deploio Review Apps", icon_url: "https://deploio.ch/meta/apple-touch-icon.png" }.freeze
108
- DEPLOIO_BLUE = "#141d50"
109
-
110
- # The color bar on the side comes from the (legacy) attachment, which an incoming webhook still renders.
111
- def post_to_slack(text)
112
- attachment = { color: DEPLOIO_BLUE, text: text, mrkdwn_in: ["text"] }
113
- payload = SENDER.merge(channel: @channel, attachments: [attachment]).to_json
114
- response = Net::HTTP.post(URI(@webhook_url), payload, "Content-Type" => "application/json")
115
- return if response.is_a?(Net::HTTPSuccess)
116
-
117
- abort "Slack refused the notification: #{response.code} #{response.body}"
118
- end
119
- end
120
- end