Class: Wooget::Github
- Inherits:
-
Object
- Object
- Wooget::Github
- Defined in:
- lib/wooget/github.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#git_command ⇒ Object
readonly
Returns the value of attribute git_command.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#working_dir ⇒ Object
readonly
Returns the value of attribute working_dir.
Instance Method Summary collapse
- #create_release(tag_name, artifacts = nil, options) ⇒ Object
- #create_tag(tag_name, sha = nil, message = nil) ⇒ Object
- #current_commit_sha ⇒ Object
- #do_release(release_name, artifacts = nil, **options) ⇒ Object
- #github_client ⇒ Object
-
#initialize(working_dir = Dir.pwd, access_token = nil, logger = nil) ⇒ Github
constructor
A new instance of Github.
- #inside_working_tree? ⇒ Boolean
- #repo ⇒ Object
- #repo_name ⇒ Object
Constructor Details
#initialize(working_dir = Dir.pwd, access_token = nil, logger = nil) ⇒ Github
Returns a new instance of Github.
10 11 12 13 14 15 16 |
# File 'lib/wooget/github.rb', line 10 def initialize(working_dir=Dir.pwd, access_token=nil, logger=nil) @working_dir = working_dir @git_command = "git -C #{working_dir}" @github_client = nil @access_token = access_token @logger = (logger.nil?) ? Wooget.log : logger end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
8 9 10 |
# File 'lib/wooget/github.rb', line 8 def access_token @access_token end |
#git_command ⇒ Object (readonly)
Returns the value of attribute git_command.
7 8 9 |
# File 'lib/wooget/github.rb', line 7 def git_command @git_command end |
#logger ⇒ Object
Returns the value of attribute logger.
8 9 10 |
# File 'lib/wooget/github.rb', line 8 def logger @logger end |
#working_dir ⇒ Object (readonly)
Returns the value of attribute working_dir.
7 8 9 |
# File 'lib/wooget/github.rb', line 7 def working_dir @working_dir end |
Instance Method Details
#create_release(tag_name, artifacts = nil, options) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/wooget/github.rb', line 80 def create_release tag_name, artifacts=nil, logger.info "Creating release '#{options[:name]}' on repo #{repo_name}" release = github_client.create_release repo, tag_name, logger.info "Uploading assets..." unless artifacts.empty? artifacts.each { |artifact| logger.info "--> Upload asset: #{artifact}" github_client.upload_asset release.url, artifact, {content_type: "application/zip" } } logger.info "Publishing release.." github_client.update_release release.url, {draft: false} end |
#create_tag(tag_name, sha = nil, message = nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/wooget/github.rb', line 94 def create_tag tag_name, sha=nil, =nil sha = current_commit_sha if sha.nil? = tag_name if .nil? commit = github_client.commit(repo, sha).commit #TODO check if sha is upstream available user = commit..name email = commit..email logger.info "Create tag #{tag_name} with info from last committer" t = github_client.create_tag repo, tag_name, , sha ,"commit", user, email, Time.now.iso8601 github_client.create_ref repo, "tags/#{t.tag}", t.sha end |
#current_commit_sha ⇒ Object
50 51 52 |
# File 'lib/wooget/github.rb', line 50 def current_commit_sha `#{git_command} rev-parse --verify HEAD`.strip end |
#do_release(release_name, artifacts = nil, **options) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/wooget/github.rb', line 54 def do_release release_name, artifacts=nil, ** release_status = true = {} if .nil? artifacts = [] if artifacts.nil? = { target_commitish: current_commit_sha(), name: release_name, body: "", prerelease: false } = .merge! [:draft] = true logger.info "Check releases" releases = github_client.releases(repo).map {|release| release.name } unless releases.include? release_name create_release release_name, artifacts, else logger.error "Release #{options[:name]} already exist" release_status = false end release_status end |
#github_client ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/wooget/github.rb', line 18 def github_client if @github_client.nil? token = @access_token if ENV['OCTOKIT_ACCESS_TOKEN'] and token.nil? token = ENV['OCTOKIT_ACCESS_TOKEN'] end unless token.nil? @github_client = Octokit::Client.new access_token: token end end @github_client end |
#inside_working_tree? ⇒ Boolean
45 46 47 48 |
# File 'lib/wooget/github.rb', line 45 def inside_working_tree? inside_working_tree = `#{git_command} rev-parse --is-inside-work-tree`.strip inside_working_tree.eql? "true" end |
#repo ⇒ Object
33 34 35 36 37 38 |
# File 'lib/wooget/github.rb', line 33 def repo if @repo.nil? @repo = Octokit::Repository.new repo_name end @repo end |
#repo_name ⇒ Object
40 41 42 43 |
# File 'lib/wooget/github.rb', line 40 def repo_name remote_url = `#{git_command} config --get remote.origin.url`.strip.chomp '.git' remote_url.split(':').last.split('/')[-2..-1].join('/') end |