-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtry_push.py
More file actions
182 lines (154 loc) · 5.63 KB
/
try_push.py
File metadata and controls
182 lines (154 loc) · 5.63 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
176
177
178
179
180
181
182
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import base64
import binascii
import enum
import io
import logging
from connexion import ProblemException
from flask import (
current_app,
g,
)
from landoapi import auth
from landoapi.hgexports import (
BugReferencesCheck,
GitPatchHelper,
HgPatchHelper,
PatchCollectionAssessor,
PatchHelper,
PreventSymlinksCheck,
)
from landoapi.models.landing_job import (
LandingJobStatus,
add_job_with_revisions,
)
from landoapi.models.revisions import Revision
from landoapi.repos import (
Repo,
get_repos_for_env,
)
logger = logging.getLogger(__name__)
@enum.unique
class PatchFormat(enum.Enum):
"""Enumeration of the acceptable types of patches."""
GitFormatPatch = "git-format-patch"
HgExport = "hgexport"
PATCH_HELPER_MAPPING = {
PatchFormat.GitFormatPatch: GitPatchHelper,
PatchFormat.HgExport: HgPatchHelper,
}
def build_revision_from_patch_helper(helper: PatchHelper, repo: Repo) -> Revision:
author, email = helper.parse_author_information()
timestamp = helper.get_timestamp()
commit_message = helper.get_commit_description()
if not commit_message:
raise ValueError("Patch does not have a commit description.")
raw_diff = helper.get_diff()
return Revision.new_from_patch(
raw_diff=raw_diff,
patch_data={
"author_name": author,
"author_email": email,
"commit_message": commit_message,
"timestamp": timestamp,
},
)
def decode_json_patch_to_text(patch: str) -> str:
"""Decode from the base64 encoded patch to `str`."""
try:
return base64.b64decode(patch.encode("ascii")).decode("utf-8")
except binascii.Error:
raise ProblemException(
400,
"Patch decoding error.",
"A patch could not be decoded from base64.",
type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
)
def parse_revisions_from_request(
patches: list[str], patch_format: PatchFormat, repo: Repo
) -> list[Revision]:
"""Convert a set of base64 encoded patches to `Revision` objects."""
patches_io = (io.StringIO(decode_json_patch_to_text(patch)) for patch in patches)
try:
patch_helpers = [
PATCH_HELPER_MAPPING[patch_format](patch) for patch in patches_io
]
except ValueError as exc:
raise ProblemException(
400,
"Improper patch format.",
f"Patch does not match expected format `{patch_format.value}`: {str(exc)}",
type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
)
try:
errors = PatchCollectionAssessor(
patch_helpers=patch_helpers,
).run_patch_collection_checks(
patch_collection_checks=[BugReferencesCheck],
patch_checks=[PreventSymlinksCheck],
)
except ValueError as exc:
raise ProblemException(
400,
"Error running checks on patch collection.",
f"Error running checks on patch collection: {str(exc)}",
type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
)
if errors:
bulleted_errors = "\n - ".join(errors)
error_message = f"Patch failed checks:\n\n - {bulleted_errors}"
raise ProblemException(
400,
"Errors found in pre-submission patch checks.",
error_message,
type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
)
try:
return [
build_revision_from_patch_helper(patch_helper, repo)
for patch_helper in patch_helpers
]
except ValueError as exc:
raise ProblemException(
400,
"Improper patch format.",
f"Patch does not match expected format `{patch_format.value}`: {str(exc)}",
type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400",
)
@auth.require_auth0(scopes=("openid", "lando", "profile", "email"), userinfo=True)
# Re-enable this check once our Auth0 instance returns group membership for access
# tokens granted via the Device Authorization flow.
# @auth.enforce_request_scm_level(SCM_LEVEL_1)
def post_patches(data: dict):
base_commit = data["base_commit"]
base_commit_format = data.get("base_commit_vcs", "hg")
patches = data["patches"]
patch_format = PatchFormat(data["patch_format"])
environment_repos = get_repos_for_env(current_app.config.get("ENVIRONMENT"))
try_repo = environment_repos.get(data.get("try_repo"), "try")
if not try_repo:
raise ProblemException(
500,
"Could not find a `try` repo to submit to.",
"Could not find a `try` repo to submit to.",
type="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500",
)
# Add a landing job for this try push.
ldap_username = g.auth0_user.email
revisions = parse_revisions_from_request(patches, patch_format, try_repo)
job = add_job_with_revisions(
revisions,
repository_name=try_repo.short_name,
repository_url=try_repo.url,
requester_email=ldap_username,
status=LandingJobStatus.SUBMITTED,
target_commit_hash=base_commit,
target_commit_hash_vcs=base_commit_format,
)
logger.info(
f"Created try landing job {job.id} with {len(revisions)} "
f"changesets against {base_commit} for {ldap_username}."
)
return {"id": job.id}, 201