Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3d1775a37b |
@@ -945,6 +945,40 @@ func (c *Client) SetIssueState(ctx context.Context, token, owner, repo string, i
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MergePull merges a pull request using the specified merge style.
|
||||||
|
// Valid styles: "merge", "rebase", "rebase-merge", "squash".
|
||||||
|
// If style is empty, defaults to "merge".
|
||||||
|
func (c *Client) MergePull(ctx context.Context, token, owner, repo string, index int64, style, title, message string) error {
|
||||||
|
if style == "" {
|
||||||
|
style = "merge"
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := map[string]string{
|
||||||
|
"Do": style,
|
||||||
|
}
|
||||||
|
if title != "" {
|
||||||
|
payload["merge_message_field"] = title
|
||||||
|
}
|
||||||
|
if message != "" {
|
||||||
|
payload["merge_message_field"] = message
|
||||||
|
}
|
||||||
|
|
||||||
|
jsonData, err := json.Marshal(payload)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("marshaling merge request: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
path := fmt.Sprintf("/repos/%s/%s/pulls/%d/merge", owner, repo, index)
|
||||||
|
resp, err := c.doRequest(ctx, token, http.MethodPost, path, strings.NewReader(string(jsonData)))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("merging pull request: %w", err)
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
c.InvalidateAll()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// AddComment creates a comment on an issue and returns the created Comment.
|
// AddComment creates a comment on an issue and returns the created Comment.
|
||||||
func (c *Client) AddComment(ctx context.Context, token, owner, repo string, index int64, body string) (*Comment, error) {
|
func (c *Client) AddComment(ctx context.Context, token, owner, repo string, index int64, body string) (*Comment, error) {
|
||||||
return c.PostComment(ctx, token, owner, repo, index, body)
|
return c.PostComment(ctx, token, owner, repo, index, body)
|
||||||
|
|||||||
@@ -1456,3 +1456,81 @@ func TestRetryDelay_ExponentialBackoff(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMergePull(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
t.Errorf("expected POST, got %s", r.Method)
|
||||||
|
}
|
||||||
|
if r.URL.Path != "/api/v1/repos/owner1/repo1/pulls/5/merge" {
|
||||||
|
t.Errorf("unexpected path: %s", r.URL.Path)
|
||||||
|
}
|
||||||
|
if r.Header.Get("Authorization") != "token test-token" {
|
||||||
|
t.Error("missing or wrong Authorization header")
|
||||||
|
}
|
||||||
|
|
||||||
|
var body map[string]string
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
t.Fatalf("failed to decode body: %v", err)
|
||||||
|
}
|
||||||
|
if body["Do"] != "squash" {
|
||||||
|
t.Errorf("expected Do=squash, got %q", body["Do"])
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
c := NewClient(server.URL)
|
||||||
|
c.setCache("pulls-org1", "should-be-invalidated")
|
||||||
|
|
||||||
|
err := c.MergePull(context.Background(), "test-token", "owner1", "repo1", 5, "squash", "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify cache was invalidated.
|
||||||
|
_, ok := c.getFromCache("pulls-org1")
|
||||||
|
if ok {
|
||||||
|
t.Error("expected cache to be invalidated after MergePull")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergePull_DefaultStyle(t *testing.T) {
|
||||||
|
var receivedStyle string
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var body map[string]string
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
||||||
|
t.Fatalf("failed to decode body: %v", err)
|
||||||
|
}
|
||||||
|
receivedStyle = body["Do"]
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
c := NewClient(server.URL)
|
||||||
|
err := c.MergePull(context.Background(), "test-token", "owner1", "repo1", 5, "", "", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if receivedStyle != "merge" {
|
||||||
|
t.Errorf("expected default style 'merge', got %q", receivedStyle)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMergePull_Error(t *testing.T) {
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
fmt.Fprintln(w, `{"message":"not mergeable"}`)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
c := NewClient(server.URL)
|
||||||
|
err := c.MergePull(context.Background(), "test-token", "owner1", "repo1", 5, "merge", "", "")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for 405 response, got nil")
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "405") {
|
||||||
|
t.Errorf("error should contain status code 405, got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -19,39 +17,21 @@ func (rw *responseWriter) WriteHeader(code int) {
|
|||||||
rw.ResponseWriter.WriteHeader(code)
|
rw.ResponseWriter.WriteHeader(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// generateRequestID creates a short random hex string for request tracing.
|
// Logging returns middleware that logs each HTTP request with structured logging.
|
||||||
func generateRequestID() string {
|
|
||||||
b := make([]byte, 8)
|
|
||||||
if _, err := rand.Read(b); err != nil {
|
|
||||||
return "unknown"
|
|
||||||
}
|
|
||||||
return hex.EncodeToString(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Logging returns middleware that logs each HTTP request with structured fields:
|
|
||||||
// method, path, status, duration (ms), request-id, and remote address.
|
|
||||||
func Logging() func(http.Handler) http.Handler {
|
func Logging() func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
requestID := generateRequestID()
|
|
||||||
rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
|
rw := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK}
|
||||||
|
|
||||||
// Set request ID header for downstream correlation.
|
|
||||||
w.Header().Set("X-Request-ID", requestID)
|
|
||||||
|
|
||||||
next.ServeHTTP(rw, r)
|
next.ServeHTTP(rw, r)
|
||||||
|
|
||||||
duration := time.Since(start)
|
|
||||||
slog.Info("http request",
|
slog.Info("http request",
|
||||||
"method", r.Method,
|
"method", r.Method,
|
||||||
"path", r.URL.Path,
|
"path", r.URL.Path,
|
||||||
"status", rw.statusCode,
|
"status", rw.statusCode,
|
||||||
"duration_ms", duration.Milliseconds(),
|
"duration", time.Since(start).String(),
|
||||||
"duration", duration.String(),
|
|
||||||
"request_id", requestID,
|
|
||||||
"remote", r.RemoteAddr,
|
"remote", r.RemoteAddr,
|
||||||
"user_agent", r.UserAgent(),
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user