Coverage for webapp/endpoints/builds.py: 100%

36 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-15 22:43 +0000

1# Standard library 

2import os 

3 

4# Packages 

5import flask 

6from canonicalwebteam.store_api.dashboard import Dashboard 

7 

8# Local 

9from webapp.helpers import api_publisher_session, launchpad 

10from webapp.api.github import GitHub 

11from webapp.decorators import login_required 

12 

13GITHUB_SNAPCRAFT_USER_TOKEN = os.getenv("GITHUB_SNAPCRAFT_USER_TOKEN") 

14GITHUB_WEBHOOK_HOST_URL = os.getenv("GITHUB_WEBHOOK_HOST_URL") 

15BUILDS_PER_PAGE = 15 

16dashboard = Dashboard(api_publisher_session) 

17 

18 

19@login_required 

20def get_snap_repo(snap_name): 

21 res = {"message": "", "success": True} 

22 data = {"github_orgs": [], "github_repository": None, "github_user": None} 

23 

24 details = dashboard.get_snap_info(flask.session, snap_name) 

25 

26 # API call to make users without needed permissions refresh the session 

27 # Users needs package_upload_request permission to use this feature 

28 dashboard.get_package_upload_macaroon( 

29 session=flask.session, snap_name=snap_name, channels=["edge"] 

30 ) 

31 

32 # Get built snap in launchpad with this store name 

33 lp_snap = launchpad.get_snap_by_store_name(details["snap_name"]) 

34 

35 if lp_snap: 

36 # In this case we can use the GitHub user account or 

37 # the Snapcraft GitHub user to check the snapcraft.yaml 

38 github = GitHub( 

39 flask.session.get( 

40 "github_auth_secret", GITHUB_SNAPCRAFT_USER_TOKEN 

41 ) 

42 ) 

43 

44 # Git repository without GitHub hostname 

45 data["github_repository"] = lp_snap["git_repository_url"][19:] 

46 github_owner, github_repo = data["github_repository"].split("/") 

47 

48 if not github.check_if_repo_exists(github_owner, github_repo): 

49 data["success"] = False 

50 data["message"] = "This app has been revoked" 

51 

52 if github.get_user(): 

53 data["github_user"] = github.get_user() 

54 data["github_orgs"] = github.get_orgs() 

55 

56 else: 

57 data["github_repository"] = None 

58 github = GitHub(flask.session.get("github_auth_secret")) 

59 

60 if github.get_user(): 

61 data["github_user"] = github.get_user() 

62 data["github_orgs"] = github.get_orgs() 

63 else: 

64 data["success"] = False 

65 data["message"] = "Unauthorized" 

66 

67 res["data"] = data 

68 

69 return flask.jsonify(res)