Class: Wooget::Github

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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_tokenObject

Returns the value of attribute access_token.



8
9
10
# File 'lib/wooget/github.rb', line 8

def access_token
  @access_token
end

#git_commandObject (readonly)

Returns the value of attribute git_command.



7
8
9
# File 'lib/wooget/github.rb', line 7

def git_command
  @git_command
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/wooget/github.rb', line 8

def logger
  @logger
end

#working_dirObject (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, options
  logger.info "Creating release '#{options[:name]}' on repo #{repo_name}"
  release = github_client.create_release repo, tag_name, options

  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, message=nil
  sha = current_commit_sha if sha.nil?
  message = tag_name if message.nil?

  commit = github_client.commit(repo, sha).commit
  
  #TODO check if sha is upstream available
  user = commit.author.name
  email = commit.author.email
  
  logger.info "Create tag #{tag_name} with info from last committer"
  t = github_client.create_tag repo, tag_name, message, sha ,"commit", user, email, Time.now.iso8601
  github_client.create_ref repo, "tags/#{t.tag}", t.sha
end

#current_commit_shaObject



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, **options
  release_status = true
  options = {} if options.nil?
  artifacts = [] if artifacts.nil?

  default_options = {
    target_commitish: current_commit_sha(),
    name: release_name,
    body: "",
    prerelease: false
  }

  options = default_options.merge! options
  options[: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, options
  else
    logger.error "Release #{options[:name]} already exist"
    release_status = false
  end
  release_status
end

#github_clientObject



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

Returns:

  • (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

#repoObject



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_nameObject



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