Skip to content

Commit

Permalink
Webhook to generate metapackages for SpaceDock
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed May 30, 2020
1 parent 5b7e048 commit 8e36588
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions netkan/netkan/webhooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .inflate import inflate
from .spacedock_inflate import spacedock_inflate
from .spacedock_add import spacedock_add
from .spacedock_metapackage import spacedock_metapackage
from .github_inflate import github_inflate
from .github_mirror import github_mirror

Expand Down Expand Up @@ -43,6 +44,7 @@ def create_app():
app.register_blueprint(inflate)
app.register_blueprint(spacedock_inflate, url_prefix='/sd')
app.register_blueprint(spacedock_add, url_prefix='/sd')
app.register_blueprint(spacedock_metapackage, url_prefix='/sd')
app.register_blueprint(github_inflate, url_prefix='/gh')
app.register_blueprint(github_mirror, url_prefix='/gh')

Expand Down
49 changes: 49 additions & 0 deletions netkan/netkan/webhooks/spacedock_metapackage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from pathlib import Path
from flask import Blueprint, current_app, request, jsonify

from ..common import pull_all


spacedock_metapackage = Blueprint('spacedock_metapackage', __name__) # pylint: disable=invalid-name


# For metapackage hook on SpaceDock
# Handles: https://netkan.ksp-ckan.space/sd/metapackage
# POST form parameters:
# mod_ids: The mods' ID numbers on SpaceDock
@spacedock_metapackage.route('/metapackage', methods=['POST'])
def metapackage_hook():
# Make sure our NetKAN and CKAN-meta repos are up to date
pull_all(current_app.config['repos'])
# Get the relevant netkans
sd_ids = request.form.getlist('mod_ids')
nks = find_netkans(current_app.config['nk_repo'], sd_ids)
if nks:
return jsonify({
'missing': not_found(sd_ids, nks),
'metapackage': mk_metapackage(nks, request.form.get('name'), request.form.get('abstract')),
}), 204
return 'No such modules', 404


def mk_metapackage(netkans, name, abstract):
return {
'spec_version': 'v1.4',
'identifier': 'spacedock_metapackage',
'name': name,
'abstract': abstract,
'kind': 'metapackage',
'version': '1.0',
'license': 'unknown',
'depends': [{'name': nk.identifier} for nk in netkans]
}


def find_netkans(nk_repo, sd_ids):
return [nk for nk in nk_repo.netkans()
if nk.kref_src == 'spacedock' and nk.kref_id in sd_ids]


def not_found(sd_ids, netkans):
nk_ids = {nk.kref_id for nk in netkans}
return [i for i in sd_ids if i not in nk_ids]

0 comments on commit 8e36588

Please sign in to comment.