Exemplo n.º 1
0
def webhook_impl():
    json = request.json

    # Get package
    gitlab_url = json["project"]["web_url"].replace("https://",
                                                    "").replace("http://", "")
    package = Package.query.filter(
        Package.repo.ilike("%{}%".format(gitlab_url))).first()
    if package is None:
        return error(
            400,
            "Could not find package, did you set the VCS repo in CDB correctly? Expected {}"
            .format(gitlab_url))

    # 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 token is None:
        return error(403, "Invalid authentication")

    if not package.checkPerm(token.owner, Permission.APPROVE_RELEASE):
        return error(403, "You do not have the permission to approve releases")

    #
    # Check event
    #

    event = json["event_name"]
    if event == "push":
        ref = json["after"]
        title = ref[:5]

        branch = json["ref"].replace("refs/heads/", "")
        if branch not in ["master", "main"]:
            return jsonify({
                "success":
                False,
                "message":
                "Webhook ignored, as it's not on the master/main branch"
            })

    elif event == "tag_push":
        ref = json["ref"]
        title = ref.replace("refs/tags/", "")
    else:
        return error(
            400,
            "Unsupported event. Only 'push' and 'tag_push' are supported.")

    #
    # Perform release
    #

    if package.releases.filter_by(commit_hash=ref).count() > 0:
        return

    return api_create_vcs_release(token, package, title, ref, reason="Webhook")
Exemplo n.º 2
0
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]
	elif event == "tag_push":
		ref = json["ref"]
		title = ref.replace("refs/tags/", "")
	else:
		return error(400, "Unsupported event. Only 'push' and 'tag_push' are supported.")

	#
	# Perform release
	#

	return handleCreateRelease(token, package, title, ref)
Exemplo n.º 3
0
def webhook():
	json = request.json

	# Get package
	github_url = "github.com/" + json["repository"]["full_name"]
	package = Package.query.filter(Package.repo.ilike("%{}%".format(github_url))).first()
	if package is None:
		return error(400, "Could not find package, did you set the VCS repo in CDB correctly? Expected {}".format(github_url))

	# Get all tokens for package
	tokens_query = APIToken.query.filter(or_(APIToken.package==package,
			and_(APIToken.package==None, APIToken.owner==package.author)))

	possible_tokens = tokens_query.all()
	actual_token = None

	#
	# Check signature
	#

	header_signature = request.headers.get('X-Hub-Signature')
	if header_signature is None:
		return error(403, "Expected payload signature")

	sha_name, signature = header_signature.split('=')
	if sha_name != 'sha1':
		return error(403, "Expected SHA1 payload signature")

	for token in possible_tokens:
		mac = hmac.new(token.access_token.encode("utf-8"), msg=request.data, digestmod='sha1')

		if hmac.compare_digest(str(mac.hexdigest()), signature):
			actual_token = token
			break

	if actual_token is None:
		return error(403, "Invalid authentication, couldn't validate API token")

	if not package.checkPerm(actual_token.owner, Permission.APPROVE_RELEASE):
		return error(403, "You do not have the permission to approve releases")

	#
	# Check event
	#

	event = request.headers.get("X-GitHub-Event")
	if event == "push":
		ref = json["after"]
		title = json["head_commit"]["message"].partition("\n")[0]
	elif event == "create" and json["ref_type"] == "tag":
		ref = json["ref"]
		title = ref
	elif event == "ping":
		return jsonify({ "success": True, "message": "Ping successful" })
	else:
		return error(400, "Unsupported event. Only 'push', `create:tag`, and 'ping' are supported.")

	#
	# Perform release
	#

	return handleCreateRelease(actual_token, package, title, ref)
Exemplo n.º 4
0
def webhook():
    try:
        return webhook_impl()
    except KeyError as err:
        return error(400, "Missing field: {}".format(err.args[0]))