Add GitLab webhook support

This commit is contained in:
rubenwardy 2020-01-25 00:44:46 +00:00
parent 493917d8b1
commit b7101a403b
3 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,64 @@
# ContentDB
# Copyright (C) 2020 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask import Blueprint, request
bp = Blueprint("gitlab", __name__)
from app import csrf
from app.models import Package, APIToken, Permission
from app.blueprints.api.support import error, handleCreateRelease
@bp.route("/gitlab/webhook/", methods=["POST"])
@csrf.exempt
def webhook():
json = request.json
# Get package
gitlab_url = "gitlab.com/{}/{}".format(json["project"]["namespace"], json["project"]["name"])
package = Package.query.filter(Package.repo.like("%{}%".format(gitlab_url))).first()
if package is None:
return error(400, "Unknown package")
# Get all tokens for package
secret = request.headers.get("X-Gitlab-Token")
if secret is None:
return error(403, "Token required")
token = APIToken.query.filter_by(access_token=secret).first()
if secret is None:
return error(403, "Invalid authentication")
if not package.checkPerm(token.owner, Permission.APPROVE_RELEASE):
return error(403, "Only trusted members can use webhooks")
#
# Check event
#
event = json["event_name"]
if event == "push":
ref = json["after"]
title = ref[:5]
else:
return error(400, "Unsupported event. Only 'push' is supported.")
#
# Perform release
#
return handleCreateRelease(token, package, title, ref)

View File

@ -5,3 +5,4 @@ title: Help
* [Content Ratings and Flags](content_flags)
* [Reporting Content](reporting)
* [API](api)
* [Creating Releases using Webhooks](release_webhooks)

View File

@ -0,0 +1,72 @@
title: Creating Releases using Webhooks
## What does this mean?
ContentDB offers the ability to automatically create releases using webhooks
from either Github or Gitlab. If you're not using either of those services,
you can also use the [API](../api) to create releases.
The process is as follows:
1. The user creates an API Token and a webhook to use it. This can be done automatically
for Github.
2. The user pushes a commit to the git host (Gitlab or Github).
3. The git host posts a webhook notification to ContentDB, using the API token assigned to it.
4. ContentDB checks the API token and issues a new releases.
<p class="alert alert-info">
This feature is in beta, and is only available for Trusted Members.
</p>
## Setting up
### Github (automatic)
1. Go to your package page.
2. Make sure that the repository URL is set to a Github repository.
Only github.com is supported.
3. Click "Set up a webhook to create releases automatically" below the releases
panel on the side bar.
4. Grant ContentDB the ability to manage Webhooks
### GitHub (manual)
1. Create an API Token by visiting your profile and clicking "API Tokens: Manage".
2. Copy the access token that was generated.
3. Go to the repository's settings > Webhooks > Add Webhook.
4. Set the payload URL to `https://content.minetest.net/github/webhook/`
5. Set the content type to JSON.
6. Set the secret to the access token that you copied.
7. Set the events
* If you want a rolling release, choose "just the push event".
* Or if you want a stable release cycle based on tags,
choose "Let me select" > Branch or tag creation.
### GitLab (manual)
1. Create an API Token by visiting your profile and clicking "API Tokens: Manage".
2. Copy the access token that was generated.
3. Go to the repository's settings > Integrations.
4. Set the URL to `https://content.minetest.net/gitlab/webhook/`
6. Set the secret token to the access token that you copied.
7. Set the events
* If you want a rolling release, choose "Push events".
* Or if you want a stable release cycle based on tags,
choose "Tag push events".
## Configuring
### Setting minimum and maximum Minetest versions
<p class="alert alert-info">
This feature is unimplemented.
</p>
1. Open up the conf file for the package.
This will be `game.conf`, `mod.conf`, `modpack.conf`, or `texture_pack.conf`
depending on the content type.
2. Set `min_protocol` and `max_protocol` to the respective protocol numbers
of the Minetest versions.
* 0.4 = 32
* 5.0 = 37
* 5.1 = 38