From af50c6211bffc1e349d8e37ef06c8cb1d54b5464 Mon Sep 17 00:00:00 2001 From: Christian Steinhaus Date: Thu, 8 Aug 2019 10:05:00 +0200 Subject: [PATCH 1/4] Added json test request and how to testing --- README.md | 12 ++++++++- testrequest.json | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 testrequest.json diff --git a/README.md b/README.md index 5954c2a..41bc161 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,14 @@ Takes notifications via webhook, checks a secret and notifies a [Matrix](https://matrix.org) channel. Listens to HTTP only. Should be used -behind a reverse-proxy with HTTPS. \ No newline at end of file +behind a reverse-proxy with HTTPS. + +# Testing the Hook locally +- Start the webserver locally by `env FLASK_APP=wmn.py flask run` + - Or have your IDE do it for you +- Send a POST request using curl `curl -i -X POST "localhost:5000/matrix?channel=%21yhEUnvhAZZFKRStdXb%3Amatrix.org" -H "X-Gitlab-Event: Push Hook" -H "X-Gitlab-Token: ..." -H "Content-Type: application/json" --data-binary @./testrequest.json` + - The part after `channel=` is the room ID which can retrieved from Matrix channels you are part of + - `%21` escapes ! in HTML + - `%3A` escapes : in HTML + - The `X-Gitlab-Token` must correspond to the one provided in `config.yaml` + diff --git a/testrequest.json b/testrequest.json new file mode 100644 index 0000000..6e69905 --- /dev/null +++ b/testrequest.json @@ -0,0 +1,68 @@ +{ + "object_kind": "push", + "before": "95790bf891e76fee5e1747ab589903a6a1f80f22", + "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "ref": "refs/heads/master", + "checkout_sha": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "user_id": 4, + "user_name": "John Smith", + "user_username": "jsmith", + "user_email": "john@example.com", + "user_avatar": "https://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=8://s.gravatar.com/avatar/d4c74594d841139328695756648b6bd6?s=80", + "project_id": 15, + "project":{ + "id": 15, + "name":"Diaspora", + "description":"", + "web_url":"http://example.com/mike/diaspora", + "avatar_url":null, + "git_ssh_url":"git@example.com:mike/diaspora.git", + "git_http_url":"http://example.com/mike/diaspora.git", + "namespace":"Mike", + "visibility_level":0, + "path_with_namespace":"mike/diaspora", + "default_branch":"master", + "homepage":"http://example.com/mike/diaspora", + "url":"git@example.com:mike/diaspora.git", + "ssh_url":"git@example.com:mike/diaspora.git", + "http_url":"http://example.com/mike/diaspora.git" + }, + "repository":{ + "name": "Diaspora", + "url": "git@example.com:mike/diaspora.git", + "description": "", + "homepage": "http://example.com/mike/diaspora", + "git_http_url":"http://example.com/mike/diaspora.git", + "git_ssh_url":"git@example.com:mike/diaspora.git", + "visibility_level":0 + }, + "commits": [ + { + "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", + "message": "Update Catalan translation to e38cb41.", + "timestamp": "2011-12-12T14:27:31+02:00", + "url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", + "author": { + "name": "Jordi Mallach", + "email": "jordi@softcatala.org" + }, + "added": ["CHANGELOG"], + "modified": ["app/controller/application.rb"], + "removed": [] + }, + { + "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "message": "fixed readme", + "timestamp": "2012-01-03T23:36:29+02:00", + "url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "author": { + "name": "GitLab dev user", + "email": "gitlabdev@dv6700.(none)" + }, + "added": ["CHANGELOG"], + "modified": ["app/controller/application.rb"], + "removed": [] + } + ], + "total_commits_count": 4 +} From fb560a5bea9cd90c0fd371c48f1ab33b0b50e0e4 Mon Sep 17 00:00:00 2001 From: Christian Steinhaus Date: Thu, 8 Aug 2019 10:05:58 +0200 Subject: [PATCH 2/4] Commit messages are now links Each commit message links to its commit page in the Webinterface --- wmn.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/wmn.py b/wmn.py index fdb467f..0518792 100644 --- a/wmn.py +++ b/wmn.py @@ -87,13 +87,15 @@ def process_gitlab_request(): return sorted(commits, key=lambda commit: commit["timestamp"]) def extract_commit_message(commit): - return shorten(next(iter_first_line(commit["message"]), "$EMPTY_COMMIT_MESSAGE - impossibruh")) + msg = shorten(next(iter_first_line(commit["message"]), "$EMPTY_COMMIT_MESSAGE - impossibruh")) + url = commit["url"] + return msg, url username = request.json["user_name"] commit_messages = list(map(extract_commit_message, sort_commits_by_time(request.json["commits"]))) project_name = request.json["project"]["name"] - html_commits = "\n".join((f"
  • {msg}
  • " for msg in commit_messages)) - text_commits = "\n".join((f"- {msg}" for msg in commit_messages)) + html_commits = "\n".join((f'
  • {msg}
  • ' for (msg, url) in commit_messages)) + text_commits = "\n".join((f"- [{msg}]({url})" for (msg, url) in commit_messages)) try: room.send_html(f"{username} pushed {len(commit_messages)} commits to {project_name}
    \n" f"\n", From 9a0f0cbfba4c86796a9b3305d0d8e5c259beaeb9 Mon Sep 17 00:00:00 2001 From: Christian Steinhaus Date: Fri, 9 Aug 2019 13:29:42 +0200 Subject: [PATCH 3/4] Updated README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 41bc161..7db8943 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ behind a reverse-proxy with HTTPS. - Or have your IDE do it for you - Send a POST request using curl `curl -i -X POST "localhost:5000/matrix?channel=%21yhEUnvhAZZFKRStdXb%3Amatrix.org" -H "X-Gitlab-Event: Push Hook" -H "X-Gitlab-Token: ..." -H "Content-Type: application/json" --data-binary @./testrequest.json` - The part after `channel=` is the room ID which can retrieved from Matrix channels you are part of - - `%21` escapes ! in HTML - - `%3A` escapes : in HTML + - `%21` escapes ! in URI + - `%3A` escapes : in URI - The `X-Gitlab-Token` must correspond to the one provided in `config.yaml` From 3f7cc00962823b4e80d77672e4eb54c9e7958354 Mon Sep 17 00:00:00 2001 From: Christian Steinhaus Date: Fri, 9 Aug 2019 13:30:22 +0200 Subject: [PATCH 4/4] Changed function name `extract_commit_message` is now called `extract_commit_info` to better represent its functionality --- wmn.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wmn.py b/wmn.py index 0518792..1ad3aaf 100644 --- a/wmn.py +++ b/wmn.py @@ -86,7 +86,7 @@ def process_gitlab_request(): def sort_commits_by_time(commits): return sorted(commits, key=lambda commit: commit["timestamp"]) - def extract_commit_message(commit): + def extract_commit_info(commit): msg = shorten(next(iter_first_line(commit["message"]), "$EMPTY_COMMIT_MESSAGE - impossibruh")) url = commit["url"] return msg, url