-
Notifications
You must be signed in to change notification settings - Fork 76
/
pr_info.py
320 lines (259 loc) · 11.3 KB
/
pr_info.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
from datetime import datetime
from enum import StrEnum
from typing import Annotated, Any, Literal, Self
from pydantic import BeforeValidator, TypeAdapter, field_validator, model_validator
from conda_forge_tick.models.common import (
CondaVersionString,
NoneIsEmptyDict,
PrJsonLazyJsonReference,
StrictBaseModel,
before_validator_ensure_dict,
)
from conda_forge_tick.models.pr_json import PullRequestDataValid, PullRequestInfoSpecial
def remove_azure_error(value: Any) -> str:
if not isinstance(value, str):
raise ValueError("value is not a string.")
if not value.startswith("No azure token. Create a token and\nput it"):
raise ValueError("This is not an Azure token error.")
return list(filter(None, value.split("\n")))[-1]
CondaVersionStringWithAzureTokenError = Annotated[
CondaVersionString, BeforeValidator(remove_azure_error)
]
"""
Extracts a version from a string that contains an Azure token error.
"""
def one_plus_to_one(value: Any) -> int:
if value != "1+":
raise ValueError("This is not '1+'.")
return 1
OnePlusToOne = Annotated[int, BeforeValidator(one_plus_to_one)]
"""
Receives a string "1+" and converts it into 1.
This is just for rolling back the effects of a typo in the aws_c_http0627 migration.
When this Pydantic model is used in production, serialize and deserialize the entire graph data to remove the error.
After that, this type can be removed.
"""
class MigratorName(StrEnum):
"""
Each value here corresponds to a subclass of migrators.core.Migrator in the codebase.
"""
VERSION = "Version"
ARCH_REBUILD = "ArchRebuild"
OSX_ARM = "OSXArm"
MIGRATION_YAML = "MigrationYaml"
REBUILD = "Rebuild"
BLAS_REBUILD = "BlasRebuild"
R_BASE_REBUILD = "RBaseRebuild"
G_FORTRAN_OSX_REBUILD = "GFortranOSXRebuild"
REPLACEMENT = "Replacement"
MATPLOTLIB_BASE = "MatplotlibBase"
REBUILD_BROKEN = "RebuildBroken"
MIGRATION_YAML_CREATOR = "MigrationYamlCreator"
"""
Only operates on the conda-forge-pinning feedstock and updates the pinning version of packages.
"""
NOARCH_PYTHON_MIN_MIGRATOR = "NoarchPythonMinMigrator"
JS = "JS"
"""
This legacy migrator for JavaScript technically exists in the codebase, but does not appear in the graph.
"""
NOARCH = "Noarch"
NOARCH_R = "NoarchR"
"""
This legacy migrator R noarch packages technically exists in the codebase, but does not appear in the graph.
"""
PINNING = "Pinning"
COMPILER_REBUILD = "CompilerRebuild"
"""
This migrator is no longer present in the codebase but still appears in the graph.
"""
COMPILER = "Compiler"
"""
This migrator is no longer present in the codebase but still appears in the graph.
"""
OPEN_SSL_REBUILD = "OpenSSLRebuild"
"""
This migrator is no longer present in the codebase but still appears in the graph.
"""
class MigrationPullRequestData(StrictBaseModel):
"""
Sometimes, this object is called `migrator_uid` or `MigrationUidTypedDict` in the code.
"""
bot_rerun: bool | datetime
"""
If the migration was rerun because the bot-rerun label was added to the PR, this is set to the timestamp of the
rerun. If no rerun was performed, this is False. There are some legacy PRs where is is True, indicating an absent
timestamp of a rerun that was performed.
"""
migrator_name: MigratorName
"""
The name of the migrator that created the PR. As opposed to `pre_pr_migrator_status` and `pre_pr_migrator_attempts`
below, the names of migrators appear here with spaces and not necessarily in lowercase.
"""
migrator_version: int
"""
The version of the migrator that created the PR.
"""
version: CondaVersionString | None = None
"""
If migrator_name is "Version", this fields contains the version that was migrated to.
Otherwise, this field is None.
"""
@model_validator(mode="after")
def check_version(self) -> Self:
if self.version is None and self.migrator_name == "Version":
raise ValueError(
"The version field must be set if migrator_name is 'Version'."
)
if self.version is not None and self.migrator_name != "Version":
raise ValueError(
"The version field must be None if migrator_name is not 'Version'."
)
return self
migrator_object_version: int | OnePlusToOne | None = None
"""
This field is taken from the migration YAML for YAML-based migrations. In the migration YAML, this field is called
`migration_number`. Increasing the migrator object version is a way to force a migration to be rerun.
The version of the migrator object (see above) is different from this value.
https://github.com/conda-forge/conda-forge-pinning-feedstock/blob/0d70e56969f0fcba4e6211cd93224abbfe3c919f/recipe/migrations/example.exyaml#L9-L11
Non-YAML migrators do not set this field.
Refer to OnePlusToOne for more information about the 1+ case (legacy typo fix).
"""
name: str | None = None
"""
The name of the migration executed by the migrator. This is only used for YAML-based migrations and is the filename
of the migration YAML file, without the .yaml or .yml extension. Otherwise, this field is missing.
"""
branch: str | None = None
"""
The branch that was migrated. Only set if not equal to "master" or "main", which seems a bit questionable.
"""
# noinspection PyNestedDecorators
@field_validator("branch")
@classmethod
def check_branch(cls, value: str) -> str:
if value in ("master", "main"):
raise ValueError("The branch field must not be 'master' or 'main'.")
return value
pin_version: str | None = None
"""
This is only set by MigrationYamlCreator and specifies the version of the package that is pinned in the migration.
"""
@model_validator(mode="after")
def validate_pin_version(self) -> Self:
if self.pin_version is None and (
self.migrator_name == MigratorName.MIGRATION_YAML_CREATOR
):
raise ValueError(
"MigrationYamlCreator must have the pin_version field set."
)
if self.pin_version is not None and (
self.migrator_name != MigratorName.MIGRATION_YAML_CREATOR
):
raise ValueError(
"Only MigrationYamlCreator can have the pin_version field set."
)
return self
class MigrationPullRequest(StrictBaseModel):
PR: (
PullRequestDataValid | PullRequestInfoSpecial | PrJsonLazyJsonReference | None
) = None
"""
GitHub data about the pull request.
This field may be missing.
"""
data: MigrationPullRequestData
# noinspection PyNestedDecorators
@model_validator(mode="before")
@classmethod
def validate_keys(cls, input_data: Any) -> Any:
"""
The current implementation uses a field "keys" which is a list of all keys present in the
MigrationPullRequestData object, duplicating them. This list is redundant and should be removed.
The consistency of this field is validated here, after which it is removed.
"""
input_data = before_validator_ensure_dict(input_data)
if "keys" not in input_data:
raise ValueError("The keys field is missing.")
if "data" not in input_data:
raise ValueError("The data field is missing.")
keys = input_data.pop("keys")
if not isinstance(keys, list):
raise ValueError("The keys field must be a list.")
data = input_data["data"]
if not isinstance(data, dict):
raise ValueError("The data field must be a dictionary.")
if set(keys) != set(data.keys()):
raise ValueError(
"The keys field must exactly contain all keys of the data field."
)
return input_data
class ExceptionInfo(StrictBaseModel):
"""
Information about an exception that occurred while performing migrations.
"""
exception: str
"""
The exception message.
"""
traceback: list[str]
"""
The traceback of the exception, split by newlines (may include empty strings).
"""
code: int | None = None
"""
If an HTTP error occurred, this field may contain the HTTP status code.
"""
url: str | None = None
"""
If an HTTP error occurred, this field may contain the URL that was requested.
"""
class PrInfoValid(StrictBaseModel):
PRed: list[MigrationPullRequest] = []
bad: str | ExceptionInfo | Literal[False] = False
"""
If `False`, nothing bad happened. Otherwise, it indicates an error that occurred while performing migrations.
Example: The feedstock was not found by the name defined in the graph or node attributes feedstock_name field.
"""
pinning_version: str | None = None
"""
The version of the conda-forge-pinning feedstock that was used for performing the LATEST migration of the feedstock.
This can be None the feedstock has not been migrated yet, but it is also missing in other cases. There are NO
assertions when this field is missing or present.
"""
smithy_version: (
CondaVersionString | CondaVersionStringWithAzureTokenError | None
) = None
"""
The version of conda-smithy that was used for performing the LATEST migration of the feedstock.
This can be None if the feedstock has not been migrated yet. There are NO assertions when this field is missing or
present.
A lot of feedstocks have an Azure token error in the smithy_version field, which is removed automatically.
The Azure token errors should be removed from the graph, e.g. by parsing the model and re-serializing it.
"""
pre_pr_migrator_status: NoneIsEmptyDict[str, str] = {}
"""
A dictionary (migration name -> error message) of the error status of the migrations.
Errors are added here if a non-version migration fails before a migration PR is created.
This field can contain HTML tags, which are probably intended for the status page.
The same thing for version migrations is part of the `version_pr_info` object.
There are implicit assumptions about the contents of this field, but they are not documented.
Refer to status_report.graph_migrator_status, for example.
Note: The names of migrators appear here without spaces and in lowercase. This is not always the case.
If a migration is eventually successful, the corresponding key is removed from the dictionary.
"""
pre_pr_migrator_attempts: NoneIsEmptyDict[str, int] = {}
"""
A dictionary (migration name -> number of attempts) of the number of attempts of the migrations.
This value is increased by 1 every time a migration fails before a migration PR is created.
Note: The names of migrators appear here without spaces and in lowercase. This is not always the case.
If a migration is eventually successful, the corresponding key is removed from the dictionary.
"""
@model_validator(mode="after")
def check_pre_pr_migrations(self) -> Self:
if self.pre_pr_migrator_status.keys() != self.pre_pr_migrator_attempts.keys():
raise ValueError(
"The keys (migration names) of pre_pr_migrator_status and pre_pr_migrator_attempts must match."
)
return self
PrInfo = TypeAdapter(PrInfoValid)