Module: Git

Included in:
Task
Defined in:
lib/caperoma/models/tasks/modules/git.rb

Instance Method Summary collapse

Instance Method Details

#git_branch(name) ⇒ Object



4
5
6
# File 'lib/caperoma/models/tasks/modules/git.rb', line 4

def git_branch(name)
  `git -C "#{project.folder_path}" checkout -b #{name}` if ENV['CAPEROMA_INTEGRATION_TEST'].blank? && ENV['CAPEROMA_TEST'].blank?
end

#git_checkout(branch) ⇒ Object



62
63
64
# File 'lib/caperoma/models/tasks/modules/git.rb', line 62

def git_checkout(branch)
  `git -C "#{project.folder_path}" checkout #{branch}` if ENV['CAPEROMA_INTEGRATION_TEST'].blank? && ENV['CAPEROMA_TEST'].blank?
end

#git_commit(msg) ⇒ Object



8
9
10
# File 'lib/caperoma/models/tasks/modules/git.rb', line 8

def git_commit(msg)
  `git -C "#{project.folder_path}" add -A && git -C "#{project.folder_path}" commit --allow-empty -m "#{msg}"` if ENV['CAPEROMA_INTEGRATION_TEST'].blank? && ENV['CAPEROMA_TEST'].blank?
end

#git_current_branchObject



20
21
22
# File 'lib/caperoma/models/tasks/modules/git.rb', line 20

def git_current_branch
  `git -C "#{project.folder_path}" rev-parse --abbrev-ref HEAD`.gsub("\n", '') if ENV['CAPEROMA_INTEGRATION_TEST'].blank? && ENV['CAPEROMA_TEST'].blank?
end

#git_last_commit_nameObject



16
17
18
# File 'lib/caperoma/models/tasks/modules/git.rb', line 16

def git_last_commit_name
  `git -C "#{project.folder_path}" log --pretty=format:'%s' -1` if ENV['CAPEROMA_INTEGRATION_TEST'].blank? && ENV['CAPEROMA_TEST'].blank?
end

#git_pull_request(into, title, description = '') ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/caperoma/models/tasks/modules/git.rb', line 24

def git_pull_request(into, title, description = '')
  pull_request_data = Jbuilder.encode do |j|
    j.title title
    j.body description
    j.head git_current_branch
    j.base into
  end

  conn = Faraday.new(url: 'https://api.github.com') do |c|
    c.basic_auth(Account.git.email, Account.git.password)
    c.adapter Faraday.default_adapter
  end

  conn.post do |request|
    request.url "/repos/#{project.github_repo}/pulls"
    request.body = pull_request_data
    request.headers['User-Agent'] = 'Caperoma'
    request.headers['Accept'] = 'application/vnd.github.v3+json'
    request.headers['Content-Type'] = 'application/json'
  end
end

#git_pushObject



12
13
14
# File 'lib/caperoma/models/tasks/modules/git.rb', line 12

def git_push
  `git -C "#{project.folder_path}" push --set-upstream origin #{git_current_branch}` if ENV['CAPEROMA_INTEGRATION_TEST'].blank? && ENV['CAPEROMA_TEST'].blank?
end

#git_rebase_to_upstreamObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/caperoma/models/tasks/modules/git.rb', line 46

def git_rebase_to_upstream
  if ENV['CAPEROMA_INTEGRATION_TEST'].blank? && ENV['CAPEROMA_TEST'].blank?
    has_untracked_files = !`git -C "#{project.folder_path}" ls-files --others --exclude-standard`.empty?
    has_changes = !`git -C "#{project.folder_path}" diff`.empty?
    has_staged_changes = !`git -C "#{project.folder_path}" diff HEAD`.empty?

    changes_were_made = has_untracked_files || has_changes || has_staged_changes

    `git -C "#{project.folder_path}" add -A && git -C "#{project.folder_path}" stash` if changes_were_made

    `git -C "#{project.folder_path}" fetch && git -C "#{project.folder_path}" rebase $(git -C "#{project.folder_path}" rev-parse --abbrev-ref --symbolic-full-name @{u})`

    `git -C "#{project.folder_path}" stash apply` if changes_were_made
  end
end