Class: Jackal::GithubKit::Hubber

Inherits:
Callback
  • Object
show all
Defined in:
lib/jackal-github-kit/hubber.rb

Instance Method Summary collapse

Instance Method Details

#execute(message) ⇒ Object

Add comment to GitHub commit SHA

Parameters:

  • message (Carnivore::Message)


38
39
40
41
42
43
44
45
# File 'lib/jackal-github-kit/hubber.rb', line 38

def execute(message)
  failure_wrap(message) do |payload|
    write_release(payload) if payload.get(:data, :github_kit, :release)
    write_commit_comment(payload) if payload.get(:data, :github_kit, :commit_comment)
    write_status(payload) if payload.get(:data, :github_kit, :status)
    job_completed(:github_kit, payload, message)
  end
end

#github_clientOctokit::Client

Returns:

  • (Octokit::Client)


24
25
26
27
28
29
30
31
32
33
# File 'lib/jackal-github-kit/hubber.rb', line 24

def github_client
  memoize(:github_client) do
    gh_conf = {
      :access_token => config.fetch(:github, :access_token,
        app_config.get(:github, :access_token)
      )
    }
    Octokit::Client.new(gh_conf)
  end
end

#setup(*_) ⇒ Object

Setup callback



8
9
10
# File 'lib/jackal-github-kit/hubber.rb', line 8

def setup(*_)
  require 'octokit'
end

#valid?(message) ⇒ Truthy, Falsey

Determine validity of message

Parameters:

  • message (Carnivore::Message)

Returns:

  • (Truthy, Falsey)


16
17
18
19
20
21
# File 'lib/jackal-github-kit/hubber.rb', line 16

def valid?(message)
  super do |payload|
    payload.get(:data, :github_kit, :commit_comment) ||
      payload.get(:data, :github_kit, :status)
  end
end

#write_commit_comment(payload) ⇒ TrueClass

Write comment to GitHub SHA reference

Parameters:

  • payload (Smash)

Returns:

  • (TrueClass)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jackal-github-kit/hubber.rb', line 51

def write_commit_comment(payload)
  comment = payload[:data][:github_kit].delete(:commit_comment)
  info 'Writing commit comment to GitHub'
  debug "GitHub commit comment: #{comment.inspect}"
  github_client.create_commit_comment(
    comment[:repository],
    comment[:reference],
    comment[:message]
  )
  true
end

#write_release(payload) ⇒ TrueClass

Write release to github

Parameters:

  • payload (Smash)

Returns:

  • (TrueClass)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jackal-github-kit/hubber.rb', line 84

def write_release(payload)
  info 'Creating new release on GitHub'
  rel = payload[:data][:github_kit].delete(:release)
  release = github_client.create_release(
    rel[:repository],
    rel[:tag_name],
    :name => rel[:name],
    :target_commitish => rel[:reference],
    :prerelease => rel[:prerelease],
    :body => rel[:body]
  )

  api_release_url = release.rels[:self].href
  public_release_url = release.rels[:html].href

  info 'New release created on GitHub. Uploading assets.'
  assets = rel[:assets].map do |asset|
    debug "Uploading release asset - #{asset}"
    response = github_client.upload_asset(
      api_release_url,
      asset_store.get(asset),
      :name => asset.sub(/^.+?_/, ''),
      :content_type => 'application/octet-stream'
    )
    debug "Completed release asset upload - #{asset}"
    Smash.new(
      :url => response[:url],
      :browser_url => response[:browser_download_url],
      :name => response[:name],
      :id => response[:id]
    )
  end
  payload.set(:data, :github_kit, :release, :assets, assets)
  payload.set(:data, :github_kit, :release, :api_url, api_release_url)
  payload.set(:data, :github_kit, :release, :public_url, public_release_url)

  true
end

#write_status(payload) ⇒ TrueClass

Write status of pull request to github

Parameters:

  • payload (Smash)

Returns:

  • (TrueClass)


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jackal-github-kit/hubber.rb', line 67

def write_status(payload)
  status = payload[:data][:github_kit].delete(:status)
  info 'Writing repository status to GitHub'
  debug "GitHub repository status: #{status.inspect}"
  github_client.create_status(
    status[:repository],
    status[:reference],
    status[:state].to_sym,
    status.fetch(:extras, Smash.new)
  )
  true
end