Module: Git

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

Instance Method Summary collapse

Instance Method Details

#git_actual_rebaseObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/caperoma/models/tasks/modules/git.rb', line 108

def git_actual_rebase
  if enable_git?
    pp 'Getting the latest code from Github'

    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

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

    case response.status
    when 200, 201, 202, 204, 301, 302, 303, 304, 307
      `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})`
    when 401, 403
      puts 'No access to Git. Maybe login or password are incorrect.'
    when 404
      puts "A resource on Git not found. Maybe the repository name #{project.github_repo} is incorrect."
    else
      puts 'Could not get the latest changes from Github.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without pulling the latest code from Git.'
end

#git_branchObject



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

def git_branch
  `git -C "#{project.folder_path}" checkout -b #{branch}` if enable_git?
end

#git_checkout(_branch) ⇒ Object



141
142
143
# File 'lib/caperoma/models/tasks/modules/git.rb', line 141

def git_checkout(_branch)
  `git -C "#{project.folder_path}" checkout #{_branch}` if enable_git?
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 enable_git?
end

#git_current_branchObject



49
50
51
# File 'lib/caperoma/models/tasks/modules/git.rb', line 49

def git_current_branch
  `git -C "#{project.folder_path}" rev-parse --abbrev-ref HEAD`.gsub("\n", '') if enable_git?
end

#git_last_commit_nameObject



45
46
47
# File 'lib/caperoma/models/tasks/modules/git.rb', line 45

def git_last_commit_name
  `git -C "#{project.folder_path}" log #{parent_branch}..#{branch} --oneline --pretty=format:'%s' --skip=1 -1` if enable_git?
end

#git_pull_requestObject



53
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
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/caperoma/models/tasks/modules/git.rb', line 53

def git_pull_request
  puts 'Making a pull request'

  pull_request_data = Jbuilder.encode do |j|
    j.title title
    j.body description_for_pull_request
    j.head branch
    j.base parent_branch
  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

  response = 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

  case response.status
  when 200, 201, 202, 204, 301, 302, 303, 304, 307
    puts 'The pull request was sent.'
  when 401, 403
    puts 'No access to Git. Maybe login or password are incorrect.'
  when 404
    puts "A resource on Git not found. Maybe the repository name #{project.github_repo} is incorrect."
  else
    puts 'Could not make a pull request.'
    puts "Error status: #{response.status}"
    puts "Message from server: #{response.reason_phrase}"
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without requests to Git.'
end

#git_pushObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/caperoma/models/tasks/modules/git.rb', line 12

def git_push
  if enable_git?
    puts 'Pushing the code to Github'

    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

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

    case response.status
    when 200, 201, 202, 204, 301, 302, 303, 304, 307
      `git -C "#{project.folder_path}" push --set-upstream origin #{git_current_branch}`
    when 401, 403
      puts 'No access to Git. Maybe login or password are incorrect.'
    when 404
      puts "A resource on Git was not found. Maybe the repository name #{project.github_repo} is incorrect."
    else
      puts 'Could not push to Git.'
      puts "Error status: #{response.status}"
      puts "Message from server: #{response.reason_phrase}"
    end
  end
rescue Faraday::ConnectionFailed
  puts 'Connection failed. Performing the task without pushing to Git.'
end

#git_rebase_to_upstreamObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/caperoma/models/tasks/modules/git.rb', line 92

def git_rebase_to_upstream
  if enable_git?
    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_actual_rebase

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