diff --git a/.github/workflows/static_checks.yml b/.github/workflows/static_checks.yml new file mode 100644 index 0000000..41bd117 --- /dev/null +++ b/.github/workflows/static_checks.yml @@ -0,0 +1,30 @@ +name: 📊 Static Checks +on: [push, pull_request] + +jobs: + static-checks: + name: Static Checks (black format, file format) + runs-on: ubuntu-20.04 + steps: + - name: Checkout + uses: actions/checkout@v2 + + # Azure repositories are not reliable, we need to prevent Azure giving us packages. + - name: Make apt sources.list use the default Ubuntu repositories + run: | + sudo rm -f /etc/apt/sources.list.d/* + sudo cp -f misc/ci/sources.list /etc/apt/sources.list + sudo apt-get update + + - name: Install dependencies + run: | + sudo apt-get install -qq dos2unix recode + sudo pip3 install black==20.8b1 + + - name: File formatting checks (file_format.sh) + run: | + bash ./misc/scripts/file_format.sh + + - name: Python style checks via black (black_format.sh) + run: | + bash ./misc/scripts/black_format.sh diff --git a/Dockerfile b/Dockerfile index b6310f6..159187b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,9 @@ FROM fedora:33 - + LABEL maintainer="Hein-Pieter van Braam-Stewart " RUN dnf -y install python3-websocket-client python3-requests && \ - dnf clean all + dnf clean all COPY bot.py /root/bot.py WORKDIR /root diff --git a/bot.py b/bot.py index 3c63574..858c87c 100755 --- a/bot.py +++ b/bot.py @@ -9,32 +9,34 @@ import os import time import re -DEBUG=os.environ.get('BOT_DEBUG') -ROCKET_WS_URL=os.environ.get('ROCKET_WS_URL') -ROCKET_USERNAME=os.environ.get('ROCKET_USERNAME') -ROCKET_PASSWORD=os.environ.get('ROCKET_PASSWORD') -GITHUB_PROJECT=os.environ.get('GITHUB_PROJECT') -GITHUB_USERNAME=os.environ.get('GITHUB_USERNAME') -GITHUB_TOKEN=os.environ.get('GITHUB_TOKEN') -DEFAULT_AVATAR_URL=os.environ.get('DEFAULT_AVATAR_URL') -DEFAULT_REPOSITORY=os.environ.get('DEFAULT_REPOSITORY') -REPOSITORY_SHORTNAME_MAP=os.environ.get('REPOSITORY_SHORTNAME_MAP') +DEBUG = os.environ.get("BOT_DEBUG") +ROCKET_WS_URL = os.environ.get("ROCKET_WS_URL") +ROCKET_USERNAME = os.environ.get("ROCKET_USERNAME") +ROCKET_PASSWORD = os.environ.get("ROCKET_PASSWORD") +GITHUB_PROJECT = os.environ.get("GITHUB_PROJECT") +GITHUB_USERNAME = os.environ.get("GITHUB_USERNAME") +GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN") +DEFAULT_AVATAR_URL = os.environ.get("DEFAULT_AVATAR_URL") +DEFAULT_REPOSITORY = os.environ.get("DEFAULT_REPOSITORY") +REPOSITORY_SHORTNAME_MAP = os.environ.get("REPOSITORY_SHORTNAME_MAP") -RE_TAG_PROG = re.compile('([A-Za-z0-9_.-]+)?#(\d+)') -RE_URL_PROG = re.compile('(https?://)?github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pull)/(\d+)(\S*)') +RE_TAG_PROG = re.compile("([A-Za-z0-9_.-]+)?#(\d+)") +RE_URL_PROG = re.compile("(https?://)?github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pull)/(\d+)(\S*)") -SHORTNAME_MAP={} -for item in re.sub('\s+', ' ', REPOSITORY_SHORTNAME_MAP).split(' '): - split = item.split(':') +SHORTNAME_MAP = {} +for item in re.sub("\s+", " ", REPOSITORY_SHORTNAME_MAP).split(" "): + split = item.split(":") if len(split) != 2: continue SHORTNAME_MAP[split[0]] = split[1] + def debug_print(msg): if DEBUG: print(msg) + class Bot: def __init__(self): self.sever_id = None @@ -43,11 +45,13 @@ class Bot: self.token_expires = None self.id = None - self.ws = websocket.WebSocketApp(ROCKET_WS_URL, - on_message = lambda ws,msg: self.on_message(ws, msg), - on_error = lambda ws,msg: self.on_error(ws, msg), - on_close = lambda ws: self.on_close(ws), - on_open = lambda ws: self.on_open(ws)) + self.ws = websocket.WebSocketApp( + ROCKET_WS_URL, + on_message=lambda ws, msg: self.on_message(ws, msg), + on_error=lambda ws, msg: self.on_error(ws, msg), + on_close=lambda ws: self.on_close(ws), + on_open=lambda ws: self.on_open(ws), + ) def run(self): self.ws.run_forever() @@ -61,15 +65,15 @@ class Bot: "msg": "method", "method": "login", "id": "login", - "params" : [{ - "user": { - "username": ROCKET_USERNAME - }, - "password": { - "digest": hashlib.sha256(ROCKET_PASSWORD.encode('utf-8')).hexdigest(), - "algorithm": "sha-256", - }, - }], + "params": [ + { + "user": {"username": ROCKET_USERNAME}, + "password": { + "digest": hashlib.sha256(ROCKET_PASSWORD.encode("utf-8")).hexdigest(), + "algorithm": "sha-256", + }, + } + ], } self.send(login_msg) @@ -86,15 +90,12 @@ class Bot: "msg": "sub", "id": channel, "name": "stream-room-messages", - "params":[ - channel_id, - False - ] + "params": [channel_id, False], } self.send(subscribe_msg) def format_issue(self, repository, issue, add_issue_link): - headers = { 'User-Agent': 'Godot Issuebot by hpvb', } + headers = {"User-Agent": "Godot Issuebot by hpvb"} url = f"https://api.github.com/repos/{GITHUB_PROJECT}/{repository}/issues/{issue}" debug_print(f"GitHub API request: {url}") @@ -107,9 +108,9 @@ class Bot: issue = r.json() avatar_url = DEFAULT_AVATAR_URL - if 'avatar_url' in issue['user'] and issue['user']['avatar_url']: - avatar_url = issue['user']['avatar_url'] - if 'gravatar_id' in issue['user'] and issue['user']['gravatar_id']: + if "avatar_url" in issue["user"] and issue["user"]["avatar_url"]: + avatar_url = issue["user"]["avatar_url"] + if "gravatar_id" in issue["user"] and issue["user"]["gravatar_id"]: avatar_url = f"https://www.gravatar.com/avatar/{issue['user']['gravatar_id']}" is_pr = False @@ -121,44 +122,44 @@ class Bot: status = None closed_by = None - if 'pull_request' in issue and issue['pull_request']: + if "pull_request" in issue and issue["pull_request"]: is_pr = True debug_print(f"GitHub API request: {issue['pull_request']['url']}") - prr = requests.get(issue['pull_request']['url'], headers=headers, auth=(GITHUB_USERNAME, GITHUB_TOKEN)) + prr = requests.get(issue["pull_request"]["url"], headers=headers, auth=(GITHUB_USERNAME, GITHUB_TOKEN)) if prr.status_code == 200: pr = prr.json() - status = pr['state'] + status = pr["state"] - if 'merged_by' in pr and pr['merged_by']: - pr_merged_by = pr['merged_by']['login'] - if 'mergeable' in pr: - pr_mergeable = pr['mergeable'] - if 'merged' in pr: - pr_merged = pr['merged'] - if 'draft' in pr: - pr_draft = pr['draft'] - if 'requested_reviewers' in pr and pr['requested_reviewers']: + if "merged_by" in pr and pr["merged_by"]: + pr_merged_by = pr["merged_by"]["login"] + if "mergeable" in pr: + pr_mergeable = pr["mergeable"] + if "merged" in pr: + pr_merged = pr["merged"] + if "draft" in pr: + pr_draft = pr["draft"] + if "requested_reviewers" in pr and pr["requested_reviewers"]: reviewers = [] - for reviewer in pr['requested_reviewers']: - reviewers.append(reviewer['login']) - pr_reviewers = ', '.join(reviewers) - if 'requested_teams' in pr and pr['requested_teams']: + for reviewer in pr["requested_reviewers"]: + reviewers.append(reviewer["login"]) + pr_reviewers = ", ".join(reviewers) + if "requested_teams" in pr and pr["requested_teams"]: teams = [] - for team in pr['requested_teams']: + for team in pr["requested_teams"]: teams.append(f"team:{team['name']}") if pr_reviewers: - pr_reviewers += ' and ' - pr_reviewers += ', '.join(teams) + pr_reviewers += " and " + pr_reviewers += ", ".join(teams) else: - pr_reviewers = ', '.join(teams) + pr_reviewers = ", ".join(teams) else: - status = issue['state'] + status = issue["state"] - if status == 'closed': - if 'closed_by' in issue and issue['closed_by']: - closed_by = issue['closed_by']['login'] + if status == "closed": + if "closed_by" in issue and issue["closed_by"]: + closed_by = issue["closed_by"]["login"] issue_type = None @@ -168,7 +169,7 @@ class Bot: status = "PR merged" if pr_merged_by: status += f" by {pr_merged_by}" - elif status == 'closed': + elif status == "closed": status = "PR closed" elif not pr_merged: status = "PR open" @@ -181,23 +182,23 @@ class Bot: status += " [needs rebase]" if pr_reviewers: status += f" reviews required from {pr_reviewers}" - + else: issue_type = "Issue" status = f"Status: {status}" - if not pr_merged and closed_by and status == 'closed': + if not pr_merged and closed_by and status == "closed": status += f" by {closed_by}" retval = { "author_icon": avatar_url, - "author_link": issue['html_url'], + "author_link": issue["html_url"], "author_name": f"{repository.title()} [{issue_type}]: {issue['title']} #{issue['number']}", "text": status, } if not add_issue_link: - retval.pop('author_link', None) + retval.pop("author_link", None) return retval @@ -207,43 +208,43 @@ class Bot: links = [] # First replace all the full links that rocket.chat has detected with tags - if 'urls' in msg and msg['urls']: + if "urls" in msg and msg["urls"]: urls_to_keep = [] - for url in msg['urls']: + for url in msg["urls"]: debug_print(f"URL: {url['url']}") - match = re.search(RE_URL_PROG, url['url']) - if match and not match.group(6).startswith('#') and not match.group(6).startswith('/'): + match = re.search(RE_URL_PROG, url["url"]) + if match and not match.group(6).startswith("#") and not match.group(6).startswith("/"): - match = re.search(RE_URL_PROG, url['url']) + match = re.search(RE_URL_PROG, url["url"]) repository = match.group(3) issue = int(match.group(5)) - tag = f'{repository}#{issue}' + tag = f"{repository}#{issue}" debug_print(f"Replacing url {url['url']} with {tag}") - msg['msg'] = msg['msg'].replace(url['url'], tag) + msg["msg"] = msg["msg"].replace(url["url"], tag) continue urls_to_keep.append(url) - msg['urls'] = urls_to_keep + msg["urls"] = urls_to_keep # Then we replace all of the part-urls with tags as well - for match in re.finditer(RE_URL_PROG, msg['msg']): + for match in re.finditer(RE_URL_PROG, msg["msg"]): project = match.group(2) repository = match.group(3) issue = match.group(5) - tag = f'{repository}#{issue}' + tag = f"{repository}#{issue}" if project == GITHUB_PROJECT: - if (match.group(6).startswith('#') or match.group(6).startswith('/')) and not tag in msg['msg']: - msg['msg'] = msg['msg'].replace(match.group(0), f'{match.group(0)} ({tag})') + if (match.group(6).startswith("#") or match.group(6).startswith("/")) and not tag in msg["msg"]: + msg["msg"] = msg["msg"].replace(match.group(0), f"{match.group(0)} ({tag})") add_issue_link = False else: - msg['msg'] = msg['msg'].replace(match.group(0), tag) + msg["msg"] = msg["msg"].replace(match.group(0), tag) # Then finally add the metadata for all our tags debug_print("Scanning message for tags") - for match in re.finditer(RE_TAG_PROG, msg['msg']): + for match in re.finditer(RE_TAG_PROG, msg["msg"]): repository = match.group(1) issue = int(match.group(2)) @@ -257,7 +258,9 @@ class Bot: repository = DEFAULT_REPOSITORY if repository in SHORTNAME_MAP: - debug_print(f"Found repository: {repository} in shortname map. Replacing with {SHORTNAME_MAP[repository]}") + debug_print( + f"Found repository: {repository} in shortname map. Replacing with {SHORTNAME_MAP[repository]}" + ) repository = SHORTNAME_MAP[repository] debug_print(f"Message contains issue for {repository}") @@ -268,72 +271,69 @@ class Bot: if not len(links): return - - if not 'attachments' in msg: - msg['attachments'] = [] + + if not "attachments" in msg: + msg["attachments"] = [] # We may be editing, remove all the github attachments old_attachments = [] - for attachment in msg['attachments']: - if 'author_icon' in attachment: + for attachment in msg["attachments"]: + if "author_icon" in attachment: continue old_attachments.append(attachment) - msg['attachments'] = old_attachments + msg["attachments"] = old_attachments # Hack Hack, the clients won't update without a change to this field. Even if we add or remove attachments. - msg['msg'] = msg['msg'] + " " + msg["msg"] = msg["msg"] + " " # Add timestamp to all attachments. These are visible in the mobile client. for link in links: - link['ts'] = msg['ts'], + link["ts"] = (msg["ts"],) # Deduplicate links - [msg['attachments'].append(x) for x in links if x not in msg['attachments']] + [msg["attachments"].append(x) for x in links if x not in msg["attachments"]] - update_msg = { - "msg": "method", - "method": "updateMessage", - "id": "update-message", - "params": [ msg ] - } + update_msg = {"msg": "method", "method": "updateMessage", "id": "update-message", "params": [msg]} self.send(update_msg) def on_message(self, ws, message): decoded_msg = json.loads(message) debug_print("Incoming: " + decoded_msg.__repr__()) - if 'server_id' in decoded_msg: - self.server_id = decoded_msg['server_id'] + if "server_id" in decoded_msg: + self.server_id = decoded_msg["server_id"] - if 'msg' in decoded_msg: - msg = decoded_msg['msg'] - if msg == 'ping': - self.send({'msg': 'pong'}) + if "msg" in decoded_msg: + msg = decoded_msg["msg"] + if msg == "ping": + self.send({"msg": "pong"}) - if msg == 'connected': - self.session_id = decoded_msg['session'] + if msg == "connected": + self.session_id = decoded_msg["session"] debug_print(f"Got session: {self.session_id}") self.login() - if msg == 'result': - if decoded_msg['id'] == 'login': - self.id = decoded_msg['result']['id'] - self.token = decoded_msg['result']['token'] - self.token_expires = datetime.fromtimestamp(int(decoded_msg['result']['tokenExpires']['$date']) / 1000) + if msg == "result": + if decoded_msg["id"] == "login": + self.id = decoded_msg["result"]["id"] + self.token = decoded_msg["result"]["token"] + self.token_expires = datetime.fromtimestamp( + int(decoded_msg["result"]["tokenExpires"]["$date"]) / 1000 + ) debug_print(f"Loggedin: id: {self.id}, token: {self.token}, expires: {self.token_expires}") self.get_subscriptions() - if decoded_msg['id'] == 'subscriptions': - for subscription in decoded_msg['result']: - self.subscribe(subscription['name'], subscription['rid']) + if decoded_msg["id"] == "subscriptions": + for subscription in decoded_msg["result"]: + self.subscribe(subscription["name"], subscription["rid"]) - if msg == 'changed' and decoded_msg['collection'] == 'stream-room-messages': - for chat_msg in decoded_msg['fields']['args']: - if 'editedBy' in chat_msg and chat_msg['editedBy']['_id'] == self.id: + if msg == "changed" and decoded_msg["collection"] == "stream-room-messages": + for chat_msg in decoded_msg["fields"]["args"]: + if "editedBy" in chat_msg and chat_msg["editedBy"]["_id"] == self.id: continue - if re.search(RE_TAG_PROG, chat_msg['msg']) or re.search(RE_URL_PROG, chat_msg['msg']): + if re.search(RE_TAG_PROG, chat_msg["msg"]) or re.search(RE_URL_PROG, chat_msg["msg"]): debug_print("Sending message to be update") self.replace_issue_tags(chat_msg) @@ -343,18 +343,19 @@ class Bot: def on_close(self, ws): debug_print("Disconnected, reconnecting") ws.close() - #ws.run_forever() + # ws.run_forever() def on_open(self, ws): - connect_msg = { "msg": "connect", "version": "1", "support": ["1"] } + connect_msg = {"msg": "connect", "version": "1", "support": ["1"]} self.send(connect_msg) + if __name__ == "__main__": if DEBUG: websocket.enableTrace(True) while True: - try: + try: bot = Bot() bot.run() except Exception as e: diff --git a/misc/ci/sources.list b/misc/ci/sources.list new file mode 100644 index 0000000..4d8f94f --- /dev/null +++ b/misc/ci/sources.list @@ -0,0 +1,4 @@ +deb http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse +deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted universe multiverse +deb http://archive.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse +deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse diff --git a/misc/hooks/README.md b/misc/hooks/README.md new file mode 100644 index 0000000..3b50e6a --- /dev/null +++ b/misc/hooks/README.md @@ -0,0 +1,23 @@ +# Git hooks for Godot Engine + +This folder contains Git hooks meant to be installed locally by Godot Engine +contributors to make sure they comply with our requirements. + +## List of hooks + +- Pre-commit hook for `black`: Applies `black` to the staged Python files + before accepting a commit. + +## Installation + +Copy all the files from this folder into your `.git/hooks` folder, and make +sure the hooks and helper scripts are executable. + +#### Linux/MacOS + +The hooks rely on bash scripts and tools which should be in the system `PATH`, +so they should work out of the box on Linux/macOS. + +##### black +- Python installation: make sure Python is added to the `PATH` +- Install `black` - in any console: `pip3 install black` diff --git a/misc/hooks/canonicalize_filename.sh b/misc/hooks/canonicalize_filename.sh new file mode 100755 index 0000000..5eecabf --- /dev/null +++ b/misc/hooks/canonicalize_filename.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +# Provide the canonicalize filename (physical filename with out any symlinks) +# like the GNU version readlink with the -f option regardless of the version of +# readlink (GNU or BSD). + +# This file is part of a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/githubbrowser/Pre-commit-hooks +# Contact: David Martin, david.martin.mailbox@googlemail.com + +########################################################### +# There should be no need to change anything below this line. + +# Canonicalize by recursively following every symlink in every component of the +# specified filename. This should reproduce the results of the GNU version of +# readlink with the -f option. +# +# Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac +canonicalize_filename () { + local target_file="$1" + local physical_directory="" + local result="" + + # Need to restore the working directory after work. + local working_dir="`pwd`" + + cd -- "$(dirname -- "$target_file")" + target_file="$(basename -- "$target_file")" + + # Iterate down a (possible) chain of symlinks + while [ -L "$target_file" ] + do + target_file="$(readlink -- "$target_file")" + cd -- "$(dirname -- "$target_file")" + target_file="$(basename -- "$target_file")" + done + + # Compute the canonicalized name by finding the physical path + # for the directory we're in and appending the target file. + physical_directory="`pwd -P`" + result="$physical_directory/$target_file" + + # restore the working directory after work. + cd -- "$working_dir" + + echo "$result" +} diff --git a/misc/hooks/pre-commit b/misc/hooks/pre-commit new file mode 100755 index 0000000..bb96e51 --- /dev/null +++ b/misc/hooks/pre-commit @@ -0,0 +1,49 @@ +#!/bin/sh +# Git pre-commit hook that runs multiple hooks specified in $HOOKS. +# Make sure this script is executable. Bypass hooks with git commit --no-verify. + +# This file is part of a set of unofficial pre-commit hooks available +# at github. +# Link: https://github.com/githubbrowser/Pre-commit-hooks +# Contact: David Martin, david.martin.mailbox@googlemail.com + + +########################################################### +# CONFIGURATION: +# pre-commit hooks to be executed. They should be in the same .git/hooks/ folder +# as this script. Hooks should return 0 if successful and nonzero to cancel the +# commit. They are executed in the order in which they are listed. +HOOKS="pre-commit-black" +########################################################### +# There should be no need to change anything below this line. + +. "$(dirname -- "$0")/canonicalize_filename.sh" + +# exit on error +set -e + +# Absolute path to this script, e.g. /home/user/bin/foo.sh +SCRIPT="$(canonicalize_filename "$0")" + +# Absolute path this script is in, thus /home/user/bin +SCRIPTPATH="$(dirname -- "$SCRIPT")" + + +for hook in $HOOKS +do + echo "Running hook: $hook" + # run hook if it exists + # if it returns with nonzero exit with 1 and thus abort the commit + if [ -f "$SCRIPTPATH/$hook" ]; then + "$SCRIPTPATH/$hook" + if [ $? != 0 ]; then + exit 1 + fi + else + echo "Error: file $hook not found." + echo "Aborting commit. Make sure the hook is in $SCRIPTPATH and executable." + echo "You can disable it by removing it from the list in $SCRIPT." + echo "You can skip all pre-commit hooks with --no-verify (not recommended)." + exit 1 + fi +done diff --git a/misc/hooks/pre-commit-black b/misc/hooks/pre-commit-black new file mode 100755 index 0000000..76d9729 --- /dev/null +++ b/misc/hooks/pre-commit-black @@ -0,0 +1,202 @@ +#!/usr/bin/env bash + +# git pre-commit hook that runs a black stylecheck. +# Based on pre-commit-clang-format. + +################################################################## +# SETTINGS +# Set path to black binary. +BLACK=`which black 2>/dev/null` +BLACK_OPTIONS="-l 120" + +# Remove any older patches from previous commits. Set to true or false. +DELETE_OLD_PATCHES=false + +# File types to parse. +FILE_NAMES="SConstruct SCsub" +FILE_EXTS="py" + +# Use pygmentize instead of cat to parse diff with highlighting. +# Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac) +PYGMENTIZE=`which pygmentize 2>/dev/null` +if [ ! -z "$PYGMENTIZE" ]; then + READER="pygmentize -l diff" +else + READER=cat +fi + +# Path to zenity +ZENITY=`which zenity 2>/dev/null` + +# Path to xmessage +XMSG=`which xmessage 2>/dev/null` + +# Path to powershell (Windows only) +PWSH=`which powershell 2>/dev/null` + +################################################################## +# There should be no need to change anything below this line. + +. "$(dirname -- "$0")/canonicalize_filename.sh" + +# exit on error +set -e + +# check whether the given file matches any of the set extensions +matches_name_or_extension() { + local filename=$(basename "$1") + local extension=".${filename##*.}" + + for name in $FILE_NAMES; do [[ "$name" == "$filename" ]] && return 0; done + for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done + + return 1 +} + +# necessary check for initial commit +if git rev-parse --verify HEAD >/dev/null 2>&1 ; then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +if [ ! -x "$BLACK" ] ; then + if [ ! -t 1 ] ; then + if [ -x "$ZENITY" ] ; then + $ZENITY --error --title="Error" --text="Error: black executable not found." + exit 1 + elif [ -x "$XMSG" ] ; then + $XMSG -center -title "Error" "Error: black executable not found." + exit 1 + elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then + winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")" + $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "Error: black executable not found." + exit 1 + fi + fi + printf "Error: black executable not found.\n" + printf "Set the correct path in $(canonicalize_filename "$0").\n" + exit 1 +fi + +# create a random filename to store our generated patch +prefix="pre-commit-black" +suffix="$(date +%s)" +patch="/tmp/$prefix-$suffix.patch" + +# clean up any older black patches +$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch + +# create one patch containing all changes to the files +git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; +do + # ignore thirdparty files + if grep -q "thirdparty" <<< $file; then + continue; + fi + + # ignore file if not one of the names or extensions we handle + if ! matches_name_or_extension "$file"; then + continue; + fi + + # format our file with black, create a patch with diff and append it to our $patch + # The sed call is necessary to transform the patch from + # --- $file timestamp + # +++ $file timestamp + # to both lines working on the same file and having a/ and b/ prefix. + # Else it can not be applied with 'git apply'. + "$BLACK" "$BLACK_OPTIONS" --diff "$file" | \ + sed -e "1s|--- |--- a/|" -e "2s|+++ |+++ b/|" >> "$patch" +done + +# if no patch has been generated all is ok, clean up the file stub and exit +if [ ! -s "$patch" ] ; then + printf "Files in this commit comply with the black formatter rules.\n" + rm -f "$patch" + exit 0 +fi + +# a patch has been created, notify the user and exit +printf "\nThe following differences were found between the code to commit " +printf "and the black formatter rules:\n\n" + +if [ -t 1 ] ; then + $READER "$patch" + printf "\n" + # Allows us to read user input below, assigns stdin to keyboard + exec < /dev/tty + terminal="1" +else + cat "$patch" + printf "\n" + # Allows non zero zenity/powershell output + set +e + terminal="0" +fi + +while true; do + if [ $terminal = "0" ] ; then + if [ -x "$ZENITY" ] ; then + ans=$($ZENITY --text-info --filename="$patch" --width=800 --height=600 --title="Do you want to apply that patch?" --ok-label="Apply" --cancel-label="Do not apply" --extra-button="Apply and stage") + if [ "$?" = "0" ] ; then + yn="Y" + else + if [ "$ans" = "Apply and stage" ] ; then + yn="S" + else + yn="N" + fi + fi + elif [ -x "$XMSG" ] ; then + $XMSG -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?" + ans=$? + if [ "$ans" = "100" ] ; then + yn="Y" + elif [ "$ans" = "200" ] ; then + yn="S" + else + yn="N" + fi + elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then + winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")" + $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?" + ans=$? + if [ "$ans" = "100" ] ; then + yn="Y" + elif [ "$ans" = "200" ] ; then + yn="S" + else + yn="N" + fi + else + printf "Error: zenity, xmessage, or powershell executable not found.\n" + exit 1 + fi + else + read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn + fi + case $yn in + [Yy] ) git apply $patch; + printf "The patch was applied. You can now stage the changes and commit again.\n\n"; + break + ;; + [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n"; + printf "(may need to be called from the root directory of your repository)\n"; + printf "Aborting commit. Apply changes and commit again or skip checking with"; + printf " --no-verify (not recommended).\n\n"; + break + ;; + [Ss] ) git apply $patch; + git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; + do git add $file; + done + printf "The patch was applied and the changed files staged. You can now commit.\n\n"; + break + ;; + * ) echo "Please answer yes or no." + ;; + esac +done +exit 1 # we don't commit in any case diff --git a/misc/hooks/winmessage.ps1 b/misc/hooks/winmessage.ps1 new file mode 100644 index 0000000..3672579 --- /dev/null +++ b/misc/hooks/winmessage.ps1 @@ -0,0 +1,103 @@ +Param ( + [string]$file = "", + [string]$text = "", + [string]$buttons = "OK:0", + [string]$default = "", + [switch]$nearmouse = $false, + [switch]$center = $false, + [string]$geometry = "", + [int32]$timeout = 0, + [string]$title = "Message" +) +Add-Type -assembly System.Windows.Forms + +$global:Result = 0 + +$main_form = New-Object System.Windows.Forms.Form +$main_form.Text = $title + +$geometry_data = $geometry.Split("+") +if ($geometry_data.Length -ge 1) { + $size_data = $geometry_data[0].Split("x") + if ($size_data.Length -eq 2) { + $main_form.Width = $size_data[0] + $main_form.Height = $size_data[1] + } +} +if ($geometry_data.Length -eq 3) { + $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual + $main_form.Location = New-Object System.Drawing.Point($geometry_data[1], $geometry_data[2]) +} +if ($nearmouse) { + $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::Manual + $main_form.Location = System.Windows.Forms.Cursor.Position +} +if ($center) { + $main_form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen +} + +$main_form.SuspendLayout() + +$button_panel = New-Object System.Windows.Forms.FlowLayoutPanel +$button_panel.SuspendLayout() +$button_panel.FlowDirection = [System.Windows.Forms.FlowDirection]::RightToLeft +$button_panel.Dock = [System.Windows.Forms.DockStyle]::Bottom +$button_panel.Autosize = $true + +if ($file -ne "") { + $text = [IO.File]::ReadAllText($file).replace("`n", "`r`n") +} + +if ($text -ne "") { + $text_box = New-Object System.Windows.Forms.TextBox + $text_box.Multiline = $true + $text_box.ReadOnly = $true + $text_box.Autosize = $true + $text_box.Text = $text + $text_box.Select(0,0) + $text_box.Dock = [System.Windows.Forms.DockStyle]::Fill + $main_form.Controls.Add($text_box) +} + +$buttons_array = $buttons.Split(",") +foreach ($button in $buttons_array) { + $button_data = $button.Split(":") + $button_ctl = New-Object System.Windows.Forms.Button + if ($button_data.Length -eq 2) { + $button_ctl.Tag = $button_data[1] + } else { + $button_ctl.Tag = 100 + $buttons_array.IndexOf($button) + } + if ($default -eq $button_data[0]) { + $main_form.AcceptButton = $button_ctl + } + $button_ctl.Autosize = $true + $button_ctl.Text = $button_data[0] + $button_ctl.Add_Click( + { + Param($sender) + $global:Result = $sender.Tag + $main_form.Close() + } + ) + $button_panel.Controls.Add($button_ctl) +} +$main_form.Controls.Add($button_panel) + +$button_panel.ResumeLayout($false) +$main_form.ResumeLayout($false) + +if ($timeout -gt 0) { + $timer = New-Object System.Windows.Forms.Timer + $timer.Add_Tick( + { + $global:Result = 0 + $main_form.Close() + } + ) + $timer.Interval = $timeout + $timer.Start() +} +$dlg_res = $main_form.ShowDialog() + +[Environment]::Exit($global:Result) diff --git a/misc/scripts/black_format.sh b/misc/scripts/black_format.sh new file mode 100755 index 0000000..f93e8cb --- /dev/null +++ b/misc/scripts/black_format.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash + +# This script runs black on all Python files in the repo. + +set -uo pipefail + +# Apply black. +echo -e "Formatting Python files..." +PY_FILES=$(find \( -path "./.git" \ + -o -path "./thirdparty" \ + \) -prune \ + -o \( -name "SConstruct" \ + -o -name "SCsub" \ + -o -name "*.py" \ + \) -print) +black -l 120 $PY_FILES + +git diff > patch.patch + +# If no patch has been generated all is OK, clean up, and exit. +if [ ! -s patch.patch ] ; then + printf "Files in this commit comply with the black style rules.\n" + rm -f patch.patch + exit 0 +fi + +# A patch has been created, notify the user, clean up, and exit. +printf "\n*** The following differences were found between the code " +printf "and the formatting rules:\n\n" +cat patch.patch +printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i '\n" +rm -f patch.patch +exit 1 diff --git a/misc/scripts/file_format.sh b/misc/scripts/file_format.sh new file mode 100755 index 0000000..9437b46 --- /dev/null +++ b/misc/scripts/file_format.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# This script ensures proper POSIX text file formatting and a few other things. +# This is supplementary to black_format.sh, but should be run before it. + +# We need dos2unix and recode. +if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v recode)" ]; then + printf "Install 'dos2unix' and 'recode' to use this script.\n" +fi + +set -uo pipefail +IFS=$'\n\t' + +# Loops through all text files tracked by Git. +git grep -zIl '' | +while IFS= read -rd '' f; do + # Ensure that files are UTF-8 formatted. + recode UTF-8 "$f" 2> /dev/null + # Ensure that files have LF line endings and do not contain a BOM. + dos2unix "$f" 2> /dev/null + # Remove trailing space characters and ensures that files end + # with newline characters. -l option handles newlines conveniently. + perl -i -ple 's/\s*$//g' "$f" +done + +git diff > patch.patch + +# If no patch has been generated all is OK, clean up, and exit. +if [ ! -s patch.patch ] ; then + printf "Files in this commit comply with the formatting rules.\n" + rm -f patch.patch + exit 0 +fi + +# A patch has been created, notify the user, clean up, and exit. +printf "\n*** The following differences were found between the code " +printf "and the formatting rules:\n\n" +cat patch.patch +printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i '\n" +rm -f patch.patch +exit 1 diff --git a/test-regex.py b/test-regex.py index 848443e..c935ae2 100644 --- a/test-regex.py +++ b/test-regex.py @@ -2,48 +2,55 @@ import re -def makeurl(issue, repo = ''): - if not repo: - repo = 'godot' - return f'https://github.com/godotengine/{repo}/issues/{issue}' +def makeurl(issue, repo=""): + if not repo: + repo = "godot" + + return f"https://github.com/godotengine/{repo}/issues/{issue}" + tests = [ - { 'text': '#100', 'results' : [ makeurl(100) ] }, - { 'text': 'godot#100', 'results' : [ makeurl(100) ] }, - { 'text': 'issue-bot#100', 'results' : [ makeurl(100, 'issue-bot') ] }, - { 'text': '#100,text', 'results' : [ makeurl(100) ] }, - { 'text': '#100,#101,#102', 'results' : [ makeurl(100), makeurl(101), makeurl(102) ] }, - { 'text': 'text #100,#101,#102 text', 'results' : [ makeurl(100), makeurl(101), makeurl(102) ] }, - { 'text': 'text issue-bot#100,godot#101,collada-exporter#102 text', 'results' : [ makeurl(100, 'issue-bot'), makeurl(101), makeurl(102, 'collada-exporter') ] }, - { 'text': 'text #100', 'results' : [ makeurl(100) ] }, - { 'text': 'text #100 text', 'results' : [ makeurl(100) ] }, - { 'text': 'text #100 #101 text', 'results' : [ makeurl(100), makeurl(101) ] }, - { 'text': 'godot#100', 'results' : [ makeurl(100) ] }, - { 'text': 'text godot#100 text', 'results' : [ makeurl(100) ] }, - { 'text': 'godot#100 text', 'results' : [ makeurl(100) ] }, - { 'text': 'repo.name#100 text', 'results' : [ makeurl(100, 'repo.name') ] }, - { 'text': '(#100) text', 'results' : [ makeurl(100) ] }, - { 'text': '(repo#100) text', 'results' : [ makeurl(100, 'repo') ] }, - - { 'text': 'https://github.com/godotengine/issue-bot/issues/2', 'results': [ makeurl(2, 'issue-bot') ] }, - { 'text': 'https://github.com/godotengine/godot/pull/100', 'results': [ makeurl(100) ] }, - { 'text': 'http://github.com/godotengine/godot/pull/100', 'results': [ makeurl(100) ] }, - { 'text': 'github.com/godotengine/godot/pull/100', 'results': [ makeurl(100) ] }, - { 'text': 'https://github.com/godotengine/godot/pull/100#issuecomment-1', 'results': [ makeurl(100) ] }, - - { 'text': 'https://github.com/godotengine/godot-cpp/pull/373/checks?check_run_id=1802261888', 'results': [ makeurl(373, 'godot-cpp') ] }, - - { 'text': 'a long line of text with an url https://github.com/godotengine/godot/issues/100 and some tags #102 repo#103', 'results': [ makeurl(102), makeurl(103, 'repo'), makeurl(100) ] }, - - { 'text': 'just a bunch of text', 'results' : [ ] }, - { 'text': 'Bunch of ## nonsense ##sdf $$', 'results' : [ ] }, + {"text": "#100", "results": [makeurl(100)]}, + {"text": "godot#100", "results": [makeurl(100)]}, + {"text": "issue-bot#100", "results": [makeurl(100, "issue-bot")]}, + {"text": "#100,text", "results": [makeurl(100)]}, + {"text": "#100,#101,#102", "results": [makeurl(100), makeurl(101), makeurl(102)]}, + {"text": "text #100,#101,#102 text", "results": [makeurl(100), makeurl(101), makeurl(102)]}, + { + "text": "text issue-bot#100,godot#101,collada-exporter#102 text", + "results": [makeurl(100, "issue-bot"), makeurl(101), makeurl(102, "collada-exporter")], + }, + {"text": "text #100", "results": [makeurl(100)]}, + {"text": "text #100 text", "results": [makeurl(100)]}, + {"text": "text #100 #101 text", "results": [makeurl(100), makeurl(101)]}, + {"text": "godot#100", "results": [makeurl(100)]}, + {"text": "text godot#100 text", "results": [makeurl(100)]}, + {"text": "godot#100 text", "results": [makeurl(100)]}, + {"text": "repo.name#100 text", "results": [makeurl(100, "repo.name")]}, + {"text": "(#100) text", "results": [makeurl(100)]}, + {"text": "(repo#100) text", "results": [makeurl(100, "repo")]}, + {"text": "https://github.com/godotengine/issue-bot/issues/2", "results": [makeurl(2, "issue-bot")]}, + {"text": "https://github.com/godotengine/godot/pull/100", "results": [makeurl(100)]}, + {"text": "http://github.com/godotengine/godot/pull/100", "results": [makeurl(100)]}, + {"text": "github.com/godotengine/godot/pull/100", "results": [makeurl(100)]}, + {"text": "https://github.com/godotengine/godot/pull/100#issuecomment-1", "results": [makeurl(100)]}, + { + "text": "https://github.com/godotengine/godot-cpp/pull/373/checks?check_run_id=1802261888", + "results": [makeurl(373, "godot-cpp")], + }, + { + "text": "a long line of text with an url https://github.com/godotengine/godot/issues/100 and some tags #102 repo#103", + "results": [makeurl(102), makeurl(103, "repo"), makeurl(100)], + }, + {"text": "just a bunch of text", "results": []}, + {"text": "Bunch of ## nonsense ##sdf $$", "results": []}, ] -tag_prog = re.compile('([A-Za-z0-9_.-]+)?#(\d+)') -url_prog = re.compile('(https?://)?github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pull)/(\d+)(\S*)') +tag_prog = re.compile("([A-Za-z0-9_.-]+)?#(\d+)") +url_prog = re.compile("(https?://)?github.com/([A-Za-z0-9_.-]+)/([A-Za-z0-9_.-]+)/(issues|pull)/(\d+)(\S*)") for test in tests: - text = test['text'] + text = test["text"] result = [] for match in re.finditer(tag_prog, text): @@ -52,6 +59,5 @@ for test in tests: for match in re.finditer(url_prog, text): result.append(makeurl(match.group(5), match.group(3))) - if test['results'] != result: + if test["results"] != result: print(f'FAILED for {text}: expected {test["results"]} got: {result}') -