-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from svenfuchs/sf-github-release
Add gem release --github
- Loading branch information
Showing
13 changed files
with
308 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,5 @@ group :test do | |
gem 'rspec' | ||
gem 'simplecov' | ||
gem 'coveralls' | ||
gem 'webmock' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require 'gem/release/cmds/base' | ||
require 'gem/release/context/github' | ||
|
||
module Gem | ||
module Release | ||
module Cmds | ||
class Github < Base | ||
summary "Creates a GitHub release for the current version." | ||
|
||
description <<-str.split("\n").map(&:lstrip).join("\n") | ||
Creates a GitHub release for the current version. | ||
Requires a tag `v[version]` to be present or --tag to be given. | ||
str | ||
|
||
MSGS = { | ||
release: 'Creating GitHub release for %s version %s.', | ||
no_tag: 'Tag %s does not exist. Run `gem tag` or pass `--tag`.', | ||
no_repo: 'Could not determine the repository name. Please pass `--repo REPO`, or set homepage or metadata[:github_url] to the GitHub repository URL in the gemspec.', | ||
no_token: 'Could not determine the GitHub OAuth token. Please pass `--token TOKEN`. See https://developer.github.com/v3/#oauth2-token-sent-in-a-header for more details.', | ||
}.freeze | ||
|
||
def run | ||
in_gem_dirs do | ||
announce :release, gem.name, tag_name | ||
validate | ||
release | ||
end | ||
end | ||
|
||
private | ||
|
||
def validate | ||
abort :no_tag, tag_name unless tagged? | ||
abort :no_token unless token | ||
end | ||
|
||
def tagged? | ||
git.tags.include?(tag_name) | ||
end | ||
|
||
def release | ||
return if pretend? | ||
Context::Github.new(repo, data).release | ||
end | ||
|
||
def data | ||
{ | ||
version: gem.version, | ||
tag_name: tag_name, | ||
name: "#{gem.name} #{tag_name}", | ||
descr: descr, | ||
token: token | ||
} | ||
end | ||
|
||
def tag_name | ||
"v#{gem.version}" | ||
end | ||
|
||
def repo | ||
opts[:repo] || repo_from(gem.spec.homepage) || repo_from(gem.spec.metadata[:github_url]) || abort(:no_repo) | ||
end | ||
|
||
def repo_from(url) | ||
url && url =~ %r(https://github\.com/(.*/.*)) && $1 | ||
end | ||
|
||
def token | ||
opts[:token] | ||
end | ||
|
||
def descr | ||
opts[:descr] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'json' | ||
require 'gem/release/helper/http' | ||
require 'gem/release/version' | ||
|
||
module Gem | ||
module Release | ||
class Context | ||
class Github | ||
include Helper::Http | ||
|
||
URL = 'https://api.github.com/repos/%s/releases' | ||
|
||
MSGS = { | ||
error: 'GitHub returned %s (body: %p)' | ||
}.freeze | ||
|
||
attr_reader :repo, :data | ||
|
||
def initialize(repo, data) | ||
@repo = repo | ||
@data = data | ||
end | ||
|
||
def release | ||
# Create a release | ||
# https://developer.github.com/v3/repos/releases/#create-a-release | ||
resp = post(url, body, headers) | ||
status, body = resp | ||
# success status code is 201 (created) not 200 (ok) | ||
raise Abort, MSGS.fetch(:error) % [status, body] unless status == 201 | ||
end | ||
|
||
private | ||
|
||
def url | ||
URL % repo | ||
end | ||
|
||
def body | ||
JSON.dump( | ||
tag_name: data[:tag_name], | ||
name: data[:name], | ||
body: data[:descr], | ||
prerelease: pre?(data[:version]) | ||
) | ||
end | ||
|
||
def headers | ||
{ | ||
'User-Agent' => "gem-release/v#{::Gem::Release::VERSION}", | ||
'Content-Type' => 'text/json', | ||
'Authorization' => "token #{data[:token]}", | ||
} | ||
end | ||
|
||
def pre?(version) | ||
Version::Number.new(version).pre? | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'net/http' | ||
require 'uri' | ||
|
||
module Gem | ||
module Release | ||
module Helper | ||
module Http | ||
class Client < Struct.new(:method, :url, :body, :headers) | ||
def request | ||
req = const.new(uri.request_uri, headers) | ||
req.body = body if body | ||
resp = client.request(req) | ||
[resp.code.to_i, resp.body] | ||
end | ||
|
||
private | ||
|
||
def uri | ||
@uri ||= URI.parse(url) | ||
end | ||
|
||
def client | ||
http_client = Net::HTTP.new(uri.host, uri.port) | ||
http_client.use_ssl = (uri.scheme == 'https') | ||
http_client | ||
end | ||
|
||
def const | ||
Net::HTTP.const_get(method.to_s.capitalize) | ||
end | ||
end | ||
|
||
def post(url, body = nil, headers = {}) | ||
Client.new(:post, url, body, headers).request | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,10 @@ def bump | |
parts.join(stage_delim) | ||
end | ||
|
||
def pre? | ||
!!parts[4] | ||
end | ||
|
||
private | ||
|
||
def specific? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
describe Gem::Release::Cmds::Github do | ||
let(:args) { [] } | ||
let(:opts) { { repo: 'foo/bar', token: 'token', descr: 'A new foo bar' } } | ||
let(:body) { '{"tag_name":"v1.0.0","name":"foo-bar v1.0.0","body":"A new foo bar","prerelease":false}' } | ||
# success status code is 201 (created) not 200 (ok) | ||
# https://developer.github.com/v3/repos/releases/#create-a-release | ||
let(:status) { 201 } | ||
|
||
cwd 'foo-bar' | ||
gemspec 'foo-bar' | ||
|
||
before { context.git.tags << 'v1.0.0' } | ||
before { stub_request(:post, 'https://api.github.com:443/repos/foo/bar/releases').with(body: body).to_return(status: status) } | ||
|
||
describe 'by default' do | ||
run_cmd | ||
|
||
it { should_not run_cmd 'git push --tags origin' } | ||
it { should output 'Creating GitHub release for foo-bar version v1.0.0' } | ||
it { should output 'All is good, thanks my friend.' } | ||
end | ||
|
||
describe 'not tagged' do | ||
before { context.git.tags.clear } | ||
it { expect { run }.to raise_error('Tag v1.0.0 does not exist. Run `gem tag` or pass `--tag`. Aborting.') } | ||
end | ||
|
||
describe 'invalid token' do | ||
let(:status) { 401 } | ||
it { expect { run }.to raise_error('GitHub returned 401 (body: "")') } | ||
end | ||
|
||
describe 'not found' do | ||
let(:status) { 404 } | ||
it { expect { run }.to raise_error('GitHub returned 404 (body: "")') } | ||
end | ||
end |
Oops, something went wrong.