Class: GitHub

Inherits:
Object
  • Object
show all
Defined in:
lib/github/github.rb

Overview

Functions to run on GitHub

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ GitHub

Returns a new instance of GitHub.



7
8
9
# File 'lib/github/github.rb', line 7

def initialize(api_key)
  @api_key = api_key
end

Instance Method Details

#comment(message) ⇒ Object

Comment on Pull Request



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/github/github.rb', line 12

def comment(message)
  result = comment_on_pull_request(message)

  if !result[:successful]
    puts "Status code: #{result[:status_code]}"
    puts "Body: #{result[:body]}"
    Log.fatal('Commenting on GitHub pull request failed.')
  else
    Log.success('Successfully commented on GitHub pull request.')
  end
end

#comment_on_pull_request(message) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/github/github.rb', line 24

def comment_on_pull_request(message)
  puts "Commenting on GitHub pull request: #{ENV['TRAVIS_REPO_SLUG']}/#{ENV['TRAVIS_PULL_REQUEST']}"
  uri = URI("https://api.github.com/repos/#{ENV['TRAVIS_REPO_SLUG']}/issues/#{ENV['TRAVIS_PULL_REQUEST']}/comments")

  req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
  req['Authorization'] = "token #{@api_key}"
  req.body = { body: message }.to_json

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  res = http.request(req)
  status_code = res.code.to_i
  { status_code: status_code, body: res.body, successful: res.is_a?(Net::HTTPSuccess) && status_code < 400 }
end