Coverage for webapp/endpoints/builds.py: 100%
36 statements
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-26 22:06 +0000
« prev ^ index » next coverage.py v7.10.5, created at 2025-08-26 22:06 +0000
1# Standard library
2import os
4# Packages
5import flask
6from canonicalwebteam.store_api.dashboard import Dashboard
9# Local
10from webapp.helpers import api_publisher_session, launchpad
11from webapp.api.github import GitHub
12from webapp.decorators import login_required
14GITHUB_SNAPCRAFT_USER_TOKEN = os.getenv("GITHUB_SNAPCRAFT_USER_TOKEN")
15GITHUB_WEBHOOK_HOST_URL = os.getenv("GITHUB_WEBHOOK_HOST_URL")
16BUILDS_PER_PAGE = 15
17dashboard = Dashboard(api_publisher_session)
20@login_required
21def get_snap_repo(snap_name):
22 res = {"message": "", "success": True}
23 data = {"github_orgs": [], "github_repository": None, "github_user": None}
25 details = dashboard.get_snap_info(flask.session, snap_name)
27 # API call to make users without needed permissions refresh the session
28 # Users needs package_upload_request permission to use this feature
29 dashboard.get_package_upload_macaroon(
30 session=flask.session, snap_name=snap_name, channels=["edge"]
31 )
33 # Get built snap in launchpad with this store name
34 lp_snap = launchpad.get_snap_by_store_name(details["snap_name"])
36 if lp_snap:
37 # In this case we can use the GitHub user account or
38 # the Snapcraft GitHub user to check the snapcraft.yaml
39 github = GitHub(
40 flask.session.get(
41 "github_auth_secret", GITHUB_SNAPCRAFT_USER_TOKEN
42 )
43 )
45 # Git repository without GitHub hostname
46 data["github_repository"] = lp_snap["git_repository_url"][19:]
47 github_owner, github_repo = data["github_repository"].split("/")
49 if not github.check_if_repo_exists(github_owner, github_repo):
50 data["success"] = False
51 data["message"] = "This app has been revoked"
53 if github.get_user():
54 data["github_user"] = github.get_user()
55 data["github_orgs"] = github.get_orgs()
57 else:
58 data["github_repository"] = None
59 github = GitHub(flask.session.get("github_auth_secret"))
61 if github.get_user():
62 data["github_user"] = github.get_user()
63 data["github_orgs"] = github.get_orgs()
64 else:
65 data["success"] = False
66 data["message"] = "Unauthorized"
68 res["data"] = data
70 return flask.jsonify(res)