Skip to content

Commit

Permalink
Merge pull request zendesk#669 from zendesk/shender/webhook_commit_co…
Browse files Browse the repository at this point in the history
…mpare

Use the Github API to check if last release already contains a new commit
  • Loading branch information
henders committed Dec 7, 2015
2 parents 404bf58 + cfeaac6 commit a18226d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def docker_repo

def last_release_contains_commit?(commit)
last_release = releases.order(:id).last
last_release && repository.downstream_commit?(last_release.commit, commit)
# status values documented here: http://stackoverflow.com/questions/23943855/github-api-to-compare-commits-response-status-is-diverged
last_release && %w(behind identical).include?(GITHUB.compare(github_repo, last_release.commit, commit).status)
rescue Octokit::Error => e
Airbrake.notify(e, parameters: { github_repo: github_repo, last_commit: last_release.commit, commit: commit })
false # Err on side of caution and cause a new release to be created.
end

def auto_release_stages
Expand Down
1 change: 1 addition & 0 deletions test/controllers/integrations/base_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
end

it 're-uses last release if commit already present' do
stub_github_api("repos/bar/foo/compare/#{sha}...#{sha}", { status: 'identical' })
base_controller.expects(:head).with(:ok).twice
base_controller.create

Expand Down
14 changes: 13 additions & 1 deletion test/models/project_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@ def clone_repository(project)

before do
project.stubs(:repository).returns(repository)
repository.stubs(:downstream_commit?).with('LAST', 'NEW').returns(true)
end

it "returns true if the last release contains that commit" do
stub_github_api('repos/bar/foo/compare/LAST...NEW', { status: 'behind' })
project.releases.create!(commit: "LAST", author: author)
assert project.last_release_contains_commit?("NEW")
end

it "returns false if last release does not contain commit" do
stub_github_api('repos/bar/foo/compare/LAST...NEW', { status: 'ahead' })
project.releases.create!(commit: "LAST", author: author)
refute project.last_release_contains_commit?("NEW")
end

it "returns true if last release has the same commit" do
stub_github_api('repos/bar/foo/compare/LAST...LAST', { status: 'identical' })
project.releases.create!(commit: "LAST", author: author)
assert project.last_release_contains_commit?("LAST")
end

it "returns false if there have been no releases" do
project.releases.destroy_all
refute project.last_release_contains_commit?("NEW")
Expand Down

0 comments on commit a18226d

Please sign in to comment.