-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_github_projects.py
More file actions
320 lines (271 loc) · 9.47 KB
/
Copy pathsync_github_projects.py
File metadata and controls
320 lines (271 loc) · 9.47 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
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
"""Synchronize fyi-cli GitHub Project items into the RIOPA umbrella project.
GitHub Projects do not support nested projects or native project-to-project
mirroring. This script keeps the practical mirror aligned by ensuring every
issue or pull request in the fyi-cli roadmap also exists in the Rare Insights
on Open Policy from Aotearoa project and by copying the fields that both
projects can represent.
"""
from __future__ import annotations
import argparse
import json
import subprocess
import sys
from dataclasses import dataclass
from typing import Any
DEFAULT_OWNER = "edithatogo"
DEFAULT_SOURCE_PROJECT = 6
DEFAULT_TARGET_PROJECT = 4
DEFAULT_REPOSITORY = "edithatogo/fyi-cli"
@dataclass(frozen=True)
class Field:
id: str
name: str
type: str
options: dict[str, str]
@dataclass(frozen=True)
class Project:
id: str
number: int
fields: dict[str, Field]
@dataclass(frozen=True)
class SyncAction:
kind: str
message: str
command: list[str] | None = None
def run_gh(args: list[str], *, dry_run: bool = False) -> Any:
if dry_run:
print("+ gh " + " ".join(args))
return {}
completed = subprocess.run(
["gh", *args],
check=True,
encoding="utf-8",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
output = completed.stdout.strip()
if not output:
return {}
return json.loads(output)
def run_gh_raw(args: list[str], *, dry_run: bool = False) -> str:
if dry_run:
print("+ gh " + " ".join(args))
return ""
completed = subprocess.run(
["gh", *args],
check=True,
encoding="utf-8",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
return completed.stdout.strip()
def load_project(owner: str, number: int) -> Project:
project_payload = run_gh(
["project", "view", str(number), "--owner", owner, "--format", "json"]
)
fields_payload = run_gh(
["project", "field-list", str(number), "--owner", owner, "--format", "json"]
)
fields: dict[str, Field] = {}
for raw_field in fields_payload["fields"]:
options = {
option["name"]: option["id"] for option in raw_field.get("options", [])
}
fields[raw_field["name"]] = Field(
id=raw_field["id"],
name=raw_field["name"],
type=raw_field["type"],
options=options,
)
return Project(id=project_payload["id"], number=number, fields=fields)
def load_items(owner: str, project_number: int, limit: int) -> list[dict[str, Any]]:
payload = run_gh(
[
"project",
"item-list",
str(project_number),
"--owner",
owner,
"--limit",
str(limit),
"--format",
"json",
]
)
return payload["items"]
def content_url(item: dict[str, Any]) -> str | None:
content = item.get("content")
if not isinstance(content, dict):
return None
return content.get("url")
def content_repository(item: dict[str, Any]) -> str | None:
content = item.get("content")
if not isinstance(content, dict):
return None
return content.get("repository")
def source_items_for_repository(
source_items: list[dict[str, Any]], repository: str
) -> list[dict[str, Any]]:
return [
item
for item in source_items
if content_url(item) and content_repository(item) == repository
]
def target_items_for_repository(
target_items: list[dict[str, Any]], repository: str
) -> dict[str, dict[str, Any]]:
by_url: dict[str, dict[str, Any]] = {}
for item in target_items:
if content_repository(item) != repository:
continue
url = content_url(item)
if url:
by_url[url] = item
return by_url
def select_option_id(field: Field | None, value: str | None) -> str | None:
if not field or not value:
return None
return field.options.get(value)
def plan_sync(
*,
owner: str,
source_project: Project,
target_project: Project,
source_items: list[dict[str, Any]],
target_by_url: dict[str, dict[str, Any]],
mirror_source: str,
) -> list[SyncAction]:
actions: list[SyncAction] = []
target_status = target_project.fields.get("Status")
target_mirror = target_project.fields.get("Mirror source")
mirror_option = select_option_id(target_mirror, mirror_source) or select_option_id(
target_mirror, "other"
)
for source_item in source_items:
url = content_url(source_item)
if not url:
continue
target_item = target_by_url.get(url)
if target_item is None:
actions.append(
SyncAction(
kind="add",
message=f"add missing target item: {url}",
command=[
"project",
"item-add",
str(target_project.number),
"--owner",
owner,
"--url",
url,
],
)
)
continue
source_status = source_item.get("status")
target_status_value = target_item.get("status")
status_option = select_option_id(target_status, source_status)
if status_option and source_status != target_status_value:
actions.append(
SyncAction(
kind="field",
message=(
f"set Status={source_status!r} for "
f"{target_item.get('title', url)}"
),
command=[
"project",
"item-edit",
"--id",
target_item["id"],
"--project-id",
target_project.id,
"--field-id",
target_status.id,
"--single-select-option-id",
status_option,
],
)
)
if target_mirror and mirror_option:
target_mirror_value = target_item.get("mirror source")
expected_mirror = (
mirror_source if mirror_source in target_mirror.options else "other"
)
if target_mirror_value != expected_mirror:
actions.append(
SyncAction(
kind="field",
message=(
f"set Mirror source={expected_mirror!r} for "
f"{target_item.get('title', url)}"
),
command=[
"project",
"item-edit",
"--id",
target_item["id"],
"--project-id",
target_project.id,
"--field-id",
target_mirror.id,
"--single-select-option-id",
mirror_option,
],
)
)
return actions
def apply_actions(actions: list[SyncAction], *, dry_run: bool) -> None:
for action in actions:
print(f"{action.kind}: {action.message}")
if action.command:
run_gh_raw(action.command, dry_run=dry_run)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Mirror fyi-cli project items into the RIOPA umbrella project."
)
parser.add_argument("--owner", default=DEFAULT_OWNER)
parser.add_argument("--source-project", type=int, default=DEFAULT_SOURCE_PROJECT)
parser.add_argument("--target-project", type=int, default=DEFAULT_TARGET_PROJECT)
parser.add_argument("--repository", default=DEFAULT_REPOSITORY)
parser.add_argument("--source-limit", type=int, default=200)
parser.add_argument("--target-limit", type=int, default=500)
parser.add_argument("--mirror-source", default="fyi-cli")
parser.add_argument("--dry-run", action="store_true")
return parser.parse_args()
def main() -> int:
args = parse_args()
try:
source_project = load_project(args.owner, args.source_project)
target_project = load_project(args.owner, args.target_project)
source_items = source_items_for_repository(
load_items(args.owner, args.source_project, args.source_limit),
args.repository,
)
target_by_url = target_items_for_repository(
load_items(args.owner, args.target_project, args.target_limit),
args.repository,
)
actions = plan_sync(
owner=args.owner,
source_project=source_project,
target_project=target_project,
source_items=source_items,
target_by_url=target_by_url,
mirror_source=args.mirror_source,
)
print(
"sync summary: "
f"source_items={len(source_items)} "
f"target_items={len(target_by_url)} "
f"actions={len(actions)} "
f"dry_run={args.dry_run}"
)
apply_actions(actions, dry_run=args.dry_run)
return 0
except subprocess.CalledProcessError as error:
sys.stderr.write(error.stderr)
return error.returncode
if __name__ == "__main__":
raise SystemExit(main())