Class: Yolo::Tools::Github

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

Overview

Communicates with Github

Author:

  • Alex Fish

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGithub

Creates the class with default variables



21
22
23
24
25
26
27
# File 'lib/yolo/tools/github.rb', line 21

def initialize
  @token = Yolo::Config::Settings.instance.github_token
  if !@token
    error = Yolo::Formatters::ErrorFormatter.new
    error.no_github_token
  end
end

Instance Attribute Details

#repoObject

The github repo that all actions will target



16
17
18
# File 'lib/yolo/tools/github.rb', line 16

def repo
  @repo
end

Instance Method Details

#release(bundle, version, body) ⇒ Object

Release the bundle using the github releases API, the bundle will be zipped and uploaded as well as the release notes used as the release body and version for the release title

 @param version [String] The version of the release

Parameters:

  • bundle (String)

    The full path to the bundle folder to release

  • body (String)

    The body of the release



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yolo/tools/github.rb', line 37

def release(bundle, version, body)
  @progress = Yolo::Formatters::ProgressFormatter.new
  @progress.creating_github_release

  uri = URI.parse("https://api.github.com/repos/#{self.repo}/releases?access_token=#{@token}")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.request_uri)
  request.body = options(body, version)

  # Tweak headers, removing this will default to application/x-www-form-urlencoded
  request["Content-Type"] = "application/json"

  response = http.request(request)
  response = JSON.parse(response.body)

  if response.has_key?("upload_url")
    @progress.created_release(version)
    url = response["upload_url"].gsub("{?name}","")
    upload_bundle(bundle, url, "#{version}.zip")
  else
    error = Yolo::Formatters::ErrorFormatter.new
    error.no_github_release
  end
end

#upload_bundle(bundle, url, name) ⇒ Object

Upload the bundle to the github release url

 @param url [String] The github asset url returned from create_release

Parameters:

  • bundle (String)

    The full path to the bundle folder to release



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/yolo/tools/github.rb', line 70

def upload_bundle(bundle, url, name)
  @progress = Yolo::Formatters::ProgressFormatter.new
  @progress.github_uploading
  zipped_bundle = zip_bundle(bundle)

  response = ""
  curl = curl_string(name, zipped_bundle, url)
  IO.popen(curl) do |io|
    begin
      while line = io.readline
        response << line
      end
      if response.length == 0
        @error_formatter.github_upload_failed("Upload error")
      end
    rescue EOFError
    end
  end

  @progress.github_released
end