Beispiel #1
0
def get_transformed_commit_info(bb_commit, ref, default_branch, repository_name, lookup_author):
    """ Returns the BitBucket commit information transformed into our own
      payload format.
  """
    try:
        validate(bb_commit, BITBUCKET_COMMIT_INFO_SCHEMA)
    except Exception as exc:
        logger.exception(
            "Exception when validating Bitbucket commit information: %s from %s",
            exc.message,
            bb_commit,
        )
        raise InvalidPayloadException(exc.message)

    commit = JSONPathDict(bb_commit)

    config = SafeDictSetter()
    config["commit"] = commit["node"]
    config["ref"] = ref
    config["default_branch"] = default_branch
    config["git_url"] = "[email protected]:%s.git" % repository_name

    config["commit_info.url"] = _BITBUCKET_COMMIT_URL % (repository_name, commit["node"])
    config["commit_info.message"] = commit["message"]
    config["commit_info.date"] = commit["timestamp"]

    match = _RAW_AUTHOR_REGEX.match(commit["raw_author"])
    if match:
        author = lookup_author(match.group(1))
        author_info = JSONPathDict(author) if author is not None else None
        if author_info:
            config["commit_info.author.username"] = author_info["user.display_name"]
            config["commit_info.author.avatar_url"] = author_info["user.avatar"]

    return config.dict_value()
Beispiel #2
0
def get_transformed_webhook_payload(bb_payload, default_branch=None):
    """
    Returns the BitBucket webhook JSON payload transformed into our own payload format.

    If the bb_payload is not valid, returns None.
    """
    try:
        validate(bb_payload, BITBUCKET_WEBHOOK_PAYLOAD_SCHEMA)
    except Exception as exc:
        logger.exception(
            "Exception when validating Bitbucket webhook payload: %s from %s",
            exc.message,
            bb_payload,
        )
        raise InvalidPayloadException(exc.message)

    payload = JSONPathDict(bb_payload)
    change = payload["push.changes[-1].new"]
    if not change:
        raise SkipRequestException

    is_branch = change["type"] == "branch"
    ref = "refs/heads/" + change[
        "name"] if is_branch else "refs/tags/" + change["name"]

    repository_name = payload["repository.full_name"]
    target = change["target"]

    config = SafeDictSetter()
    config["commit"] = target["hash"]
    config["ref"] = ref
    config["default_branch"] = default_branch
    config["git_url"] = "[email protected]:%s.git" % repository_name

    config["commit_info.url"] = target["links.html.href"] or ""
    config["commit_info.message"] = target["message"]
    config["commit_info.date"] = target["date"]

    config["commit_info.author.username"] = target["author.user.display_name"]
    config["commit_info.author.avatar_url"] = target[
        "author.user.links.avatar.href"]

    config["commit_info.committer.username"] = payload["actor.display_name"]
    config["commit_info.committer.avatar_url"] = payload[
        "actor.links.avatar.href"]
    return config.dict_value()
Beispiel #3
0
def get_transformed_webhook_payload(bb_payload, default_branch=None):
    """ Returns the BitBucket webhook JSON payload transformed into our own payload
      format. If the bb_payload is not valid, returns None.
  """
    try:
        validate(bb_payload, BITBUCKET_WEBHOOK_PAYLOAD_SCHEMA)
    except Exception as exc:
        logger.exception(
            'Exception when validating Bitbucket webhook payload: %s from %s',
            exc.message, bb_payload)
        raise InvalidPayloadException(exc.message)

    payload = JSONPathDict(bb_payload)
    change = payload['push.changes[-1].new']
    if not change:
        raise SkipRequestException

    is_branch = change['type'] == 'branch'
    ref = 'refs/heads/' + change[
        'name'] if is_branch else 'refs/tags/' + change['name']

    repository_name = payload['repository.full_name']
    target = change['target']

    config = SafeDictSetter()
    config['commit'] = target['hash']
    config['ref'] = ref
    config['default_branch'] = default_branch
    config['git_url'] = '[email protected]:%s.git' % repository_name

    config['commit_info.url'] = target['links.html.href'] or ''
    config['commit_info.message'] = target['message']
    config['commit_info.date'] = target['date']

    config['commit_info.author.username'] = target['author.user.display_name']
    config['commit_info.author.avatar_url'] = target[
        'author.user.links.avatar.href']

    config['commit_info.committer.username'] = payload['actor.display_name']
    config['commit_info.committer.avatar_url'] = payload[
        'actor.links.avatar.href']
    return config.dict_value()
Beispiel #4
0
def get_transformed_webhook_payload(gh_payload, default_branch=None, lookup_user=None):
    """
    Returns the GitHub webhook JSON payload transformed into our own payload format.

    If the gh_payload is not valid, returns None.
    """
    try:
        validate(gh_payload, GITHUB_WEBHOOK_PAYLOAD_SCHEMA)
    except Exception as exc:
        raise InvalidPayloadException(exc.message)

    payload = JSONPathDict(gh_payload)

    if payload["head_commit"] is None:
        raise SkipRequestException

    config = SafeDictSetter()
    config["commit"] = payload["head_commit.id"]
    config["ref"] = payload["ref"]
    config["default_branch"] = payload["repository.default_branch"] or default_branch
    config["git_url"] = payload["repository.ssh_url"]

    config["commit_info.url"] = payload["head_commit.url"]
    config["commit_info.message"] = payload["head_commit.message"]
    config["commit_info.date"] = payload["head_commit.timestamp"]

    config["commit_info.author.username"] = payload["head_commit.author.username"]
    config["commit_info.author.url"] = payload.get("head_commit.author.html_url")
    config["commit_info.author.avatar_url"] = payload.get("head_commit.author.avatar_url")

    config["commit_info.committer.username"] = payload.get("head_commit.committer.username")
    config["commit_info.committer.url"] = payload.get("head_commit.committer.html_url")
    config["commit_info.committer.avatar_url"] = payload.get("head_commit.committer.avatar_url")

    # Note: GitHub doesn't always return the extra information for users, so we do the lookup
    # manually if possible.
    if (
        lookup_user
        and not payload.get("head_commit.author.html_url")
        and payload.get("head_commit.author.username")
    ):
        author_info = lookup_user(payload["head_commit.author.username"])
        if author_info:
            config["commit_info.author.url"] = author_info["html_url"]
            config["commit_info.author.avatar_url"] = author_info["avatar_url"]

    if (
        lookup_user
        and payload.get("head_commit.committer.username")
        and not payload.get("head_commit.committer.html_url")
    ):
        committer_info = lookup_user(payload["head_commit.committer.username"])
        if committer_info:
            config["commit_info.committer.url"] = committer_info["html_url"]
            config["commit_info.committer.avatar_url"] = committer_info["avatar_url"]

    return config.dict_value()
Beispiel #5
0
def get_transformed_webhook_payload(
    gl_payload, default_branch=None, lookup_user=None, lookup_commit=None
):
    """
    Returns the Gitlab webhook JSON payload transformed into our own payload format.

    If the gl_payload is not valid, returns None.
    """
    try:
        validate(gl_payload, GITLAB_WEBHOOK_PAYLOAD_SCHEMA)
    except Exception as exc:
        raise InvalidPayloadException(exc.message)

    payload = JSONPathDict(gl_payload)

    if payload["object_kind"] != "push" and payload["object_kind"] != "tag_push":
        # Unknown kind of webhook.
        raise SkipRequestException

    # Check for empty commits. The commits list will be empty if the branch is deleted.
    commits = payload["commits"]
    if payload["object_kind"] == "push" and not commits:
        raise SkipRequestException

    # Check for missing commit information.
    commit_sha = payload["checkout_sha"] or payload["after"]
    if commit_sha is None or commit_sha == "0000000000000000000000000000000000000000":
        raise SkipRequestException

    config = SafeDictSetter()
    config["commit"] = commit_sha
    config["ref"] = payload["ref"]
    config["default_branch"] = default_branch
    config["git_url"] = payload["repository.git_ssh_url"]

    found_commit = JSONPathDict({})
    if payload["object_kind"] == "push" or payload["object_kind"] == "tag_push":
        # Find the commit associated with the checkout_sha. Gitlab doesn't (necessary) send this in
        # any order, so we cannot simply index into the commits list.
        found_commit = None
        if commits is not None:
            for commit in commits:
                if commit["id"] == payload["checkout_sha"]:
                    found_commit = JSONPathDict(commit)
                    break

        if found_commit is None and lookup_commit:
            checkout_sha = payload["checkout_sha"] or payload["after"]
            found_commit_info = lookup_commit(payload["project_id"], checkout_sha)
            found_commit = JSONPathDict(dict(found_commit_info) if found_commit_info else {})

        if found_commit is None:
            raise SkipRequestException

    config["commit_info.url"] = found_commit["url"]
    config["commit_info.message"] = found_commit["message"]
    config["commit_info.date"] = found_commit["timestamp"]

    # Note: Gitlab does not send full user information with the payload, so we have to
    # (optionally) look it up.
    author_email = found_commit["author.email"] or found_commit["author_email"]
    if lookup_user and author_email:
        author_info = lookup_user(author_email)
        if author_info:
            config["commit_info.author.username"] = author_info["username"]
            config["commit_info.author.url"] = author_info["html_url"]
            config["commit_info.author.avatar_url"] = author_info["avatar_url"]

    return config.dict_value()
Beispiel #6
0
def test_volume_version_easy(vcfg, expected):
    output = DockerV1ToACIManifestTranslator._build_volumes(JSONPathDict(vcfg))
    assert output == expected
Beispiel #7
0
    def build_manifest(tag, manifest, synthetic_image_id):
        """
        Builds an ACI manifest of an existing repository image.
        """
        docker_layer_data = JSONPathDict(
            json.loads(manifest.leaf_layer.raw_v1_metadata))
        config = docker_layer_data["config"] or JSONPathDict({})

        namespace = tag.repository.namespace_name
        repo_name = tag.repository.name
        source_url = "%s://%s/%s/%s:%s" % (
            app.config["PREFERRED_URL_SCHEME"],
            app.config["SERVER_HOSTNAME"],
            namespace,
            repo_name,
            tag.name,
        )

        # ACI requires that the execution command be absolutely referenced. Therefore, if we find
        # a relative command, we give it as an argument to /bin/sh to resolve and execute for us.
        entrypoint = config["Entrypoint"] or []
        exec_path = entrypoint + (config["Cmd"] or [])
        if exec_path and not exec_path[0].startswith("/"):
            exec_path = ["/bin/sh", "-c", '""%s""' % " ".join(exec_path)]

        # TODO: ACI doesn't support : in the name, so remove any ports.
        hostname = app.config["SERVER_HOSTNAME"]
        hostname = hostname.split(":", 1)[0]

        # Calculate the environment variables.
        docker_env_vars = config.get("Env") or []
        env_vars = []
        for var in docker_env_vars:
            pieces = var.split("=")
            if len(pieces) != 2:
                continue

            env_vars.append(pieces)

        manifest = {
            "acKind":
            "ImageManifest",
            "acVersion":
            "0.6.1",
            "name":
            "%s/%s/%s" %
            (hostname.lower(), namespace.lower(), repo_name.lower()),
            "labels": [
                {
                    "name": "version",
                    "value": tag.name,
                },
                {
                    "name": "arch",
                    "value": docker_layer_data.get("architecture") or "amd64"
                },
                {
                    "name": "os",
                    "value": docker_layer_data.get("os") or "linux"
                },
            ],
            "app": {
                "exec":
                exec_path,
                # Below, `or 'root'` is required to replace empty string from Dockerfiles.
                "user":
                config.get("User") or "root",
                "group":
                config.get("Group") or "root",
                "eventHandlers": [],
                "workingDirectory":
                config.get("WorkingDir") or "/",
                "environment": [{
                    "name": key,
                    "value": value
                } for (key, value) in env_vars],
                "isolators":
                DockerV1ToACIManifestTranslator._build_isolators(config),
                "mountPoints":
                DockerV1ToACIManifestTranslator._build_volumes(config),
                "ports":
                DockerV1ToACIManifestTranslator._build_ports(config),
                "annotations": [
                    {
                        "name": "created",
                        "value": docker_layer_data.get("created") or ""
                    },
                    {
                        "name": "homepage",
                        "value": source_url
                    },
                    {
                        "name": "quay.io/derived-image",
                        "value": synthetic_image_id
                    },
                ],
            },
        }

        return manifest