update_status 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.7
1
+ 0.2.0
data/bin/status CHANGED
@@ -4,5 +4,13 @@
4
4
  $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
5
5
 
6
6
  require 'status'
7
+ require 'optparse'
7
8
 
8
- Status::Base.new(*ARGV).update
9
+ OptionParser.new do |o|
10
+ o.on("-s STATE", "--state", "Status of QA (default pending) [pending/pass]") {|s| $state = s }
11
+ o.on("-b BRANCH", "--branch BRANCH", "Branch name") {|b| $branch = b }
12
+ o.on("-h", "--help", "Display this screen") {puts o; exit }
13
+ o.parse!
14
+ end
15
+
16
+ Status::Base.new({:state => $state, :branch => $branch}).update
data/lib/status/base.rb CHANGED
@@ -6,14 +6,19 @@ module Status
6
6
  class Base
7
7
  attr_reader :qa_status
8
8
 
9
- def initialize(qa_status="pending")
10
- @qa_status = qa_status
11
- @statuses = Status::Github::Statuses.new(qa_status)
9
+ def initialize(options)
10
+ @qa_status = options[:state] || "pending"
11
+ @branch = options[:branch] || branch
12
+ @statuses = Status::Github::Statuses.new(@qa_status, @branch)
13
+ end
14
+
15
+ def branch
16
+ `git rev-parse --abbrev-ref HEAD`.chomp
12
17
  end
13
18
 
14
19
  def update
15
20
  puts "Updating..."
16
- pull = Status::Github::PullRequest.new
21
+ pull = Status::Github::PullRequest.new(@branch)
17
22
  pull.create_pull_request unless pull.pull_request_found?
18
23
  @statuses.request
19
24
  puts "Done."
data/lib/status/config.rb CHANGED
@@ -1,21 +1,16 @@
1
1
  # coding: utf-8
2
+ require "yaml"
2
3
 
3
4
  module Status
4
5
  class Config
5
- FILE = "#{ENV['HOME']}/.status.conf"
6
-
7
- attr_reader :attrs
6
+ FILE = ".status.yml"
8
7
 
8
+ attr_reader :parsed
9
9
  def initialize
10
- bootstrap unless File.exist?(file)
11
- load_config
10
+ bootstrap
12
11
  validate
13
12
  end
14
13
 
15
- def file
16
- FILE
17
- end
18
-
19
14
  def validate
20
15
  validate_presence_of :owner
21
16
  validate_presence_of :repo
@@ -25,37 +20,40 @@ module Status
25
20
  validate_presence_of :password, "ci"
26
21
  end
27
22
 
28
- def validate_presence_of(sym, type="github")
29
- unless attrs.include?(sym.to_s)
30
- puts "You have not entered a #{sym} for #{type}"
31
- attribute = gets
32
- self.attrs = ({sym => attribute.chomp})
33
- end
34
- end
35
-
36
23
  def bootstrap
37
- @attrs = {
38
- # eg:
39
- # :owner => "dougdroper",
40
- # :repo => "status",
41
- # :ci_url => "http://jenkins-ci.org/"
42
- }
43
- save
44
- abort("Config setup: Run status")
24
+ @parsed = begin
25
+ YAML.load(File.open(FILE))
26
+ rescue Exception => e
27
+ create_file
28
+ puts "Could not parse YAML file #{FILE}: #{e.message}"
29
+ abort("exit")
30
+ end
45
31
  end
46
32
 
47
- def attrs=(attribute={})
48
- @attrs.merge!(attribute)
49
- save
50
- end
33
+ private
51
34
 
52
- def load_config
53
- @attrs = MultiJson.decode(File.new(file, 'r').read)
35
+ def validate_presence_of(sym, type="github")
36
+ unless parsed.include?(sym)
37
+ puts "Please update #{FILE} with a #{sym} for #{type}"
38
+ abort("exit")
39
+ end
54
40
  end
55
41
 
56
- def save
57
- json = MultiJson.encode(attrs)
58
- File.open(file, 'w') {|f| f.write(json) }
42
+ def create_file
43
+ puts "No #{FILE} found, create one? (y/n)"
44
+ answer = gets
45
+ if answer.chomp.downcase == "y"
46
+ data = {
47
+ :username => "Jenkins username",
48
+ :password => "Jenkins password",
49
+ :ci_url => "eg. http://ci.jenkins.com",
50
+ :owner => "eg. dougdroper",
51
+ :repo => "eg. status",
52
+ :token => "Githubs API token (http://developer.github.com/v3/oauth/)"}
53
+ File.open(FILE, 'w') {|f| f.write(data.to_yaml)}
54
+ puts "Please update #{FILE} and then run status"
55
+ abort("exit")
56
+ end
59
57
  end
60
58
  end
61
59
  end
@@ -3,12 +3,16 @@
3
3
  module Status
4
4
  module Github
5
5
  class PullRequest
6
+ def initialize(branch)
7
+ @branch = branch
8
+ end
9
+
6
10
  def pull_request_found?
