Last active
January 11, 2020 22:56
-
-
Save dctrwatson/4669113 to your computer and use it in GitHub Desktop.
Jenkins Phabricator Inegration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "## Getting base rev for diff" | |
BASE_GIT_COMMIT=$(echo {\"diff_id\": ${DIFF_ID}} | arc call-conduit differential.getdiff | awk -v RS=',' -v FS=':' '$1~/\"sourceControlBaseRevision\"/ {print $2}' | tr -d \") | |
echo "## Reset to base commit" | |
git reset --hard ${BASE_GIT_COMMIT} | |
echo "## Cleaning out repo" | |
git clean -fdx | |
echo "## Apply diff" | |
arc patch --nobranch --no-ansi --diff $DIFF_ID --nocommit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"events.listeners": ["JenkinsDiffEventListener"], | |
"jenkins.uri": "https:\/\/jenkins.example.com", | |
"jenkins.job": "repo-phabricator" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Copyright 2013 Disqus, Inc. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
class JenkinsDiffEventListener extends PhutilEventListener { | |
public function register() { | |
$this->listen(ArcanistEventType::TYPE_DIFF_WASCREATED); | |
} | |
public function handleEvent(PhutilEvent $event) { | |
$diff_id = $event->getValue('diffID'); | |
/* Need to send a get request to jenkins to trigger the job. We pass the | |
* diff id to jenkins via its api. | |
*/ | |
$workflow = $event->getValue('workflow'); | |
$jenkins_uri = $workflow->getConfigFromAnySource('jenkins.uri'); | |
$jenkins_job = $workflow->getConfigFromAnySource('jenkins.job'); | |
if (!$jenkins_uri || !$jenkins_job) { | |
return; | |
} | |
$url = $jenkins_uri."/job/".$jenkins_job."/buildWithParameters?DIFF_ID=".$diff_id; | |
file_get_contents($url); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Copyright 2013 Disqus, Inc. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
""" | |
import json | |
import os | |
import sys | |
import urllib | |
from phabricator import Phabricator | |
if __name__ == '__main__': | |
jenkins_build_url = os.environ['BUILD_URL'] | |
console_output_url = jenkins_build_url + "console" | |
test_report_url = jenkins_build_url + "aggregatedTestReport/" | |
build_data_url = jenkins_build_url + "api/json" | |
p = Phabricator() | |
diff_id = os.environ['DIFF_ID'] | |
differential = p.differential.querydiffs(ids=[diff_id]) | |
differential_id = differential[diff_id]['revisionID'] | |
print "Reading Jenkins Status..." | |
data = json.load(urllib.urlopen(build_data_url)) | |
result = data['result'] | |
if result == "FAILURE": | |
actions = data['actions'] | |
num_failures = 0 | |
for a in actions: | |
if 'failCount' in a: | |
num_failures = a['failCount'] | |
print "D%s failed tests on diff id %s" % (differential_id, diff_id) | |
if int(num_failures) == 0: | |
message = "Jenkins is unable to run tests with this patch. See %s to see why." % console_output_url | |
else: | |
message = "This patch causes %s tests to fail! See %s to see which tests are failing." % (num_failures, test_report_url) | |
action = "reject" | |
silent = False | |
elif result == "SUCCESS": | |
print "D%s passed all tests with diff id %s" % (differential_id, diff_id) | |
message = "This patch does not break any tests. +1. See the passing build here: %s" % test_report_url | |
action = "none" | |
# This comment should not email everyone. | |
silent = True | |
# If the jenkins job was ABORTED or any other state. | |
else: | |
print "Jenkins status is: %s. Exiting." % result | |
sys.exit(0) | |
# This will fail if the differential has been closed already or if | |
# phabricator is not working correctly. We should silently fail and print | |
# out the exception for debugging. By catching the exception we prevent the | |
# error code of the script from being not 0. | |
try: | |
print "Creating comment: %s" % message | |
print "Comment has type: %s" % action | |
p.differential.createcomment(revision_id=int(differential_id), message=message, action=action, silent=silent) | |
except BaseException as e: | |
print "Exception When Trying to Create Comment" | |
print e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def proc = ["sh", "-c", "echo {} | arc call-conduit --conduit-uri=https://phabricator.example.com user.whoami"].execute() | |
proc.waitFor() | |
println proc.in.text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"phid" : "PHID-USER-snovkf3y34d37i4e4p4t", | |
"userName" : "dctrwatson", | |
"realName" : "John Watson", | |
"image" : "https:\/\/secure.phabricator.com\/file\/data\/chf32yop27c7sz4ocupp\/PHID-FILE-wjy7rr4yygzqj5gttc5h\/github-profile.jpg", | |
"uri" : "https:\/\/secure.phabricator.com\/p\/dctrwatson\/", | |
"roles" : [ | |
"verified" | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please take a look at https://github.com/facebook/phabricator/issues/457 ?