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
321
"""Merge request tag view helpers and fast-forward merge endpoint."""
from __future__ import annotations
import asyncio
import json
from typing import Annotated, Any
from urllib.parse import quote, urlencode
import pygit2
from fastapi import APIRouter, Depends, Form, HTTPException, Query, Request
from fastapi.responses import HTMLResponse, RedirectResponse, StreamingResponse
from pygittools.merge import MergeRequest, MergeRequestStatus, get_merge_request_by_oid
from pygitweb.action_runs import ACTION_RUNS, parse_run_ids, start_mr_action_runs
from pygitweb.api.action import MrTrigger
from pygitweb.auth import access_token_from_request, principal_from_session, require_permission
from pygitweb.dependencies import (
ValidatedReadableFormProject,
ValidatedReadableQueryProject,
)
from pygitweb.git_helpers import open_repo, resolve_mr_opener
from pygitweb.permissions import Permission
from pygitweb.templates_env import render_page
from pygitweb.validation import is_valid_ref_format
def _branch_short_name(ref: str) -> str:
return ref.removeprefix("refs/heads/") if ref.startswith("refs/heads/") else ref
merge_router = APIRouter(tags=["merge_requests"])
_STATUS_LABELS: dict[MergeRequestStatus, str] = {
MergeRequestStatus.CAN_FAST_FORWARD: "Can fast-forward",
MergeRequestStatus.UP_TO_DATE: "Already up to date",
MergeRequestStatus.BLOCKED_DIVERGED: "Blocked: histories diverged (not fast-forward)",
MergeRequestStatus.OURS_REF_MISSING: "Blocked: ours ref is missing",
MergeRequestStatus.MR_HEAD_MISSING: "Blocked: merge request head is missing from the repository",
}
def try_merge_request_from_tag(repo: pygit2.Repository, tag: pygit2.Tag) -> MergeRequest | None:
try:
return get_merge_request_by_oid(repo, tag.id)
except (ValueError, json.JSONDecodeError, TypeError, KeyError):
return None
def resolve_merge_request_tag_to_tip(
repo: pygit2.Repository, obj: pygit2.Tag
) -> tuple[pygit2.Tag, MergeRequest] | None:
"""If ``obj`` is an MR tag and ``refs/tags/mr/…`` points to a newer tag object, return that tip."""
mr = try_merge_request_from_tag(repo, obj)
if mr is None:
return None
if mr.name not in repo.references:
return obj, mr
tip_id = repo.references[mr.name].resolve().target
if tip_id == obj.id:
return obj, mr
tip_obj = repo[tip_id]
if not isinstance(tip_obj, pygit2.Tag):
return obj, mr
mr_tip = try_merge_request_from_tag(repo, tip_obj)
if mr_tip is None:
return obj, mr
return tip_obj, mr_tip
def merge_request_status_fields(repo: pygit2.Repository, mr: MergeRequest) -> dict[str, Any]:
try:
status = mr.check_status(repo)
except ValueError as exc:
return {
"status_code": "error",
"status_label": "Unable to analyze merge",
"status_detail": str(exc),
"show_merge_button": False,
}
label = _STATUS_LABELS.get(status, status.value)
return {
"status_code": status.value,
"status_label": label,
"status_detail": "",
"show_merge_button": status == MergeRequestStatus.CAN_FAST_FORWARD,
}
def _head_view_url(project: str, repo: pygit2.Repository, head_hex: str) -> tuple[str, str]:
try:
oid = pygit2.Oid(hex=head_hex)
obj = repo[oid]
except (KeyError, ValueError, TypeError):
return head_hex[:7], ""
t = getattr(obj, "type_str", "") or ""
short = head_hex[:7]
if t == "commit":
return short, f"/project/{project}?a=commit&h={quote(head_hex, safe='')}"
if t == "tag":
return short, f"/project/{project}?a=tag&h={quote(head_hex, safe='')}"
return short, ""
def merge_request_tag_response(
project: str,
repo: pygit2.Repository,
mr: MergeRequest,
tag_name: str,
tag_oid: str,
tagger: str,
tagger_date: str,
notice: str | None = None,
run_ids: list[str] | None = None,
) -> HTMLResponse | RedirectResponse:
new_oid = mr.refresh_from_theirs(repo)
if new_oid is not None:
updated_runs = start_mr_action_runs(project, MrTrigger.UPDATED, mr)
params: dict[str, str] = {"a": "tag", "h": str(new_oid)}
all_runs = [*(run_ids or []), *updated_runs]
if all_runs:
params["runs"] = ",".join(all_runs)
if notice:
params["notice"] = notice
return RedirectResponse(
url=f"/project/{project}?{urlencode(params)}",
status_code=302,
)
status_fields = merge_request_status_fields(repo, mr)
head_short, head_url = _head_view_url(project, repo, str(mr.target))
ours_ref = mr.ours
ours_url = f"/project/{project}?a=log&h={quote(ours_ref, safe='')}"
theirs_ref = mr.theirs or ""
theirs_url = f"/project/{project}?a=log&h={quote(theirs_ref, safe='')}" if mr.theirs else ""
comments_rows: list[tuple[str, str]] = []
for c in mr.comments:
ch = str(c)
comments_rows.append((ch[:7], f"/project/{project}?a=tag&h={quote(ch, safe='')}"))
current_tag_oid = str(repo.references[mr.name].resolve().target)
requested_ids = run_ids or []
known = {run.run_id: run.snapshot() for run in ACTION_RUNS.get_many(requested_ids)}
action_runs = [
known.get(
rid,
{
"run_id": rid,
"action": rid,
"project": project,
"trigger": "",
"mr_ref": mr.name,
"mr_oid": current_tag_oid,
"created_at": "",
"message": "queued",
"fraction": 0.0,
"done": False,
"ok": None,
},
)
for rid in requested_ids
]
ctx = {
"project": project,
"project_url": f"/project/{project}",
"tag_name": tag_name,
"tag_oid": current_tag_oid,
"tag_view_url": f"/project/{project}?a=tag&h={quote(current_tag_oid, safe='')}",
"mr_title": mr.title,
"mr_description": mr.description or "",
"ours_ref": ours_ref,
"ours_url": ours_url,
"theirs_ref": theirs_ref,
"theirs_url": theirs_url,
"head_short": head_short,
"head_url": head_url,
"head_full": str(mr.target),
"tagger": tagger,
"tagger_date": tagger_date,
"opener": str(mr.opener),
"created_at": mr.created_at.isoformat() if mr.created_at else "",
"updated_at": mr.updated_at.isoformat() if mr.updated_at else "",
"comments": comments_rows,
"merge_action_url": f"/mr/ff?project={quote(project, safe='')}&h={quote(current_tag_oid, safe='')}",
"notice": notice or "",
"action_runs": action_runs,
"action_run_ids": ",".join(requested_ids),
**status_fields,
}
title = f"Merge request: {mr.title or ''} — {project or ''}"
return render_page("merge_request_tag.html", title=title, **ctx)
@merge_router.post(
"/create",
dependencies=[Depends(require_permission(Permission.MR_CREATE, project_from_form=True))],
)
def merge_request_create(
project: ValidatedReadableFormProject,
theirs: str = Form(...),
ours: str = Form(...),
title: str = Form(""),
token: Annotated[str | None, Depends(access_token_from_request)] = None,
) -> RedirectResponse:
theirs_ref = theirs.strip()
ours_ref = ours.strip()
if not theirs_ref or not ours_ref:
raise HTTPException(status_code=400, detail="theirs and ours are required")
if not is_valid_ref_format(theirs_ref) or not is_valid_ref_format(ours_ref):
raise HTTPException(status_code=400, detail="Invalid ref format")
repo = open_repo(project)
try:
theirs_tip = repo.revparse_single(theirs_ref).peel(pygit2.Commit).id
except (KeyError, ValueError, pygit2.GitError) as exc:
raise HTTPException(status_code=404, detail=f"Cannot resolve theirs ref {theirs_ref!r}") from exc
try:
repo.revparse_single(ours_ref)
except (KeyError, ValueError, pygit2.GitError) as exc:
raise HTTPException(status_code=404, detail=f"Cannot resolve ours ref {ours_ref!r}") from exc
opener, opener_warning = resolve_mr_opener(repo, principal_from_session(token))
t = title.strip()
if not t:
t = f"Merge {_branch_short_name(theirs_ref)} into {_branch_short_name(ours_ref)}"
mr = MergeRequest(
target=theirs_tip,
opener=opener,
ours=ours_ref,
theirs=theirs_ref,
title=t,
description="",
)
oid = mr.write(repo)
run_ids = start_mr_action_runs(project, MrTrigger.OPENED, mr)
params: dict[str, str] = {"a": "tag", "h": str(oid)}
if run_ids:
params["runs"] = ",".join(run_ids)
if opener_warning:
params["notice"] = opener_warning
dest = f"/project/{project}?{urlencode(params)}"
return RedirectResponse(url=dest, status_code=303)
@merge_router.post(
"/ff",
dependencies=[Depends(require_permission(Permission.MR_MERGE, project_from_query=True))],
)
def merge_fast_forward(
project: ValidatedReadableQueryProject,
h: str = Query(..., description="Annotated tag object id (hex)"),
) -> RedirectResponse:
if not h or not is_valid_ref_format(h):
raise HTTPException(status_code=400, detail="Invalid tag hash (h)")
repo = open_repo(project)
try:
obj = repo.revparse_single(h)
except (KeyError, pygit2.GitError, OSError) as exc:
raise HTTPException(status_code=404, detail="Tag not found") from exc
if not isinstance(obj, pygit2.Tag):
raise HTTPException(status_code=400, detail="Not an annotated tag")
resolved = resolve_merge_request_tag_to_tip(repo, obj)
if resolved is None:
raise HTTPException(status_code=400, detail="Not a merge request tag")
obj, mr = resolved
mr.refresh_from_theirs(repo)
try:
mr.merge(repo)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
tag_oid = str(repo.references[mr.name].resolve().target)
dest = f"/project/{project}?a=tag&h={quote(tag_oid, safe='')}"
return RedirectResponse(url=dest, status_code=303)
@merge_router.get("/runs")
def merge_request_runs(
project: ValidatedReadableQueryProject,
ids: Annotated[str, Query(description="Comma-separated action run ids")] = "",
) -> dict[str, object]:
"""Snapshot progress for one or more MR-triggered action runs."""
requested = parse_run_ids(ids)
runs = ACTION_RUNS.get_many(requested)
missing = [rid for rid in requested if ACTION_RUNS.get(rid) is None]
return {
"project": project,
"runs": [run.snapshot() for run in runs],
"missing": missing,
}
@merge_router.get("/runs/{run_id}/events")
async def merge_request_run_events(
run_id: str,
request: Request,
project: ValidatedReadableQueryProject,
) -> StreamingResponse:
"""Server-sent events stream of progress for a single action run."""
run = ACTION_RUNS.get(run_id)
if run is None or run.project != project:
raise HTTPException(status_code=404, detail="Action run not found")
async def event_gen() -> Any:
async for progress in ACTION_RUNS.stream(run):
if await request.is_disconnected():
return
payload = {
"run_id": run.run_id,
"action": run.action_name,
"message": progress.message,
"fraction": progress.fraction,
"done": progress.done,
"ok": progress.ok,
}
yield f"data: {json.dumps(payload)}\n\n"
if progress.done:
return
await asyncio.sleep(0)
return StreamingResponse(event_gen(), media_type="text/event-stream")