This repository has been archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
github.rb
61 lines (49 loc) · 1.69 KB
/
github.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
require "chandler/github/client"
require "chandler/github/errors"
require "chandler/github/remote"
module Chandler
# A facade for performing GitHub API operations on a given GitHub repository
# (specified as a git URL or as `owner/repo` format). Requires either that
# "~/.netrc" is properly configured with GitHub credentials or an auth token
# is available in the host environment at "CHANDLER_GITHUB_API_TOKEN""
#
class GitHub
attr_reader :repository, :config
def initialize(repository:, config:)
@repository = repository
@remote = Remote.parse(repository)
@config = config
end
def create_or_update_release(tag:, title:, description:)
return if config.dry_run?
release = existing_release(tag)
return update_release(release, title, description) if release
create_release(tag, title, description)
rescue Octokit::NotFound
raise InvalidRepository, repository
end
private
attr_reader :remote
def existing_release(tag)
release = client.release_for_tag(remote.repository, tag)
release.id.nil? ? nil : release
rescue Octokit::NotFound
nil
end
def update_release(release, title, desc)
return if release_unchanged?(release, title, desc)
client.update_release(release.url, :name => title, :body => desc)
end
def release_unchanged?(release, title, desc)
release.name == title && release.body.to_s.strip == desc.strip
end
def create_release(tag, title, desc)
client.create_release(
remote.repository, tag, :name => title, :body => desc
)
end
def client
@client ||= Client.new(:host => remote.host).tap(&:login!)
end
end
end