-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_hub.rb
57 lines (48 loc) · 1.76 KB
/
git_hub.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
require 'octokit'
require_relative 'git_repo'
require_relative 'credentials'
class GitHub
# @param [String] dir_repo
# @param [String] base
# @param [String] head
# @param [String] title
# @param [String] body
def self.create_pull_request(dir_repo, base, head, title, body)
repo_url = GitRepo.new(dir_repo).repo_url
repo_url = repo_url.chomp '.git' if repo_url.end_with? '.git'
repo = Octokit::Repository.from_url repo_url
client = Octokit::Client.new
client.access_token = $github_access_token
client.create_pull_request repo, base, head, title, body
end
def self.create_release(dir_repo, tag, title, body, artifact = nil)
fail_script_unless_file_exists artifact unless artifact.nil?
repo_url = GitRepo.new(dir_repo).repo_url
repo_url = repo_url.chomp '.git' if repo_url.end_with? '.git'
repo = Octokit::Repository.from_url repo_url
client = Octokit::Client.new
client.access_token = $github_access_token
release = client.create_release repo, tag, name: title, body: body, draft: true
unless artifact.nil?
client.upload_asset release.url, artifact, content_type: 'application/gzip'
end
end
def self.get_latest_release(git_repo)
releases = list_releases git_repo
releases.first
end
def self.list_releases(git_repo)
repo = Octokit::Repository.from_url git_repo
client = Octokit::Client.new
comparator = lambda { |a, b|
Gem::Version.new(extract_version_from_tag(b.tag_name)) <=> Gem::Version.new(extract_version_from_tag(a.tag_name))
}
releases = client.releases(repo).sort(&comparator)
fail_script_if releases.empty?, "Can't resolve Github releases"
releases
end
def self.extract_version_from_tag(tag)
tag =~ /(\d+\.\d+(.\d+)?)/
not_nil $1
end
end