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

1# Standard library 

2import os 

3 

4# Packages 

5import flask 

6from canonicalwebteam.store_api.dashboard import Dashboard 

7 

8 

9# Local 

10from webapp.helpers import api_publisher_session, launchpad 

11from webapp.api.github import GitHub 

12from webapp.decorators import login_required 

13 

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) 

18 

19 

20@login_required 

21def get_snap_repo(snap_name): 

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

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

24 

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

26 

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 ) 

32 

33 # Get built snap in launchpad with this store name 

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

35 

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 ) 

44 

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("/") 

48 

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

50 data["success"] = False 

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

52 

53 if github.get_user(): 

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

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

56 

57 else: 

58 data["github_repository"] = None 

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

60 

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" 

67 

68 res["data"] = data 

69 

70 return flask.jsonify(res)