Class: Amanuensis::Github::Push

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

Instance Method Summary collapse

Instance Method Details

#run(changelog) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/amanuensis/github/push.rb', line 5

def run(changelog)
  client = Octokit::Client.new(access_token: Github.oauth_token, auto_paginate: true)

  repo_name      = Github.repo
  default_branch = client.repo(repo_name)[:default_branch]
  ref            = "heads/#{default_branch}"

  content = (client.contents repo_name, path: Github.file_name, ref: ref, accept: 'application/vnd.github.V3.raw' rescue '')

  if content.empty?
    client.create_content(repo_name, Github.file_name, 'Creating changelog', changelog, branch: default_branch)
  else
    content.prepend(changelog)
    content_object = client.contents repo_name, path: Github.file_name, ref: ref

    client.update_content(repo_name, Github.file_name, 'Updating changelog', content_object.sha, content, branch: default_branch)
  end
end