fix: support issue links in remote task dialog (#1462)
This commit is contained in:
@@ -16,6 +16,9 @@ type Client interface {
|
||||
// GetPR retrieves a single pull request by number.
|
||||
GetPR(ctx context.Context, owner, repo string, number int) (*PR, error)
|
||||
|
||||
// GetIssue retrieves a single GitHub issue by number.
|
||||
GetIssue(ctx context.Context, owner, repo string, number int) (*Issue, error)
|
||||
|
||||
// FindPRByBranch finds an open PR for the given head branch.
|
||||
FindPRByBranch(ctx context.Context, owner, repo, branch string) (*PR, error)
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ func (c *Controller) RegisterHTTPRoutes(router *gin.Engine) {
|
||||
api.POST("/prs/statuses", c.httpGetPRStatusesBatch)
|
||||
api.POST("/prs/:owner/:repo/:number/reviews", c.httpSubmitReview)
|
||||
api.PUT("/prs/:owner/:repo/:number/merge", c.httpMergePR)
|
||||
api.GET("/issues/:owner/:repo/:number/info", c.httpGetIssueInfo)
|
||||
|
||||
api.GET("/watches/pr", c.httpListPRWatches)
|
||||
api.DELETE("/watches/pr/:id", c.httpDeletePRWatch)
|
||||
@@ -354,32 +355,69 @@ func (c *Controller) httpGetPRStatusesBatch(ctx *gin.Context) {
|
||||
func (c *Controller) httpGetPRInfo(ctx *gin.Context) {
|
||||
owner := ctx.Param("owner")
|
||||
repo := ctx.Param("repo")
|
||||
numberStr := ctx.Param("number")
|
||||
number, err := strconv.Atoi(numberStr)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid PR number"})
|
||||
number, ok := parseRouteNumber(ctx, "invalid PR number")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
pr, err := c.service.GetPR(ctx.Request.Context(), owner, repo, number)
|
||||
if err != nil {
|
||||
status := http.StatusInternalServerError
|
||||
var apiErr *GitHubAPIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.StatusCode {
|
||||
case http.StatusNotFound:
|
||||
status = http.StatusNotFound
|
||||
case http.StatusUnauthorized:
|
||||
status = http.StatusUnauthorized
|
||||
case http.StatusForbidden:
|
||||
status = http.StatusForbidden
|
||||
}
|
||||
}
|
||||
ctx.JSON(status, gin.H{"error": err.Error()})
|
||||
handleGitHubInfoError(ctx, err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, pr)
|
||||
}
|
||||
|
||||
func (c *Controller) httpGetIssueInfo(ctx *gin.Context) {
|
||||
owner := ctx.Param("owner")
|
||||
repo := ctx.Param("repo")
|
||||
number, ok := parseRouteNumber(ctx, "invalid issue number")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
issue, err := c.service.GetIssue(ctx.Request.Context(), owner, repo, number)
|
||||
if err != nil {
|
||||
handleGitHubInfoError(ctx, err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, issue)
|
||||
}
|
||||
|
||||
func parseRouteNumber(ctx *gin.Context, invalidMessage string) (int, bool) {
|
||||
number, err := strconv.Atoi(ctx.Param("number"))
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": invalidMessage})
|
||||
return 0, false
|
||||
}
|
||||
return number, true
|
||||
}
|
||||
|
||||
func handleGitHubInfoError(ctx *gin.Context, err error) {
|
||||
if errors.Is(err, ErrNoClient) {
|
||||
ctx.JSON(http.StatusServiceUnavailable, gin.H{
|
||||
"error": "GitHub is not configured. Connect GitHub in Settings > Integrations.",
|
||||
"code": "github_not_configured",
|
||||
})
|
||||
return
|
||||
}
|
||||
status := http.StatusInternalServerError
|
||||
message := "failed to fetch GitHub info"
|
||||
var apiErr *GitHubAPIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.StatusCode {
|
||||
case http.StatusNotFound:
|
||||
status = http.StatusNotFound
|
||||
case http.StatusUnauthorized:
|
||||
status = http.StatusUnauthorized
|
||||
case http.StatusForbidden:
|
||||
status = http.StatusForbidden
|
||||
}
|
||||
}
|
||||
if status != http.StatusInternalServerError {
|
||||
message = err.Error()
|
||||
}
|
||||
ctx.JSON(status, gin.H{"error": message})
|
||||
}
|
||||
|
||||
func (c *Controller) httpSubmitReview(ctx *gin.Context) {
|
||||
owner := ctx.Param("owner")
|
||||
repo := ctx.Param("repo")
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
// stubClient implements Client with no-op defaults; override fields as needed.
|
||||
type stubClient struct {
|
||||
getPRFunc func(ctx context.Context, owner, repo string, number int) (*PR, error)
|
||||
getIssueFunc func(ctx context.Context, owner, repo string, number int) (*Issue, error)
|
||||
mergePRFn func(ctx context.Context, owner, repo string, number int, mergeMethod string) error
|
||||
getRepoMergeMethodsFn func() (RepoMergeMethods, error)
|
||||
}
|
||||
@@ -37,6 +38,12 @@ func (s *stubClient) GetPR(ctx context.Context, owner, repo string, number int)
|
||||
}
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
func (s *stubClient) GetIssue(ctx context.Context, owner, repo string, number int) (*Issue, error) {
|
||||
if s.getIssueFunc != nil {
|
||||
return s.getIssueFunc(ctx, owner, repo, number)
|
||||
}
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
func (s *stubClient) FindPRByBranch(context.Context, string, string, string) (*PR, error) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -337,6 +344,143 @@ func TestHttpGetPRInfo_ServiceError(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpGetPRInfo_NoClient(t *testing.T) {
|
||||
router, _ := setupControllerTest(nil)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/github/prs/acme/widget/99/info", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("expected 503, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var got struct {
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if got.Code != "github_not_configured" {
|
||||
t.Fatalf("expected github_not_configured code, got %q", got.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpGetPRInfo_GitHubAPIErrorStatus(t *testing.T) {
|
||||
sc := &stubClient{
|
||||
getPRFunc: func(context.Context, string, string, int) (*PR, error) {
|
||||
return nil, &GitHubAPIError{
|
||||
StatusCode: http.StatusNotFound,
|
||||
Endpoint: "/repos/acme/widget/pulls/99",
|
||||
Body: "upstream error",
|
||||
}
|
||||
},
|
||||
}
|
||||
router, _ := setupControllerTest(sc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/github/prs/acme/widget/99/info", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Fatalf("expected 404, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpGetIssueInfo_Success(t *testing.T) {
|
||||
sc := &stubClient{
|
||||
getIssueFunc: func(_ context.Context, owner, repo string, number int) (*Issue, error) {
|
||||
if owner != "acme" || repo != "widget" || number != 1456 {
|
||||
t.Errorf("unexpected params: %s/%s#%d", owner, repo, number)
|
||||
}
|
||||
return &Issue{Number: 1456, Title: "fix remote picker"}, nil
|
||||
},
|
||||
}
|
||||
router, _ := setupControllerTest(sc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/github/issues/acme/widget/1456/info", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var issue Issue
|
||||
if err := json.NewDecoder(w.Body).Decode(&issue); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
}
|
||||
if issue.Number != 1456 {
|
||||
t.Errorf("expected issue number 1456, got %d", issue.Number)
|
||||
}
|
||||
if issue.Title != "fix remote picker" {
|
||||
t.Errorf("expected issue title, got %q", issue.Title)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpGetIssueInfo_InvalidNumber(t *testing.T) {
|
||||
router, _ := setupControllerTest(&stubClient{})
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/github/issues/acme/widget/abc/info", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpGetIssueInfo_ServiceError(t *testing.T) {
|
||||
sc := &stubClient{
|
||||
getIssueFunc: func(context.Context, string, string, int) (*Issue, error) {
|
||||
return nil, fmt.Errorf("not found")
|
||||
},
|
||||
}
|
||||
router, _ := setupControllerTest(sc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/github/issues/acme/widget/99/info", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("expected 500, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpGetIssueInfo_GitHubAPIErrorStatus(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
apiStatus int
|
||||
wantStatus int
|
||||
}{
|
||||
{name: "not found", apiStatus: http.StatusNotFound, wantStatus: http.StatusNotFound},
|
||||
{name: "unauthorized", apiStatus: http.StatusUnauthorized, wantStatus: http.StatusUnauthorized},
|
||||
{name: "forbidden", apiStatus: http.StatusForbidden, wantStatus: http.StatusForbidden},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
sc := &stubClient{
|
||||
getIssueFunc: func(context.Context, string, string, int) (*Issue, error) {
|
||||
return nil, &GitHubAPIError{
|
||||
StatusCode: tt.apiStatus,
|
||||
Endpoint: "/repos/acme/widget/issues/99",
|
||||
Body: "upstream error",
|
||||
}
|
||||
},
|
||||
}
|
||||
router, _ := setupControllerTest(sc)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/v1/github/issues/acme/widget/99/info", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != tt.wantStatus {
|
||||
t.Fatalf("expected %d, got %d", tt.wantStatus, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpMergePR_Success(t *testing.T) {
|
||||
var called struct {
|
||||
owner string
|
||||
|
||||
@@ -204,11 +204,39 @@ type ghPR struct {
|
||||
} `json:"author"`
|
||||
}
|
||||
|
||||
type ghIssue struct {
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
URL string `json:"url"`
|
||||
State string `json:"state"`
|
||||
Body string `json:"body"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
// The gh CLI currently emits an empty string for open issues.
|
||||
ClosedAt string `json:"closedAt"`
|
||||
Author struct {
|
||||
Login string `json:"login"`
|
||||
} `json:"author"`
|
||||
Labels []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"labels"`
|
||||
Assignees []struct {
|
||||
Login string `json:"login"`
|
||||
} `json:"assignees"`
|
||||
}
|
||||
|
||||
func (c *GHClient) GetPR(ctx context.Context, owner, repo string, number int) (*PR, error) {
|
||||
out, err := c.run(ctx, "pr", "view", fmt.Sprintf("%d", number),
|
||||
"--repo", fmt.Sprintf("%s/%s", owner, repo),
|
||||
"--json", "number,title,url,state,body,headRefName,headRefOid,baseRefName,author,isDraft,mergeable,mergeStateStatus,additions,deletions,createdAt,updatedAt,mergedAt,closedAt,reviewRequests")
|
||||
if err != nil {
|
||||
if isNotFoundErr(err) {
|
||||
return nil, &GitHubAPIError{
|
||||
StatusCode: http.StatusNotFound,
|
||||
Endpoint: fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repo, number),
|
||||
Body: err.Error(),
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("get PR #%d: %w", number, err)
|
||||
}
|
||||
var raw ghPR
|
||||
@@ -218,6 +246,27 @@ func (c *GHClient) GetPR(ctx context.Context, owner, repo string, number int) (*
|
||||
return convertGHPR(&raw, owner, repo), nil
|
||||
}
|
||||
|
||||
func (c *GHClient) GetIssue(ctx context.Context, owner, repo string, number int) (*Issue, error) {
|
||||
out, err := c.run(ctx, "issue", "view", fmt.Sprintf("%d", number),
|
||||
"--repo", fmt.Sprintf("%s/%s", owner, repo),
|
||||
"--json", "number,title,url,state,body,author,labels,assignees,createdAt,updatedAt,closedAt")
|
||||
if err != nil {
|
||||
if isNotFoundErr(err) {
|
||||
return nil, &GitHubAPIError{
|
||||
StatusCode: http.StatusNotFound,
|
||||
Endpoint: fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, number),
|
||||
Body: err.Error(),
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("get issue #%d: %w", number, err)
|
||||
}
|
||||
var raw ghIssue
|
||||
if err := json.Unmarshal([]byte(out), &raw); err != nil {
|
||||
return nil, fmt.Errorf("parse issue response: %w", err)
|
||||
}
|
||||
return convertGHIssue(&raw, owner, repo), nil
|
||||
}
|
||||
|
||||
func (c *GHClient) FindPRByBranch(ctx context.Context, owner, repo, branch string) (*PR, error) {
|
||||
out, err := c.run(ctx, "pr", "list",
|
||||
"--repo", fmt.Sprintf("%s/%s", owner, repo),
|
||||
@@ -1053,6 +1102,33 @@ func convertGHPR(raw *ghPR, owner, repo string) *PR {
|
||||
return pr
|
||||
}
|
||||
|
||||
func convertGHIssue(raw *ghIssue, owner, repo string) *Issue {
|
||||
labels := make([]string, len(raw.Labels))
|
||||
for i, label := range raw.Labels {
|
||||
labels[i] = label.Name
|
||||
}
|
||||
assignees := make([]string, len(raw.Assignees))
|
||||
for i, assignee := range raw.Assignees {
|
||||
assignees[i] = assignee.Login
|
||||
}
|
||||
return &Issue{
|
||||
Number: raw.Number,
|
||||
Title: raw.Title,
|
||||
URL: raw.URL,
|
||||
HTMLURL: raw.URL,
|
||||
State: strings.ToLower(raw.State),
|
||||
Body: raw.Body,
|
||||
AuthorLogin: raw.Author.Login,
|
||||
RepoOwner: owner,
|
||||
RepoName: repo,
|
||||
Labels: labels,
|
||||
Assignees: assignees,
|
||||
CreatedAt: raw.CreatedAt,
|
||||
UpdatedAt: raw.UpdatedAt,
|
||||
ClosedAt: parseTimePtr(raw.ClosedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func convertGHRequestedReviewers(raw []ghRequestedReviewer) []RequestedReviewer {
|
||||
reviewers := make([]RequestedReviewer, 0, len(raw))
|
||||
for _, req := range raw {
|
||||
|
||||
@@ -18,6 +18,12 @@ type prKey struct {
|
||||
Number int
|
||||
}
|
||||
|
||||
type issueKey struct {
|
||||
Owner string
|
||||
Repo string
|
||||
Number int
|
||||
}
|
||||
|
||||
// branchKey is a composite key for PR lookups by owner/repo/branch.
|
||||
type branchKey struct {
|
||||
Owner string
|
||||
@@ -70,6 +76,7 @@ type MockClient struct {
|
||||
// the whole mock client out of the wiring.
|
||||
reposUnavailable bool
|
||||
prs map[prKey]*PR
|
||||
issues map[issueKey]*Issue
|
||||
prsByBranch map[branchKey]*PR
|
||||
orgs []GitHubOrg
|
||||
repos map[string][]GitHubRepo
|
||||
@@ -115,6 +122,7 @@ func NewMockClient() *MockClient {
|
||||
user: mockDefaultUser,
|
||||
authenticated: true,
|
||||
prs: make(map[prKey]*PR),
|
||||
issues: make(map[issueKey]*Issue),
|
||||
prsByBranch: make(map[branchKey]*PR),
|
||||
repos: make(map[string][]GitHubRepo),
|
||||
branches: make(map[repoKey][]RepoBranch),
|
||||
@@ -152,6 +160,16 @@ func (m *MockClient) GetPR(_ context.Context, owner, repo string, number int) (*
|
||||
return pr, nil
|
||||
}
|
||||
|
||||
func (m *MockClient) GetIssue(_ context.Context, owner, repo string, number int) (*Issue, error) {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
issue, ok := m.issues[issueKey{owner, repo, number}]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mock: issue %s/%s#%d not found", owner, repo, number)
|
||||
}
|
||||
return issue, nil
|
||||
}
|
||||
|
||||
func (m *MockClient) FindPRByBranch(_ context.Context, owner, repo, branch string) (*PR, error) {
|
||||
m.findPRByBranchCalls.Add(1)
|
||||
m.mu.RLock()
|
||||
@@ -523,6 +541,12 @@ func (m *MockClient) AddPR(pr *PR) {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockClient) AddIssue(issue *Issue) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.issues[issueKey{issue.RepoOwner, issue.RepoName, issue.Number}] = issue
|
||||
}
|
||||
|
||||
// AddOrgs appends organizations to the mock data store.
|
||||
func (m *MockClient) AddOrgs(orgs []GitHubOrg) {
|
||||
m.mu.Lock()
|
||||
@@ -625,6 +649,7 @@ func (m *MockClient) Reset() {
|
||||
m.authError = ""
|
||||
m.reposUnavailable = false
|
||||
m.prs = make(map[prKey]*PR)
|
||||
m.issues = make(map[issueKey]*Issue)
|
||||
m.prsByBranch = make(map[branchKey]*PR)
|
||||
m.orgs = nil
|
||||
m.repos = make(map[string][]GitHubRepo)
|
||||
|
||||
@@ -69,6 +69,37 @@ func TestMockClient_AddPR_GetPR(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_AddIssueGetIssueAndReset(t *testing.T) {
|
||||
m := NewMockClient()
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := m.GetIssue(ctx, "owner", "repo", 1456)
|
||||
if err == nil || err.Error() != "mock: issue owner/repo#1456 not found" {
|
||||
t.Fatalf("expected missing issue error, got %v", err)
|
||||
}
|
||||
|
||||
m.AddIssue(&Issue{
|
||||
Number: 1456,
|
||||
Title: "Test issue",
|
||||
RepoOwner: "owner",
|
||||
RepoName: "repo",
|
||||
})
|
||||
|
||||
issue, err := m.GetIssue(ctx, "owner", "repo", 1456)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if issue.Title != "Test issue" {
|
||||
t.Fatalf("expected title 'Test issue', got %q", issue.Title)
|
||||
}
|
||||
|
||||
m.Reset()
|
||||
_, err = m.GetIssue(ctx, "owner", "repo", 1456)
|
||||
if err == nil || err.Error() != "mock: issue owner/repo#1456 not found" {
|
||||
t.Fatalf("expected reset issue error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_FindPRByBranch(t *testing.T) {
|
||||
m := NewMockClient()
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -35,6 +35,7 @@ func (c *MockController) RegisterRoutes(router *gin.Engine) {
|
||||
api := router.Group("/api/v1/github/mock")
|
||||
api.PUT("/user", c.setUser)
|
||||
api.POST("/prs", c.addPRs)
|
||||
api.POST("/issues", c.addIssues)
|
||||
api.POST("/orgs", c.addOrgs)
|
||||
api.POST("/repos", c.addRepos)
|
||||
api.POST("/reviews", c.addReviews)
|
||||
@@ -111,6 +112,20 @@ func (c *MockController) addPRs(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, gin.H{"added": len(req.PRs)})
|
||||
}
|
||||
|
||||
func (c *MockController) addIssues(ctx *gin.Context) {
|
||||
var req struct {
|
||||
Issues []Issue `json:"issues"`
|
||||
}
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"})
|
||||
return
|
||||
}
|
||||
for i := range req.Issues {
|
||||
c.mock.AddIssue(&req.Issues[i])
|
||||
}
|
||||
ctx.JSON(http.StatusOK, gin.H{"added": len(req.Issues)})
|
||||
}
|
||||
|
||||
func (c *MockController) addOrgs(ctx *gin.Context) {
|
||||
var req struct {
|
||||
Orgs []GitHubOrg `json:"orgs"`
|
||||
|
||||
@@ -1,11 +1,69 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func setupMockControllerTestForAddIssues() (*gin.Engine, *MockClient) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
mock := NewMockClient()
|
||||
ctrl := NewMockController(mock, nil, nil, nil, newControllerTestLogger())
|
||||
router := gin.New()
|
||||
ctrl.RegisterRoutes(router)
|
||||
return router, mock
|
||||
}
|
||||
|
||||
func TestMockControllerAddIssues(t *testing.T) {
|
||||
router, mock := setupMockControllerTestForAddIssues()
|
||||
body := bytes.NewBufferString(`{"issues":[{"number":1456,"title":"Fix picker","repo_owner":"owner","repo_name":"repo"}]}`)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/github/mock/issues", body)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var got struct {
|
||||
Added int `json:"added"`
|
||||
}
|
||||
if err := json.NewDecoder(w.Body).Decode(&got); err != nil {
|
||||
t.Fatalf("decode response: %v", err)
|
||||
}
|
||||
if got.Added != 1 {
|
||||
t.Fatalf("expected added=1, got %d", got.Added)
|
||||
}
|
||||
issue, err := mock.GetIssue(context.Background(), "owner", "repo", 1456)
|
||||
if err != nil {
|
||||
t.Fatalf("get seeded issue: %v", err)
|
||||
}
|
||||
if issue.Title != "Fix picker" {
|
||||
t.Fatalf("expected seeded issue title, got %q", issue.Title)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockControllerAddIssuesInvalidPayload(t *testing.T) {
|
||||
router, _ := setupMockControllerTestForAddIssues()
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/api/v1/github/mock/issues", bytes.NewBufferString(`{`))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureMockPRForRequestCopiesMergeableState(t *testing.T) {
|
||||
mock := NewMockClient()
|
||||
controller := &MockController{mock: mock}
|
||||
|
||||
@@ -25,6 +25,10 @@ func (c *NoopClient) GetPR(context.Context, string, string, int) (*PR, error) {
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
|
||||
func (c *NoopClient) GetIssue(context.Context, string, string, int) (*Issue, error) {
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
|
||||
func (c *NoopClient) FindPRByBranch(context.Context, string, string, string) (*PR, error) {
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
|
||||
@@ -165,6 +165,15 @@ func (c *PATClient) GetPR(ctx context.Context, owner, repo string, number int) (
|
||||
return convertPatPR(&raw, owner, repo), nil
|
||||
}
|
||||
|
||||
func (c *PATClient) GetIssue(ctx context.Context, owner, repo string, number int) (*Issue, error) {
|
||||
var raw patIssue
|
||||
endpoint := fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, number)
|
||||
if err := c.get(ctx, endpoint, &raw); err != nil {
|
||||
return nil, fmt.Errorf("get issue #%d: %w", number, err)
|
||||
}
|
||||
return convertPatIssue(&raw, owner, repo), nil
|
||||
}
|
||||
|
||||
func (c *PATClient) FindPRByBranch(ctx context.Context, owner, repo, branch string) (*PR, error) {
|
||||
statuses, err := runBatchedBranchQuery(ctx, c, []graphQLBranchRef{{
|
||||
Owner: owner,
|
||||
@@ -905,6 +914,26 @@ type patPR struct {
|
||||
} `json:"base"`
|
||||
}
|
||||
|
||||
type patIssue struct {
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
Body string `json:"body"`
|
||||
State string `json:"state"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ClosedAt *string `json:"closed_at"`
|
||||
User struct {
|
||||
Login string `json:"login"`
|
||||
} `json:"user"`
|
||||
Labels []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"labels"`
|
||||
Assignees []struct {
|
||||
Login string `json:"login"`
|
||||
} `json:"assignees"`
|
||||
}
|
||||
|
||||
type patSearchItem struct {
|
||||
Number int `json:"number"`
|
||||
Title string `json:"title"`
|
||||
@@ -958,6 +987,36 @@ func convertPatPR(raw *patPR, owner, repo string) *PR {
|
||||
return pr
|
||||
}
|
||||
|
||||
func convertPatIssue(raw *patIssue, owner, repo string) *Issue {
|
||||
labels := make([]string, len(raw.Labels))
|
||||
for i, label := range raw.Labels {
|
||||
labels[i] = label.Name
|
||||
}
|
||||
assignees := make([]string, len(raw.Assignees))
|
||||
for i, assignee := range raw.Assignees {
|
||||
assignees[i] = assignee.Login
|
||||
}
|
||||
issue := &Issue{
|
||||
Number: raw.Number,
|
||||
Title: raw.Title,
|
||||
URL: raw.HTMLURL,
|
||||
HTMLURL: raw.HTMLURL,
|
||||
State: strings.ToLower(raw.State),
|
||||
Body: raw.Body,
|
||||
AuthorLogin: raw.User.Login,
|
||||
RepoOwner: owner,
|
||||
RepoName: repo,
|
||||
Labels: labels,
|
||||
Assignees: assignees,
|
||||
CreatedAt: raw.CreatedAt,
|
||||
UpdatedAt: raw.UpdatedAt,
|
||||
}
|
||||
if raw.ClosedAt != nil {
|
||||
issue.ClosedAt = parseTimePtr(*raw.ClosedAt)
|
||||
}
|
||||
return issue
|
||||
}
|
||||
|
||||
func convertPatRequestedReviewers(raw *patPR) []RequestedReviewer {
|
||||
reviewers := make([]RequestedReviewer, 0, len(raw.RequestedReviewers)+len(raw.RequestedTeams))
|
||||
for _, reviewer := range raw.RequestedReviewers {
|
||||
|
||||
@@ -102,11 +102,20 @@ func pickDefaultMergeMethod(m RepoMergeMethods) string {
|
||||
// GetPR fetches basic PR details from GitHub.
|
||||
func (s *Service) GetPR(ctx context.Context, owner, repo string, number int) (*PR, error) {
|
||||
if s.client == nil {
|
||||
return nil, fmt.Errorf("github client not available")
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
return s.client.GetPR(ctx, owner, repo, number)
|
||||
}
|
||||
|
||||
// GetIssue fetches basic issue details from GitHub. The create-task dialog is
|
||||
// currently the only caller and dedupes requests per URL on the frontend.
|
||||
func (s *Service) GetIssue(ctx context.Context, owner, repo string, number int) (*Issue, error) {
|
||||
if s.client == nil {
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
return s.client.GetIssue(ctx, owner, repo, number)
|
||||
}
|
||||
|
||||
// GetPRFeedback fetches live PR feedback from GitHub. Cached briefly with
|
||||
// singleflight coalescing: the task page fires this for the same PR many times
|
||||
// in quick succession (chat-bar CI chip, detail panel, hover popover — each on
|
||||
|
||||
@@ -179,8 +179,8 @@ func TestGetPR_NilClient(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("expected error when client is nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "github client not available") {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
if !errors.Is(err, ErrNoClient) {
|
||||
t.Errorf("err = %v, want ErrNoClient", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user