def get_update_gh_webhooks(snap_name): try: details = publisher_api.get_snap_info(snap_name, flask.session) 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) lp_snap = launchpad.get_snap_by_store_name(details["snap_name"]) if not lp_snap: flask.flash( "This snap is not linked with a GitHub repository", "negative" ) return flask.redirect( flask.url_for(".get_settings", snap_name=snap_name) ) github = GitHub(flask.session.get("github_auth_secret")) try: github.get_user() except Unauthorized: return flask.redirect(f"/github/auth?back={flask.request.path}") gh_link = lp_snap["git_repository_url"][19:] gh_owner, gh_repo = gh_link.split("/") # Remove old BSI webhook if present old_url = f"https://build.snapcraft.io/{gh_owner}/{gh_repo}/webhook/notify" old_hook = github.get_hook_by_url(gh_owner, gh_repo, old_url) if old_hook: github.remove_hook( gh_owner, gh_repo, old_hook["id"], ) # Remove current hook github_hook_url = f"{GITHUB_WEBHOOK_HOST_URL}{snap_name}/webhook/notify" snapcraft_hook = github.get_hook_by_url(gh_owner, gh_repo, github_hook_url) if snapcraft_hook: github.remove_hook( gh_owner, gh_repo, snapcraft_hook["id"], ) # Create webhook in the repo github.create_hook(gh_owner, gh_repo, github_hook_url) flask.flash("The webhook has been created successfully", "positive") return flask.redirect(flask.url_for(".get_settings", snap_name=snap_name))
def post_snap_builds(snap_name): try: details = api.get_snap_info(snap_name, flask.session) 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) # Don't allow changes from Admins that are no contributors account_snaps = api.get_account_snaps(flask.session) if snap_name not in account_snaps: flask.flash("You do not have permissions to modify this Snap", "negative") return flask.redirect( flask.url_for(".get_snap_builds", snap_name=snap_name)) redirect_url = flask.url_for(".get_snap_builds", snap_name=snap_name) # Get built snap in launchpad with this store name github = GitHub(flask.session.get("github_auth_secret")) owner, repo = flask.request.form.get("github_repository").split("/") if not github.check_permissions_over_repo(owner, repo): flask.flash( "Your GitHub account doesn't have permissions in the repository", "negative", ) return flask.redirect(redirect_url) repo_validation = validate_repo(flask.session.get("github_auth_secret"), snap_name, owner, repo) if not repo_validation["success"]: flask.flash(repo_validation["error"]["message"], "negative") return flask.redirect(redirect_url) lp_snap = launchpad.get_snap_by_store_name(details["snap_name"]) git_url = f"https://github.com/{owner}/{repo}" if not lp_snap: lp_snap_name = md5(git_url.encode("UTF-8")).hexdigest() try: repo_exist = launchpad.get_snap(lp_snap_name) except HTTPError as e: if e.response.status_code == 404: repo_exist = False else: raise e if repo_exist: # The user registered the repo in BSI but didn't register a name # We can remove it and continue with the normal process if not repo_exist["store_name"]: # This conditional should be removed when issue 2657 is solved launchpad._request(path=repo_exist["self_link"][32:], method="DELETE") else: flask.flash( "The specified repository is being used by another snap:" f" {repo_exist['store_name']}", "negative", ) return flask.redirect(redirect_url) macaroon = api.get_package_upload_macaroon(session=flask.session, snap_name=snap_name, channels=["edge" ])["macaroon"] launchpad.create_snap(snap_name, git_url, macaroon) flask.flash("The GitHub repository was linked correctly.", "positive") # Create webhook in the repo, it should also trigger the first build github_hook_url = f"https://snapcraft.io/{snap_name}/webhook/notify" try: hook = github.get_hook_by_url(owner, repo, github_hook_url) # We create the webhook if doesn't exist already in this repo if not hook: github.create_hook(owner, repo, github_hook_url) except HTTPError: flask.flash( "The GitHub Webhook could not be created. " "Please trigger a new build manually.", "caution", ) elif lp_snap["git_repository_url"] != git_url: # In the future, create a new record, delete the old one raise AttributeError( f"Snap {snap_name} already has a build repository associated") return flask.redirect(redirect_url)
def post_snap_builds(snap_name): try: details = api.get_snap_info(snap_name, flask.session) 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) redirect_url = flask.url_for(".get_snap_builds", snap_name=snap_name) # Get built snap in launchpad with this store name github = GitHub(flask.session.get("github_auth_secret")) owner, repo = flask.request.form.get("github_repository").split("/") if not github.check_permissions_over_repo(owner, repo): flask.flash( "Your GitHub account doesn't have permissions in the repository", "negative", ) return flask.redirect(redirect_url) repo_validation = validate_repo(flask.session.get("github_auth_secret"), snap_name, owner, repo) if not repo_validation["success"]: flask.flash(repo_validation["error"]["message"], "negative") return flask.redirect(redirect_url) lp_snap = launchpad.get_snap_by_store_name(details["snap_name"]) git_url = f"https://github.com/{owner}/{repo}" if not lp_snap: lp_snap_name = md5(git_url.encode("UTF-8")).hexdigest() try: repo_exist = bool(launchpad.get_snap(lp_snap_name)) except HTTPError as e: if e.response.status_code == 404: repo_exist = False else: raise e if repo_exist: error_msg = ( "The specified GitHub repository is being used by another snap" ) flask.flash(error_msg, "negative") return flask.redirect(redirect_url) macaroon = api.get_package_upload_macaroon(session=flask.session, snap_name=snap_name, channels=["edge" ])["macaroon"] launchpad.create_snap(snap_name, git_url, macaroon) # Create webhook in the repo, it should also trigger the first build github.create_hook(owner, repo, f"https://snapcraft.io/{snap_name}/webhook/notify") flask.flash("The GitHub repository was linked correctly.", "positive") elif lp_snap["git_repository_url"] != git_url: # In the future, create a new record, delete the old one raise AttributeError( f"Snap {snap_name} already has a build repository associated") return flask.redirect(redirect_url)