Class: Anticuado::GitHub

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_name, enterprise: true, api_endpoint: nil) ⇒ GitHub

Returns a new instance of GitHub.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/anticuado/github.rb', line 8

def initialize(repository_name, enterprise: true, api_endpoint: nil)
  @repo_name = repository_name

  @client = if enterprise
              ::Octokit::Client.new(
                  access_token: ENV['GHE_ACCESS_TOKEN'] || 'dummy_token',
                  api_endpoint: api_endpoint || ENV['GHE_API_ENDPOINT'], # 'https://example.api.endpoint/api/v3/'
                  )
            else
              ::Octokit::Client.new(access_token: ENV['GITHUB_TOKEN'])
            end
  @repo_uri = "git@#{URI.parse(enterprise ? @client.api_endpoint : @client.web_endpoint).host}:#{@repo_name}.git"
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/anticuado/github.rb', line 6

def client
  @client
end

#gitObject (readonly)

Returns the value of attribute git.



6
7
8
# File 'lib/anticuado/github.rb', line 6

def git
  @git
end

#repo_nameObject (readonly)

Returns the value of attribute repo_name.



6
7
8
# File 'lib/anticuado/github.rb', line 6

def repo_name
  @repo_name
end

#repo_uriObject (readonly)

Returns the value of attribute repo_uri.



6
7
8
# File 'lib/anticuado/github.rb', line 6

def repo_uri
  @repo_uri
end

Instance Method Details

#clone_or_open_to(target_path) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/anticuado/github.rb', line 22

def clone_or_open_to(target_path)
  @git = begin
    ::Git.clone(@repo_uri, target_path)
  rescue
    g = ::Git.open(target_path)
    g.pull
    g
  end
end

#create_a_new_pull_request(base_branch:, head_branch: (Time.now.strftime '%Y%m%d-%H%M%S'), update_libraries: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/anticuado/github.rb', line 32

def create_a_new_pull_request(base_branch:, head_branch: (Time.now.strftime '%Y%m%d-%H%M%S'), update_libraries: nil)
  remote_name = 'origin'

  begin
    create_a_branch_local head_branch
    commit_all_changes

    git_push_to_remote remote_name, head_branch
    create_pull_request(base_branch: base_branch, head_branch: head_branch, title: github_pr_title(head_branch), body: github_pr_body(update_libraries))

    delete_a_branch_local head_branch
  rescue Git::GitExecuteError => e
    puts "no changes: #{e}, #{e.message}"
  end
end