Add gitlab webhook support

This commit is contained in:
Abel Luck 2022-12-01 13:47:27 +00:00
parent 9d41d56e0c
commit a1ae717c8f
26 changed files with 1824 additions and 8 deletions

View file

@ -0,0 +1,75 @@
{%- macro repo_link(project) -%}
<a data-mautrix-exclude-plaintext href="{{ project.web_url }}">{{ project.path_with_namespace|e }}</a>
{%- endmacro -%}
{%- macro user_link(user) -%}
<a data-mautrix-exclude-plaintext href="{{ user.web_url }}">{{ user.username|e }}</a>
{%- endmacro -%}
{%- macro pluralize(value) -%}
{% if value != 1 %}s{% endif %}
{%- endmacro -%}
{%- macro issue_link(issue, title = true, important = true) -%}
<a href="{{ issue.url }}" {% if not important %}data-mautrix-exclude-plaintext{% endif %}>
{%- if issue.confidential and (not changes or not changes.confidential) -%}confidential {% endif -%}
issue #{{ issue.issue_id -}}
</a>
{%- if title -%}
: {{ issue.title|e -}}
{% endif %}
{%- endmacro -%}
{%- macro merge_request_link(merge_request, title = true, important = true) -%}
<a href="{{ merge_request.url }}" {% if not important %}data-mautrix-exclude-plaintext{% endif %}>
merge request !{{ merge_request.merge_request_id -}}
</a>
{%- if title -%}
: {{ merge_request.title|e -}}
{% endif %}
{%- endmacro -%}
{%- macro issue_or_merge_link(attrs, title = true, important = true) -%}
{%- if attrs.issue_id -%}
{{- issue_link(attrs, title, important) -}}
{%- elif attrs.merge_request_id -%}
{{- merge_request_link(attrs, title, important) -}}
{%- else -%}
unknown object {{ attrs.title|e }}
{%- endif -%}
{%- endmacro -%}
{%- macro fancy_label(label) -%}
<span data-mx-color="{{ label.foreground_color }}"
data-mx-bg-color="{{ label.color }}"
title="{{ label.description }}"
>&nbsp;{{ util.bold_scope(label.title) }}&nbsp;</span>
{%- endmacro -%}
{%- macro fancy_labels(labels) -%}
{% for label in labels %}
{{ fancy_label(label) }}
{% endfor %}
{%- endmacro -%}
{%- macro list_changes(added, removed, add_word, remove_word, mutate) -%}
{%- if added -%}
{{ add_word }} {{ util.join_human_list(added, mutate=mutate) }}
{% if not removed %}
to
{% endif %}
{%- endif -%}
{%- if removed -%}
{%- if added %}
and
{% endif -%}
{{ remove_word }} {{ util.join_human_list(removed, mutate=mutate) }}
from
{%- endif -%}
{%- endmacro -%}
{%- macro label_changes(added, removed) -%}
{{ list_changes(added, removed, "added", "removed", fancy_label) }}
{%- endmacro -%}
{%- macro assignee_changes(added, removed) -%}
{{ list_changes(added, removed, "assigned", "unassigned", user_link) }}
{%- endmacro -%}

View file

@ -0,0 +1,20 @@
{{ templates.repo_sender_prefix }}
<a data-mautrix-exclude-plaintext href="{{ object_attributes.url }}">
{%- if object_attributes.type == CommentType.DISCUSSION_NOTE -%}
replied to a thread
{%- else -%}
commented
{%- endif -%}
</a> on
{% if issue and object_attributes.noteable_type == NoteableType.ISSUE %}
{{ issue_link(issue, important=false) }}
{% elif merge_request and object_attributes.noteable_type == NoteableType.MERGE_REQUEST %}
{{ merge_request_link(merge_request, important=false) }}
{% else %}
{# unsupported comment target #}
{% do abort() %}
{% endif %}
<br/>
{% if object_attributes.description %}
<blockquote>{{ object_attributes.description|markdown }}</blockquote>
{% endif %}

View file

@ -0,0 +1,2 @@
{{ templates.repo_sender_prefix }}
closed {{ issue_link(object_attributes) }}

View file

@ -0,0 +1,6 @@
{{ templates.repo_sender_prefix }}
opened {{ issue_link(object_attributes) }}<br/>
{% if object_attributes.description %}
<blockquote>{{ object_attributes.description|markdown }}</blockquote>
{% endif %}
{{ fancy_labels(object_attributes.labels) }}

View file

@ -0,0 +1,2 @@
{{ templates.repo_sender_prefix }}
reopened {{ issue_link(object_attributes) }}

View file

@ -0,0 +1,77 @@
{{ templates.repo_sender_prefix }}
{% if changes.labels %}
{{ label_changes(changes.labels.added, changes.labels.removed) }}
{{ issue_or_merge_link(object_attributes) }}
{# Milestone webhooks don't have the milestone displayname 3:< #}
{#{% elif changes.milestone_id %}#}
{# {% if not changes.milestone_id.current %}#}
{# removed the milestone from {{ issue_or_merge_link(object_attributes) }}#}
{# {% else %}#}
{# added {{ issue_or_merge_link(object_attributes) }} to milestone#}
{# {% endif %}#}
{% elif changes.assignees %}
{{ assignee_changes(changes.assignees.added, changes.assignees.removed) }}
{{ issue_or_merge_link(object_attributes) }}
{% elif changes.time_estimate %}
{% if not changes.time_estimate.current %}
removed the time estimate of {{ issue_or_merge_link(object_attributes) }}
{% elif not changes.time_estimate.previous %}
set the time estimate of {{ issue_or_merge_link(object_attributes) }} to
<strong>{{ util.format_time(changes.time_estimate.current) }}</strong>
{% else %}
{% if changes.time_estimate.current > changes.time_estimate.previous %}
increased
{% else %}
decreased
{% endif %}
the time estimate of {{ issue_or_merge_link(object_attributes) }} by
<strong>{{ util.format_time(changes.time_estimate.current - changes.time_estimate.previous) }}</strong>
{% endif %}
{% elif changes.total_time_spent %}
{% if not changes.total_time_spent.current %}
removed the time spent
{% else %}
{% if changes.total_time_spent.current > (changes.total_time_spent.previous or 0) %}
spent <strong>{{ util.format_time(changes.total_time_spent.current - (changes.total_time_spent.previous or 0)) }}</strong>
{% else %}
subtracted <strong>{{ util.format_time(changes.total_time_spent.current - changes.total_time_spent.previous) }}</strong>
from the time spent
{% endif %}
{% endif %}
on {{ issue_or_merge_link(object_attributes) }}
{% elif changes.weight %}
{% if not changes.weight.current %}
removed
{% else %}
changed
{% endif %}
the weight of {{ issue_or_merge_link(object_attributes) }}
{% if changes.weight.current %}
to <strong>{{ changes.weight.current }}</strong>
{% endif %}
{% elif changes.due_date %}
{% if not changes.due_date.current %}
removed the due date of {{ issue_or_merge_link(object_attributes) }}
{% else %}
set the due date of {{ issue_or_merge_link(object_attributes) }}
to <strong>{{ changes.due_date.current.strftime("%B %d, %Y") }}</strong>
{% endif %}
{% elif changes.confidential %}
made {{ issue_or_merge_link(object_attributes) }}
{% if changes.confidential.current %}
confidential
{% else %}
non-confidential
{% endif %}
{% elif changes.discussion_locked %}
{% if changes.discussion_locked.current %}
locked discussion in
{% else %}
unlocked discussion in
{% endif %}
{{ issue_or_merge_link(object_attributes) }}
{% elif changes.title %}
changed the title of {{ issue_or_merge_link(object_attributes, title=false) }} to {{ changes.title.current }}
{% else %}
{% do abort() %}
{% endif %}

View file

@ -0,0 +1,9 @@
{% if build_status == BuildStatus.FAILED and not build_allow_failure %}
<strong>[<a data-mautrix-exclude-plaintext href="{{ repository.homepage }}">{{ repository.path|e }}</a>]</strong>
<a href="{{ build_url }}">Job {{ build_id }}: {{ build_name }}</a>
<strong><font color="#ff0000">failed</font></strong>
after {{ util.format_time(build_duration) }}
(build triggered by {{ user_link(user) }})
{% else %}
{% do abort() %}
{% endif %}

View file

@ -0,0 +1,11 @@
{{ templates.repo_sender_prefix }}
{% if action == OPEN %}
opened {{ merge_request_link(object_attributes) }}
{% if object_attributes.description %}
<blockquote>{{ object_attributes.description|markdown }}</blockquote>
{% endif %}
{{ fancy_labels(labels) }}
{% else %}
{{ object_attributes.action.past_tense }}
{{ merge_request_link(object_attributes) }}
{% endif %}

View file

@ -0,0 +1,27 @@
{{ templates.repo_sender_prefix }}
{% if is_deleted_ref %}
deleted branch
{% else %}
pushed
<a href="{{ diff_url }}" data-mautrix-exclude-plaintext>
{{- total_commits_count }} commit{{ pluralize(total_commits_count) -}}
</a>
to
{% endif %}
<a data-mautrix-exclude-plaintext href="{{ ref_url }}">{{ ref_name }}</a>
{%- if is_new_ref %} (new branch){% endif -%}
{%- if commits|length > 0 %}:
<ul>
{% for commit in commits[-5:] %}
<li>
<code><a data-mautrix-exclude-plaintext href="{{ commit.url }}">
{{- commit.id[:8] -}}
</a></code>
{{ commit.cut_message|e }}
{% if commit.author.name != user_name and commit.author.name != user_username %}
by {{ commit.author.name }}
{% endif %}
</li>
{% endfor %}
</ul>
{% endif -%}

View file

@ -0,0 +1,13 @@
{{ templates.repo_sender_prefix }}
{% if is_deleted_ref %}
deleted tag
{{ ref_name }}
{% else %}
created tag
<a data-mautrix-exclude-plaintext href="{{ ref_url }}">{{ ref_name }}</a>
{% endif %}
{%- if message -%}:
<blockquote>
{{ message | markdown }}
</blockquote>
{%- endif -%}

View file

@ -0,0 +1 @@
{{ templates.repo_sender_prefix }}

View file

@ -0,0 +1,5 @@
{{ templates.repo_sender_prefix }}
{{ "edited" if object_attributes.action == UPDATE else object_attributes.action.past_tense }}
<a href="{{ object_attributes.url }}">{{ object_attributes.title }}</a>
on the wiki
{%- if object_attributes.message -%}: {{ object_attributes.message }}{% endif %}

View file

@ -0,0 +1 @@
<strong data-mautrix-exclude-plaintext>[{{ repo_link(project) }}]</strong> {{ user_link(user) }}