forked from canonical/snapcraft.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilds.py
More file actions
175 lines (144 loc) · 5.45 KB
/
builds.py
File metadata and controls
175 lines (144 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Standard library
import os
# Packages
import flask
from canonicalwebteam.store_api.dashboard import Dashboard
from requests.exceptions import HTTPError
# Local
from webapp.helpers import api_publisher_session, launchpad
from webapp.api.github import GitHub, InvalidYAML
from webapp.decorators import login_required
from cache.cache_utility import redis_cache
GITHUB_WEBHOOK_HOST_URL = os.getenv("GITHUB_WEBHOOK_HOST_URL")
dashboard = Dashboard(api_publisher_session)
@login_required
def get_snap_build_page(snap_name, build_id):
# If this fails, the page will 404
dashboard.get_snap_info(flask.session, snap_name)
return flask.render_template(
"store/publisher.html", snap_name=snap_name, build_id=build_id
)
def validate_repo(github_token, snap_name, gh_owner, gh_repo):
github = GitHub(github_token)
result = {"success": True, "data": None, "error": None}
yaml_location = github.get_snapcraft_yaml_location(gh_owner, gh_repo)
default_branch = github.get_default_branch(gh_owner, gh_repo)
# The snapcraft.yaml is not present
if not yaml_location:
result["data"] = {"default_branch": default_branch}
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."
),
}
else:
try:
gh_snap_name = github.get_snapcraft_yaml_data(
gh_owner, gh_repo
).get("name")
# The property name inside the yaml file doesn't match the snap
if gh_snap_name is not None and 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
@login_required
def get_validate_repo(snap_name):
snap_info_key = f"snap_info:{snap_name}"
details = redis_cache.get(snap_info_key, expected_type=dict)
if not details:
details = dashboard.get_snap_info(flask.session, snap_name)
redis_cache.set(snap_info_key, details, ttl=3600)
owner, repo = flask.request.args.get("repo").split("/")
return flask.jsonify(
validate_repo(
flask.session.get("github_auth_secret"),
details["snap_name"],
owner,
repo,
)
)
@login_required
def post_build(snap_name):
# Don't allow builds from no contributors
account_snaps = dashboard.get_account_snaps(flask.session)
if snap_name not in account_snaps:
return flask.jsonify(
{
"success": False,
"error": {
"type": "FORBIDDEN",
"message": "You are not allowed to request "
"builds for this snap",
},
}
)
try:
if launchpad.is_snap_building(snap_name):
launchpad.cancel_snap_builds(snap_name)
build_id = launchpad.build_snap(snap_name)
except HTTPError as e:
return flask.jsonify(
{
"success": False,
"error": {
"message": "An error happened building "
"this snap, please try again."
},
"details": e.response.text,
"status_code": e.response.status_code,
}
)
return flask.jsonify({"success": True, "build_id": build_id})
@login_required
def post_disconnect_repo(snap_name):
snap_info_key = f"snap_info:{snap_name}"
details = redis_cache.get(snap_info_key, expected_type=dict)
if not details:
details = dashboard.get_snap_info(flask.session, snap_name)
redis_cache.set(snap_info_key, details, ttl=3600)
lp_snap = launchpad.get_snap_by_store_name(snap_name)
launchpad.delete_snap(details["snap_name"])
# Try to remove the GitHub webhook if possible
if flask.session.get("github_auth_secret"):
github = GitHub(flask.session.get("github_auth_secret"))
try:
gh_owner, gh_repo = lp_snap["git_repository_url"][19:].split("/")
old_hook = github.get_hook_by_url(
gh_owner,
gh_repo,
f"{GITHUB_WEBHOOK_HOST_URL}api/{snap_name}/webhook/notify",
)
if old_hook:
github.remove_hook(
gh_owner,
gh_repo,
old_hook["id"],
)
except HTTPError:
pass
return flask.redirect(
flask.url_for(".get_snap_builds", snap_name=snap_name)
)