7
- !get_pull_request.select {|pull| pull["head"]["ref"] == Status.branch}.empty?
11
+ !get_pull_request.select {|pull| pull["head"]["ref"] == @branch}.empty?
8
12
  end
9
13
 
10
14
  def create_pull_request
11
- puts "no pull request found create one? (y/n)"
15
+ puts "No pull request found, create one? (y/n)"
12
16
  answer = gets
13
17
  answer.chomp.downcase == "y" ? new_pull_request : abort("exit")
14
18
  end
@@ -22,7 +26,11 @@ module Status
22
26
  puts "enter a description"
23
27
  body = gets
24
28
 
25
- {:title => Status.title, :body => body, :base => "master", :head => Status.branch }
29
+ {:title => title, :body => body, :base => "master", :head => @branch }
30
+ end
31
+
32
+ def title
33
+ `git log #{@branch} -1 --pretty=format:'%s'`
26
34
  end
27
35
 
28
36
  def get_pull_request
@@ -3,8 +3,10 @@
3
3
  module Status
4
4
  module Github
5
5
  class Statuses
6
- def initialize(qa_status="pending")
6
+ def initialize(qa_status, branch)
7
7
  @qa_status = qa_status
8
+ @branch = branch
9
+ @jenkins = Jenkins.new(branch)
8
10
  end
9
11
 
10
12
  def request
@@ -14,11 +16,11 @@ module Status
14
16
  private
15
17
 
16
18
  def status_api
17
- "/repos/#{Status.owner}/#{Status.repo}/statuses/" + Status.sha + "?access_token=" + Status.token
19
+ "/repos/#{Status.owner}/#{Status.repo}/statuses/" + sha + "?access_token=" + Status.token
18
20
  end
19
21
 
20
22
  def description
21
- "Build status: #{Jenkins.state}, QA #{@qa_status}"
23
+ "Build status: #{@jenkins.state}, QA #{@qa_status}"
22
24
  end
23
25
 
24
26
  def payload
@@ -26,19 +28,25 @@ module Status
26
28
  end
27
29
 
28
30
  def target_url
29
- "#{Status.ci_url}/job/#{Status.branch}"
31
+ "#{Status.ci_url}/job/#{@branch}"
30
32
  end
31
33
 
32
34
  def state
33
- (Jenkins.pass? && @qa_status == "pass") ? states[3] : git_state
35
+ return "success" if @jenkins.pass? && @qa_status == "pass"
36
+ return "pending" if @jenkins.pass? && @qa_status != "pass"
37
+ git_state
34
38
  end
35
39
 
36
40
  def git_state
37
- states.include?(Jenkins.state) ? states[states.index(Jenkins.state)] : "error"
41
+ states.include?(@jenkins.state) ? states[states.index(@jenkins.state)] : "error"
38
42
  end
39
43
 
40
44
  def states
41
- %w(error failure pending success)
45
+ %w(error failure)
46
+ end
47
+
48
+ def sha
49
+ `git log #{@branch} -1 --pretty=format:'%H'`
42
50
  end
43
51
  end
44
52
  end
@@ -1,8 +1,10 @@
1
1
  # coding: utf-8
2
2
 
3
3
  module Status
4
- module Jenkins
5
- extend self
4
+ class Jenkins
5
+ def initialize(branch)
6
+ @branch = branch
7
+ end
6
8
 
7
9
  def state
8
10
  return "success" if pass?
@@ -24,7 +26,7 @@ module Status
24
26
  end
25
27
 
26
28
  def path
27
- "/job/#{Status.branch}/lastBuild/api/json"
29
+ "/job/#{@branch}/lastBuild/api/json"
28
30
  end
29
31
  end
30
32
  end
data/lib/status.rb CHANGED
@@ -26,40 +26,28 @@ module Status
26
26
  @config ||= Status::Config.new
27
27
  end
28
28
 
29
- def sha
30
- `git log -1 --pretty=format:'%H'`
31
- end
32
-
33
- def title
34
- `git log -1 --pretty=format:'%s'`
35
- end
36
-
37
- def branch
38
- `git rev-parse --abbrev-ref HEAD`.chomp
39
- end
40
-
41
29
  def token
42
- Status.config.attrs["token"]
30
+ Status.config.parsed[:token]
43
31
  end
44
32
 
45
33
  def repo
46
- Status.config.attrs["repo"]
34
+ Status.config.parsed[:repo]
47
35
  end
48
36
 
49
37
  def owner
50
- Status.config.attrs["owner"]
38
+ Status.config.parsed[:owner]
51
39
  end
52
40
 
53
41
  def ci_url
54
- Status.config.attrs["ci_url"]
42
+ Status.config.parsed[:ci_url]
55
43
  end
56
44
 
57
45
  def ci_user
58
- Status.config.attrs["username"]
46
+ Status.config.parsed[:username]
59
47
  end
60
48
 
61
49
  def ci_password
62
- Status.config.attrs["password"]
50
+ Status.config.parsed[:password]
63
51
  end
64
52
 
65
53
  end
@@ -1,10 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Status::Github::PullRequest do
4
- subject { Status::Github::PullRequest.new }
4
+ subject { Status::Github::PullRequest.new("") }
5
5
 
