Created
February 2, 2018 15:40
-
-
Save phozzy/a62445a7c022b1d7b2b9276a55eb64ee to your computer and use it in GitHub Desktop.
downloads artifact from maven repository and notifies via webhook
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 python3 | |
import xml.etree.ElementTree as ET | |
import urllib.request | |
import argparse | |
import os | |
# argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-g", "--group", type = str, help = "artifact's group id", required = True) | |
parser.add_argument("-a", "--artifact", type = str, help = "artifact's id", required = True) | |
parser.add_argument("-v", "--version", type = str, help = "artifact's version", required = True) | |
parser.add_argument("-u", "--url", type = str, help = "maven repository url", required = True) | |
parser.add_argument("-d", "--dest", type = str, help = "path to deploy artifact", required = True) | |
args = parser.parse_args() | |
# slack integration | |
webhook = os.environ['NOTIFYHOOK'] | |
def __getjarver__(url): | |
# open connection to maven repository | |
with urllib.request.urlopen(url + "maven-metadata.xml") as data: | |
tree = ET.fromstring(data.read().decode("utf-8")) | |
# get java file version | |
timestamp = tree.findall("./versioning/snapshot/timestamp")[0].text | |
build_num = tree.findall("./versioning/snapshot/buildNumber")[0].text | |
jar_ver = "-" + timestamp + "-" + build_num | |
return jar_ver | |
def sendnotification(artifact_aid, webhook): | |
user = os.getlogin().encode() | |
host = os.uname().nodename.encode() | |
artifact = artifact_aid.encode() | |
data = b'payload={\"channel\": \"#deployment\", \"username\": \"' + user + b'\" , \"text\": \"This is deployment of ' + artifact + b' to ' + host + b' by ' + user + b'.\", \"icon_emoji\": \":ghost:\"}' | |
req = urllib.request.Request(webhook, data=data, method='POST') | |
with urllib.request.urlopen(req) as f: | |
pass | |
return None | |
def downloadjar(url, artifact_gid, artifact_aid, artifact_ver, dest): | |
# url variable | |
url = url + "/" + "/".join((artifact_gid.replace(".", "/"), artifact_aid, artifact_ver)) + "/" | |
# get jar version. there is convention that snapshot of an artifact looks like 9.9.9-SNAPSHOT | |
artifact_ver = artifact_ver.split('-') | |
jar_ver = artifact_ver[0] + ((len(artifact_ver) > 1) and __getjarver__(url) or '') | |
# open connection to maven repository | |
with urllib.request.urlopen(url + artifact_aid + "-" + jar_ver + ".jar") as jar: | |
with open(dest + "/" + artifact_aid + ".jar", 'wb') as fname: | |
fname.write(jar.read()) | |
return None | |
downloadjar(args.url, args.group, args.artifact, args.version, args.dest) | |
sendnotification(args.artifact, webhook) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment