Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save diederich/f49670cf08613101b59ace318758b226 to your computer and use it in GitHub Desktop.
Save diederich/f49670cf08613101b59ace318758b226 to your computer and use it in GitHub Desktop.
Post notification to Slack after Xcode bot build
#!/usr/bin/env python
import os
import requests
import json
print "Slack it up v.1.3"
slack_hook = "HOOK"
xcode_server_url = "xcbot://your.server.name"
"""
Sample environment passed to script after integration
"""
sample_environment = {
'XCS' : 1,
'XCS_ANALYZER_WARNING_CHANGE' : 0,
'XCS_ANALYZER_WARNING_COUNT' : 0,
'XCS_BOT_ID' : "710e92380c223f0cc4553eb09c02142f",
'XCS_BOT_NAME' : "TestingFromCommandLine",
'XCS_BOT_TINY_ID' : "4BA7AF5",
'XCS_ERROR_CHANGE' : 0,
'XCS_ERROR_COUNT' : 0,
'XCS_INTEGRATION_ID' : "e6087498151a723b692c579232115bb9",
'XCS_INTEGRATION_NUMBER' : 14,
'XCS_INTEGRATION_RESULT' : "succeeded",
'XCS_INTEGRATION_TINY_ID' : "64E9825",
'XCS_OUTPUT_DIR' : "/Library/Developer/XcodeServer/Integrations/Integration-e6087498151a723b692c579232115bb9",
'XCS_SOURCE_DIR' : "/Library/Developer/XcodeServer/Integrations/Caches/710e92380c223f0cc4553eb09c02142f/Source",
'XCS_PRIMARY_REPO_BRANCH' : "feature/test",
'XCS_TESTS_CHANGE' : 0,
'XCS_TESTS_COUNT' : 11,
'XCS_TEST_FAILURE_CHANGE' : 0,
'XCS_TEST_FAILURE_COUNT' : 0,
'XCS_WARNING_CHANGE' : 0,
'XCS_WARNING_COUNT' : 0,
'XCS_XCODEBUILD_LOG' : "/Library/Developer/XcodeServer/Integrations/Integration-e6087498151a723b692c579232115bb9/build.log",
}
e = os.environ if 'XCS' in os.environ else sample_environment
botname = e['XCS_BOT_NAME']
status = e['XCS_INTEGRATION_RESULT']
build_number = e['XCS_INTEGRATION_NUMBER']
bot_id = e['XCS_BOT_ID']
integration_id = e['XCS_INTEGRATION_ID']
primary_repo_branch = e['XCS_PRIMARY_REPO_BRANCH']
integration_url = xcode_server_url + "/botID/" + bot_id + "/integrationID/" + integration_id
theKeys = [ 'e', 'w', 'a', 't']
names = {
'e' : "Error",
'w' : "Warning",
't' : "Test Failure",
'a' : "Analyzer Warning",
}
error = {
'e' : int(e['XCS_ERROR_COUNT']),
'w' : int(e['XCS_WARNING_COUNT']),
't' : int(e['XCS_TEST_FAILURE_COUNT']),
'a' : int(e['XCS_ANALYZER_WARNING_COUNT']),
}
change = {
'e' : int(e['XCS_ERROR_CHANGE']),
'w' : int(e['XCS_WARNING_CHANGE']),
't' : int(e['XCS_TEST_FAILURE_CHANGE']),
'a' : int(e['XCS_ANALYZER_WARNING_CHANGE']),
}
colors = {
'e' : "#ff1a1a",
'w' : "#999999",
't' : "#ff1a1a",
'a' : "#999999",
's' : "#36a641", #success
}
test_count = int(e['XCS_TESTS_COUNT'])
test_fail = error['t']
test_pass = test_count - test_fail
slack_color = colors["e"] if error['e'] > 0 else colors["w"] if error['w'] > 0 else colors["t"] if error['t'] > 0 else colors["a"] if error['a'] > 0 else colors["s"]
title = "Run %s from branch '%s' finished and %s." % (build_number, primary_repo_branch, status)
msg = "Passed %s of %s tests." % (test_pass, test_count)
if error['e'] == error['w'] == error['a'] == error['t'] == 0:
msg += "\n Light is green, trap is clean. No problems detected."
else:
for k in theKeys:
if error[k] == 0: continue
msg += "\n Found %s problems in %s, a change of %s." % (error[k], names[k], change[k])
print title
print msg
attachments={
"title" : title,
"title_link" : integration_url,
"text" : msg,
"fallback" : "fallback not done",
"color": slack_color,
"icon_emoji": ":robot_face:",
"fields": [
]
}
payload={
"username": botname,
"attachments": [attachments],
}
print json.dumps(payload, indent=4, sort_keys=True)
post_response = requests.post(url=slack_hook, data=json.dumps(payload))
print "Post response:", post_response.text
@diederich
Copy link
Author

screen shot 2017-09-30 at 18 55 11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment