Esempio n. 1
0
def get_snap_builds(snap_name):
    try:
        details = api.get_snap_info(snap_name, flask.session)

        # API call to make users without needed permissions refresh the session
        # Users needs package_upload_request permission to use this feature
        api.get_package_upload_macaroon(session=flask.session,
                                        snap_name=snap_name,
                                        channels=["edge"])
    except ApiResponseErrorList as api_response_error_list:
        if api_response_error_list.status_code == 404:
            return flask.abort(404, "No snap named {}".format(snap_name))
        else:
            return _handle_error_list(api_response_error_list.errors)
    except ApiError as api_error:
        return _handle_error(api_error)

    context = {
        "snap_id": details["snap_id"],
        "snap_name": details["snap_name"],
        "snap_title": details["title"],
        "snap_builds_enabled": False,
        "snap_builds": [],
        "total_builds": 0,
    }

    # Get built snap in launchpad with this store name
    github = GitHub(flask.session.get("github_auth_secret"))
    lp_snap = launchpad.get_snap_by_store_name(details["snap_name"])

    if lp_snap:
        # Git repository without GitHub hostname
        context["github_repository"] = lp_snap["git_repository_url"][19:]
        github_owner, github_repo = context["github_repository"].split("/")

        context["yaml_file_exists"] = github.get_snapcraft_yaml_location(
            github_owner, github_repo)

        if not context["yaml_file_exists"]:
            flask.flash("This repository doesn't contain a snapcraft.yaml",
                        "negative")
        context = get_builds(lp_snap, context, slice(0, BUILDS_PER_PAGE))
    else:
        try:
            context["github_user"] = github.get_user()
        except Unauthorized:
            context["github_user"] = None

        if context["github_user"]:
            context["github_orgs"] = github.get_orgs()

    return flask.render_template("publisher/builds.html", **context)
Esempio n. 2
0
def validate_repo(github_token, snap_name, gh_owner, gh_repo):
    github = GitHub(github_token)
    result = {"success": True}
    yaml_location = github.get_snapcraft_yaml_location(gh_owner, gh_repo)

    # The snapcraft.yaml is not present
    if not yaml_location:
        result["success"] = False
        result["error"] = {
            "type":
            "MISSING_YAML_FILE",
            "message":
            ("Missing snapcraft.yaml: this repo needs a snapcraft.yaml "
             "file, so that Snapcraft can make it buildable, installable "
             "and runnable."),
        }
    # The property name inside the yaml file doesn't match the snap
    else:
        try:
            gh_snap_name = github.get_snapcraft_yaml_name(gh_owner, gh_repo)

            if gh_snap_name != snap_name:
                result["success"] = False
                result["error"] = {
                    "type":
                    "SNAP_NAME_DOES_NOT_MATCH",
                    "message":
                    ("Name mismatch: the snapcraft.yaml uses the snap "
                     f'name "{gh_snap_name}", but you\'ve registered'
                     f' the name "{snap_name}". Update your '
                     "snapcraft.yaml to continue."),
                    "yaml_location":
                    yaml_location,
                    "gh_snap_name":
                    gh_snap_name,
                }
        except InvalidYAML:
            result["success"] = False
            result["error"] = {
                "type":
                "INVALID_YAML_FILE",
                "message":
                ("Invalid snapcraft.yaml: there was an issue parsing the "
                 f"snapcraft.yaml for {snap_name}."),
            }

    return result
Esempio n. 3
0
def get_snap_builds(snap_name):
    try:
        details = publisher_api.get_snap_info(snap_name, flask.session)

        # API call to make users without needed permissions refresh the session
        # Users needs package_upload_request permission to use this feature
        publisher_api.get_package_upload_macaroon(session=flask.session,
                                                  snap_name=snap_name,
                                                  channels=["edge"])
    except StoreApiResponseErrorList as api_response_error_list:
        if api_response_error_list.status_code == 404:
            return flask.abort(404, "No snap named {}".format(snap_name))
        else:
            return _handle_error_list(api_response_error_list.errors)
    except (StoreApiError, ApiError) as api_error:
        return _handle_error(api_error)

    context = {
        "snap_id": details["snap_id"],
        "snap_name": details["snap_name"],
        "snap_title": details["title"],
        "snap_builds_enabled": False,
        "snap_builds": [],
        "total_builds": 0,
    }

    # Get built snap in launchpad with this store name
    lp_snap = launchpad.get_snap_by_store_name(details["snap_name"])

    if lp_snap:
        # In this case we can use the GitHub user account or
        # the Snapcraft GitHub user to check the snapcraft.yaml
        github = GitHub(
            flask.session.get("github_auth_secret",
                              GITHUB_SNAPCRAFT_USER_TOKEN))

        # Git repository without GitHub hostname
        context["github_repository"] = lp_snap["git_repository_url"][19:]
        github_owner, github_repo = context["github_repository"].split("/")

        context["github_repository_exists"] = github.check_if_repo_exists(
            github_owner, github_repo)

        context["yaml_file_exists"] = github.get_snapcraft_yaml_location(
            github_owner, github_repo)

        context.update(get_builds(lp_snap, slice(0, BUILDS_PER_PAGE)))

        context["snap_builds_enabled"] = bool(context["snap_builds"])
    else:
        github = GitHub(flask.session.get("github_auth_secret"))

        try:
            context["github_user"] = github.get_user()
        except Unauthorized:
            context["github_user"] = None

        if context["github_user"]:
            context["github_orgs"] = github.get_orgs()

    return flask.render_template("publisher/builds.html", **context)