Class: GithubHelper::Github

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/github_helper.rb

Constant Summary collapse

@@target_user =
''
@@target_repo =
''

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Github

Returns a new instance of Github.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/github_helper.rb', line 31

def initialize(options)
  @config = YAML::load( File.open( '.git/githelper.config.yml' ) )

  # Credentials setting from YML
  @@my_user = @config['credentials']['user']
  @@my_token = @config['credentials']['token']

  # Remote settings from YML
  @@target_user = @config['remote']['user']
  @@target_repo = @config['remote']['repo']

  @options = options
  @verbose = true if options[:verbose]
  @currentBranch = ''
  @Git = Git.open('.');
  @@my_name = @Git.config['user.name']
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



20
21
22
# File 'lib/github_helper.rb', line 20

def options
  @options
end

Instance Method Details

#checkHeaders(resp) ⇒ Object

Do we have a good HTTP status ?



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/github_helper.rb', line 112

def checkHeaders(resp)
  code = resp.headers['status'][0..2];

  #If the status isnt 200 (OK) or 201 (Created), raise an exception
  good = case code.to_i
  when 200..201 then true
  when 401 then 
    raise Exception, "Bad credentials, maybe your OAuth token isn't valid anymore.\nReset your config with ghh --reset-config and try again"
  else
    error = formatError(resp)
    raise Exception, "Error (HTTP #{code}) : "+error
  end
  good
end

#commit!Object

Commits staged changes



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/github_helper.rb', line 95

def commit!
  if(options[:commitmessage])
    #Do we have files to commit ?
    count = @Git.status.staged.count

    raise Exception, "You have no staged files, aborting your commit" if (count == 0)

    message = options[:commitmessage]

    ci = @Git.commit(message)
    pp ci if @verbose
  else
    raise Exception, "Commit needs a message"
  end
end

#createPull!Object

Creates a pull request



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
# File 'lib/github_helper.rb', line 58

def createPull!
  # Formats the HEAD to user:branch
  head = @@my_user+':'+currentBranch?

  # Do we have a title ? If not, takes it from the last commit
  pullTitle = (@options[:pulltitle] == false) ? lastCommit? : @options[:pulltitle]

  body = @options[:pullbody]
  body = "#{body}\n##{options[:issueid]}\n" if (options[:issueid] != false)

  data = {:title => pullTitle, :body => body, :base => @options[:base], :head => head}
  pp data if @verbose
  puts "Asking for #{data[:head]} to be merged into #{@@target_user}/#{data[:base]}"

  # Let's send the request
  resp = post("/repos/#{@@target_user}/#{@@target_repo}/pulls", :body => data.to_json)
  pp resp if @verbose
  checkHeaders(resp)

  # So far so good. Lets check if it's mergeable and give the user the proper link
  puts "Pull request succeeded. Getting more informations..\n"

  infos_resp = get("/repos/#{@@target_user}/#{@@target_repo}/pulls/"+resp.parsed_response['number'].to_s)
  pp infos_resp if @verbose
  checkHeaders(infos_resp)

  infos = infos_resp.parsed_response

  puts 'Url : '+infos['_links']['html']['href']
  if(infos['mergeable'])
    puts 'Your pull request is mergeable.'
  else
    puts "/!\\ ..But your pull request cannot be merged. Try to rebase then push again. /!\\"
  end
end

#currentBranch?Boolean

Return your current branch’s name

Returns:

  • (Boolean)


154
155
156
157
158
159
# File 'lib/github_helper.rb', line 154

def currentBranch?
  if(@currentBranch == '')
    @currentBranch = `git branch | grep '*' | cut -d " " -f2`.strip!
  end
  @currentBranch
end

#displaypulls!Object

Display the open pull requests on the target repo



23
24
25
26
27
28
29
# File 'lib/github_helper.rb', line 23

def displaypulls!
  puts "Pull requests for #{@@target_user}/#{@@target_repo}\n"
  pulls = get("/repos/#{@@target_user}/#{@@target_repo}/pulls")
  pp pulls if @verbose
  checkHeaders(pulls)
  pulls.each { |pull| print "*\t#{pull['number']}\t"+pull['title'] + "\t" + pull['user']['login'] + "\n" } 
end

#formatError(resp) ⇒ Object

Format errors from the github api



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/github_helper.rb', line 128

def formatError(resp)
  output = "";

  output << resp.parsed_response['message'];
  if(resp.parsed_response['errors'] != nil)
    resp.parsed_response['errors'].each { |e| 
      output << "\n" + e['message'] if e['message'] != nil
    }
  end
  output
end

#get(url) ⇒ Object



49
50
51
# File 'lib/github_helper.rb', line 49

def get(url)
  self.class.get(url+"?access_token=#{@@my_token}")
end

#lastCommit?Boolean

Return your last commit’s title

Returns:

  • (Boolean)


147
148
149
150
151
# File 'lib/github_helper.rb', line 147

def lastCommit?
  message = ''
  @Git.log(1).author(@@my_name).each { |c| message = c.message }
  message.split("\n")[0]
end

#post(url, data) ⇒ Object



53
54
55
# File 'lib/github_helper.rb', line 53

def post(url,data)
  self.class.post(url+"?access_token=#{@@my_token}",data)
end

#pushBranch!Object

Push the branch to origin



141
142
143
144
# File 'lib/github_helper.rb', line 141

def pushBranch!
  @Git.forcepush('origin',currentBranch?)
  puts "Pushed #{@currentBranch} to origin"
end