shipyrd 0.4.0 → 0.4.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 +4 -4
- data/.rubocop.yml +33 -0
- data/AGENTS.md +109 -0
- data/CLAUDE.md +1 -0
- data/Gemfile +9 -2
- data/Guardfile +4 -3
- data/README.md +36 -0
- data/Rakefile +4 -2
- data/lib/shipyrd/application.rb +20 -4
- data/lib/shipyrd/client.rb +259 -228
- data/lib/shipyrd/configuration.rb +57 -55
- data/lib/shipyrd/destination.rb +112 -91
- data/lib/shipyrd/logger.rb +12 -10
- data/lib/shipyrd/user.rb +8 -6
- data/lib/shipyrd/version.rb +1 -1
- data/shipyrd.gemspec +1 -0
- metadata +5 -2
- data/.standard.yml +0 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3409deaaaca01ad90e38d4b65cebe32f045be73346db55e0e4829e9bb9821db3
|
|
4
|
+
data.tar.gz: c0d149c65e8960b48ab9395157eff0de516009a27834a40c0ab339b678bf23d0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e1b91d84865573794c5dc3dbcf2bb8fcab8039fe015d1c3a9973315d8f610acbd86ab309da1471c49af1bcc53089bdafa70d79cc33059be8566150db96847060
|
|
7
|
+
data.tar.gz: bb371dad80381abe6bc7b6763865c321a446f3d337a6d77c5449671cadbc507e68ed572d54a5a2a8f307fd14376e3795f5de2c78fbfba03019a13e6dd6829400
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
plugins:
|
|
2
|
+
- rubocop-performance
|
|
3
|
+
|
|
4
|
+
AllCops:
|
|
5
|
+
NewCops: enable
|
|
6
|
+
SuggestExtensions: false
|
|
7
|
+
TargetRubyVersion: 3.0
|
|
8
|
+
Exclude:
|
|
9
|
+
- "bin/**/*"
|
|
10
|
+
- "vendor/**/*"
|
|
11
|
+
- "tmp/**/*"
|
|
12
|
+
|
|
13
|
+
Style/StringLiterals:
|
|
14
|
+
EnforcedStyle: double_quotes
|
|
15
|
+
Style/StringLiteralsInInterpolation:
|
|
16
|
+
EnforcedStyle: double_quotes
|
|
17
|
+
|
|
18
|
+
Layout/SpaceInsideHashLiteralBraces:
|
|
19
|
+
EnforcedStyle: no_space
|
|
20
|
+
|
|
21
|
+
Layout/ArgumentAlignment:
|
|
22
|
+
EnforcedStyle: with_fixed_indentation
|
|
23
|
+
Layout/MultilineMethodCallIndentation:
|
|
24
|
+
EnforcedStyle: indented
|
|
25
|
+
|
|
26
|
+
Layout/LineLength:
|
|
27
|
+
Enabled: false
|
|
28
|
+
Metrics:
|
|
29
|
+
Enabled: false
|
|
30
|
+
Style/Documentation:
|
|
31
|
+
Enabled: false
|
|
32
|
+
Style/FrozenStringLiteralComment:
|
|
33
|
+
Enabled: false
|
data/AGENTS.md
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# shipyrd/shipyrd-gem
|
|
2
|
+
|
|
3
|
+
Guidance for agents working in this repository. `CLAUDE.md` is a symlink to this
|
|
4
|
+
file.
|
|
5
|
+
|
|
6
|
+
## What this gem does
|
|
7
|
+
|
|
8
|
+
The companion gem for [Shipyrd](https://shipyrd.io). It hooks into Kamal and pings
|
|
9
|
+
a Shipyrd dashboard on deploy start and finish, and wraps Shipyrd's JSON API
|
|
10
|
+
(`Shipyrd::Client`) for applications, destinations, locks and tokens.
|
|
11
|
+
|
|
12
|
+
This is a **published gem**, so treat its public API as public: a change that
|
|
13
|
+
forces users to bump a dependency needs to be the explicit intent, not a
|
|
14
|
+
side effect. `shipyrd/shipyrd` and `shipyrd/mcp` both consume it.
|
|
15
|
+
|
|
16
|
+
## Commands
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
bin/ci # the whole suite — see below
|
|
20
|
+
bin/test # tests only
|
|
21
|
+
bin/console # IRB with the gem loaded
|
|
22
|
+
bundle exec rubocop # lint; -a safe autocorrect, -A includes unsafe
|
|
23
|
+
bundle exec rake # test + rubocop (the default task)
|
|
24
|
+
bundle exec guard # watch mode (tests + lint)
|
|
25
|
+
bin/release 0.4.1 # bump, tag, push, publish
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
**`bin/ci` is the definition of green. Always run it locally first. Never wait on
|
|
29
|
+
GitHub Actions to tell you something `bin/ci` already knows** —
|
|
30
|
+
`.github/workflows/main.yml` runs this very script, so a remote run is the same
|
|
31
|
+
suite, minutes slower, and tells you less. Run `bin/ci` before opening a PR,
|
|
32
|
+
after every push to one, and before calling anything finished.
|
|
33
|
+
|
|
34
|
+
Its steps, in order: rubocop, `rake test`, undercover (coverage on changed lines —
|
|
35
|
+
must follow the tests, which write `coverage/coverage.json`), then `gh signoff` on
|
|
36
|
+
success.
|
|
37
|
+
|
|
38
|
+
`bin/ci` is a plain shell script here, not the Rails
|
|
39
|
+
`ActiveSupport::ContinuousIntegration` shim that `shipyrd/shipyrd` and
|
|
40
|
+
`shipyrd/mcp` use: this is a gem, and taking activesupport as a development
|
|
41
|
+
dependency just to print step banners is not worth it.
|
|
42
|
+
|
|
43
|
+
Note that `bin/test` runs the `test` task, not `default`, so it does **not** lint.
|
|
44
|
+
`bin/ci` is the gate.
|
|
45
|
+
|
|
46
|
+
### What GitHub Actions runs
|
|
47
|
+
|
|
48
|
+
`.github/workflows/main.yml` runs one job per Ruby — 3.3, 3.4 and 4.0 — with a
|
|
49
|
+
named step per check, mirroring `bin/ci`. The two are kept in step by hand: a check
|
|
50
|
+
added to `bin/ci` belongs in the workflow too. Checks stop at the first failure, which
|
|
51
|
+
the 16-second run makes cheap.
|
|
52
|
+
|
|
53
|
+
Two steps behave differently on a runner:
|
|
54
|
+
|
|
55
|
+
- **`gh signoff` is skipped**, keyed on `ENV["GITHUB_ACTIONS"]`. The runner has no
|
|
56
|
+
such gh extension, and a workflow already reports its own conclusion.
|
|
57
|
+
- **Undercover reports nothing.** With no `--compare` ref it diffs the working
|
|
58
|
+
tree, and a fresh checkout is clean. Coverage on changed lines is therefore a
|
|
59
|
+
local gate only — one more reason the local run is the one that counts.
|
|
60
|
+
|
|
61
|
+
Lint is **rubocop** with `rubocop-performance`. No omakase, no `standard`. There is
|
|
62
|
+
no `rubocop-rails` here: this is a plain gem, not a Rails app. A clean tree reports
|
|
63
|
+
**zero** offences, so any output is something the current change introduced.
|
|
64
|
+
Rubocop's "unsafe" autocorrections can change behaviour, so always run the tests
|
|
65
|
+
after `-A`.
|
|
66
|
+
|
|
67
|
+
`.rubocop.yml` sets `TargetRubyVersion: 3.0` to match the gemspec's
|
|
68
|
+
`required_ruby_version = ">= 3.0.7"`. Raising one means raising the other.
|
|
69
|
+
|
|
70
|
+
**Users need Ruby >= 3.0.7; contributors need >= 3.2.** The gem itself still
|
|
71
|
+
supports the gemspec floor, and rubocop lints the library against it, but the
|
|
72
|
+
development bundle does not reach that far back: `undercover` declares
|
|
73
|
+
`required_ruby_version >= 3.2.0`, so `bundle install` cannot resolve on 3.0 or
|
|
74
|
+
3.1. CI covers 3.3, 3.4 and 4.0. If you need to check library behaviour on 3.0,
|
|
75
|
+
run it against the gemspec rather than through this Gemfile.
|
|
76
|
+
|
|
77
|
+
Note also that `SimpleCov.cover` in `test/test_helper.rb` is the current API and
|
|
78
|
+
`track_files` is its deprecated predecessor — simplecov 1.x prints a deprecation
|
|
79
|
+
notice pointing at `cover`, so do not "fix" it in the other direction.
|
|
80
|
+
|
|
81
|
+
There is no archspec here, unlike the Rails apps: `lib/shipyrd` is flat, with no
|
|
82
|
+
layers to assert.
|
|
83
|
+
|
|
84
|
+
`Style/FrozenStringLiteralComment` is off, matching the other repos: on Ruby 4 the
|
|
85
|
+
magic comment is not required.
|
|
86
|
+
|
|
87
|
+
## Comments
|
|
88
|
+
|
|
89
|
+
Add a comment only when it is absolutely necessary. Prefer making the code
|
|
90
|
+
self-explanatory, and let the tests be the reference for how something behaves —
|
|
91
|
+
a test states the intent and fails when the intent is broken, which no comment
|
|
92
|
+
does. Comments and code both have to be changed when behaviour changes, so a
|
|
93
|
+
comment that restates the code is a second thing to maintain and a second thing
|
|
94
|
+
to get out of date.
|
|
95
|
+
|
|
96
|
+
## Layout
|
|
97
|
+
|
|
98
|
+
- `lib/shipyrd/client.rb` — the HTTP client and the whole public API surface
|
|
99
|
+
- `lib/shipyrd/configuration.rb` — reads the `KAMAL_*` and `SHIPYRD_*` environment
|
|
100
|
+
- `lib/shipyrd/application.rb`, `destination.rb`, `user.rb` — API response wrappers
|
|
101
|
+
- `bin/pre-build`, `bin/pre-connect`, `bin/pre-deploy`, `bin/post-deploy` — the
|
|
102
|
+
Kamal hook shims users copy into their own apps
|
|
103
|
+
|
|
104
|
+
## Testing
|
|
105
|
+
|
|
106
|
+
Minitest with mocha and WebMock. No test may reach a real Shipyrd — WebMock
|
|
107
|
+
stubs every request. SimpleCov writes `coverage/coverage.json` on every run,
|
|
108
|
+
which is what `bin/ci`'s undercover step reads. `Gemfile.lock` is gitignored, so
|
|
109
|
+
there is no lockfile to commit.
|
data/CLAUDE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
AGENTS.md
|
data/Gemfile
CHANGED
|
@@ -8,10 +8,17 @@ gemspec
|
|
|
8
8
|
gem "gem-release", "~> 2.2"
|
|
9
9
|
gem "guard", "~> 2.18"
|
|
10
10
|
gem "guard-minitest", "~> 2.4"
|
|
11
|
-
gem "guard-
|
|
11
|
+
gem "guard-rubocop", "~> 1.5"
|
|
12
12
|
gem "minitest", "~> 5.20"
|
|
13
13
|
gem "minitest-reporters", "~> 1.3"
|
|
14
14
|
gem "minitest-silence"
|
|
15
15
|
gem "mocha", "~> 3.1.0"
|
|
16
16
|
gem "rake", "~> 13.0"
|
|
17
|
-
|
|
17
|
+
|
|
18
|
+
gem "rubocop"
|
|
19
|
+
gem "rubocop-performance"
|
|
20
|
+
|
|
21
|
+
gem "simplecov"
|
|
22
|
+
gem "undercover"
|
|
23
|
+
|
|
24
|
+
gem "webmock", "~> 4.0"
|
data/Guardfile
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
guard :
|
|
1
|
+
guard :rubocop, all_on_start: false do
|
|
2
2
|
watch(/.+\.rb$/)
|
|
3
|
+
watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
|
|
3
4
|
end
|
|
4
5
|
|
|
5
6
|
guard :minitest, all_on_start: false do
|
|
6
7
|
watch(%r{^test/(.*/)?([^/]+)_test\.rb$})
|
|
7
|
-
watch(%r{^lib/(.*/)?([^/]+)\.rb$})
|
|
8
|
+
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) do |m|
|
|
8
9
|
"test/#{m[1]}#{m[2]}_test.rb"
|
|
9
|
-
|
|
10
|
+
end
|
|
10
11
|
watch(%r{^test/test_helper\.rb$}) { "test" }
|
|
11
12
|
end
|
data/README.md
CHANGED
|
@@ -111,6 +111,36 @@ client.destination("production", application: "shipyrd-web")
|
|
|
111
111
|
`destination` takes the same arguments as `lock_destination` and raises the same
|
|
112
112
|
`Shipyrd::Destination::NotFound` and `AmbiguousApplication`.
|
|
113
113
|
|
|
114
|
+
## Creating applications and destinations
|
|
115
|
+
|
|
116
|
+
```ruby
|
|
117
|
+
client.create_application(name: "Billing", organization_id: 7, repository_url: "https://github.com/acme/billing")
|
|
118
|
+
# => {"id", "slug", "name", "organization" => {"id", "name"}, "destinations" => [], "setup_url", ...}
|
|
119
|
+
|
|
120
|
+
client.create_destination("staging", application: "billing", branch: "develop")
|
|
121
|
+
# => {"name", "branch", "locked", ...}
|
|
122
|
+
client.create_destination(nil, application: "billing") # the default destination
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`create_application` needs the `organization_id` to create it in; `client.me`
|
|
126
|
+
lists your organizations. An organization you are not in raises
|
|
127
|
+
`Shipyrd::Client::NotFound`. The answer never includes the application's deploy
|
|
128
|
+
token: `setup_url` is the page that shows it. Destinations are created
|
|
129
|
+
separately, with `create_destination`.
|
|
130
|
+
|
|
131
|
+
`create_destination` takes the same arguments as `destination`, plus `branch` and
|
|
132
|
+
`url`. A `nil`, blank or `"default"` name creates the default destination.
|
|
133
|
+
|
|
134
|
+
Anything Shipyrd will not save, such as a taken slug or a name the application
|
|
135
|
+
already has, raises `Shipyrd::Client::Invalid`. Its `#errors` is Shipyrd's
|
|
136
|
+
answer by attribute, e.g. `{"slug" => ["has already been taken"]}`, and its
|
|
137
|
+
message puts that in a sentence.
|
|
138
|
+
|
|
139
|
+
Neither is retried. If an answer is lost, check `client.applications` before
|
|
140
|
+
trying again: retrying `create_application` without a `slug` creates a second
|
|
141
|
+
application (`billing-2`), and retrying with a slug or a destination name raises
|
|
142
|
+
`Invalid` for something that was in fact created.
|
|
143
|
+
|
|
114
144
|
## Who a token belongs to
|
|
115
145
|
|
|
116
146
|
```ruby
|
|
@@ -127,6 +157,12 @@ it needs a user's API token, and raises the same `Shipyrd::Client` errors.
|
|
|
127
157
|
|
|
128
158
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
129
159
|
|
|
160
|
+
`bin/ci` is the full suite — lint, tests and coverage on changed lines — and is what CI runs too, so run it before opening a pull request. Lint is vanilla [RuboCop](https://rubocop.org) with `rubocop-performance`; coverage on changed lines is [undercover](https://github.com/grodowski/undercover).
|
|
161
|
+
|
|
162
|
+
```
|
|
163
|
+
bin/ci
|
|
164
|
+
```
|
|
165
|
+
|
|
130
166
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, run `bin/release x.y.z`, which will run tests, change the version in version.rb, commit the version change, create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
131
167
|
|
|
132
168
|
## Contributing
|
data/Rakefile
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
require "bundler/gem_tasks"
|
|
4
4
|
require "rake/testtask"
|
|
5
|
-
require "
|
|
5
|
+
require "rubocop/rake_task"
|
|
6
|
+
|
|
7
|
+
RuboCop::RakeTask.new
|
|
6
8
|
|
|
7
9
|
Rake::TestTask.new(:test) do |t|
|
|
8
10
|
t.libs << "test"
|
|
@@ -10,4 +12,4 @@ Rake::TestTask.new(:test) do |t|
|
|
|
10
12
|
t.test_files = FileList["test/**/*_test.rb"]
|
|
11
13
|
end
|
|
12
14
|
|
|
13
|
-
task default: %i[test
|
|
15
|
+
task default: %i[test rubocop]
|
data/lib/shipyrd/application.rb
CHANGED
|
@@ -1,8 +1,24 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
module Shipyrd
|
|
4
|
+
class Application
|
|
5
|
+
def self.all(client: Shipyrd::Client.new, organization_id: nil)
|
|
6
|
+
query = organization_id ? {organization_id: organization_id} : {}
|
|
7
|
+
client.request(:get, "/applications", query: query)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# POST /applications as JSON, in the organization organization_id names.
|
|
11
|
+
# Returns the new application, with its setup_url but never its deploy
|
|
12
|
+
# token. Anything Shipyrd will not save, such as a taken slug, raises
|
|
13
|
+
# Shipyrd::Client::Invalid.
|
|
14
|
+
def self.create(name:, organization_id:, slug: nil, repository_url: nil, client: Shipyrd::Client.new)
|
|
15
|
+
raise ArgumentError, "organization_id is required" if organization_id.nil?
|
|
16
|
+
|
|
17
|
+
application = {name: name, slug: slug, repository_url: repository_url}.compact
|
|
18
|
+
|
|
19
|
+
client.request(:post, "/applications", body: {organization_id: organization_id, application: application})
|
|
20
|
+
rescue Shipyrd::Client::UnprocessableEntity => e
|
|
21
|
+
raise Shipyrd::Client::Invalid.new(status: e.status, body: e.body)
|
|
22
|
+
end
|
|
7
23
|
end
|
|
8
24
|
end
|
data/lib/shipyrd/client.rb
CHANGED
|
@@ -1,276 +1,307 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
1
|
+
module Shipyrd
|
|
2
|
+
class Client
|
|
3
|
+
attr_reader :host, :api_key, :logger, :valid_configuration
|
|
4
|
+
|
|
5
|
+
ENV_VARS = %w[
|
|
6
|
+
KAMAL_COMMAND
|
|
7
|
+
KAMAL_DESTINATION
|
|
8
|
+
KAMAL_HOSTS
|
|
9
|
+
KAMAL_PERFORMER
|
|
10
|
+
KAMAL_RECORDED_AT
|
|
11
|
+
KAMAL_ROLE
|
|
12
|
+
KAMAL_RUNTIME
|
|
13
|
+
KAMAL_SERVICE_VERSION
|
|
14
|
+
KAMAL_SUBCOMMAND
|
|
15
|
+
KAMAL_VERSION
|
|
16
|
+
SHIPYRD_API_KEY
|
|
17
|
+
SHIPYRD_COMMAND
|
|
18
|
+
SHIPYRD_COMMIT_MESSAGE
|
|
19
|
+
SHIPYRD_DESTINATION
|
|
20
|
+
SHIPYRD_HOST
|
|
21
|
+
SHIPYRD_HOSTS
|
|
22
|
+
SHIPYRD_PERFORMER
|
|
23
|
+
SHIPYRD_RECORDED_AT
|
|
24
|
+
SHIPYRD_ROLE
|
|
25
|
+
SHIPYRD_RUNTIME
|
|
26
|
+
SHIPYRD_SERVICE_VERSION
|
|
27
|
+
SHIPYRD_SUBCOMMAND
|
|
28
|
+
SHIPYRD_VERSION
|
|
29
|
+
].freeze
|
|
30
|
+
|
|
31
|
+
class DestinationBlocked < StandardError; end
|
|
32
|
+
|
|
33
|
+
# Raised by #request, so by everything built on it, like Shipyrd::Destination.
|
|
34
|
+
# Unlike #trigger, which logs a failure so a deploy hook never breaks a deploy,
|
|
35
|
+
# a request raises: the caller asked for something and needs to know whether it
|
|
36
|
+
# happened. #body is the API's JSON error body, which says what went wrong.
|
|
37
|
+
class Error < StandardError
|
|
38
|
+
attr_reader :status, :body
|
|
39
|
+
|
|
40
|
+
def initialize(message = nil, status: nil, body: {})
|
|
41
|
+
@status = status
|
|
42
|
+
@body = body.is_a?(Hash) ? body : {}
|
|
43
|
+
super(message || @body["error"] || "Shipyrd responded #{status}")
|
|
44
|
+
end
|
|
43
45
|
end
|
|
44
|
-
end
|
|
45
46
|
|
|
46
|
-
|
|
47
|
-
|
|
47
|
+
# 401: the token is missing, unknown, or not the kind this request needs.
|
|
48
|
+
class Unauthorized < Error; end
|
|
48
49
|
|
|
49
|
-
|
|
50
|
+
class NotFound < Error; end
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
# 409
|
|
53
|
+
class Conflict < Error; end
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
# 422
|
|
56
|
+
class UnprocessableEntity < Error; end
|
|
56
57
|
|
|
57
|
-
|
|
58
|
+
# A 422 from a create: what Shipyrd would not save. #errors is Shipyrd's
|
|
59
|
+
# answer, by attribute, e.g. {"slug" => ["has already been taken"]}, and the
|
|
60
|
+
# message puts it in a sentence.
|
|
61
|
+
class Invalid < UnprocessableEntity
|
|
62
|
+
WHOLE_RECORD = %w[base error].freeze
|
|
58
63
|
|
|
59
|
-
|
|
60
|
-
|
|
64
|
+
def initialize(status: nil, body: {})
|
|
65
|
+
problems = body.flat_map do |attribute, messages|
|
|
66
|
+
next Array(messages) if WHOLE_RECORD.include?(attribute.to_s)
|
|
61
67
|
|
|
62
|
-
|
|
63
|
-
|
|
68
|
+
Array(messages).map { |message| "#{attribute.to_s.tr("_", " ").capitalize} #{message}" }
|
|
69
|
+
end
|
|
70
|
+
super(problems.empty? ? nil : problems.join(", "), status: status, body: body)
|
|
71
|
+
end
|
|
64
72
|
|
|
65
|
-
|
|
73
|
+
def errors = body
|
|
74
|
+
end
|
|
66
75
|
|
|
67
|
-
|
|
68
|
-
@host = parse_host(host)
|
|
69
|
-
@api_key = api_key
|
|
70
|
-
@logger = options[:logger] || Shipyrd::Logger.new
|
|
71
|
-
@valid_configuration = validate_configuration
|
|
72
|
-
end
|
|
76
|
+
class RateLimited < Error; end
|
|
73
77
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
config = Shipyrd.configuration
|
|
78
|
-
uri = URI("#{host}/deploys.json")
|
|
79
|
-
headers = {
|
|
80
|
-
"Content-Type": "application/json",
|
|
81
|
-
"Authorization": "Bearer #{api_key}"
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
details = {
|
|
85
|
-
deploy: {
|
|
86
|
-
status: event,
|
|
87
|
-
recorded_at: config.recorded_at,
|
|
88
|
-
performer: performer,
|
|
89
|
-
commit_message: config.commit_message || commit_message,
|
|
90
|
-
version: config.version,
|
|
91
|
-
service_version: config.service_version,
|
|
92
|
-
hosts: config.hosts,
|
|
93
|
-
command: config.command,
|
|
94
|
-
subcommand: config.subcommand,
|
|
95
|
-
role: config.role,
|
|
96
|
-
destination: config.destination,
|
|
97
|
-
runtime: config.runtime
|
|
98
|
-
}
|
|
99
|
-
}
|
|
78
|
+
# A 5xx, a timeout, or no connection at all.
|
|
79
|
+
class Unavailable < Error; end
|
|
100
80
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
http.write_timeout = 5
|
|
104
|
-
http.open_timeout = 5
|
|
105
|
-
http.use_ssl = true
|
|
106
|
-
request = Net::HTTP::Post.new(uri.request_uri, headers)
|
|
107
|
-
request.body = details.to_json
|
|
81
|
+
# A status or body this client does not know how to read.
|
|
82
|
+
class UnexpectedResponse < Error; end
|
|
108
83
|
|
|
109
|
-
|
|
84
|
+
NETWORK_ERRORS = [Timeout::Error, SocketError, SystemCallError, IOError, OpenSSL::SSL::SSLError].freeze
|
|
110
85
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
86
|
+
def initialize(host: Shipyrd.configuration.host, api_key: Shipyrd.configuration.api_key, **options)
|
|
87
|
+
@host = parse_host(host)
|
|
88
|
+
@api_key = api_key
|
|
89
|
+
@logger = options[:logger] || Shipyrd::Logger.new
|
|
90
|
+
@valid_configuration = validate_configuration
|
|
91
|
+
end
|
|
115
92
|
|
|
116
|
-
|
|
117
|
-
|
|
93
|
+
def trigger(event)
|
|
94
|
+
return false unless valid_configuration
|
|
95
|
+
|
|
96
|
+
config = Shipyrd.configuration
|
|
97
|
+
uri = URI("#{host}/deploys.json")
|
|
98
|
+
headers = {
|
|
99
|
+
"Content-Type": "application/json",
|
|
100
|
+
Authorization: "Bearer #{api_key}"
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
details = {
|
|
104
|
+
deploy: {
|
|
105
|
+
status: event,
|
|
106
|
+
recorded_at: config.recorded_at,
|
|
107
|
+
performer: performer,
|
|
108
|
+
commit_message: config.commit_message || commit_message,
|
|
109
|
+
version: config.version,
|
|
110
|
+
service_version: config.service_version,
|
|
111
|
+
hosts: config.hosts,
|
|
112
|
+
command: config.command,
|
|
113
|
+
subcommand: config.subcommand,
|
|
114
|
+
role: config.role,
|
|
115
|
+
destination: config.destination,
|
|
116
|
+
runtime: config.runtime
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
121
|
+
http.read_timeout = 5
|
|
122
|
+
http.write_timeout = 5
|
|
123
|
+
http.open_timeout = 5
|
|
124
|
+
http.use_ssl = true
|
|
125
|
+
request = Net::HTTP::Post.new(uri.request_uri, headers)
|
|
126
|
+
request.body = details.to_json
|
|
127
|
+
|
|
128
|
+
response = http.request(request)
|
|
129
|
+
|
|
130
|
+
if response.is_a?(Net::HTTPSuccess)
|
|
131
|
+
logger.info "#{event} triggered successfully for #{details[:deploy][:service_version]}"
|
|
132
|
+
elsif response.is_a?(Net::HTTPUnprocessableEntity)
|
|
133
|
+
json_response = JSON.parse(response.body)
|
|
134
|
+
|
|
135
|
+
if (lock = json_response.dig("errors", "lock"))
|
|
136
|
+
raise DestinationBlocked, lock
|
|
137
|
+
else
|
|
138
|
+
logger.info "#{event} trigger failed with errors => #{json_response["errors"]}"
|
|
139
|
+
end
|
|
118
140
|
else
|
|
119
|
-
logger.info "#{event} trigger failed with
|
|
141
|
+
logger.info "#{event} trigger failed with #{response.code}(#{response.message})"
|
|
120
142
|
end
|
|
121
|
-
|
|
122
|
-
|
|
143
|
+
rescue DestinationBlocked
|
|
144
|
+
raise
|
|
145
|
+
rescue StandardError => e
|
|
146
|
+
logger.info "#{event} trigger failed with error => #{e}"
|
|
123
147
|
end
|
|
124
|
-
rescue DestinationBlocked
|
|
125
|
-
raise
|
|
126
|
-
rescue => e
|
|
127
|
-
logger.info "#{event} trigger failed with error => #{e}"
|
|
128
|
-
end
|
|
129
148
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
149
|
+
# Helpers for the Shipyrd API, in the style of twilio-ruby: create one client
|
|
150
|
+
# with a user's API token and call these on it. Each delegates to the class
|
|
151
|
+
# that knows that part of the API, so nothing has to be built per call.
|
|
152
|
+
#
|
|
153
|
+
# client = Shipyrd::Client.new(host: "https://api.shipyrd.io", api_key: user_api_token)
|
|
154
|
+
# client.me
|
|
155
|
+
# client.lock_destination("production", application: "shipyrd-web")
|
|
156
|
+
# client.unlock_destination("production", application: "shipyrd-web")
|
|
157
|
+
|
|
158
|
+
# Who the API token belongs to. See Shipyrd::User.me.
|
|
159
|
+
def me
|
|
160
|
+
Shipyrd::User.me(client: self)
|
|
161
|
+
end
|
|
143
162
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
163
|
+
def applications(organization_id: nil)
|
|
164
|
+
Shipyrd::Application.all(client: self, organization_id: organization_id)
|
|
165
|
+
end
|
|
147
166
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
167
|
+
def destination(name, application:, organization_id: nil)
|
|
168
|
+
destination_for(name, application, organization_id).get
|
|
169
|
+
end
|
|
151
170
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
171
|
+
# Locks a destination as the token's user. name nil or "" is the destination
|
|
172
|
+
# with a blank name; application is the slug or id; organization_id is only
|
|
173
|
+
# needed when the slug is in several of the user's organizations. Returns the
|
|
174
|
+
# destination's state, and raises Shipyrd::Destination::LockHeld if someone
|
|
175
|
+
# else holds it. See Shipyrd::Destination#lock.
|
|
176
|
+
def lock_destination(name, application:, organization_id: nil)
|
|
177
|
+
destination_for(name, application, organization_id).lock
|
|
178
|
+
end
|
|
160
179
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
180
|
+
# Unlocks a destination, whoever holds the lock. See Shipyrd::Destination#unlock.
|
|
181
|
+
def unlock_destination(name, application:, organization_id: nil)
|
|
182
|
+
destination_for(name, application, organization_id).unlock
|
|
183
|
+
end
|
|
165
184
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
raise ArgumentError, "an API key is required" unless api_key
|
|
185
|
+
def create_application(name:, organization_id:, slug: nil, repository_url: nil)
|
|
186
|
+
Shipyrd::Application.create(
|
|
187
|
+
name: name, organization_id: organization_id, slug: slug, repository_url: repository_url, client: self
|
|
188
|
+
)
|
|
189
|
+
end
|
|
172
190
|
|
|
173
|
-
|
|
174
|
-
|
|
191
|
+
def create_destination(name, application:, organization_id: nil, branch: nil, url: nil)
|
|
192
|
+
destination_for(name, application, organization_id).create(branch: branch, url: url)
|
|
193
|
+
end
|
|
175
194
|
|
|
176
|
-
|
|
177
|
-
|
|
195
|
+
# Sends one request to the Shipyrd API as the holder of api_key, always over
|
|
196
|
+
# https, and returns the JSON object it answers with. method is a symbol, such
|
|
197
|
+
# as :get, :post, :put or :delete; path is the path from the host, already
|
|
198
|
+
# escaped; body, when given, is sent as JSON. A failure raises one of the
|
|
199
|
+
# errors above.
|
|
200
|
+
def request(method, path, query: {}, body: nil)
|
|
201
|
+
raise ArgumentError, "an API key is required" unless api_key
|
|
178
202
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
end
|
|
203
|
+
uri = URI("#{host}#{path}")
|
|
204
|
+
uri.query = URI.encode_www_form(query) unless query.empty?
|
|
182
205
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
end
|
|
206
|
+
read(perform(method, uri, body))
|
|
207
|
+
end
|
|
186
208
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
"" # gh config get returns an empty string when not set
|
|
191
|
-
end
|
|
209
|
+
def env
|
|
210
|
+
ENV.slice(*ENV_VARS)
|
|
211
|
+
end
|
|
192
212
|
|
|
193
|
-
|
|
194
|
-
|
|
213
|
+
def performer
|
|
214
|
+
github_username.empty? ? Shipyrd.configuration.performer : "https://github.com/#{github_username}"
|
|
215
|
+
end
|
|
195
216
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
217
|
+
def github_username
|
|
218
|
+
`gh config get -h github.com username`.chomp
|
|
219
|
+
rescue StandardError
|
|
220
|
+
"" # gh config get returns an empty string when not set
|
|
200
221
|
end
|
|
201
|
-
end
|
|
202
222
|
|
|
203
|
-
|
|
204
|
-
|
|
223
|
+
def commit_message
|
|
224
|
+
message = `git show -s --format=%s --no-show-signature`.chomp
|
|
205
225
|
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
226
|
+
if message.length >= 90
|
|
227
|
+
"#{message[0..90]}..."
|
|
228
|
+
else
|
|
229
|
+
message
|
|
230
|
+
end
|
|
231
|
+
end
|
|
209
232
|
|
|
210
|
-
|
|
211
|
-
|
|
233
|
+
def validate_configuration
|
|
234
|
+
valid_api_key?
|
|
212
235
|
|
|
213
|
-
|
|
214
|
-
|
|
236
|
+
true
|
|
237
|
+
rescue ArgumentError => e
|
|
238
|
+
logger.info(e.to_s)
|
|
215
239
|
|
|
216
|
-
|
|
217
|
-
|
|
240
|
+
false
|
|
241
|
+
end
|
|
218
242
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
return host if host.start_with?("https")
|
|
243
|
+
def valid_api_key?
|
|
244
|
+
raise ArgumentError, "ENV['SHIPYRD_API_KEY'] is not configured, disabling" unless api_key
|
|
222
245
|
|
|
223
|
-
|
|
224
|
-
|
|
246
|
+
true
|
|
247
|
+
end
|
|
225
248
|
|
|
226
|
-
|
|
249
|
+
def parse_host(host)
|
|
250
|
+
return "https://hooks.shipyrd.io" if host.nil?
|
|
251
|
+
return host if host.start_with?("https")
|
|
227
252
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
end
|
|
253
|
+
"https://#{host}"
|
|
254
|
+
end
|
|
231
255
|
|
|
232
|
-
|
|
233
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
|
234
|
-
http.use_ssl = true
|
|
235
|
-
http.open_timeout = 5
|
|
236
|
-
http.read_timeout = 5
|
|
237
|
-
http.write_timeout = 5
|
|
238
|
-
# Net::HTTP quietly retries an idempotent request (PUT and DELETE included)
|
|
239
|
-
# once after a lost response. Replaying an unlock could clear a lock someone
|
|
240
|
-
# took after the first one landed, so a failure goes to the caller instead.
|
|
241
|
-
http.max_retries = 0
|
|
242
|
-
http.send_request(method.to_s.upcase, uri.request_uri, nil, {
|
|
243
|
-
"Accept" => "application/json",
|
|
244
|
-
"Authorization" => "Bearer #{api_key}"
|
|
245
|
-
})
|
|
246
|
-
rescue *NETWORK_ERRORS => e
|
|
247
|
-
raise Unavailable.new("Shipyrd could not be reached: #{e.class}")
|
|
248
|
-
end
|
|
256
|
+
private
|
|
249
257
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
body = parse_body(response)
|
|
253
|
-
|
|
254
|
-
case status
|
|
255
|
-
when 200..299
|
|
256
|
-
return body if body.is_a?(Hash) || body.is_a?(Array)
|
|
257
|
-
|
|
258
|
-
raise UnexpectedResponse.new("Shipyrd responded #{status} without JSON", status: status)
|
|
259
|
-
when 401 then raise Unauthorized.new(status: status, body: body)
|
|
260
|
-
when 404 then raise NotFound.new(status: status, body: body)
|
|
261
|
-
when 409 then raise Conflict.new(status: status, body: body)
|
|
262
|
-
when 422 then raise UnprocessableEntity.new(status: status, body: body)
|
|
263
|
-
when 429 then raise RateLimited.new(status: status, body: body)
|
|
264
|
-
when 500..599 then raise Unavailable.new(status: status, body: body)
|
|
265
|
-
else raise UnexpectedResponse.new(status: status, body: body)
|
|
258
|
+
def destination_for(name, application, organization_id)
|
|
259
|
+
Shipyrd::Destination.new(application: application, name: name, organization_id: organization_id, client: self)
|
|
266
260
|
end
|
|
267
|
-
end
|
|
268
261
|
|
|
269
|
-
|
|
270
|
-
|
|
262
|
+
def perform(method, uri, body = nil)
|
|
263
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
264
|
+
http.use_ssl = true
|
|
265
|
+
http.open_timeout = 5
|
|
266
|
+
http.read_timeout = 5
|
|
267
|
+
http.write_timeout = 5
|
|
268
|
+
# Net::HTTP quietly retries an idempotent request (PUT and DELETE included)
|
|
269
|
+
# once after a lost response. Replaying an unlock could clear a lock someone
|
|
270
|
+
# took after the first one landed, so a failure goes to the caller instead.
|
|
271
|
+
http.max_retries = 0
|
|
272
|
+
headers = {"Accept" => "application/json", "Authorization" => "Bearer #{api_key}"}
|
|
273
|
+
headers["Content-Type"] = "application/json" if body
|
|
274
|
+
|
|
275
|
+
http.send_request(method.to_s.upcase, uri.request_uri, body && JSON.generate(body), headers)
|
|
276
|
+
rescue *NETWORK_ERRORS => e
|
|
277
|
+
raise Unavailable, "Shipyrd could not be reached: #{e.class}"
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
def read(response)
|
|
281
|
+
status = response.code.to_i
|
|
282
|
+
body = parse_body(response)
|
|
283
|
+
|
|
284
|
+
case status
|
|
285
|
+
when 200..299
|
|
286
|
+
return body if body.is_a?(Hash) || body.is_a?(Array)
|
|
287
|
+
|
|
288
|
+
raise UnexpectedResponse.new("Shipyrd responded #{status} without JSON", status: status)
|
|
289
|
+
when 401 then raise Unauthorized.new(status: status, body: body)
|
|
290
|
+
when 404 then raise NotFound.new(status: status, body: body)
|
|
291
|
+
when 409 then raise Conflict.new(status: status, body: body)
|
|
292
|
+
when 422 then raise UnprocessableEntity.new(status: status, body: body)
|
|
293
|
+
when 429 then raise RateLimited.new(status: status, body: body)
|
|
294
|
+
when 500..599 then raise Unavailable.new(status: status, body: body)
|
|
295
|
+
else raise UnexpectedResponse.new(status: status, body: body)
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def parse_body(response)
|
|
300
|
+
return nil if response.body.nil? || response.body.empty?
|
|
271
301
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
302
|
+
JSON.parse(response.body)
|
|
303
|
+
rescue JSON::ParserError
|
|
304
|
+
nil
|
|
305
|
+
end
|
|
275
306
|
end
|
|
276
307
|
end
|
|
@@ -1,59 +1,61 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
3
|
+
module Shipyrd
|
|
4
|
+
class Configuration
|
|
5
|
+
attr_writer :host, :api_key, :commit_message,
|
|
6
|
+
:recorded_at, :performer, :version, :service_version,
|
|
7
|
+
:hosts, :command, :subcommand, :role, :destination, :runtime
|
|
8
|
+
|
|
9
|
+
def host
|
|
10
|
+
@host || ENV.fetch("SHIPYRD_HOST", nil)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def api_key
|
|
14
|
+
@api_key || ENV.fetch("SHIPYRD_API_KEY", nil)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def commit_message
|
|
18
|
+
@commit_message || ENV.fetch("SHIPYRD_COMMIT_MESSAGE", nil)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def recorded_at
|
|
22
|
+
@recorded_at || ENV["SHIPYRD_RECORDED_AT"] || ENV.fetch("KAMAL_RECORDED_AT", nil)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def performer
|
|
26
|
+
@performer || ENV["SHIPYRD_PERFORMER"] || ENV.fetch("KAMAL_PERFORMER", nil)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def version
|
|
30
|
+
@version || ENV["SHIPYRD_VERSION"] || ENV.fetch("KAMAL_VERSION", nil)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def service_version
|
|
34
|
+
@service_version || ENV["SHIPYRD_SERVICE_VERSION"] || ENV.fetch("KAMAL_SERVICE_VERSION", nil)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def hosts
|
|
38
|
+
@hosts || ENV["SHIPYRD_HOSTS"] || ENV.fetch("KAMAL_HOSTS", nil)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def command
|
|
42
|
+
@command || ENV["SHIPYRD_COMMAND"] || ENV.fetch("KAMAL_COMMAND", nil)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def subcommand
|
|
46
|
+
@subcommand || ENV["SHIPYRD_SUBCOMMAND"] || ENV.fetch("KAMAL_SUBCOMMAND", nil)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def role
|
|
50
|
+
@role || ENV["SHIPYRD_ROLE"] || ENV.fetch("KAMAL_ROLE", nil)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def destination
|
|
54
|
+
@destination || ENV["SHIPYRD_DESTINATION"] || ENV.fetch("KAMAL_DESTINATION", nil)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def runtime
|
|
58
|
+
@runtime || ENV["SHIPYRD_RUNTIME"] || ENV.fetch("KAMAL_RUNTIME", nil)
|
|
59
|
+
end
|
|
58
60
|
end
|
|
59
61
|
end
|
data/lib/shipyrd/destination.rb
CHANGED
|
@@ -11,96 +11,117 @@
|
|
|
11
11
|
#
|
|
12
12
|
# The client needs a user's API token, not an application's SHIPYRD_API_KEY,
|
|
13
13
|
# because a lock is attributed to a person.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
14
|
+
module Shipyrd
|
|
15
|
+
class Destination
|
|
16
|
+
# Locking a destination someone else holds. Nothing was changed.
|
|
17
|
+
class LockHeld < Shipyrd::Client::Conflict
|
|
18
|
+
def locked_by = body["locked_by"]
|
|
19
|
+
|
|
20
|
+
def locked_at = body["locked_at"]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The slug names an application in more than one of the user's organizations.
|
|
24
|
+
# Pass organization_id; #organizations lists the candidates.
|
|
25
|
+
class AmbiguousApplication < Shipyrd::Client::UnprocessableEntity
|
|
26
|
+
def organizations = body["organizations"]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# No such application, or no such destination on it. #destinations lists the
|
|
30
|
+
# application's destinations when it was the latter, and is nil when the
|
|
31
|
+
# application itself was not found.
|
|
32
|
+
class NotFound < Shipyrd::Client::NotFound
|
|
33
|
+
def destinations = body["destinations"]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
attr_reader :application, :name, :organization_id, :client
|
|
37
|
+
|
|
38
|
+
# application is the application's slug (or its id). name is the destination's; nil or ""
|
|
39
|
+
# mean the destination with a blank name. organization_id is only needed when
|
|
40
|
+
# the slug exists in more than one of the user's organizations.
|
|
41
|
+
def initialize(application:, name: nil, organization_id: nil, client: Shipyrd::Client.new)
|
|
42
|
+
@application = application
|
|
43
|
+
@name = name
|
|
44
|
+
@organization_id = organization_id
|
|
45
|
+
@client = client
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def get
|
|
49
|
+
translating_errors { client.request(:get, destination_path, query: query) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Locks the destination as the client's user, so nobody else can deploy it.
|
|
53
|
+
# Returns the destination's state as a Hash. Your own lock again is a no-op;
|
|
54
|
+
# someone else's raises LockHeld.
|
|
55
|
+
def lock
|
|
56
|
+
change_lock(:put, "lock")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Unlocks the destination, whoever holds the lock. Returns the destination's
|
|
60
|
+
# state as a Hash. Not locked is a no-op.
|
|
61
|
+
def unlock
|
|
62
|
+
change_lock(:delete, "unlock")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Creates the destination on the application. The default destination is
|
|
66
|
+
# created by sending no name. Returns its state as a Hash. A name the
|
|
67
|
+
# application already has raises Shipyrd::Client::Invalid.
|
|
68
|
+
def create(branch: nil, url: nil)
|
|
69
|
+
attributes = {name: (path_name unless path_name == "default"), branch: branch, url: url}.compact
|
|
70
|
+
|
|
71
|
+
translating_errors do
|
|
72
|
+
client.request(:post, collection_path, query: query, body: {destination: attributes})
|
|
73
|
+
end
|
|
74
|
+
rescue AmbiguousApplication
|
|
75
|
+
raise
|
|
76
|
+
rescue Shipyrd::Client::UnprocessableEntity => e
|
|
77
|
+
raise Shipyrd::Client::Invalid.new(status: e.status, body: e.body)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def change_lock(method, action)
|
|
83
|
+
translating_errors { client.request(method, "#{destination_path}/#{action}", query: query) }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def query
|
|
87
|
+
organization_id ? {organization_id: organization_id} : {}
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Told apart by what the body carries: a held lock says who holds it, an
|
|
91
|
+
# ambiguous slug lists the organizations.
|
|
92
|
+
def translating_errors
|
|
93
|
+
yield
|
|
94
|
+
rescue Shipyrd::Client::Conflict => e
|
|
95
|
+
raise LockHeld.new(status: e.status, body: e.body) if e.body.key?("locked_by")
|
|
96
|
+
|
|
97
|
+
raise
|
|
98
|
+
rescue Shipyrd::Client::UnprocessableEntity => e
|
|
99
|
+
raise AmbiguousApplication.new(status: e.status, body: e.body) if e.body.key?("organizations")
|
|
100
|
+
|
|
101
|
+
raise
|
|
102
|
+
rescue Shipyrd::Client::NotFound => e
|
|
103
|
+
raise NotFound.new(status: e.status, body: e.body)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def destination_path
|
|
107
|
+
"#{collection_path}/#{escape(path_name)}"
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def collection_path
|
|
111
|
+
"/applications/#{escape(application)}/destinations"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# The API names the blank destination "default".
|
|
115
|
+
def path_name
|
|
116
|
+
name.nil? || name.to_s.empty? ? "default" : name.to_s
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Percent-encodes one path segment. Not URI.encode_www_form_component, which
|
|
120
|
+
# writes a space as "+", and that is a literal plus in a path. Dots are encoded
|
|
121
|
+
# too: Rails reads a literal dot in a route's :id as the start of a format, so
|
|
122
|
+
# "eu.production" would not route, and "eu%2Eproduction" does.
|
|
123
|
+
def escape(segment)
|
|
124
|
+
segment.to_s.gsub(/[^a-zA-Z0-9_~-]/) { |char| char.bytes.map { |byte| format("%%%02X", byte) }.join }
|
|
125
|
+
end
|
|
105
126
|
end
|
|
106
127
|
end
|
data/lib/shipyrd/logger.rb
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
require "logger"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
module Shipyrd
|
|
4
|
+
class Logger < Logger
|
|
5
|
+
def initialize
|
|
6
|
+
super(
|
|
7
|
+
$stdout,
|
|
8
|
+
level: Logger::INFO,
|
|
9
|
+
progname: "Shipyrd",
|
|
10
|
+
formatter: proc do |severity, _datetime, progname, msg|
|
|
11
|
+
"#{severity} -- #{progname}: #{msg}\n"
|
|
12
|
+
end
|
|
13
|
+
)
|
|
14
|
+
end
|
|
13
15
|
end
|
|
14
16
|
end
|
data/lib/shipyrd/user.rb
CHANGED
|
@@ -10,11 +10,13 @@
|
|
|
10
10
|
#
|
|
11
11
|
# Like Shipyrd::Destination, it needs a user's API token, not an application's
|
|
12
12
|
# SHIPYRD_API_KEY.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
module Shipyrd
|
|
14
|
+
class User
|
|
15
|
+
# GET /me as JSON: the token's user, the organizations they belong to, each
|
|
16
|
+
# with whether it has an active subscription, and where to subscribe. Raises
|
|
17
|
+
# Shipyrd::Client::Unauthorized for an unknown or application token.
|
|
18
|
+
def self.me(client: Shipyrd::Client.new)
|
|
19
|
+
client.request(:get, "/me")
|
|
20
|
+
end
|
|
19
21
|
end
|
|
20
22
|
end
|
data/lib/shipyrd/version.rb
CHANGED
data/shipyrd.gemspec
CHANGED
|
@@ -16,6 +16,7 @@ Gem::Specification.new do |spec|
|
|
|
16
16
|
spec.metadata["homepage_uri"] = spec.homepage
|
|
17
17
|
spec.metadata["source_code_uri"] = "https://github.com/shipyrd/shipyrd-gem"
|
|
18
18
|
spec.metadata["changelog_uri"] = "https://github.com/shipyrd/shipyrd-gem/releases"
|
|
19
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
|
19
20
|
|
|
20
21
|
# Specify which files should be added to the gem when it is released.
|
|
21
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: shipyrd
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.4.
|
|
4
|
+
version: 0.4.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Nick Hammond
|
|
@@ -16,7 +16,9 @@ executables: []
|
|
|
16
16
|
extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
|
18
18
|
files:
|
|
19
|
-
- ".
|
|
19
|
+
- ".rubocop.yml"
|
|
20
|
+
- AGENTS.md
|
|
21
|
+
- CLAUDE.md
|
|
20
22
|
- CODE_OF_CONDUCT.md
|
|
21
23
|
- Dockerfile
|
|
22
24
|
- Gemfile
|
|
@@ -38,6 +40,7 @@ metadata:
|
|
|
38
40
|
homepage_uri: https://shipyrd.io
|
|
39
41
|
source_code_uri: https://github.com/shipyrd/shipyrd-gem
|
|
40
42
|
changelog_uri: https://github.com/shipyrd/shipyrd-gem/releases
|
|
43
|
+
rubygems_mfa_required: 'true'
|
|
41
44
|
rdoc_options: []
|
|
42
45
|
require_paths:
|
|
43
46
|
- lib
|