6
6
  before do
7
- Status.stub(:config => stub(:attrs => {}))
7
+ Status.stub(:config => stub(:parsed => {}))
8
8
  end
9
9
 
10
10
  context "#pull_request_found?" do
@@ -14,12 +14,12 @@ describe Status::Github::PullRequest do
14
14
 
15
15
  it "is not found if a pull request does not exists" do
16
16
  Status.stub(:branch => "new_branch")
17
- subject.pull_request_found?.should be_false
17
+ Status::Github::PullRequest.new("new_branch").pull_request_found?.should be_false
18
18
  end
19
19
 
20
20
  it "is found if a pull request does exists" do
21
21
  Status.stub(:branch => "branch_name")
22
- subject.pull_request_found?.should be_true
22
+ Status::Github::PullRequest.new("branch_name").pull_request_found?.should be_true
23
23
  end
24
24
  end
25
25
 
@@ -2,37 +2,42 @@ require 'spec_helper'
2
2
 
3
3
  describe Status::Github::Statuses do
4
4
  before do
5
- stub_const("Status::Jenkins", stub)
5
+ @jenkins = stub
6
+ stub_const("Status::Jenkins", @jenkins)
6
7
  end
7
8
 
8
9
  subject { Status::Github::Statuses }
9
10
  it "has a payload target URL of the ci server URL and the branch name" do
11
+ @jenkins.stub(:new => stub(:state => "success", :pass? => true))
10
12
  Status.stub(:ci_url => "http://jenkins-ci.org", :branch => "feature_branch")
11
- subject.new.send(:target_url).should == "http://jenkins-ci.org/job/feature_branch"
13
+ subject.new("pending", "feature_branch").send(:target_url).should == "http://jenkins-ci.org/job/feature_branch"
12
14
  end
13
15
 
14
16
  it "has a payload description of the ci state and qa status" do
15
- Status::Jenkins.stub(:state => "success")
16
- subject.new.send(:description).should == "Build status: success, QA pending"
17
+ @jenkins.stub(:new => stub(:state => "success"))
18
+ subject.new("pending", "feature_branch").send(:description).should == "Build status: success, QA pending"
17
19
  end
18
20
 
19
21
  it "has a payload pending state when ci is passing but qa hasn't passed" do
20
- Status::Jenkins.stub(:pass? => true, :state => "pending")
21
- subject.new.send(:state).should == "pending"
22
+ @jenkins.stub(:new => stub(:state => "pending", :pass? => true))
23
+ subject.new("pending", "feature_branch").send(:state).should == "pending"
22
24
  end
23
25
 
24
26
  it "has a payload success state when ci is passing and qa has passed" do
25
- Status::Jenkins.stub(:pass? => true, :state => "success")
26
- subject.new("pass").send(:state).should == "success"
27
+ @jenkins.stub(:new => stub(:state => "success", :pass? => true))
28
+ subject.new("pass", "feature_branch").send(:state).should == "success"
27
29
  end
28
30
 
29
31
  it "has a payload error state when ci has an error" do
30
- Status::Jenkins.stub(:pass? => false, :state => "nothing")
31
- subject.new("pass").send(:state).should == "error"
32
+ @jenkins.stub(:new => stub(:state => "nothing", :pass? => false))
33
+ subject.new("pass", "feature_branch").send(:state).should == "error"
32
34
  end
33
35
 
34
36
  it "goes to the correct status api" do
35
- Status.stub(:owner => "owner", :repo => "status", :sha => "99efgd", :token => "123")
36
- subject.new.send(:status_api).should == "/repos/owner/status/statuses/99efgd?access_token=123"
37
+ @jenkins.stub(:new => stub)
38
+ Status.stub(:owner => "owner", :repo => "status", :token => "123", :ci_url => "")
39
+ status = subject.new("pending", "feature_branch")
40
+ status.stub(:sha => "99efgd")
41
+ status.send(:status_api).should == "/repos/owner/status/statuses/99efgd?access_token=123"
37
42
  end
38
43
  end
@@ -1,15 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Status::Jenkins do
4
- class Test
5
- include Status::Jenkins
6
- end
7
4
 
8
- subject {Test.new}
5
+ subject {Status::Jenkins.new("")}
9
6
 
10
7
  before do
11
8
  Status.stub(:config => stub(:attrs => {}))
12
- Status.stub(:branch => "")
13
9
 
14
10
  stub(Status::Request)
15
11
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "update_status"
8
- s.version = "0.1.7"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Douglas Roper"]
12
- s.date = "2012-12-11"
12
+ s.date = "2012-12-12"
13
13
  s.description = "Updates pull requests on github, with latest build from Jenkins and QA status"
14
14
  s.email = "douglasroper@notonthehighstreet.com"
15
15
  s.executables = ["status"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: update_status
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-11 00:00:00.000000000 Z
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -170,7 +170,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
170
170
  version: '0'
171
171
  segments:
172
172
  - 0
173
- hash: -2510346470153262609
173
+ hash: 3307849087401865744
174
174
  required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  none: false
176
176
  requirements: