Skip to content

Commit

Permalink
Merge pull request #144 from voxpupuli/ci-check-failures-2
Browse files Browse the repository at this point in the history
Set a PR label / comment if CI passes/fails
  • Loading branch information
bastelfreak authored May 1, 2020
2 parents e259cc9 + 4074d67 commit 6daf342
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/models/label.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ class Label < ApplicationRecord
def self.needs_rebase
find_or_create_by(name: 'merge-conflicts', color: '207de5')
end

def self.tests_fail
find_or_create_by(name: 'tests-fail', color: 'e11d21')
end
end
58 changes: 50 additions & 8 deletions app/models/pull_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ def self.update_with_github(gh_pull_request)
end
end

##
# helper method to create a github object for the pullrequest
def github
@github ||= Github.client.pull_request(gh_repository_id, number)
end

##
# Shortcut to check if the PullRequest is closed

Expand Down Expand Up @@ -144,6 +150,24 @@ def validate(saved_changes)
# if the pull request is now closed, dont attach/remove labels/comments
return if closed?

# check merge status and do work if required
validate_mergeable

# check CI status and wo work if required
validate_status
end

private

##
# Since we want to be a fancy responsive application we to all the validation
# stuff which might result in some new querys and api requests asyncronously.

def queue_validation
ValidatePullRequestWorker.perform_async(id, saved_changes) if saved_changes? || mergeable.nil?
end

def validate_mergeable
label = Label.needs_rebase
if mergeable == true
ensure_label_is_detached(label)
Expand All @@ -162,13 +186,31 @@ def validate(saved_changes)
end
end

private

##
# Since we want to be a fancy responsive application we to all the validation
# stuff which might result in some new querys and api requests asyncronously.

def queue_validation
ValidatePullRequestWorker.perform_async(id, saved_changes) if saved_changes? || mergeable.nil?
def validate_status
label = Label.tests_fail
# get current status
state = begin
statuses = Github.client.combined_status(gh_repository_id, github[:head][:sha])
statuses[:state]
rescue StandardError => e
Raven.capture_message('validate status', extra: { error: e.inspect })
nil
end

case state
when 'failure'
repository.ensure_label_exists(label)
add_comment(I18n.t('comment.tests_fail', author: author)) if ensure_label_is_attached(label)
when 'success'
ensure_label_is_detached(label)
when 'pending'
# recheck in 10min? do get get an event if the status changes?
true
else
Raven.capture_message('Unknown PR state /o\\',
extra: { state: state,
repo: repository.github_url,
title: title })
end
end
end
13 changes: 12 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ en:
Dear @%{author}, thanks for the PR!
This is pccibot, your friendly
This is Vox Pupuli Tasks, your friendly
Vox Pupuli GitHub Bot. I noticed that your pull request contains
merge conflict. Can you please rebase?
You can find my sourcecode at [voxpupuli/vox-pupuli-tasks](https://github.com/voxpupuli/vox-pupuli-tasks#vox-pupuli-tasks---the-webapp-for-community-management)
tests_fail: >
Dear @%{author}, thanks for the PR!
This is Vox Pupuli Tasks, your friendly
Vox Pupuli Github Bot. I noticed that your pull request has CI failures. Can you please have a look at the failing CI jobs?
If you need any help, you can reach out to us on our IRC channel voxpupuli on Freenode or our Slack channel voxpupuli at [slack.puppet.com](https://slack.puppet.com/).
You can find my sourcecode at [voxpupuli/vox-pupuli-tasks](https://github.com/voxpupuli/vox-pupuli-tasks#vox-pupuli-tasks---the-webapp-for-community-management)
repo_states:
Expand Down

0 comments on commit 6daf342

Please sign in to comment.