85aa3ac0aa
* feat(github): add /github page for PRs and issues Dedicated page under /github lists pull requests and issues across the authenticated user's repos with preset filters, custom GitHub search queries, per-repo narrowing, saved queries, and one-click task launching for any PR or issue. Performance: - Cache user PR/issue search responses for 30s with singleflight to coalesce concurrent misses, so preset switching and pagination avoid upstream round trips. - Replace the per-row PR status fetch (N+1) with a batch endpoint POST /api/v1/github/prs/statuses that fans out concurrently on the backend with a semaphore and caches per-PR. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * style: apply Prettier formatting Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: address review feedback on PR cache + status batching - Harden searchCacheKey against separator collisions from user-controllable customQuery (length-prefix encoding). - Drop repositories.loaded hydration when inner workspace fetch fails so the client refetches instead of trusting a partial snapshot. - Guard useGitHubSearch with a monotonic request counter to drop stale responses when filter+page changes overlap. - Fix usePRStatuses teardown: depend only on composed key so a new array identity per render doesn't cancel the in-flight batch. - Correct godocs (duplicate, outdated state:open claim). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: address additional review feedback - Restore state:open for issue watcher: buildIssueSearchQuery no longer injects it as a default, so existing watches were starting to surface closed issues. Inject state:open in buildIssueFilter so only the watcher path is affected; the /github page presets already set their own state qualifiers. - countCheckResults now excludes in-progress checks from the denominator so a `1/3` badge reflects pass rate on completed checks, not a mix of running and failed. The pending state is still carried via ChecksState. - usePRStatuses retries after a transient fetch error (only mark the key as completed once the fetch resolves). This also fixes missing badges under React Strict Mode's intentional double-mount, since the first effect's cleanup fires before the response lands and the guard stays unset for the second mount to retry. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * test(github): add tests for my-github hooks Covers use-committed-query, buildParams from use-github-search, use-saved-presets readStorage validation, use-known-repos recordForKey accumulator semantics, and use-pr-statuses batch fetch lifecycle. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix: address review feedback on search cache and repo accumulator - Clamp perPage before composing the search cache key so identical pages (e.g. perPage=150 and =100) share a cache entry instead of creating duplicates keyed on the raw value. - Add resetKnownReposStore and call it on /github unmount so the module-level repo accumulator doesn't carry across navigations. - Rename use-pr-statuses test to match its assertion (it verifies stale statuses are cleared when the key changes and fetch fails). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * style: apply Prettier formatting Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * fix(web): distinguish blocked from dirty in PR mergeable badge GitHub's blocked state (required reviews/checks/branch protection) is not a merge conflict, so don't label it "Conflicts". Surface it as "Blocked" with the pending tone; keep "Conflicts" for dirty. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix: address remaining /github code review comments - Route PR status + batch through handleSearchError so ErrNoClient surfaces as 503 instead of 500 (matches the search endpoints). - Sort workflow steps by position in the quick-task-launcher default pick, matching the convention in issue-watch-dialog. - Make PresetItem keyboard-accessible: role=button, tabIndex, and Enter/Space handling on the wrapping div (it contains a nested delete button, so it can't itself be a button). - Extract useRepoOptions out of useGitHubPageState to bring the hook back under the 100-line complexity limit. - Honor ctx.Done() when acquiring the PR-status batch semaphore so a cancelled HTTP request releases queued goroutines instead of running each upstream fetch to completion. - Clarify parsePaginationQuery doc: the upper bound (100) is enforced by clampSearchPage in the client layer, not here. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * fix(web): address /github review follow-ups - Merge duplicate @/lib/utils imports in pr-list. - Use rel="noopener noreferrer" on external PR/issue links (codebase convention) and add cursor-pointer per the UI guidelines. - Rewrite onOpenSaveDialog as an explicit if-block so the callback returns void rather than boolean | void. - Use PaginationEllipsis in its own PaginationItem so the ellipsis and the adjacent page link are separate list items instead of visually fused into a single cell. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * refactor(web): derive QuickTaskLauncher dialog state from props Replaces the useEffect+setState anti-pattern with useMemo-derived state driven directly by the payload prop. Drops the eslint suppression that was needed for react-hooks/set-state-in-effect. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Kandev Agent <agent@kandev.dev> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
613 lines
17 KiB
Go
613 lines
17 KiB
Go
package github
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// buildReviewSearchQuery assembles the full GitHub search query.
|
|
// When customQuery is non-empty, it is used verbatim as the entire query.
|
|
// Otherwise a query is built from scope + optional filter qualifier.
|
|
func buildReviewSearchQuery(scope, filter, customQuery string) string {
|
|
if customQuery != "" {
|
|
return customQuery
|
|
}
|
|
var base string
|
|
if scope == ReviewScopeUser {
|
|
base = "type:pr state:open user-review-requested:@me -is:draft"
|
|
} else {
|
|
base = "type:pr state:open review-requested:@me -is:draft"
|
|
}
|
|
if filter != "" {
|
|
base += " " + filter
|
|
}
|
|
return base
|
|
}
|
|
|
|
// getPRFeedback fetches aggregated feedback for a PR using any Client implementation.
|
|
// This shared function eliminates duplication between GHClient and PATClient.
|
|
func getPRFeedback(ctx context.Context, c Client, owner, repo string, number int) (*PRFeedback, error) {
|
|
pr, err := c.GetPR(ctx, owner, repo, number)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reviews, err := c.ListPRReviews(ctx, owner, repo, number)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
comments, err := c.ListPRComments(ctx, owner, repo, number, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
checks, err := c.ListCheckRuns(ctx, owner, repo, pr.HeadSHA)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Ensure non-nil slices so JSON serialization produces [] instead of null.
|
|
if reviews == nil {
|
|
reviews = []PRReview{}
|
|
}
|
|
if comments == nil {
|
|
comments = []PRComment{}
|
|
}
|
|
if checks == nil {
|
|
checks = []CheckRun{}
|
|
}
|
|
hasIssues := hasFailingChecks(checks) || hasChangesRequested(reviews)
|
|
return &PRFeedback{
|
|
PR: pr,
|
|
Reviews: reviews,
|
|
Comments: comments,
|
|
Checks: checks,
|
|
HasIssues: hasIssues,
|
|
}, nil
|
|
}
|
|
|
|
// getPRStatus fetches lightweight PR status using any Client implementation.
|
|
// Unlike getPRFeedback, it skips comments to reduce API calls.
|
|
func getPRStatus(ctx context.Context, c Client, owner, repo string, number int) (*PRStatus, error) {
|
|
pr, err := c.GetPR(ctx, owner, repo, number)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reviews, err := c.ListPRReviews(ctx, owner, repo, number)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
checks, err := c.ListCheckRuns(ctx, owner, repo, pr.HeadSHA)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if reviews == nil {
|
|
reviews = []PRReview{}
|
|
}
|
|
if checks == nil {
|
|
checks = []CheckRun{}
|
|
}
|
|
reviewState, pendingReviewCount := deriveReviewSyncState(pr, reviews)
|
|
total, passing := countCheckResults(checks)
|
|
return &PRStatus{
|
|
PR: pr,
|
|
ReviewState: reviewState,
|
|
ChecksState: computeOverallCheckStatus(checks),
|
|
MergeableState: pr.MergeableState,
|
|
ReviewCount: len(reviews),
|
|
PendingReviewCount: pendingReviewCount,
|
|
ChecksTotal: total,
|
|
ChecksPassing: passing,
|
|
}, nil
|
|
}
|
|
|
|
// countCheckResults returns (total, passing) counts using the same logic as
|
|
// computeOverallCheckStatus: skipped/neutral and still-running checks are
|
|
// excluded from the total so the X/Y badge communicates pass rate rather
|
|
// than conflating in-flight checks with failures. The pending state is
|
|
// carried separately via ChecksState.
|
|
func countCheckResults(checks []CheckRun) (int, int) {
|
|
total, passing := 0, 0
|
|
for _, c := range checks {
|
|
if c.Status != checkStatusCompleted {
|
|
continue
|
|
}
|
|
switch c.Conclusion {
|
|
case checkConclusionSkipped, checkConclusionNeutral:
|
|
continue
|
|
case checkConclusionFail, checkConclusionTimedOut,
|
|
checkConclusionCancelled, checkConclusionActionRequired:
|
|
total++
|
|
default:
|
|
total++
|
|
passing++
|
|
}
|
|
}
|
|
return total, passing
|
|
}
|
|
|
|
// convertRawCheckRuns converts raw ghCheckRun structs into the domain CheckRun type.
|
|
func convertRawCheckRuns(raw []ghCheckRun) []CheckRun {
|
|
checks := make([]CheckRun, len(raw))
|
|
for i, cr := range raw {
|
|
conclusion := ""
|
|
if cr.Conclusion != nil {
|
|
conclusion = *cr.Conclusion
|
|
}
|
|
output := ""
|
|
if cr.Output.Summary != nil {
|
|
output = *cr.Output.Summary
|
|
}
|
|
checks[i] = CheckRun{
|
|
Name: cr.Name,
|
|
Source: checkSourceCheckRun,
|
|
Status: cr.Status,
|
|
Conclusion: conclusion,
|
|
HTMLURL: cr.HTMLURL,
|
|
Output: output,
|
|
StartedAt: parseTimePtr(cr.StartedAt),
|
|
CompletedAt: parseTimePtr(cr.CompletedAt),
|
|
}
|
|
}
|
|
return checks
|
|
}
|
|
|
|
// convertRawComments converts raw review comments into the domain PRComment type.
|
|
func convertRawComments(raw []ghComment) []PRComment {
|
|
comments := make([]PRComment, len(raw))
|
|
for i, c := range raw {
|
|
comments[i] = PRComment{
|
|
ID: c.ID,
|
|
Author: c.User.Login,
|
|
AuthorAvatar: c.User.AvatarURL,
|
|
AuthorIsBot: isGitHubBot(c.User.Type),
|
|
Body: c.Body,
|
|
Path: c.Path,
|
|
Line: c.Line,
|
|
Side: c.Side,
|
|
CommentType: commentTypeReview,
|
|
CreatedAt: c.CreatedAt,
|
|
UpdatedAt: c.UpdatedAt,
|
|
InReplyTo: c.InReplyTo,
|
|
}
|
|
}
|
|
return comments
|
|
}
|
|
|
|
// ghIssueComment is the JSON shape for issue comments from the GitHub API.
|
|
type ghIssueComment struct {
|
|
ID int64 `json:"id"`
|
|
Body string `json:"body"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
User struct {
|
|
Login string `json:"login"`
|
|
AvatarURL string `json:"avatar_url"`
|
|
Type string `json:"type"`
|
|
} `json:"user"`
|
|
}
|
|
|
|
// convertRawIssueComments converts PR conversation comments into PRComment rows.
|
|
func convertRawIssueComments(raw []ghIssueComment) []PRComment {
|
|
comments := make([]PRComment, len(raw))
|
|
for i, c := range raw {
|
|
comments[i] = PRComment{
|
|
ID: c.ID,
|
|
Author: c.User.Login,
|
|
AuthorAvatar: c.User.AvatarURL,
|
|
AuthorIsBot: isGitHubBot(c.User.Type),
|
|
Body: c.Body,
|
|
CommentType: commentTypeIssue,
|
|
CreatedAt: c.CreatedAt,
|
|
UpdatedAt: c.UpdatedAt,
|
|
}
|
|
}
|
|
return comments
|
|
}
|
|
|
|
// mergeAndSortPRComments combines review and issue comments and sorts by creation time.
|
|
func mergeAndSortPRComments(reviewComments, issueComments []PRComment) []PRComment {
|
|
comments := make([]PRComment, 0, len(reviewComments)+len(issueComments))
|
|
comments = append(comments, reviewComments...)
|
|
comments = append(comments, issueComments...)
|
|
sort.SliceStable(comments, func(i, j int) bool {
|
|
if comments[i].CreatedAt.Equal(comments[j].CreatedAt) {
|
|
return comments[i].ID < comments[j].ID
|
|
}
|
|
return comments[i].CreatedAt.Before(comments[j].CreatedAt)
|
|
})
|
|
return comments
|
|
}
|
|
|
|
func isGitHubBot(userType string) bool {
|
|
return strings.EqualFold(userType, githubUserTypeBot)
|
|
}
|
|
|
|
// ghStatusContext is the JSON shape from /commits/:ref/status.
|
|
type ghStatusContext struct {
|
|
Context string `json:"context"`
|
|
State string `json:"state"` // error, failure, pending, success
|
|
TargetURL string `json:"target_url"`
|
|
Description string `json:"description"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
}
|
|
|
|
// convertRawStatusContexts converts commit status contexts into CheckRun rows.
|
|
func convertRawStatusContexts(raw []ghStatusContext) []CheckRun {
|
|
checks := make([]CheckRun, len(raw))
|
|
for i, st := range raw {
|
|
status := checkStatusCompleted
|
|
conclusion := st.State
|
|
switch st.State {
|
|
case commitStatusSuccess:
|
|
conclusion = checkStatusSuccess
|
|
case commitStatusFailure, commitStatusError:
|
|
conclusion = checkConclusionFail
|
|
case commitStatusPending:
|
|
status = "in_progress"
|
|
conclusion = ""
|
|
}
|
|
checks[i] = CheckRun{
|
|
Name: st.Context,
|
|
Source: checkSourceStatusContext,
|
|
Status: status,
|
|
Conclusion: conclusion,
|
|
HTMLURL: st.TargetURL,
|
|
Output: st.Description,
|
|
StartedAt: parseTimePtr(st.CreatedAt),
|
|
}
|
|
if status == checkStatusCompleted {
|
|
checks[i].CompletedAt = parseTimePtr(st.UpdatedAt)
|
|
}
|
|
}
|
|
return checks
|
|
}
|
|
|
|
// mergeChecks deduplicates check runs and commit statuses by normalized name.
|
|
// Check-run source wins over status-context; among same-source duplicates the
|
|
// most recent entry (by StartedAt) is kept.
|
|
func mergeChecks(checkRuns, statusChecks []CheckRun) []CheckRun {
|
|
merged := make([]CheckRun, 0, len(checkRuns)+len(statusChecks))
|
|
byKey := make(map[string]int)
|
|
|
|
for _, check := range statusChecks {
|
|
key := checkMergeKey(check)
|
|
if idx, ok := byKey[key]; ok {
|
|
if isNewerCheck(check, merged[idx]) {
|
|
merged[idx] = check
|
|
}
|
|
continue
|
|
}
|
|
byKey[key] = len(merged)
|
|
merged = append(merged, check)
|
|
}
|
|
for _, check := range checkRuns {
|
|
key := checkMergeKey(check)
|
|
if idx, ok := byKey[key]; ok {
|
|
if merged[idx].Source == checkSourceStatusContext || isNewerCheck(check, merged[idx]) {
|
|
merged[idx] = check
|
|
}
|
|
continue
|
|
}
|
|
byKey[key] = len(merged)
|
|
merged = append(merged, check)
|
|
}
|
|
return merged
|
|
}
|
|
|
|
func checkMergeKey(check CheckRun) string {
|
|
return strings.ToLower(strings.TrimSpace(check.Name))
|
|
}
|
|
|
|
// isNewerCheck reports whether a started more recently than b.
|
|
func isNewerCheck(a, b CheckRun) bool {
|
|
if a.StartedAt == nil {
|
|
return false
|
|
}
|
|
if b.StartedAt == nil {
|
|
return true
|
|
}
|
|
return a.StartedAt.After(*b.StartedAt)
|
|
}
|
|
|
|
// convertSearchItemToPR converts common search result fields into a PR struct.
|
|
func convertSearchItemToPR(
|
|
number int, title, htmlURL, state, authorLogin, repositoryURL string,
|
|
draft bool, createdAt, updatedAt time.Time,
|
|
) *PR {
|
|
owner, repo := parseRepoURL(repositoryURL)
|
|
return &PR{
|
|
Number: number,
|
|
Title: title,
|
|
HTMLURL: htmlURL,
|
|
State: state,
|
|
AuthorLogin: authorLogin,
|
|
Draft: draft,
|
|
RepoOwner: owner,
|
|
RepoName: repo,
|
|
CreatedAt: createdAt,
|
|
UpdatedAt: updatedAt,
|
|
}
|
|
}
|
|
|
|
// clampSearchPage normalizes page and perPage to GitHub's Search API limits:
|
|
// page >= 1, 1 <= perPage <= 100. Defaults: page=1, perPage=50.
|
|
func clampSearchPage(page, perPage int) (int, int) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if perPage <= 0 {
|
|
perPage = 50
|
|
}
|
|
if perPage > 100 {
|
|
perPage = 100
|
|
}
|
|
return page, perPage
|
|
}
|
|
|
|
// hasTypeQualifier reports whether the query already pins the result type via
|
|
// `type:pr`, `type:issue`, `is:pr`, or `is:issue` (case-insensitive).
|
|
func hasTypeQualifier(query string) bool {
|
|
lower := strings.ToLower(query)
|
|
for _, tok := range strings.Fields(lower) {
|
|
switch tok {
|
|
case "type:pr", "type:issue", "is:pr", "is:issue":
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// buildIssueSearchQuery assembles a GitHub search query for issues.
|
|
// The `type:issue` qualifier is always injected (unless the caller already
|
|
// pinned it) because `/search/issues` returns both PRs and issues otherwise.
|
|
func buildIssueSearchQuery(filter, customQuery string) string {
|
|
return buildSearchQuery("type:issue", filter, customQuery)
|
|
}
|
|
|
|
// buildPRSearchQuery assembles a GitHub search query for pull requests.
|
|
// The `type:pr` qualifier is always injected (unless the caller already pinned
|
|
// it) because `/search/issues` returns both PRs and issues otherwise.
|
|
func buildPRSearchQuery(filter, customQuery string) string {
|
|
return buildSearchQuery("type:pr", filter, customQuery)
|
|
}
|
|
|
|
func buildSearchQuery(typeQualifier, filter, customQuery string) string {
|
|
if customQuery != "" {
|
|
if hasTypeQualifier(customQuery) {
|
|
return customQuery
|
|
}
|
|
return typeQualifier + " " + customQuery
|
|
}
|
|
if filter != "" {
|
|
return typeQualifier + " " + filter
|
|
}
|
|
return typeQualifier
|
|
}
|
|
|
|
// issueSearchItem is an issue item from the GitHub search API.
|
|
type issueSearchItem struct {
|
|
Number int `json:"number"`
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
HTMLURL string `json:"html_url"`
|
|
State string `json:"state"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
ClosedAt *string `json:"closed_at"`
|
|
RepositoryURL string `json:"repository_url"`
|
|
User struct {
|
|
Login string `json:"login"`
|
|
} `json:"user"`
|
|
Labels []struct {
|
|
Name string `json:"name"`
|
|
} `json:"labels"`
|
|
Assignees []struct {
|
|
Login string `json:"login"`
|
|
} `json:"assignees"`
|
|
PullRequest *struct {
|
|
URL string `json:"url"`
|
|
} `json:"pull_request"`
|
|
}
|
|
|
|
// convertSearchItemToIssue converts a search API item into an Issue struct.
|
|
func convertSearchItemToIssue(item issueSearchItem) *Issue {
|
|
owner, repo := parseRepoURL(item.RepositoryURL)
|
|
labels := make([]string, len(item.Labels))
|
|
for i, l := range item.Labels {
|
|
labels[i] = l.Name
|
|
}
|
|
assignees := make([]string, len(item.Assignees))
|
|
for i, a := range item.Assignees {
|
|
assignees[i] = a.Login
|
|
}
|
|
issue := &Issue{
|
|
Number: item.Number,
|
|
Title: item.Title,
|
|
Body: item.Body,
|
|
HTMLURL: item.HTMLURL,
|
|
State: item.State,
|
|
AuthorLogin: item.User.Login,
|
|
RepoOwner: owner,
|
|
RepoName: repo,
|
|
Labels: labels,
|
|
Assignees: assignees,
|
|
CreatedAt: item.CreatedAt,
|
|
UpdatedAt: item.UpdatedAt,
|
|
}
|
|
if item.ClosedAt != nil {
|
|
issue.ClosedAt = parseTimePtr(*item.ClosedAt)
|
|
}
|
|
return issue
|
|
}
|
|
|
|
// parseIssueSearchResults parses GitHub search API response items into Issue structs,
|
|
// filtering out any pull requests (which also appear in search/issues results).
|
|
func parseIssueSearchResults(items []issueSearchItem) []*Issue {
|
|
var issues []*Issue
|
|
for _, item := range items {
|
|
// GitHub search/issues returns both issues and PRs; skip PRs
|
|
if item.PullRequest != nil {
|
|
continue
|
|
}
|
|
issues = append(issues, convertSearchItemToIssue(item))
|
|
}
|
|
return issues
|
|
}
|
|
|
|
// latestReviewByAuthor returns a map of the most recent review per author.
|
|
func latestReviewByAuthor(reviews []PRReview) map[string]PRReview {
|
|
latest := make(map[string]PRReview)
|
|
for _, r := range reviews {
|
|
existing, ok := latest[r.Author]
|
|
if !ok || r.CreatedAt.After(existing.CreatedAt) {
|
|
latest[r.Author] = r
|
|
}
|
|
}
|
|
return latest
|
|
}
|
|
|
|
// hasFailingChecks returns true if any completed check run has failed.
|
|
func hasFailingChecks(checks []CheckRun) bool {
|
|
for _, c := range checks {
|
|
if c.Status == checkStatusCompleted && c.Conclusion == checkConclusionFail {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// hasChangesRequested returns true if any review has requested changes.
|
|
func hasChangesRequested(reviews []PRReview) bool {
|
|
for _, r := range reviews {
|
|
if r.State == reviewStateChangesRequested {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// --- PR files and commits parsing ---
|
|
|
|
// ghPRFile is the JSON shape from the GitHub REST API for PR files.
|
|
type ghPRFile struct {
|
|
Filename string `json:"filename"`
|
|
Status string `json:"status"` // added, removed, modified, renamed, copied, changed, unchanged
|
|
Additions int `json:"additions"`
|
|
Deletions int `json:"deletions"`
|
|
Patch string `json:"patch"`
|
|
PreviousFilename string `json:"previous_filename"`
|
|
}
|
|
|
|
// ghPRCommit is the JSON shape from the GitHub REST API for PR commits.
|
|
type ghPRCommit struct {
|
|
SHA string `json:"sha"`
|
|
Commit struct {
|
|
Message string `json:"message"`
|
|
Author struct {
|
|
Date string `json:"date"`
|
|
} `json:"author"`
|
|
} `json:"commit"`
|
|
Author *struct {
|
|
Login string `json:"login"`
|
|
} `json:"author"`
|
|
}
|
|
|
|
// parsePRFilesJSON parses the JSON response from the PR files API.
|
|
func parsePRFilesJSON(data string) ([]PRFile, error) {
|
|
var raw []ghPRFile
|
|
if err := json.Unmarshal([]byte(data), &raw); err != nil {
|
|
return nil, fmt.Errorf("parse PR files: %w", err)
|
|
}
|
|
return convertRawPRFiles(raw), nil
|
|
}
|
|
|
|
// convertRawPRFiles converts raw GitHub API file structs to domain PRFile.
|
|
func convertRawPRFiles(raw []ghPRFile) []PRFile {
|
|
files := make([]PRFile, len(raw))
|
|
for i, f := range raw {
|
|
files[i] = PRFile{
|
|
Filename: f.Filename,
|
|
Status: f.Status,
|
|
Additions: f.Additions,
|
|
Deletions: f.Deletions,
|
|
Patch: buildFullDiff(f.Filename, f.Status, f.Patch, f.PreviousFilename),
|
|
OldPath: f.PreviousFilename,
|
|
}
|
|
}
|
|
return files
|
|
}
|
|
|
|
// buildFullDiff wraps a GitHub patch fragment into a complete unified diff.
|
|
func buildFullDiff(filename, status, patch, previousFilename string) string {
|
|
if patch == "" {
|
|
return ""
|
|
}
|
|
var b strings.Builder
|
|
switch status {
|
|
case "added":
|
|
fmt.Fprintf(&b, "diff --git a/%s b/%s\n", filename, filename)
|
|
b.WriteString("new file mode 100644\n")
|
|
fmt.Fprintf(&b, "--- /dev/null\n")
|
|
fmt.Fprintf(&b, "+++ b/%s\n", filename)
|
|
case "removed":
|
|
fmt.Fprintf(&b, "diff --git a/%s b/%s\n", filename, filename)
|
|
b.WriteString("deleted file mode 100644\n")
|
|
fmt.Fprintf(&b, "--- a/%s\n", filename)
|
|
fmt.Fprintf(&b, "+++ /dev/null\n")
|
|
case "renamed":
|
|
oldName := previousFilename
|
|
if oldName == "" {
|
|
oldName = filename
|
|
}
|
|
fmt.Fprintf(&b, "diff --git a/%s b/%s\n", oldName, filename)
|
|
fmt.Fprintf(&b, "rename from %s\n", oldName)
|
|
fmt.Fprintf(&b, "rename to %s\n", filename)
|
|
fmt.Fprintf(&b, "--- a/%s\n", oldName)
|
|
fmt.Fprintf(&b, "+++ b/%s\n", filename)
|
|
default:
|
|
fmt.Fprintf(&b, "diff --git a/%s b/%s\n", filename, filename)
|
|
fmt.Fprintf(&b, "--- a/%s\n", filename)
|
|
fmt.Fprintf(&b, "+++ b/%s\n", filename)
|
|
}
|
|
b.WriteString(patch)
|
|
if !strings.HasSuffix(patch, "\n") {
|
|
b.WriteByte('\n')
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// parsePRCommitsJSON parses the JSON response from the PR commits API.
|
|
func parsePRCommitsJSON(data string) ([]PRCommitInfo, error) {
|
|
var raw []ghPRCommit
|
|
if err := json.Unmarshal([]byte(data), &raw); err != nil {
|
|
return nil, fmt.Errorf("parse PR commits: %w", err)
|
|
}
|
|
return convertRawPRCommits(raw), nil
|
|
}
|
|
|
|
// convertRawPRCommits converts raw GitHub API commit structs to domain PRCommitInfo.
|
|
func convertRawPRCommits(raw []ghPRCommit) []PRCommitInfo {
|
|
commits := make([]PRCommitInfo, len(raw))
|
|
for i, c := range raw {
|
|
author := ""
|
|
if c.Author != nil {
|
|
author = c.Author.Login
|
|
}
|
|
// First line of commit message
|
|
msg := c.Commit.Message
|
|
if idx := strings.Index(msg, "\n"); idx >= 0 {
|
|
msg = msg[:idx]
|
|
}
|
|
commits[i] = PRCommitInfo{
|
|
SHA: c.SHA,
|
|
Message: msg,
|
|
AuthorLogin: author,
|
|
AuthorDate: c.Commit.Author.Date,
|
|
}
|
|
}
|
|
return commits
|
|
}
|