Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update messaging integration on build server #2426

Merged
merged 3 commits into from
Dec 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 42 additions & 31 deletions installer/build-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import datetime
import platform
import shutil
from slacker import Slacker
import re
import stat
import subprocess
Expand All @@ -42,6 +41,8 @@
import time
import traceback
from github3 import login
from requests.auth import HTTPBasicAuth
from requests import post

# Access info class (for version info)
sys.path.append(os.path.join(PATH, 'src', 'classes'))
Expand All @@ -50,8 +51,7 @@
freeze_command = None
errors_detected = []
make_command = "make"
slack_token = None
slack_object = None
zulip_token = None
s3_access_key = None
s3_secret_key = None
s3_connection = None
Expand All @@ -75,6 +75,7 @@ def run_command(command, working_dir=None):
stderr=subprocess.STDOUT)
return iter(p.stdout.readline, b"")


def output(line):
"""Append output to list and print it"""
print(line)
Expand All @@ -86,6 +87,7 @@ def output(line):
line += "\n"
log.write(line)


def error(line):
"""Append error output to list and print it"""
print("Error: %s" % line)
Expand All @@ -95,43 +97,53 @@ def error(line):
else:
log.write(line)


def truncate(message, max=256):
"""Truncate the message with ellipses"""
if len(message) < max:
return message
else:
return "%s..." % message[:max]

def slack(message):
"""Append a message to slack #build-server channel"""
print("Slack: %s" % message)
if slack_object:
slack_object.chat.post_message("#build-server", truncate(message[:256]))

def slack_upload_log(log, title, comment=None):
"""Upload a file to slack and notify a slack channel"""
def zulip_upload_log(log, title, comment=None):
"""Upload a file to zulip and notify a zulip channel"""
output("Zulip Upload: %s" % log_path)

# Close log file
log.close()

print("Slack Upload: %s" % log_path)
if slack_object:
for attempt in range(3):
try:
# Upload build log to slack (and append platform icon to comment [:linux:, :windows:, or :darwin:])
slack_object.files.upload(log_path, filetype="txt", filename="%s-build-server.txt" % platform.system(),
title=title, initial_comment=':%s: %s' % (platform.system().lower(), comment), channels="#build-server")
# Successfully uploaded!
break
except Exception as ex:
# Quietly fail, and try again
if attempt < 2:
output("Upload log to Slack failed... trying again")
else:
# Throw loud exception
raise Exception('Log upload to Slack failed: %s' % log_path, ex)
# Authentication for Zulip
zulip_auth = HTTPBasicAuth('builder-bot@openshot.zulipchat.com', zulip_token)
filename = "%s-build-server.txt" % platform.system()

# Upload file to Zulip
zulip_url = 'https://openshot.zulipchat.com/api/v1/user_uploads'
zulip_upload_url = ''
resp = post(zulip_url, data={}, auth=zulip_auth, files={filename: (filename, open(log_path, "rb"))})
if resp.ok:
zulip_upload_url = resp.json().get("uri", "")
print(resp)

# Determine topic
topic = "Successful Builds"
if "skull" in comment:
topic = "Failed Builds"

# SEND MESSAGE
zulip_url = 'https://openshot.zulipchat.com/api/v1/messages'
zulip_data = {
"type": "stream",
"to": "build-server",
"subject": topic,
"content": ':%s: %s [Build Log](%s)' % (platform.system().lower(), comment, zulip_upload_url)
}

resp = post(zulip_url, data=zulip_data, auth=zulip_auth)

# Re-open the log (for append)
log = open(log_path, "a")
print(resp)

def get_release(repo, tag_name):
"""Fetch the GitHub release tagged with the given tag and return it
Expand Down Expand Up @@ -191,8 +203,7 @@ def parse_version_info(version_path):
try:
# Validate command-line arguments
if len(sys.argv) >= 2:
slack_token = sys.argv[1]
slack_object = Slacker(slack_token)
zulip_token = sys.argv[1]
if len(sys.argv) >= 4:
s3_access_key = sys.argv[2]
s3_secret_key = sys.argv[3]
Expand Down Expand Up @@ -492,8 +503,8 @@ def parse_version_info(version_path):
# Torrent succeeded! Upload the torrent to github
url = upload(torrent_path, github_release)

# Notify Slack
slack_upload_log(log, "%s: Build logs for %s" % (platform.system(), app_name), "Successful *%s* build: %s" % (git_branch_name, download_url))
# Notify Zulip
zulip_upload_log(log, "%s: Build logs for %s" % (platform.system(), app_name), "Successful *%s* build: %s" % (git_branch_name, download_url))

except Exception as ex:
tb = traceback.format_exc()
Expand All @@ -502,6 +513,6 @@ def parse_version_info(version_path):

# Report any errors detected
if errors_detected:
slack_upload_log(log, "%s: Error log for *%s* build" % (platform.system(), git_branch_name), ":skull_and_crossbones: %s" % truncate(errors_detected[0], 150))
zulip_upload_log(log, "%s: Error log for *%s* build" % (platform.system(), git_branch_name), ":skull_and_crossbones: %s" % truncate(errors_detected[0], 100))
exit(1)