pullermann 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/pullermann/pullermann.rb +47 -20
  2. metadata +1 -1
@@ -46,7 +46,7 @@ class Pullermann
46
46
  self.test_block.call
47
47
  # Unless already set, the success/failure is determined by the last
48
48
  # command's return code.
49
- @result ||= $? == 0
49
+ @test_success ||= $? == 0
50
50
  # We need to switch back to the original branch in case we need to test
51
51
  # more pull requests.
52
52
  switch_branch_back
@@ -75,22 +75,28 @@ class Pullermann
75
75
  # Find environment (tasks, project, ...).
76
76
  @prepare_block ||= lambda {}
77
77
  @test_block ||= lambda { `rake test` }
78
- connect_to_github
78
+ @github = connect_to_github
79
+ @github_fail = if self.username == self.username_fail
80
+ @github
81
+ else
82
+ connect_to_github self.username_fail, self.password_fail
83
+ end
79
84
  end
80
85
 
81
86
  def connect_to_github(user = self.username, pass = self.password)
82
- @github = Octokit::Client.new(
87
+ github = Octokit::Client.new(
83
88
  :login => user,
84
89
  :password => pass
85
90
  )
86
91
  # Check user login to GitHub.
87
- @github.login
88
- @log.info "Successfully logged into GitHub (API v#{@github.api_version}) with user '#{user}'."
92
+ github.login
93
+ @log.info "Successfully logged into GitHub (API v#{github.api_version}) with user '#{user}'."
89
94
  # Ensure the user has access to desired project.
90
- @project = /:(.*)\.git/.match(git_config['remote.origin.url'])[1]
95
+ @project ||= /:(.*)\.git/.match(git_config['remote.origin.url'])[1]
91
96
  begin
92
- @github.repo @project
97
+ github.repo @project
93
98
  @log.info "Successfully accessed GitHub project '#{@project}'"
99
+ github
94
100
  rescue Octokit::Unauthorized => e
95
101
  @log.error "Unable to access GitHub project with user '#{user}':\n#{e.message}"
96
102
  abort
@@ -115,6 +121,9 @@ class Pullermann
115
121
  @log.info 'Pull request not auto-mergeable, skipping... '
116
122
  return false
117
123
  end
124
+ # Compare current sha ids of target and source branch with those from the last test run.
125
+ @target_head_sha = @github.commits(@project).first.sha
126
+ @pull_head_sha = pull_request.head.sha
118
127
  comments = @github.issue_comments(@project, @request_id)
119
128
  comments = comments.select{ |c| [username, username_fail].include?(c.user.login) }.reverse
120
129
  if comments.empty?
@@ -122,14 +131,15 @@ class Pullermann
122
131
  @log.info 'New pull request detected, test run needed.'
123
132
  return true
124
133
  else
125
- # Compare current sha ids of target and source branch with those from the last test run.
126
- @target_head_sha ||= @github.commits(@project).first.sha
127
- @pull_head_sha = pull_request.head.sha
128
134
  # Initialize shas to ensure it will live on after the 'each' block.
129
135
  shas = nil
130
136
  comments.each do |comment|
131
137
  shas = /master sha# ([\w]+) ; pull sha# ([\w]+)/.match(comment.body)
132
- break if shas && shas[1] && shas[2]
138
+ if shas && shas[1] && shas[2]
139
+ # Remember @comment to be able to update or delete it later.
140
+ @comment = comment
141
+ break
142
+ end
133
143
  end
134
144
  # We finally found the latest comment that includes the necessary information.
135
145
  if shas && shas[1] && shas[2]
@@ -171,19 +181,36 @@ class Pullermann
171
181
  `git co master &> /dev/null`
172
182
  end
173
183
 
184
+ # Analyze old comment to see whether it was a successful or a failing one.
185
+ def old_comment_success?
186
+ return unless @comment
187
+ # Determine boolean value.
188
+ @comment['body'].include? 'Well done!'
189
+ end
190
+
174
191
  # Output the result to a comment on the pull request on GitHub.
175
192
  def comment_on_github
176
- sha_string = "\n( master sha# #{@target_head_sha} ; pull sha# #{@pull_head_sha} )"
177
- if @result
178
- message = 'Well done! All tests are still passing after merging this pull request. '
193
+ # Determine comment message.
194
+ message = if @test_success
195
+ 'Well done! All tests are still passing after merging this pull request.'
179
196
  else
180
- unless self.username == self.username_fail
181
- # Re-connect with username_fail and password_fail.
182
- connect_to_github(self.username, self.password)
183
- end
184
- message = 'Unfortunately your tests are failing after merging this pull request. '
197
+ 'Unfortunately your tests are failing after merging this pull request.'
198
+ end
199
+ message += "\n( master sha# #{@target_head_sha} ; pull sha# #{@pull_head_sha} )"
200
+ if old_comment_success? == @test_success
201
+ # Replace existing @comment's body with the correct connection.
202
+ call_github(@test_success).update_comment(@project, @comment['id'], message)
203
+ else
204
+ # Delete old @comment with correct connection (if @comment exists).
205
+ call_github(!@test_success).delete_comment(@project, @comment['id']) if @comment
206
+ # Create new comment with correct connection.
207
+ call_github.add_comment(@project, @request_id, message)
185
208
  end
186
- @github.add_comment(@project, @request_id, message + sha_string)
209
+ end
210
+
211
+ # Determine which connection to GitHub should be used for the call.
212
+ def call_github(use_default_user = true)
213
+ use_default_user ? @github : @github_fail
187
214
  end
188
215
 
189
216
  # Collect git config information in a Hash for easy access.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pullermann
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: