-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da53b88
commit 37b1fcd
Showing
11 changed files
with
285 additions
and
8 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,101 @@ | ||
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 | ||
|
||
DEFAULTS = { | ||
tag: false, | ||
} | ||
|
||
DESCR = { | ||
tag: 'Shortcut for running the `gem tag` command', | ||
name: 'Name of the release (defaults to "[gem name] [version]")', | ||
descr: 'Description of the release', | ||
repo: "Full name of the repository on GitHub, e.g. svenfuchs/gem-release (defaults to the repo name from the gemspec's homepage if this is a GitHub URL)", | ||
token: 'GitHub OAuth token' | ||
} | ||
|
||
opt '-d', '--description DESCRIPTION', descr(:desc) do |value| | ||
opts[:descr] = value | ||
end | ||
|
||
opt '-r', '--repo REPO', descr(:repo) do |value| | ||
opts[:repo] = value | ||
end | ||
|
||
opt '-t', '--token TOKEN', descr(:token) do |value| | ||
opts[:token] = value | ||
end | ||
|
||
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.' | ||
} | ||
|
||
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 | ||
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,57 @@ | ||
require 'json' | ||
require 'gem/release/helper/http' | ||
|
||
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)' | ||
} | ||
|
||
attr_reader :repo, :data | ||
|
||
def initialize(repo, data) | ||
@repo = repo | ||
@data = data | ||
end | ||
|
||
def release | ||
resp = post(url, body, headers) | ||
status, body = resp | ||
raise Abort, MSGS[:error] % [status, body] unless status == 200 | ||
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' | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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 | ||
Net::HTTP.new(uri.host, uri.port) | ||
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
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,35 @@ | ||
describe Gem::Release::Cmds::Github do | ||
let(:args) { [] } | ||
let(:opts) { { repo: 'foo/bar', token: 'token' } } | ||
let(:body) { '{"tag_name":"v1.0.0","name":"foo-bar v1.0.0","body":null,"prerelease":false}' } | ||
let(:status) { 200 } | ||
|
||
cwd 'foo-bar' | ||
gemspec 'foo-bar' | ||
|
||
before { context.git.tags << 'v1.0.0' } | ||
before { stub_request(:post, 'http://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 |
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