Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
gitlab
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
maubot
gitlab
Commits
f0235daf
Unverified
Commit
f0235daf
authored
5 years ago
by
Lorenz Steinert
Browse files
Options
Downloads
Patches
Plain Diff
make push hook work
parent
86205609
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
base-config.yaml
+1
-0
1 addition, 0 deletions
base-config.yaml
gitlab/__init__.py
+125
-13
125 additions, 13 deletions
gitlab/__init__.py
maubot.yaml
+1
-1
1 addition, 1 deletion
maubot.yaml
with
127 additions
and
14 deletions
base-config.yaml
+
1
−
0
View file @
f0235daf
...
...
@@ -2,3 +2,4 @@ path: "/webhooks"
port
:
29313
secret
:
"
passwd"
base_command
:
"
gitlab"
send_as_notice
:
true
This diff is collapsed.
Click to expand it.
gitlab/__init__.py
+
125
−
13
View file @
f0235daf
import
asyncio
from
typing
import
List
,
Type
from
typing
import
List
,
Type
,
Awaitable
from
aiohttp
import
web
from
maubot.handlers
import
event
from
mautrix.types
import
(
EventType
,
EventID
,
MessageType
,
TextMessageEventContent
,
Format
)
from
mautrix.util.config
import
BaseProxyConfig
,
ConfigUpdateHelper
from
maubot
import
Plugin
from
maubot
import
Plugin
,
MessageEvent
from
maubot.matrix
import
parse_markdown
def
handlePushEvent
(
body
)
->
str
:
branch
=
body
[
'
ref
'
].
replace
(
'
refs/heads/
'
,
''
)
if
int
(
body
[
'
total_commits_count
'
])
==
0
:
msg
=
"
\[{2!s}/{3!s}\] {4!s} force pushed to
"
\
"
or deleted branch [{1!s}]({0!s}/tree/{1!s})
"
return
msg
.
format
(
body
[
'
project
'
][
'
web_url
'
],
branch
,
body
[
'
project
'
][
'
namespace
'
],
body
[
'
project
'
][
'
name
'
],
body
[
'
user_username
'
]
)
pluralizer
:
str
=
''
if
int
(
body
[
'
total_commits_count
'
])
!=
1
:
pluralizer
=
'
s
'
msg
=
"
\[[{2!s}/{3!s}]({0!s}/tree/{1!s})\]
"
\
"
{4:d} new commit{6!s} by {5!s}
\n\n
"
msg
=
msg
.
format
(
body
[
'
project
'
][
'
web_url
'
],
branch
,
body
[
'
project
'
][
'
namespace
'
],
body
[
'
project
'
][
'
name
'
],
body
[
'
total_commits_count
'
],
body
[
'
user_username
'
],
pluralizer
)
for
commit
in
reversed
(
body
[
'
commits
'
]):
lines
=
commit
[
'
message
'
].
split
(
'
\n
'
)
if
len
(
lines
)
>
1
and
len
(
''
.
join
(
lines
[
1
:]))
>
0
:
lines
[
0
]
+=
"
(...)
"
msg
+=
"
+ {0!s} ({1!s})
\n
"
.
format
(
lines
[
0
],
commit
[
'
id
'
][:
8
])
return
msg
def
handleTagEvent
(
body
):
pass
def
handleIssueEvent
(
body
):
pass
def
handleNoteEvent
(
body
):
pass
def
handleMergeRequestEvent
(
body
):
pass
def
handleWikiPageEvent
(
body
):
pass
def
handlePipelineEvent
(
body
):
pass
EventParse
=
{
'
Push Hook
'
:
handlePushEvent
,
'
Tag Push Hook
'
:
handleTagEvent
,
'
Issue Hook
'
:
handleIssueEvent
,
'
Note Hook
'
:
handleNoteEvent
,
'
Merge Request Hook
'
:
handleMergeRequestEvent
,
'
Wiki Page Hook
'
:
handleWikiPageEvent
,
'
Pipeline Hook
'
:
handlePipelineEvent
}
class
Config
(
BaseProxyConfig
):
...
...
@@ -15,19 +92,47 @@ class Config(BaseProxyConfig):
helper
.
copy
(
"
port
"
)
helper
.
copy
(
"
secret
"
)
helper
.
copy
(
"
base_command
"
)
helper
.
copy
(
"
send_as_notice
"
)
class
Gitlab
(
Plugin
):
routes
=
web
.
RouteTableDef
()
async
def
process_hook
(
self
,
request
:
web
.
Request
)
->
None
:
if
not
request
.
has_body
():
await
self
.
client
.
send_text
(
request
.
query
[
'
room
'
],
"
Webhook doesn
'
t have a Body.
"
)
self
.
log
.
debug
(
str
(
request
))
self
.
log
.
debug
(
str
(
request
.
query
[
'
room
'
]))
await
self
.
client
.
send_text
(
request
.
query
[
'
room
'
],
str
(
request
))
def
send_gitlab_event
(
self
,
room
:
str
,
msg
:
str
)
->
Awaitable
[
EventID
]:
if
self
.
config
[
'
send_as_notice
'
]:
msgtype
=
MessageType
.
NOTICE
else
:
msgtype
=
MessageType
.
TEXT
content
=
TextMessageEventContent
(
msgtype
=
msgtype
,
body
=
msg
)
content
.
format
=
Format
.
HTML
content
.
body
,
content
.
formatted_body
=
parse_markdown
(
content
.
body
,
allow_html
=
True
)
return
self
.
client
.
send_message_event
(
room
,
EventType
.
ROOM_MESSAGE
,
content
)
async
def
process_hook
(
self
,
req
:
web
.
Request
)
->
None
:
if
not
req
.
has_body
:
self
.
log
.
debug
(
'
no body
'
)
return
body
=
await
req
.
json
()
if
'
X-Gitlab-Event
'
not
in
req
.
headers
:
self
.
log
.
debug
(
'
missing X-Gitlab-Event Header
'
)
return
None
GitlabEvent
=
req
.
headers
[
'
X-Gitlab-Event
'
]
msg
=
EventParse
[
GitlabEvent
](
body
)
await
self
.
send_gitlab_event
(
req
.
query
[
'
room
'
],
msg
)
async
def
post_handler
(
self
,
request
:
web
.
Request
)
->
web
.
Response
:
# check the authorisation of the request
...
...
@@ -48,9 +153,7 @@ class Gitlab(Plugin):
)
# check if the bot is in the specified room
# TODO: make joined_rooms a clas property which is updated on startup and room join/leave
joined_rooms
=
await
self
.
client
.
get_joined_rooms
()
if
request
.
query
[
'
room
'
]
not
in
joined_rooms
:
if
request
.
query
[
'
room
'
]
not
in
self
.
joined_rooms
:
resp_text
=
'
The Bot is not in the room.
'
return
web
.
Response
(
text
=
resp_text
,
status
=
403
...
...
@@ -63,13 +166,18 @@ class Gitlab(Plugin):
return
web
.
Response
(
status
=
406
,
headers
=
{
'
Content-Type
'
:
'
application/json
'
}
)
self
.
task_list
.
append
(
asyncio
.
create_task
(
self
.
process_hook
(
request
)))
task
=
self
.
loop
.
create_task
(
self
.
process_hook
(
request
))
self
.
task_list
+=
[
task
]
await
task
return
web
.
Response
(
status
=
202
)
async
def
start
(
self
)
->
None
:
self
.
config
.
load_and_update
()
self
.
joined_rooms
=
await
self
.
client
.
get_joined_rooms
()
self
.
task_list
:
List
[
asyncio
.
Task
]
=
[]
self
.
app
=
web
.
Application
()
...
...
@@ -87,6 +195,10 @@ class Gitlab(Plugin):
await
asyncio
.
wait_for
(
task
,
timeout
=
1.0
)
await
self
.
runner
.
cleanup
()
@event.on
(
EventType
.
ROOM_MEMBER
)
async
def
member_handler
(
self
,
evt
:
MessageEvent
)
->
None
:
pass
@classmethod
def
get_config_class
(
cls
)
->
Type
[
BaseProxyConfig
]:
return
Config
This diff is collapsed.
Click to expand it.
maubot.yaml
+
1
−
1
View file @
f0235daf
...
...
@@ -31,7 +31,7 @@ extra_files:
# List of dependencies
#dependencies:
#-
foo
#-
#soft_dependencies:
#- bar>=0.1
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment