Skip to content

Commit e49c371

Browse files
authored
Merge pull request #5 from K-and-R/optional-repo-integration
Additional optional config and optional repo integration
2 parents d55c94c + d0511c2 commit e49c371

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ after 'deploy:published', 'sentry:notice_deployment'
5656

5757
* `sentry_repo`: The `repository` name to be used when reporting repository details to Sentry [computed from `fetch(:repo_url)` by default -- `https://github.com/codeur/capistrano-sentry` becomes `//github.com/codeur/capistrano-sentry` and `[email protected]:codeur/capistrano-sentry.git` becomes `codeur/capistrano-sentry`]
5858

59+
* `sentry_repo_integration`: this enables/disables submission of git repo information (`release_refs` below) to Sentry [Enabled (`true`) by default].
60+
61+
* `sentry_release_refs`: Repository details about this realease (`repository`, `commit`, `previousCommit`) to Sentry [computed from `sentry_repo`, `current_revision`, and `previous_revision`)].
62+
63+
* `sentry_release_version`: Version number (tag, etc.) used to identify this release to Sentry [computed from `current_revision` or repository `HEAD`)].
64+
65+
* `deploy_name`: A name (revision, version number, tag, etc.) used to identify this release deploy to Sentry [computed from `sentry_release_version`+`fetch(:release_timestamp)`)].
66+
5967
### Sentry API Documentation
6068
* [Project Releases](https://docs.sentry.io/api/releases/post-project-releases/)
6169
* [Release Deploys](https://docs.sentry.io/api/releases/post-release-deploys/)

capistrano-sentry.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
77
spec.version = Capistrano::Sentry::VERSION
88
spec.authors = ['Brice Texier']
99
spec.email = ['[email protected]']
10-
10+
spec.description = 'Sentry release/deployment integration'
1111
spec.summary = 'Sentry release/deployment integration'
1212
spec.homepage = 'https://github.com/codeur/capistrano-sentry'
1313
spec.license = 'MIT'

lib/capistrano/tasks/sentry.rake

+23-17
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,50 @@ namespace :sentry do
3333
require 'net/https'
3434
require 'json'
3535

36-
version = `git rev-parse HEAD`.strip
36+
head_revision = fetch(:current_revision) || `git rev-parse HEAD`.strip
37+
prev_revision = fetch(:previous_revision) || `git rev-parse #{fetch(:current_revision)}^`.strip
3738

3839
sentry_host = ENV['SENTRY_HOST'] || fetch(:sentry_host, 'https://sentry.io')
39-
orga_slug = fetch(:sentry_organization) || fetch(:application)
40+
organization_slug = fetch(:sentry_organization) || fetch(:application)
4041
project = fetch(:sentry_project) || fetch(:application)
4142
environment = fetch(:stage) || 'default'
4243
api_token = ENV['SENTRY_API_TOKEN'] || fetch(:sentry_api_token)
43-
repo_name = fetch(:sentry_repo) || fetch(:repo_url).split(':').last.gsub(/\.git$/, '')
44+
repo_integration_enabled = fetch(:sentry_repo_integration, true)
45+
release_refs = fetch(:sentry_release_refs, [{
46+
repository: fetch(:sentry_repo) || fetch(:repo_url).split(':').last.gsub(/\.git$/, ''),
47+
commit: head_revision,
48+
previousCommit: prev_revision,
49+
}])
50+
release_version = fetch(:sentry_release_version) || head_revision
51+
deploy_name = fetch(:sentry_deploy_name) || "#{release_version}-#{fetch(:release_timestamp)}"
4452

4553
uri = URI.parse(sentry_host)
4654
http = Net::HTTP.new(uri.host, uri.port)
4755
http.use_ssl = true
4856

4957
headers = {
5058
'Content-Type' => 'application/json',
51-
'Authorization' => 'Bearer ' + api_token.to_s
59+
'Authorization' => 'Bearer ' + api_token.to_s,
5260
}
5361

54-
req = Net::HTTP::Post.new("/api/0/organizations/#{orga_slug}/releases/", headers)
55-
req.body = JSON.generate(
56-
version: version,
57-
refs: [{
58-
repository: repo_name,
59-
commit: fetch(:current_revision) || `git rev-parse HEAD`.strip
60-
}],
61-
projects: [project]
62-
)
62+
req = Net::HTTP::Post.new("/api/0/organizations/#{organization_slug}/releases/", headers)
63+
body = {
64+
version: release_version,
65+
projects: [project],
66+
}
67+
body[:refs] = release_refs if repo_integration_enabled
68+
req.body = JSON.generate(body)
6369
response = http.request(req)
6470
if response.is_a? Net::HTTPSuccess
65-
info 'Uploaded release infos to Sentry'
66-
req = Net::HTTP::Post.new("/api/0/organizations/#{orga_slug}/releases/#{version}/deploys/", headers)
71+
info "Notified Sentry of new release: #{release_version}"
72+
req = Net::HTTP::Post.new("/api/0/organizations/#{organization_slug}/releases/#{release_version}/deploys/", headers)
6773
req.body = JSON.generate(
6874
environment: environment,
69-
name: "#{version}-#{fetch(:release_timestamp)}"
75+
name: deploy_name,
7076
)
7177
response = http.request(req)
7278
if response.is_a? Net::HTTPSuccess
73-
info 'Uploaded deployment infos to Sentry'
79+
info "Notified Sentry of new deployment: #{deploy_name}"
7480
else
7581
warn "Cannot notify sentry for new deployment. Response: #{response.code.inspect}: #{response.body}"
7682
end

0 commit comments

Comments
 (0)