brasa 0.2.2 → 0.3.0

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: 5a49c4bccb1e9a9074615b14ea63f96a85163c5a8af134cbbf7fe8fa5c662b3a
4
- data.tar.gz: bdc9b16edbdf065340d199a1a4d254a2e49c51b641000bd701d09a35bd94af91
3
+ metadata.gz: 7285bea2b7d7a7f1db549db5faa7e0a3c4f60c15baf185210b46c0f8e162c792
4
+ data.tar.gz: c826e60cc8895e4ab2bb0aa26db0600b86abcd9fe1ff0d77f49d3701d07111c2
5
5
  SHA512:
6
- metadata.gz: 447bed84e8814068dc16e6eaae55fc82474acd5435e28da5e732376431c79c485e8f677d8f095598d399f5e56827328db86145e65e5bd43426c0172ff1743074
7
- data.tar.gz: be3aa908f2e07f3d84c9b52e26355a50e2bd469a3d3a01d9009a5552affeba50b7de0655ad55546bbe40614e310e09a54aa7402f17615e771a995b3be7b6d1fd
6
+ metadata.gz: dce0daf704b7880388fd0233fa0308152a871480aeace5fdd88cb31514049ae2ee83b96bbf936cb340db6a4a66a1a96ebbd7c55eba0a4708d116873201124d35
7
+ data.tar.gz: c3b1504e6fd93b18fcb813b3c4abd0f669674dff8f28dc74dcc49fad3357331351ad22db6642cedb89dd0ed0ff1a41eca55674ceb578e01286290ef247981f07
@@ -71,11 +71,18 @@ module Brasa
71
71
  raise AuthenticationError.new("Não autenticado. Execute `brasa login`.", status: 401, body: response.body)
72
72
  when 404
73
73
  raise NotFoundError.new("Recurso não encontrado.", status: 404, body: response.body)
74
+ when 403
75
+ msg = response.body&.dig("error") || "Sem permissão para esta ação"
76
+ raise ApiError.new(msg, status: 403, body: response.body)
77
+ when 409
78
+ msg = response.body&.dig("error") || "Conflito — recurso já existe ou operação em andamento"
79
+ raise ApiError.new(msg, status: 409, body: response.body)
74
80
  when 422
75
- msg = response.body&.dig("errors")&.join(", ") || "Dados inválidos"
81
+ msg = response.body&.dig("errors")&.join(", ") || response.body&.dig("error") || "Dados inválidos"
76
82
  raise ValidationError.new(msg, status: 422, body: response.body)
77
83
  else
78
- raise ApiError.new("Erro na API (#{response.status})", status: response.status, body: response.body)
84
+ msg = response.body&.dig("error") || "Erro na API (#{response.status})"
85
+ raise ApiError.new(msg, status: response.status, body: response.body)
79
86
  end
80
87
  end
81
88
  end
@@ -10,21 +10,24 @@ module Brasa
10
10
  require_auth!
11
11
  config = project_config
12
12
 
13
- info("Criando app #{config[:app]}...")
14
-
15
- app = create_app(config)
16
- info("App #{app["slug"]} criado. Provisionando infraestrutura...")
13
+ app = find_or_create_app(config)
14
+ slug = app["slug"]
17
15
 
18
- unless wait_for_provisioning(app["slug"])
19
- return
16
+ if app["status"] == "active"
17
+ info("Iniciando deploy...")
18
+ else
19
+ info("Provisionando infraestrutura...")
20
+ unless wait_for_provisioning(slug)
21
+ return
22
+ end
23
+ success("Infraestrutura provisionada!")
24
+ info("Iniciando primeiro deploy...")
20
25
  end
21
- success("Infraestrutura provisionada!")
22
26
 
23
- info("Iniciando primeiro deploy...")
24
- deploy = trigger_deploy(app["slug"])
27
+ deploy = trigger_deploy(slug)
25
28
 
26
- if wait_for_deploy(app["slug"], deploy["id"])
27
- success("Deploy concluído! App disponível em https://#{app["slug"]}.usebrasa.com.br")
29
+ if wait_for_deploy(slug, deploy["id"])
30
+ success("Deploy concluído! App disponível em https://#{slug}.usebrasa.com.br")
28
31
  end
29
32
  rescue Api::Client::ValidationError => e
30
33
  error("Erro ao criar app: #{e.message}")
@@ -34,12 +37,32 @@ module Brasa
34
37
 
35
38
  private
36
39
 
40
+ def find_or_create_app(config)
41
+ existing = api.get("/api/v1/apps/#{config[:app]}")
42
+ info("App #{existing["slug"]} encontrado (#{existing["status"]}).")
43
+ ensure_repo_url(existing["slug"])
44
+ existing
45
+ rescue Api::Client::NotFoundError
46
+ info("Criando app #{config[:app]}...")
47
+ create_app(config)
48
+ end
49
+
50
+ def ensure_repo_url(slug)
51
+ repo = detect_git_remote
52
+ return unless repo
53
+
54
+ api.patch("/api/v1/apps/#{slug}", body: { app: { repo_url: repo } })
55
+ rescue Api::Client::ApiError
56
+ # Não bloquear o fluxo se falhar ao atualizar repo
57
+ end
58
+
37
59
  def create_app(config)
38
60
  api.post("/api/v1/apps", body: {
39
61
  name: config[:app],
40
62
  stack: config[:stack],
41
63
  preset: config[:preset],
42
64
  region: config[:region],
65
+ repo_url: detect_git_remote,
43
66
  repo_branch: config[:branch]
44
67
  })
45
68
  end
@@ -48,6 +71,11 @@ module Brasa
48
71
  api.post("/api/v1/apps/#{slug}/deploys")
49
72
  end
50
73
 
74
+ def detect_git_remote
75
+ remote = `git remote get-url origin 2>/dev/null`.strip
76
+ remote.empty? ? nil : remote
77
+ end
78
+
51
79
  def wait_for_provisioning(slug)
52
80
  MAX_POLLS.times do
53
81
  app = api.get("/api/v1/apps/#{slug}")
data/lib/brasa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Brasa
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brasa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brasa