1+ #!/usr/bin/env ruby
2+
3+ require 'optparse'
4+ require 'octokit'
5+
6+ options = { }
7+ OptionParser . new do |opt |
8+ opt . on ( '-s' , '--secret SECRET' , 'GitHub access token' ) { |o | options [ :secret ] = o }
9+ opt . on ( '-r' , '--repo-slug REPO_SLUG' , 'Repo slug. i.e.: apple/swift' ) { |o | options [ :repo_slug ] = o }
10+ opt . on ( '-c' , '--changelog-file CHANGELOG_FILE' , 'Changelog path' ) { |o | options [ :changelog_file ] = o }
11+ opt . on ( '-t' , '--tag TAG' , 'Tag name' ) { |o | options [ :tag_name ] = o }
12+ end . parse!
13+
14+ raise OptionParser ::MissingArgument if options [ :secret ] . nil?
15+ raise OptionParser ::MissingArgument if options [ :repo_slug ] . nil?
16+ raise OptionParser ::MissingArgument if options [ :changelog_file ] . nil?
17+ raise OptionParser ::MissingArgument if options [ :tag_name ] . nil?
18+
19+ client = Octokit ::Client . new ( :access_token => options [ :secret ] )
20+ user = client . user
21+ user . login
22+
23+ unless client . scopes . include? 'public_repo' or client . scopes . include? 'repo'
24+ raise Error , "Insufficient permissions. Make sure your token contains the repo or public_repo scope."
25+ end
26+
27+ puts "Logged in as #{ user . name } "
28+ puts "Deploying to repo: #{ options [ :repo_slug ] } "
29+
30+ tag_matched = false
31+ release_url = nil
32+ releases = client . releases ( options [ :repo_slug ] )
33+ body = File . open ( options [ :changelog_file ] , "rb" ) . read [ /##.*?$(.*?)##/m , 1 ] . strip
34+
35+ releases . each do |release |
36+ if release . tag_name == options [ :tag_name ]
37+ release_url = release . rels [ :self ] . href
38+ tag_matched = true
39+ end
40+ end
41+
42+ # if tag has been pushed directly to git, create a github release
43+ if tag_matched == false
44+ client . create_release ( options [ :repo_slug ] , options [ :tag_name ] , { :name => options [ :tag_name ] , :body => body } )
45+ else
46+ client . update_release ( release_url , { :name => options [ :tag_name ] , :body => body } )
47+ end
0 commit comments