feat: implement POST /pulls merge handler #234
@@ -56,6 +56,7 @@ func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
|
|||||||
mux.HandleFunc("GET /pulls/{owner}/{repo}/{index}", h.PullDetail)
|
mux.HandleFunc("GET /pulls/{owner}/{repo}/{index}", h.PullDetail)
|
||||||
mux.HandleFunc("POST /pulls/{owner}/{repo}/{index}/review", h.SubmitReview)
|
mux.HandleFunc("POST /pulls/{owner}/{repo}/{index}/review", h.SubmitReview)
|
||||||
mux.HandleFunc("POST /pulls/{owner}/{repo}/{index}/state", h.SetPullState)
|
mux.HandleFunc("POST /pulls/{owner}/{repo}/{index}/state", h.SetPullState)
|
||||||
|
mux.HandleFunc("POST /pulls/{owner}/{repo}/{index}/merge", h.MergePull)
|
||||||
|
|
||||||
// Settings (handled separately for auth bypass).
|
// Settings (handled separately for auth bypass).
|
||||||
settingsHandler := &SettingsHandler{
|
settingsHandler := &SettingsHandler{
|
||||||
@@ -1102,3 +1103,49 @@ func (h *Handler) SubmitReview(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
http.Redirect(w, r, fmt.Sprintf("/pulls/%s/%s/%d", owner, repo, index), http.StatusSeeOther)
|
http.Redirect(w, r, fmt.Sprintf("/pulls/%s/%s/%d", owner, repo, index), http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MergePull handles POST /pulls/{owner}/{repo}/{index}/merge.
|
||||||
|
func (h *Handler) MergePull(w http.ResponseWriter, r *http.Request) {
|
||||||
|
token := getToken(r)
|
||||||
|
owner := r.PathValue("owner")
|
||||||
|
repo := r.PathValue("repo")
|
||||||
|
indexStr := r.PathValue("index")
|
||||||
|
|
||||||
|
index, err := strconv.ParseInt(indexStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "invalid PR index", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.ParseForm(); err != nil {
|
||||||
|
http.Error(w, "bad request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
style := r.FormValue("Do")
|
||||||
|
if style == "" {
|
||||||
|
style = "merge"
|
||||||
|
}
|
||||||
|
title := r.FormValue("merge_title")
|
||||||
|
message := r.FormValue("merge_message")
|
||||||
|
|
||||||
|
if err := h.Client.MergePull(r.Context(), token, owner, repo, index, style, title, message); err != nil {
|
||||||
|
slog.Error("failed to merge pull request", "error", err, "owner", owner, "repo", repo, "index", index)
|
||||||
|
if isHTMX(r) {
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
fmt.Fprint(w, `<span style="color:#f85149">Failed to merge pull request.</span>`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
http.Error(w, "failed to merge pull request", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if isHTMX(r) {
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
fmt.Fprint(w, `<span style="color:#a371f7;font-weight:bold">Merged</span>`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
http.Redirect(w, r, fmt.Sprintf("/pulls/%s/%s/%d", owner, repo, index), http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|||||||
@@ -185,6 +185,11 @@ func mockGiteaAPI(t *testing.T) *httptest.Server {
|
|||||||
json.NewEncoder(w).Encode(map[string]interface{}{"id": 1, "state": "APPROVED"})
|
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.
|
// POST /api/v1/markdown — render markdown.
|
||||||
mux.HandleFunc("POST /api/v1/markdown", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("POST /api/v1/markdown", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/html")
|
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 ---
|
// --- Issue #110: Integration tests for POST /issues/{owner}/{repo}/{index}/labels ---
|
||||||
|
|
||||||
func TestIntegration_ApplyLabels_Valid(t *testing.T) {
|
func TestIntegration_ApplyLabels_Valid(t *testing.T) {
|
||||||
|
|||||||
@@ -33,6 +33,23 @@
|
|||||||
{{end}}
|
{{end}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{{if and .Pull.Mergeable (eq .Pull.State "open")}}
|
||||||
|
<div class="card" style="margin-top:1rem;" id="merge-section">
|
||||||
|
<h2>Merge Pull Request</h2>
|
||||||
|
<form hx-post="/pulls/{{.Pull.RepoOwner}}/{{.Pull.RepoName}}/{{.Pull.Number}}/merge" hx-target="#merge-section" hx-swap="innerHTML">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="merge-style">Merge Style</label>
|
||||||
|
<select id="merge-style" name="Do">
|
||||||
|
<option value="merge">Merge Commit</option>
|
||||||
|
<option value="rebase">Rebase</option>
|
||||||
|
<option value="squash">Squash</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary" style="background-color:var(--accent-purple,#a371f7);">Merge PR</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
<div class="card" style="margin-top:1rem;">
|
<div class="card" style="margin-top:1rem;">
|
||||||
<h2>Submit Review</h2>
|
<h2>Submit Review</h2>
|
||||||
<form hx-post="/pulls/{{.Pull.RepoOwner}}/{{.Pull.RepoName}}/{{.Pull.Number}}/review" hx-swap="outerHTML">
|
<form hx-post="/pulls/{{.Pull.RepoOwner}}/{{.Pull.RepoName}}/{{.Pull.Number}}/review" hx-swap="outerHTML">
|
||||||
|
|||||||
Reference in New Issue
Block a user