captive-cli 1.0.0 → 1.2.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.
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "json"
4
- require "open3"
5
- require_relative "errors"
6
-
7
- module Captive
8
- class GhCLI
9
- def trigger_workflow(repo, workflow: "ci.yml", ref: "main", fields: {})
10
- field_args = fields.flat_map { |k, v| [ "--field", "#{k}=#{v}" ] }
11
- run("workflow", "run", workflow, "--repo", repo, "--ref", ref, *field_args)
12
- rescue RuntimeError => e
13
- raise WorkflowDispatchNotConfigured,
14
- "Le workflow '#{workflow}' de '#{repo}' n'a pas de trigger workflow_dispatch.\n" \
15
- " Regénère le ci.yml : captive apps launch <app> dans le repo de l'app." \
16
- if e.message.include?("workflow_dispatch")
17
- raise
18
- end
19
-
20
- def workflow_exists?(repo)
21
- %w[deploy.yml ci.yml].any? do |file|
22
- run("api", "/repos/#{repo}/contents/.github/workflows/#{file}") && true
23
- rescue RuntimeError
24
- false
25
- end
26
- end
27
-
28
- def latest_run_status(repo)
29
- output = run("run", "list", "--repo", repo, "--limit", "1",
30
- "--json", "status,conclusion", "--jq", ".[0]")
31
- data = JSON.parse(output)
32
- return :success if data["status"] == "completed" && data["conclusion"] == "success"
33
- return :failure if data["status"] == "completed"
34
-
35
- :in_progress
36
- end
37
-
38
- def image_exists?(repo, tag)
39
- output = run("api", "/repos/#{repo}/packages/container/#{repo.split("/").last}/versions",
40
- "--jq", ".[].metadata.container.tags[]")
41
- output.split("\n").include?(tag)
42
- rescue RuntimeError => e
43
- return false if e.message.include?("404") || e.message.include?("Not Found")
44
-
45
- raise
46
- end
47
-
48
- def app_installed?(repo)
49
- run("api", "repos/#{repo}/installation")
50
- true
51
- rescue RuntimeError
52
- false
53
- end
54
-
55
- def detect_type(repo)
56
- data = JSON.parse(run("api", "repos/#{repo}"))
57
- case data["language"]
58
- when "Ruby" then "rails-app"
59
- when "JavaScript", "TypeScript" then "node-app"
60
- else raise UnknownAppType, "language non supportée : #{data["language"]}"
61
- end
62
- end
63
-
64
- PLATFORM_REPO = "captive-studio/captive-platform"
65
-
66
- def dispatch_build(repo)
67
- repo_name = repo.split("/").last.downcase
68
- sha = run("api", "repos/#{repo}/git/ref/heads/main", "--jq", ".object.sha").strip
69
- body = JSON.generate(
70
- event_type: "build-app",
71
- client_payload: { repo: repo, repo_name: repo_name, sha: sha, ref: "refs/heads/main" }
72
- )
73
- run_with_input("api", "repos/#{PLATFORM_REPO}/dispatches", "-X", "POST", "--input", "-",
74
- input: body)
75
- end
76
-
77
- def run(*args, input: nil)
78
- opts = input ? { stdin_data: input } : {}
79
- out, err, status = Open3.capture3("gh", *args, **opts)
80
- raise "gh #{args.join(" ")} failed: #{err}" unless status.success?
81
-
82
- out
83
- end
84
-
85
- def run_with_input(*args, input:)
86
- run(*args, input: input)
87
- end
88
- end
89
- end
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "pathname"
4
- require_relative "errors"
5
-
6
- module Captive
7
- # Où est le clone de captive-platform. Installé en gem, le CLI ne vit plus dans le monorepo :
8
- # la racine se trouve en remontant depuis le dossier courant, comme git trouve .git, ou se
9
- # désigne par CAPTIVE_PLATFORM_ROOT. Seul `apps rebuild` en a encore besoin (ADR 0018).
10
- module PlatformRoot
11
- ENV_VAR = "CAPTIVE_PLATFORM_ROOT"
12
-
13
- def self.find(start: Dir.pwd, env: ENV)
14
- env[ENV_VAR] || ascend(start) ||
15
- raise(PlatformRootNotFound, "Clone de captive-platform introuvable depuis #{start}. " \
16
- "Lance la commande depuis le clone ou définis #{ENV_VAR}.")
17
- end
18
-
19
- def self.ascend(start)
20
- Pathname.new(start).realpath.ascend
21
- .find { |dir| dir.join("services").directory? && dir.join(".sops.yaml").file? }&.to_s
22
- end
23
- private_class_method :ascend
24
- end
25
- end