feat: implement POST /pulls merge handler with merge button in PR detail
Build and Push / test (pull_request) Successful in 17s
Build and Push / build (pull_request) Has been skipped

Add MergePull HTTP handler that calls the existing client.MergePull()
method, register the POST /pulls/{owner}/{repo}/{index}/merge route,
and add a merge button/form to the PR detail template gated on
Mergeable and open state. Supports merge, rebase, and squash styles.

- Handler returns HTMX fragment on HX-Request, redirect otherwise
- Error path returns inline error fragment for HTMX requests
- Add mock merge endpoint and 3 integration tests
- Merge button only shows when PR is mergeable and open

Closes leeworks-agents/gitea-mobile#229

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
agent-company
2026-05-18 21:44:16 +00:00
parent d34916c276
commit 062f85cf1b
3 changed files with 130 additions and 0 deletions
+66
View File
@@ -185,6 +185,11 @@ func mockGiteaAPI(t *testing.T) *httptest.Server {
json.NewEncoder(w).Encode(map[string]interface{}{"id": 1, "state": "APPROVED"})
})
// POST /api/v1/repos/{owner}/{repo}/pulls/{index}/merge — merge PR.
mux.HandleFunc("POST /api/v1/repos/{owner}/{repo}/pulls/{index}/merge", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
// POST /api/v1/markdown — render markdown.
mux.HandleFunc("POST /api/v1/markdown", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
@@ -917,6 +922,67 @@ func TestIntegration_SubmitReview_RequestChanges(t *testing.T) {
}
}
// --- Issue #229: Integration tests for POST /pulls/{owner}/{repo}/{index}/merge ---
func TestIntegration_MergePull_Valid(t *testing.T) {
h, srv := newTestHandlerWithMock(t)
defer srv.Close()
mux := http.NewServeMux()
mux.HandleFunc("POST /pulls/{owner}/{repo}/{index}/merge", h.MergePull)
form := url.Values{"Do": {"merge"}}
req := reqWithToken(http.MethodPost, "/pulls/test-org/repo1/1/merge", form.Encode())
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Errorf("status = %d, want %d", w.Code, http.StatusSeeOther)
}
}
func TestIntegration_MergePull_HTMX(t *testing.T) {
h, srv := newTestHandlerWithMock(t)
defer srv.Close()
mux := http.NewServeMux()
mux.HandleFunc("POST /pulls/{owner}/{repo}/{index}/merge", h.MergePull)
form := url.Values{"Do": {"squash"}}
req := reqWithToken(http.MethodPost, "/pulls/test-org/repo1/1/merge", form.Encode())
req.Header.Set("HX-Request", "true")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("status = %d, want %d", w.Code, http.StatusOK)
}
body := w.Body.String()
if !contains(body, "Merged") {
t.Errorf("expected 'Merged' in HTMX response, got: %s", body)
}
}
func TestIntegration_MergePull_DefaultStyle(t *testing.T) {
h, srv := newTestHandlerWithMock(t)
defer srv.Close()
mux := http.NewServeMux()
mux.HandleFunc("POST /pulls/{owner}/{repo}/{index}/merge", h.MergePull)
// No "Do" form value — should default to "merge".
req := reqWithToken(http.MethodPost, "/pulls/test-org/repo1/1/merge", "")
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
if w.Code != http.StatusSeeOther {
t.Errorf("status = %d, want %d", w.Code, http.StatusSeeOther)
}
}
// --- Issue #110: Integration tests for POST /issues/{owner}/{repo}/{index}/labels ---
func TestIntegration_ApplyLabels_Valid(t *testing.T) {