Skip to content

Instantly share code, notes, and snippets.

@lambdan
Last active April 17, 2024 22:30
Show Gist options
  • Save lambdan/270d46b448618e93982a2ad56a188058 to your computer and use it in GitHub Desktop.
Save lambdan/270d46b448618e93982a2ad56a188058 to your computer and use it in GitHub Desktop.
Simple python script that just checks for p4 commits and sends them to a Discord webhook
import subprocess, time, os, requests
CHECK_INTERVAL = 1 # check changes every n secs
DISCORD_WEBHOOK = "https://discord.com/api/webhooks/..."
LAST_CACHE = "last.txt"
# read last commit from cache file
last = 0
if os.path.isfile(LAST_CACHE):
with open(LAST_CACHE,'r') as f:
last = int(f.readlines()[0].strip())
print("last read from file:", last)
def ping_discord(msg):
print("sending to discord:")
data = {"content": msg}
print(data)
#return
response = requests.post(DISCORD_WEBHOOK, json=data)
print(response.status_code)
print(response.content)
print("--- ping discord end ---")
def process_new_commit(cmd_output):
parts = cmd_output.split(" ")
print(parts)
commit_no = int(parts[1])
who = parts[5].split("@")[0]
commit_msg = " ".join(parts[6:]).replace("'", "").strip()
result = "**" + who + "** commited #" + str(commit_no) + ":\n```" + commit_msg + "```"
ping_discord(result)
print("running! go commit something...")
while True:
time.sleep(CHECK_INTERVAL)
# limit 1 (latest), only submitted (avoid pending/shelved), full changelog description
cmd = subprocess.check_output("p4 changes -m 1 -s submitted -l", shell=True).decode("utf8")
cmd = cmd.replace("\t", "")
cmd = cmd.replace("\n\n"," ")
#print(cmd)
if "Change " not in cmd:
print("something seems wrong, output of last cmd:")
print(cmd)
sys.exit(1)
this_number = int(cmd.split(" ")[1])
if (this_number != last):
print("new changelist", this_number)
last = this_number
process_new_commit(cmd)
with open(LAST_CACHE,'w') as f: # write to cache file
f.write(str(this_number))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment