mirror of
https://github.com/jlu5/SupyPlugins.git
synced 2025-04-26 04:51:08 -05:00
Remove GitLab plugin fork
All meaningful changes have since been upstreamed to https://github.com/mlq/limnoria-gitlab
This commit is contained in:
parent
77f57a7d93
commit
79a4453875
3
GitLab/.gitignore
vendored
3
GitLab/.gitignore
vendored
@ -1,3 +0,0 @@
|
||||
*.pyc
|
||||
*.sw[a-z]
|
||||
.ropeproject
|
@ -1,99 +0,0 @@
|
||||
# limnoria-gitlab
|
||||
|
||||
limnoria-gitlab is a plugin for [limnoria](https://github.com/ProgVal/Limnoria)
|
||||
that provides support for [gitlab](https://gitlab.com) webhook notifications.
|
||||
Currently it has the following features:
|
||||
|
||||
- Support of push, tag, issue, comment and merge request events
|
||||
- Commands to manage subscribed projects per channel
|
||||
- Localization
|
||||
|
||||
### Installation
|
||||
|
||||
To install this plugin just copy its directory to the
|
||||
`supybot.directories.plugins` directory of your limnoria instance and enable it
|
||||
in your configuration file under `supybot.plugins`. For more information
|
||||
checkout the [Supybot user
|
||||
guide](http://doc.supybot.aperio.fr/en/latest/use/index.html).
|
||||
|
||||
### Configuration
|
||||
|
||||
The _limnoria-gitlab_ plugin uses the build-in web service of Limnoria therefore
|
||||
it listens on the address configured by `supybot.servers.http.hosts[4,6]` and
|
||||
`supybot.servers.http.port`. For more information on the HTTP server of Limnoria
|
||||
checkout the '[Using the HTTP
|
||||
server](http://doc.supybot.aperio.fr/en/latest/use/httpserver.html)' chapter of
|
||||
their documentation.
|
||||
|
||||
Depending on the configuration of your Limnoria instance and your web server the
|
||||
plugin now listens on the following address where it accepts the network and the
|
||||
channel as a parameter:
|
||||
|
||||
`http://<host>:<port>/gitlab/<network>/<channel>`
|
||||
|
||||
The placeholders are defined as followed:
|
||||
|
||||
- `<host>` - The host defined by the external IP of the service
|
||||
- `<port>` - The port that the HTTP server of Limnoria listens to
|
||||
- `<network>` - The network that the Limnoria instance is connected to
|
||||
- `<channel>` - The channel that the Limnoria instance is in
|
||||
|
||||
For instance if your bot is in the _OFTC_ network and in the _#limnoria-gitlab_
|
||||
channel, the plugin listens on the following URL for webhook notifications:
|
||||
|
||||
`http://limnoria.example.com:8080/gitlab/OFTC/limnoria-gitlab`
|
||||
|
||||
Now you need to add this address as a new webhook in the project settings of
|
||||
your Gitlab instance. Therefore you go to `Settings -> Webhooks`
|
||||
and click `Add Web Hook` after you've entered the above address under URL and
|
||||
selected the checkboxes for the types of notifications you want to be send to
|
||||
the channel.
|
||||
|
||||
### Commands
|
||||
|
||||
- `gitlab project add [<channel>] <project-slug> <project-host>` -
|
||||
This command subscribes a new project to the channel:
|
||||
- `[<channel>]` - The channel that should be used. _(Optional, defaults to the current channel)_
|
||||
- `<project-slug>` - The slug of the gitlab project
|
||||
- `<project-host>` - The host of the gitlab project
|
||||
|
||||
Example: To subscribe the _example_project_ to the current channel you can run the following command: `gitlab project add example_project https://gitlab.example.com/foo/example_project`
|
||||
|
||||
- `gitlab project remove [<channel>] <project-slug>` - This command removes a subscribed project from the channel:
|
||||
- `[<channel>]` - The channel that should be used. _(Optional, defaults to the current channel)_
|
||||
- `<project-slug` - The slug of the gitlab project
|
||||
|
||||
- `gitlab project list [<channel>]` - Lists the subscribed projects from the channel:
|
||||
- `[<channel>]` - The channel that should be used. _(Optional, defaults to the current channel)_
|
||||
|
||||
### Options
|
||||
|
||||
The following option can be set for each channel and defines the list of subscribed projects (this option should only be set by the commands of this plugin).
|
||||
|
||||
- `plugins.Gitlab.projects` - Saves the subscribed project mappings _(Default: empty)_ **Readonly!**
|
||||
|
||||
In addition all the formats that are used to notify the channel about changes on the Gitlab project can be configured:
|
||||
|
||||
- `plugins.Gitlab.format.push` - The format that is used if a milestone has been created
|
||||
- `plugins.Gitlab.format.commit` - The format that is used if a milestone has been deleted
|
||||
- `plugins.Gitlab.format.tag` - The format that is used if a milestone has been changed
|
||||
- `plugins.Gitlab.format.issue-open` - The format that is used if an issue has been created
|
||||
- `plugins.Gitlab.format.issue-update` - The format that is used if an issue has been updated
|
||||
- `plugins.Gitlab.format.issue-close` - The format that is used if an issue has been closed
|
||||
- `plugins.Gitlab.format.issue-reopen` - The format that is used if an issue has been reopened
|
||||
- `plugins.Gitlab.format.merge-request-open` - The format that is used if an merge request has been created
|
||||
- `plugins.Gitlab.format.merge-request-update` - The format that is used if an merge request has been updated
|
||||
- `plugins.Gitlab.format.merge-request-close` - The format that is used if an merge request has been closed
|
||||
- `plugins.Gitlab.format.merge-request-reopen` - The format that is used if an merge request has been reopened
|
||||
- `plugins.Gitlab.format.merge-request-merge` - The format that is used if an merge request has been merged
|
||||
- `plugins.Gitlab.format.note-merge-request` - The format that is used if someone commented on a merge request
|
||||
- `plugins.Gitlab.format.note-commit` - The format that is used if someone commented on a commit
|
||||
- `plugins.Gitlab.format.note-issue` - The format that is used if someone commented on a issue
|
||||
- `plugins.Gitlab.format.note-snippet` - The format that is used if someone commented on a snippet
|
||||
|
||||
For those formats you can pass different arguments that contain the values of the notification. The default values are:
|
||||
|
||||
- The data of the payload as described
|
||||
[here](https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/web_hooks/web_hooks.md)
|
||||
- `project` - The project containing the *name* and the *id* of the project
|
||||
- `url` - The direct url to the data described by this notification
|
@ -1,61 +0,0 @@
|
||||
###
|
||||
# Copyright (c) 2015, Moritz Lipp
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# * Neither the name of the author of this software nor the name of
|
||||
# contributors to this software may be used to endorse or promote products
|
||||
# derived from this software without specific prior written consent.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
###
|
||||
|
||||
"""
|
||||
GitLab: Plugin for communication and notifications with a GitLab instance
|
||||
"""
|
||||
|
||||
import supybot
|
||||
import supybot.world as world
|
||||
|
||||
__version__ = "2022.02.01+git"
|
||||
|
||||
__author__ = supybot.Author('Moritz Lipp', 'mlq', 'mail@mlq.me')
|
||||
__maintainer__ = getattr(supybot.authors, 'jlu',
|
||||
supybot.Author('James Lu', 'jlu5', 'james@overdrivenetworks.com'))
|
||||
|
||||
__contributors__ = {}
|
||||
|
||||
__url__ = ''
|
||||
|
||||
from . import config
|
||||
from . import plugin
|
||||
from imp import reload
|
||||
# In case we're being reloaded.
|
||||
reload(config)
|
||||
reload(plugin)
|
||||
# Add more reloads here if you add third-party modules and want them to be
|
||||
# reloaded when this plugin is reloaded. Don't forget to import them as well!
|
||||
|
||||
Class = plugin.Class
|
||||
configure = config.configure
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
115
GitLab/config.py
115
GitLab/config.py
@ -1,115 +0,0 @@
|
||||
###
|
||||
# Copyright (c) 2015, Moritz Lipp
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# * Neither the name of the author of this software nor the name of
|
||||
# contributors to this software may be used to endorse or promote products
|
||||
# derived from this software without specific prior written consent.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
###
|
||||
|
||||
import supybot.conf as conf
|
||||
import supybot.registry as registry
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
_ = PluginInternationalization('GitLab')
|
||||
except:
|
||||
# Placeholder that allows to run the plugin on a bot
|
||||
# without the i18n module
|
||||
_ = lambda x: x
|
||||
|
||||
|
||||
def configure(advanced):
|
||||
# This will be called by supybot to configure this module. advanced is
|
||||
# a bool that specifies whether the user identified themself as an advanced
|
||||
# user or not. You should effect your configuration by manipulating the
|
||||
# registry as appropriate.
|
||||
from supybot.questions import expect, anything, something, yn
|
||||
conf.registerPlugin('GitLab', True)
|
||||
|
||||
|
||||
GitLab = conf.registerPlugin('GitLab')
|
||||
|
||||
# Settings
|
||||
conf.registerChannelValue(GitLab, 'projects',
|
||||
registry.Json({}, _("""List of projects""")))
|
||||
|
||||
# Format
|
||||
conf.registerGroup(GitLab, 'format')
|
||||
|
||||
conf.registerChannelValue(GitLab, 'use-notices',
|
||||
registry.Boolean(False, _("""Determines whether the bot should announce using NOTICE instead of PRIVMSG.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'push',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 {user_name} pushed \x02{total_commits_count} commit(s)\x02 to \x02{ref}\x02:"""),
|
||||
_("""Format for push events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'commit',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 {short_id} \x02{short_message}\x02 by {author[name]}"""),
|
||||
_("""Format for commits.""")))
|
||||
|
||||
conf.registerChannelValue(GitLab.format, 'tag',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 {user_name} created a new tag {ref}"""),
|
||||
_("""Format for tag push events.""")))
|
||||
|
||||
conf.registerChannelValue(GitLab.format, 'issue-open',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Issue \x02#{issue[id]} {issue[title]}\x02 created by {user[name]} {issue[url]}"""),
|
||||
_("""Format for issue/open events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'issue-update',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Issue \x02#{issue[id]} {issue[title]}\x02 updated by {user[name]} {issue[url]}"""),
|
||||
_("""Format for issue/update events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'issue-close',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Issue \x02#{issue[id]} {issue[title]}\x02 closed by {user[name]} {issue[url]}"""),
|
||||
_("""Format for issue/close events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'issue-reopen',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Issue \x02#{issue[id]} {issue[title]}\x02 reopend by {user[name]} {issue[url]}"""),
|
||||
_("""Format for issue/reopen events.""")))
|
||||
|
||||
conf.registerChannelValue(GitLab.format, 'merge-request-open',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Merge request \x02#{merge_request[id]} {merge_request[title]}\x02 created by {user[name]} {merge_request[url]}"""),
|
||||
_("""Format for merge-request/open events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'merge-request-update',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Merge request \x02#{merge_request[id]} {merge_request[title]}\x02 updated by {user[name]} {merge_request[url]}"""),
|
||||
_("""Format for merge-request/open events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'merge-request-close',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Merge request \x02#{merge_request[id]} {merge_request[title]}\x02 closed by {user[name]} {merge_request[url]}"""),
|
||||
_("""Format for merge-request/open events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'merge-request-reopen',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Merge request \x02#{merge_request[id]} {merge_request[title]}\x02 reopened by {user[name]} {merge_request[url]}"""),
|
||||
_("""Format for merge-request/open events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'merge-request-merge',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 Merge request \x02#{merge_request[id]} {merge_request[title]}\x02 merged by {user[name]} {merge_request[url]}"""),
|
||||
_("""Format for merge-request/open events.""")))
|
||||
|
||||
conf.registerChannelValue(GitLab.format, 'note-merge-request',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 {user[name]} commented on Merge request \x02#{merge_request[id]} {merge_request[title]}\x02 {note[url]}"""),
|
||||
_("""Format for note/merge-request events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'note-commit',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 {user[name]} commented on Commit \x02#{commit[id]}\x02 {commit[url]}"""),
|
||||
_("""Format for note/commit events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'note-issue',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 {user[name]} commented on Issue \x02#{issue[id]} {issue[title]}\x02 {note[url]}"""),
|
||||
_("""Format for note/issue events.""")))
|
||||
conf.registerChannelValue(GitLab.format, 'note-snippet',
|
||||
registry.String(_("""\x02[{project[name]}]\x02 {user[name]} commented on Snippet \x02#{snippet[id]} {snippet[title]}\x02 {note[url]}"""),
|
||||
_("""Format for note/snippet events.""")))
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
|
@ -1 +0,0 @@
|
||||
# Stub so local is a module, used for third-party modules
|
385
GitLab/plugin.py
385
GitLab/plugin.py
@ -1,385 +0,0 @@
|
||||
###
|
||||
# Copyright (c) 2015, Moritz Lipp
|
||||
# Copyright (c) 2018-2019, James Lu <james@overdrivenetworks.com>
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions, and the following disclaimer in the
|
||||
# documentation and/or other materials provided with the distribution.
|
||||
# * Neither the name of the author of this software nor the name of
|
||||
# contributors to this software may be used to endorse or promote products
|
||||
# derived from this software without specific prior written consent.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
# POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
###
|
||||
|
||||
import json
|
||||
|
||||
from supybot.commands import wrap
|
||||
import supybot.ircdb as ircdb
|
||||
import supybot.ircmsgs as ircmsgs
|
||||
import supybot.callbacks as callbacks
|
||||
import supybot.log as log
|
||||
import supybot.httpserver as httpserver
|
||||
import supybot.world as world
|
||||
try:
|
||||
from supybot.i18n import PluginInternationalization
|
||||
from supybot.i18n import internationalizeDocstring
|
||||
_ = PluginInternationalization('GitLab')
|
||||
except ImportError:
|
||||
# Placeholder that allows to run the plugin on a bot
|
||||
# without the i18n module
|
||||
def _(x):
|
||||
return x
|
||||
|
||||
def internationalizeDocstring(x):
|
||||
return x
|
||||
|
||||
|
||||
class GitLabHandler(object):
|
||||
|
||||
"""Handle gitlab messages"""
|
||||
|
||||
def __init__(self, plugin):
|
||||
self.plugin = plugin
|
||||
self.log = log.getPluginLogger('GitLab')
|
||||
# HACK: instead of refactoring everything, I can just replace this with each handle_payload() call.
|
||||
self.irc = None
|
||||
|
||||
def handle_payload(self, headers, payload, irc):
|
||||
if 'X-Gitlab-Event' not in headers:
|
||||
self.log.info('Invalid header: Missing X-Gitlab-Event entry')
|
||||
return
|
||||
self.irc = irc
|
||||
self.log.debug('GitLab: running on network %r', irc.network)
|
||||
|
||||
event_type = headers['X-Gitlab-Event']
|
||||
if event_type not in ['Push Hook', 'Tag Push Hook', 'Note Hook', 'Issue Hook', 'Merge Request Hook']:
|
||||
self.log.info('Unsupported X-Gitlab-Event type')
|
||||
return
|
||||
|
||||
# Check if any channel has subscribed to this project
|
||||
for channel in irc.state.channels.keys():
|
||||
projects = self.plugin._load_projects(channel)
|
||||
for slug, url in projects.items():
|
||||
# Parse project url
|
||||
if event_type == 'Push Hook' or event_type == 'Tag Push Hook' or event_type == 'Note Hook':
|
||||
if url != payload['repository']['homepage']:
|
||||
continue
|
||||
elif event_type == 'Issue Hook':
|
||||
if url not in payload['object_attributes']['url']:
|
||||
continue
|
||||
elif event_type == 'Merge Request Hook':
|
||||
if url not in payload['object_attributes']['target']['http_url']:
|
||||
continue
|
||||
else:
|
||||
continue
|
||||
|
||||
# Update payload
|
||||
payload['project'] = {
|
||||
'name': slug,
|
||||
'url': url
|
||||
}
|
||||
|
||||
if event_type == 'Issue Hook':
|
||||
payload['project']['id'] = payload[
|
||||
'object_attributes']['project_id']
|
||||
elif event_type == 'Merge Request Hook':
|
||||
payload['project']['id'] = payload[
|
||||
'object_attributes']['target_project_id']
|
||||
else:
|
||||
payload['project']['id'] = payload['project_id']
|
||||
|
||||
# Handle types
|
||||
if event_type == 'Push Hook':
|
||||
self._push_hook(channel, payload)
|
||||
elif event_type == 'Tag Push Hook':
|
||||
self._tag_push_hook(channel, payload)
|
||||
elif event_type == 'Issue Hook':
|
||||
self._issue_hook(channel, payload)
|
||||
elif event_type == 'Note Hook':
|
||||
self._note_hook(channel, payload)
|
||||
elif event_type == 'Merge Request Hook':
|
||||
self._merge_request_hook(channel, payload)
|
||||
|
||||
def _push_hook(self, channel, payload):
|
||||
# Send general message
|
||||
msg = self._build_message(channel, 'push', payload)
|
||||
self._send_message(channel, msg)
|
||||
|
||||
# Send commits
|
||||
for commit in payload['commits']:
|
||||
commit['project'] = {
|
||||
'id': payload['project_id'],
|
||||
'name': payload['project']['name'],
|
||||
'url': payload['project']['url']
|
||||
}
|
||||
commit['short_message'] = commit['message'].splitlines()[0]
|
||||
commit['short_id'] = commit['id'][0:10]
|
||||
|
||||
msg = self._build_message(channel, 'commit', commit)
|
||||
self._send_message(channel, msg)
|
||||
|
||||
def _tag_push_hook(self, channel, payload):
|
||||
msg = self._build_message(channel, 'tag', payload)
|
||||
self._send_message(channel, msg)
|
||||
|
||||
# Send commits
|
||||
for commit in payload['commits']:
|
||||
commit['project'] = {
|
||||
'id': payload['project_id'],
|
||||
'name': payload['project']['name'],
|
||||
'url': payload['project']['url']
|
||||
}
|
||||
commit['short_message'] = commit['message'].splitlines()[0]
|
||||
commit['short_id'] = commit['id'][0:10]
|
||||
|
||||
msg = self._build_message(channel, 'commit', commit)
|
||||
self._send_message(channel, msg)
|
||||
|
||||
def _note_hook(self, channel, payload):
|
||||
noteable_type = payload['object_attributes']['noteable_type']
|
||||
if noteable_type not in ['Commit', 'MergeRequest', 'Issue', 'Snippet']:
|
||||
self.log.info("Unsupported note type '%s'" % noteable_type)
|
||||
return
|
||||
|
||||
noteable_type = noteable_type.lower()
|
||||
if noteable_type == "mergerequest":
|
||||
noteable_type = "merge-request"
|
||||
|
||||
payload['note'] = payload['object_attributes']
|
||||
|
||||
msg = self._build_message(channel, 'note-' + noteable_type, payload)
|
||||
self._send_message(channel, msg)
|
||||
|
||||
def _issue_hook(self, channel, payload):
|
||||
action = payload['object_attributes']['action']
|
||||
if action not in ['open', 'update', 'close', 'reopen']:
|
||||
self.log.info("Unsupported issue action '%s'" % action)
|
||||
return
|
||||
|
||||
payload['issue'] = payload['object_attributes']
|
||||
|
||||
msg = self._build_message(channel, 'issue-' + action, payload)
|
||||
self._send_message(channel, msg)
|
||||
|
||||
def _merge_request_hook(self, channel, payload):
|
||||
action = payload['object_attributes']['action']
|
||||
if action not in ['open', 'update', 'close', 'reopen', 'merge']:
|
||||
self.log.info("Unsupported issue action '%s'" % action)
|
||||
return
|
||||
|
||||
payload['merge_request'] = payload['object_attributes']
|
||||
|
||||
msg = self._build_message(channel, 'merge-request-' + action, payload)
|
||||
self._send_message(channel, msg)
|
||||
|
||||
def _build_message(self, channel, format_string_identifier, args):
|
||||
format_string = str(
|
||||
self.plugin.registryValue(
|
||||
'format.' +
|
||||
format_string_identifier,
|
||||
channel))
|
||||
msg = format_string.format(**args)
|
||||
return msg
|
||||
|
||||
def _send_message(self, channel, msg):
|
||||
if self.plugin.registryValue('use-notices', channel):
|
||||
announce_msg = ircmsgs.notice(channel, msg)
|
||||
else:
|
||||
announce_msg = ircmsgs.privmsg(channel, msg)
|
||||
self.irc.queueMsg(announce_msg)
|
||||
|
||||
|
||||
class GitLabWebHookService(httpserver.SupyHTTPServerCallback):
|
||||
"""https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/web_hooks/web_hooks.md"""
|
||||
|
||||
name = "GitLabWebHookService"
|
||||
defaultResponse = """This plugin handles only POST request, please don't use other requests."""
|
||||
|
||||
def __init__(self, plugin):
|
||||
self.log = log.getPluginLogger('GitLab')
|
||||
self.gitlab = GitLabHandler(plugin)
|
||||
self.plugin = plugin
|
||||
|
||||
def _send_error(self, handler, message):
|
||||
handler.send_response(403)
|
||||
handler.send_header('Content-type', 'text/plain')
|
||||
handler.end_headers()
|
||||
handler.wfile.write(message.encode('utf-8'))
|
||||
|
||||
def _send_ok(self, handler):
|
||||
handler.send_response(200)
|
||||
handler.send_header('Content-type', 'text/plain')
|
||||
handler.end_headers()
|
||||
handler.wfile.write(bytes('OK', 'utf-8'))
|
||||
|
||||
def doPost(self, handler, path, form):
|
||||
headers = dict(self.headers)
|
||||
|
||||
network = None
|
||||
try:
|
||||
information = path.split('/')[1:]
|
||||
network = information[0]
|
||||
except IndexError:
|
||||
self._send_error(handler, _("""Error: You need to provide the
|
||||
network name in the URL."""))
|
||||
return
|
||||
|
||||
irc = world.getIrc(network)
|
||||
if irc is None:
|
||||
self._send_error(handler, (_('Error: Unknown network %r') % network))
|
||||
return
|
||||
|
||||
# Handle payload
|
||||
payload = None
|
||||
try:
|
||||
payload = json.JSONDecoder().decode(form.decode('utf-8'))
|
||||
except Exception as e:
|
||||
self.log.info(e)
|
||||
self._send_error(handler, _('Error: Invalid JSON data sent.'))
|
||||
return
|
||||
|
||||
try:
|
||||
self.gitlab.handle_payload(headers, payload, irc)
|
||||
except Exception as e:
|
||||
self.log.info(e)
|
||||
self._send_error(handler, _('Error: Invalid data sent.'))
|
||||
return
|
||||
|
||||
# Return OK
|
||||
self._send_ok(handler)
|
||||
|
||||
|
||||
class GitLab(callbacks.Plugin):
|
||||
"""Plugin for communication and notifications of a GitLab project
|
||||
management tool instance"""
|
||||
threaded = True
|
||||
|
||||
def __init__(self, irc):
|
||||
global instance
|
||||
|
||||
# Store the super() information so that reloads don't fail
|
||||
self.__parent = super(GitLab, self)
|
||||
self.__parent.__init__(irc)
|
||||
instance = self
|
||||
|
||||
callback = GitLabWebHookService(self)
|
||||
httpserver.hook('gitlab', callback)
|
||||
|
||||
def die(self):
|
||||
httpserver.unhook('gitlab')
|
||||
|
||||
self.__parent.die()
|
||||
|
||||
def _load_projects(self, channel):
|
||||
projects = self.registryValue('projects', channel)
|
||||
if projects is None:
|
||||
return {}
|
||||
else:
|
||||
return projects
|
||||
|
||||
def _save_projects(self, projects, channel):
|
||||
self.setRegistryValue('projects', value=projects, channel=channel)
|
||||
|
||||
def _check_capability(self, irc, msg):
|
||||
if ircdb.checkCapability(msg.prefix, 'admin'):
|
||||
return True
|
||||
else:
|
||||
irc.errorNoCapability('admin')
|
||||
return False
|
||||
|
||||
class gitlab(callbacks.Commands):
|
||||
"""GitLab commands"""
|
||||
|
||||
class project(callbacks.Commands):
|
||||
"""Project commands"""
|
||||
|
||||
@internationalizeDocstring
|
||||
def add(self, irc, msg, args, channel, project_slug, project_url):
|
||||
"""[<channel>] <project-slug> <project-url>
|
||||
|
||||
Announces the changes of the project with the slug
|
||||
<project-slug> and the url <project-url> to <channel>.
|
||||
"""
|
||||
if not instance._check_capability(irc, msg):
|
||||
return
|
||||
|
||||
projects = instance._load_projects(channel)
|
||||
if project_slug in projects:
|
||||
irc.error(
|
||||
_('This project is already announced to this channel.'))
|
||||
return
|
||||
|
||||
# Save new project mapping
|
||||
projects[project_slug] = project_url
|
||||
instance._save_projects(projects, channel)
|
||||
|
||||
irc.replySuccess()
|
||||
|
||||
add = wrap(add, ['channel', 'somethingWithoutSpaces', 'httpUrl'])
|
||||
|
||||
@internationalizeDocstring
|
||||
def remove(self, irc, msg, args, channel, project_slug):
|
||||
"""[<channel>] <project-slug>
|
||||
|
||||
Stops announcing the changes of the project slug <project-slug>
|
||||
to <channel>.
|
||||
"""
|
||||
if not instance._check_capability(irc, msg):
|
||||
return
|
||||
|
||||
projects = instance._load_projects(channel)
|
||||
if project_slug not in projects:
|
||||
irc.error(
|
||||
_('This project is not registered to this channel.'))
|
||||
return
|
||||
|
||||
# Remove project mapping
|
||||
del projects[project_slug]
|
||||
instance._save_projects(projects, channel)
|
||||
|
||||
irc.replySuccess()
|
||||
|
||||
remove = wrap(remove, ['channel', 'somethingWithoutSpaces'])
|
||||
|
||||
@internationalizeDocstring
|
||||
def list(self, irc, msg, args, channel):
|
||||
"""[<channel>]
|
||||
|
||||
Lists the registered projects in <channel>.
|
||||
"""
|
||||
if not instance._check_capability(irc, msg):
|
||||
return
|
||||
|
||||
projects = instance._load_projects(channel)
|
||||
if projects is None or len(projects) == 0:
|
||||
irc.error(_('This channel has no registered projects.'))
|
||||
return
|
||||
|
||||
for project_slug, project_url in projects.items():
|
||||
irc.reply("%s: %s" % (project_slug, project_url))
|
||||
|
||||
list = wrap(list, ['channel'])
|
||||
|
||||
|
||||
Class = GitLab
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
@ -1,6 +0,0 @@
|
||||
|
||||
from supybot.setup import plugin_setup
|
||||
|
||||
plugin_setup(
|
||||
'GitLab',
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user