Skip to content

Instantly share code, notes, and snippets.

@IMelker
Created December 8, 2022 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IMelker/a1ad123881414e918a2d91f24731d566 to your computer and use it in GitHub Desktop.
Save IMelker/a1ad123881414e918a2d91f24731d566 to your computer and use it in GitHub Desktop.
SVN updates monitor to discord weboohook
from time import sleep
import requests
import os.path
import sys
import pprint
import svn.local
import svn.remote
# Add chrontab job to run script every 5 minutes
#*/5 * * * * python3 /path/to/py/file/svn_monit.py > /dev/null 2>&1
# Discord has a message length limit of 2000 characters, so we need to set some maximum string length limits
MAX_LENGTH_LOG = 400
MAX_LENGTH_CHANGELIST = 1000
# Base name to give our bot that'll post the payload message
botusernamebase = "SVN"
botavatarurl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f4/Antu_smartsvn.svg/1200px-Antu_smartsvn.svg.png"
reporootdir = "/local/svn/"
webhookurl = "https://discord.com/api/webhooks/**/**"
repohost = "https://mysvn.ru/example/"
repolist = [
"repo1",
"repo2",
"repo3",
]
svnusername="admin"
svnpassword="admin"
def truncate(msg, length):
# Truncate end of log if it exceeds max length
res = msg
if (len(msg) > (length - 6)):
res = msg[0:(length - 6)]
res = "{0} <...>".format(res)
return res
def notify_commit(reponame, revision, author, msg, changelog):
botusername = "{0} [{1}]".format(reponame, botusernamebase)
log = ""
for line in changelog:
if line[0] == 'M':
log += "! "
elif line[0] == 'A':
log += "+ "
elif line[0] == 'D' or line[0] == 'R':
log += "- "
else:
log += line[0] +" "
log += line[1].strip() + "\n"
commits_messages = "> " + msg.replace("\n", "\n> ")
message = "Revision **{0}** commited by **{1}**\n{2}\n```diff\n{3}```\n".format(revision, author, truncate(commits_messages, MAX_LENGTH_LOG), truncate(log, MAX_LENGTH_CHANGELIST))
payload = {
'username': botusername,
'avatar_url': botavatarurl,
'content': message
}
# Send our payload to the webhook with an HTTP POST request
requests.post(webhookurl, data=payload)
def main():
for reponame in repolist:
repopath = "{0}{1}".format(reporootdir, reponame).strip()
repourl = (repohost + reponame).strip()
os.chdir(repopath)
local = svn.local.LocalClient(repopath, username=svnusername, password=svnpassword, trust_cert=True)
local_info = local.info()
remote = svn.remote.RemoteClient(repourl, username=svnusername, password=svnpassword)
remote_info = remote.info()
local_revision = local_info["commit#revision"]
remote_revision = remote_info["commit#revision"]
# check if need to update
if local_revision != remote_revision:
# update current state
remote.checkout(repopath)
local_revision = str(int(local_revision) + 1)
# get commits list from log
log = local.log_default(revision_from=local_revision, revision_to=remote_revision, changelist=True)
for e in log:
msg = "<empty commit message>"
if e.msg is not None:
msg = e.msg
notify_commit(reponame, e.revision, e.author, msg.strip(), e.changelist)
sleep(2)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment