2020-09-24 19:51:27 +02:00
|
|
|
from flask import Flask, request
|
2022-04-13 22:04:51 +02:00
|
|
|
from gb_secrets import credentials
|
2022-04-13 21:49:00 +02:00
|
|
|
import logging, requests, os
|
2021-03-22 22:38:08 +01:00
|
|
|
import re
|
2020-09-24 19:51:27 +02:00
|
|
|
|
2022-04-13 21:49:00 +02:00
|
|
|
LOGLEVEL = os.environ.get('LOGLEVEL', 'INFO').upper()
|
|
|
|
logging.basicConfig(level=LOGLEVEL)
|
|
|
|
|
2020-09-24 19:51:27 +02:00
|
|
|
app = Flask(__name__)
|
2022-04-13 22:04:51 +02:00
|
|
|
session = requests.Session()
|
|
|
|
session.post("https://www.planet-casio.com/Fr/compte",
|
|
|
|
data=credentials)
|
2020-09-24 19:51:27 +02:00
|
|
|
|
2020-10-13 22:25:27 +02:00
|
|
|
def make_url_user(user):
|
|
|
|
return f"[url=https://gitea.planet-casio.com/{user['username']}]{user['username']}[/url]"
|
|
|
|
def make_url_repository(repository):
|
|
|
|
fork = " (fork)" if repository["fork"] else ""
|
|
|
|
return f"[url={repository['html_url']}]{repository['name']}{fork}[/url]"
|
|
|
|
|
2020-09-24 19:51:27 +02:00
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
|
|
def main():
|
2022-04-13 22:04:51 +02:00
|
|
|
global session
|
2020-09-24 19:51:27 +02:00
|
|
|
data = request.json
|
|
|
|
event = request.headers['X-Gitea-Event']
|
|
|
|
msg = None
|
|
|
|
|
2023-10-09 17:21:01 +02:00
|
|
|
if data["repository"]["private"] or data["repository"]["mirror"]:
|
2020-10-13 23:46:28 +02:00
|
|
|
return "IGN"
|
2020-10-13 22:25:27 +02:00
|
|
|
|
2021-03-14 09:38:15 +01:00
|
|
|
url_repository = make_url_repository(data["repository"])
|
2021-01-11 21:24:01 +01:00
|
|
|
|
2020-10-13 22:25:27 +02:00
|
|
|
if event == "push" and data["commits"] != []:
|
2021-03-22 22:38:08 +01:00
|
|
|
# Heuristic to handle pull request merges by detecting default merge message
|
|
|
|
RE_MERGE = r"^Merge pull request '(.+)' \(#([0-9]+)\)"#from .* into"
|
|
|
|
m = re.match(RE_MERGE, data["commits"][0]["message"])
|
|
|
|
if len(data["commits"]) > 1 and m:
|
|
|
|
pr_url = data["repository"]["html_url"] + "/pulls/" + m[2]
|
|
|
|
pr_url = f"[url={pr_url}]{m[1]}[/url]"
|
|
|
|
data["commits"] = data["commits"][1:]
|
|
|
|
else:
|
|
|
|
pr_url = None
|
|
|
|
|
2020-10-13 22:25:27 +02:00
|
|
|
commits = data["commits"]
|
2024-03-18 22:56:54 +01:00
|
|
|
total_commits = data.get("total_commits", len(commits))
|
|
|
|
commit_count = f"{total_commits} commit{'s' if total_commits > 1 else ''}"
|
2020-10-13 22:25:27 +02:00
|
|
|
message = "[inlinecode]" + commits[0]["message"].split('\n',1)[0] + "[/inlinecode]"
|
2020-10-21 17:42:30 +02:00
|
|
|
|
2024-03-18 22:56:54 +01:00
|
|
|
if total_commits <= 1:
|
2020-10-21 17:42:30 +02:00
|
|
|
others = ""
|
2024-03-18 22:56:54 +01:00
|
|
|
elif total_commits == 2:
|
2020-10-21 17:42:30 +02:00
|
|
|
others = " (et 1 autre commit)"
|
|
|
|
else:
|
2024-03-18 22:56:54 +01:00
|
|
|
others = f" (et {total_commits-1} autres commits)"
|
2020-10-13 22:25:27 +02:00
|
|
|
|
|
|
|
url_user = make_url_user(data['pusher'])
|
|
|
|
|
2021-03-22 22:38:08 +01:00
|
|
|
# Pull request merge
|
|
|
|
if pr_url:
|
|
|
|
msg = f"{url_user} a fusionné la PR {pr_url}: {message}{others}"
|
2021-01-11 21:24:01 +01:00
|
|
|
# Ref doesn't have a previous commit = new branch
|
2021-03-22 22:38:08 +01:00
|
|
|
elif all(c == "0" for c in data["before"]) and data["ref"].startswith("refs/heads/"):
|
2021-01-11 21:24:01 +01:00
|
|
|
branch = data["ref"][11:]
|
|
|
|
url_branch = data["repository"]["html_url"] + f"/src/branch/{branch}"
|
|
|
|
url_branch = f"[url={url_branch}]{branch}[/url]"
|
2021-03-11 18:22:01 +01:00
|
|
|
msg = f"{url_user} a créé une nouvelle branche {url_branch} dans {url_repository} : {message}{others}"
|
2021-01-11 21:24:01 +01:00
|
|
|
# Pre-existing branch
|
|
|
|
else:
|
2021-12-31 11:20:08 +01:00
|
|
|
if data["ref"].startswith("refs/heads/"):
|
|
|
|
branch = " (branche " + data["ref"][11:] + ")"
|
|
|
|
else:
|
|
|
|
branch = ""
|
2021-01-11 21:24:01 +01:00
|
|
|
url_commits = f"[url={data['compare_url']}]{commit_count}[/url]"
|
2021-12-31 11:20:08 +01:00
|
|
|
msg = f"{url_user} a poussé {url_commits} dans {url_repository}{branch} : {message}{others}"
|
2020-10-13 22:25:27 +02:00
|
|
|
|
|
|
|
if event == "push" and data["ref"].startswith("refs/tags/"):
|
|
|
|
url_user = make_url_user(data['pusher'])
|
|
|
|
tag_name = data["ref"][10:]
|
|
|
|
url_tag = data["repository"]["html_url"] + f"/src/tag/{tag_name}"
|
|
|
|
url_tag = f"[url={url_tag}]{tag_name}[/url]"
|
|
|
|
|
|
|
|
if all(c == '0' for c in data["before"]):
|
|
|
|
msg = f"{url_user} a créé le tag {url_tag} dans {url_repository}"
|
|
|
|
else:
|
|
|
|
msg = f"{url_user} a mis à jour le tag {url_tag} dans {url_repository}"
|
2020-09-24 19:51:27 +02:00
|
|
|
|
|
|
|
if event == "issues":
|
2020-10-13 22:25:27 +02:00
|
|
|
url_user = make_url_user(data['sender'])
|
2020-09-24 19:51:27 +02:00
|
|
|
url_issue = f"[url={data['issue']['html_url']}]{data['issue']['title']}[/url]"
|
|
|
|
|
|
|
|
if data['action'] == "closed":
|
|
|
|
msg = f"{url_user} a fermé le ticket {url_issue} dans {url_repository}"
|
2020-10-21 17:42:30 +02:00
|
|
|
elif data['action'] in ["created", "opened"]:
|
2020-09-24 19:51:27 +02:00
|
|
|
msg = f"{url_user} a créé un ticket {url_issue} dans {url_repository}"
|
2021-01-19 09:35:18 +01:00
|
|
|
elif data['action'] == "edited":
|
|
|
|
msg = f"{url_user} a édité le ticket {url_issue} dans {url_repository}"
|
2020-09-24 19:51:27 +02:00
|
|
|
else:
|
2021-04-28 18:40:59 +02:00
|
|
|
return "IGN"
|
2020-09-24 19:51:27 +02:00
|
|
|
|
|
|
|
if event == "issue_comment":
|
2020-10-13 22:25:27 +02:00
|
|
|
url_user = make_url_user(data['sender'])
|
2020-09-24 19:51:27 +02:00
|
|
|
url_issue = f"[url={data['issue']['html_url']}]{data['issue']['title']}[/url]"
|
|
|
|
|
|
|
|
if data['action'] == "created":
|
|
|
|
msg = f"{url_user} a répondu au ticket {url_issue} dans {url_repository}"
|
2021-03-23 14:09:18 +01:00
|
|
|
else:
|
|
|
|
return "IGN"
|
2020-09-24 19:51:27 +02:00
|
|
|
|
2021-03-14 09:48:15 +01:00
|
|
|
if event == "pull_request" and data["action"] == "opened":
|
|
|
|
url_user = make_url_user(data["sender"])
|
|
|
|
url_pr = f"[url={data['pull_request']['url']}]{data['pull_request']['title']}[/url]"
|
|
|
|
|
|
|
|
msg = f"{url_user} a créé une PR: {url_pr} dans {url_repository}"
|
|
|
|
|
2021-03-14 21:40:50 +01:00
|
|
|
if event == "repository" and data["action"] == "created":
|
2021-03-12 16:29:18 +01:00
|
|
|
url_user = make_url_user(data['sender'])
|
|
|
|
msg = f"{url_user} a créé un nouveau dépôt {url_repository}"
|
|
|
|
|
2023-03-25 23:45:28 +01:00
|
|
|
if event == "repository" and data["action"] == "deleted":
|
|
|
|
return "IGN"
|
|
|
|
|
2021-03-22 18:55:31 +01:00
|
|
|
if event == "push" and all(c == '0' for c in data["after"]):
|
|
|
|
return "IGN"
|
|
|
|
|
2022-05-16 21:33:22 +02:00
|
|
|
if event == "push" and not data["ref"].startswith("refs/tags/") \
|
|
|
|
and all(c == '0' for c in data["before"]) and data["commits"] == []:
|
2022-03-11 17:11:12 +01:00
|
|
|
return "IGN"
|
|
|
|
|
2021-05-02 08:55:13 +02:00
|
|
|
if event == "pull_request" and data["action"] in ["assigned", "closed", "synchronized", "edited"]:
|
2021-03-14 21:40:50 +01:00
|
|
|
return "IGN"
|
|
|
|
|
2022-12-17 23:58:13 +01:00
|
|
|
if event == "pull_request_approved" and data["action"] == "reviewed":
|
2022-12-17 23:49:26 +01:00
|
|
|
url_user = make_url_user(data['sender'])
|
|
|
|
url_pr = f"[url={data['pull_request']['url']}]{data['pull_request']['title']}[/url]"
|
|
|
|
|
2022-12-18 00:03:10 +01:00
|
|
|
msg = f"{url_user} a approuvé la PR {url_pr} dans {url_repository}"
|
|
|
|
|
|
|
|
if event == "pull_request_rejected" and data["action"] == "reviewed":
|
|
|
|
url_user = make_url_user(data['sender'])
|
|
|
|
url_pr = f"[url={data['pull_request']['url']}]{data['pull_request']['title']}[/url]"
|
|
|
|
|
|
|
|
msg = f"{url_user} a demandé des modifications sur la PR {url_pr} dans {url_repository}"
|
2022-12-17 23:49:26 +01:00
|
|
|
|
2021-03-14 09:38:15 +01:00
|
|
|
if event == "fork":
|
|
|
|
return "IGN"
|
|
|
|
|
2021-03-14 21:40:50 +01:00
|
|
|
if event == "create" and data["ref_type"] in ["tag", "branch"]:
|
2021-03-11 18:22:01 +01:00
|
|
|
return "IGN"
|
|
|
|
|
2021-03-22 18:55:31 +01:00
|
|
|
if event == "delete":
|
|
|
|
return "IGN"
|
|
|
|
|
|
|
|
if event == "release" and data["action"] == "updated":
|
|
|
|
return "IGN"
|
|
|
|
|
2021-03-06 13:47:07 +01:00
|
|
|
if not msg:
|
|
|
|
msg = f"Événement {event}"
|
|
|
|
if "repository" in data:
|
|
|
|
msg += " sur " + make_url_repository(data["repository"])
|
|
|
|
msg += "."
|
|
|
|
|
2020-09-24 19:51:27 +02:00
|
|
|
if msg:
|
|
|
|
msg = f"[gray][i]{msg}[/i][/gray]"
|
2022-04-13 10:21:30 +02:00
|
|
|
app.logger.info(msg)
|
2022-04-13 22:04:51 +02:00
|
|
|
r = session.post("https://www.planet-casio.com/Fr/shoutbox/api/post",
|
2023-09-20 23:55:50 +02:00
|
|
|
data={"message": msg, "channel": "projets"})
|
2022-04-13 10:21:30 +02:00
|
|
|
|
|
|
|
if r.status_code == 200:
|
|
|
|
return "OK"
|
|
|
|
else:
|
|
|
|
app.logger.warn(f"Error: {r.status_code}")
|
|
|
|
return f"ERR {r.status_code}"
|
2020-09-24 19:51:27 +02:00
|
|
|
|
2020-10-13 23:46:28 +02:00
|
|
|
return "ACK"
|