Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
564 changes: 518 additions & 46 deletions cmd/margin-gui/assets/review.js

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions cmd/margin-gui/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -963,3 +963,49 @@ button.linkish:hover { text-decoration: underline; }
.gh-reply .gh-md { margin-top: 0; }
.m-gh { color: var(--accent); }
.modal.gh-panel .gh-confirm > .comment { margin: 6px 0; }

/* GitHub panel: every thread with what the agent found beside it */
.modal.gh-panel { width: min(1400px, 95vw); height: 88vh; max-height: 88vh; }
.gh-panel-body { display: flex; flex-direction: column; gap: 8px; }
.gh-filters { position: sticky; top: -10px; z-index: 2; margin: 0 -14px; padding: 8px 14px; background: var(--bg-1); border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 6px; }
.gh-frow { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; }
.gh-frow .spacer { flex: 1; }
.gh-flabel { font-size: 11.5px; color: var(--fg-faint); margin: 0 2px 0 6px; }
.gh-flabel:first-child { margin-left: 0; min-width: 72px; }
.gh-search, .gh-extra { font: 12.5px var(--sans, inherit); background: var(--bg); color: var(--fg); border: 1px solid var(--line); border-radius: 6px; padding: 3px 8px; }
.gh-search { width: 240px; }
.gh-extra { flex: 1; min-width: 200px; }
.gh-search:focus, .gh-extra:focus { border-color: var(--accent); outline: none; }
.gh-cards { display: flex; flex-direction: column; gap: 10px; }
.gh-card { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 14px; padding: 10px 12px; border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: 8px; background: var(--bg); scroll-margin-top: 120px; }
.gh-card.gh-is-resolved { border-left-color: var(--ok); }
.gh-card.gh-is-resolved .gh-card-main { opacity: .8; }
.gh-card.gh-focus { box-shadow: 0 0 0 2px var(--accent); }
@media (max-width: 900px) { .gh-card { grid-template-columns: minmax(0, 1fr); } }
.gh-card-main { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.gh-card-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.gh-card-head .spacer { flex: 1; }
.gh-loc { font: 12.5px var(--mono); }
.badge.gh-open { background: var(--issue-tint); color: var(--bad-fg); }
.gh-turn { font-size: 13px; }
.gh-turn > b { font-size: 12.5px; }
.gh-turn-reply { border-left: 2px solid var(--line); padding-left: 10px; margin-left: 2px; }
.gh-card-side { min-width: 0; }
.gh-check { border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; background: var(--bg-1); font-size: 13px; }
.gh-check.compact { margin-top: 6px; font-size: 12.5px; padding: 6px 8px; }
.gh-check-none { border-style: dashed; background: none; }
.gh-check-done { border-color: var(--ok); background: var(--good-tint); }
.gh-check-not-done { border-color: var(--issue); background: var(--bad-tint); }
.gh-check-partly, .gh-check-unclear { border-color: var(--warn); }
.gh-check-head { display: flex; align-items: center; gap: 6px; }
.gh-reply-box:empty { display: none; }
.gh-reply-box .ans-reply textarea { min-height: 70px; }
.badge.gh-status-done { background: var(--good-tint); color: var(--good-fg); }
.badge.gh-status-not-done { background: var(--bad-tint); color: var(--bad-fg); }
.badge.gh-status-partly, .badge.gh-status-unclear { color: var(--warn-fg); border: 1px solid var(--warn); }
.badge.gh-status-pending { color: var(--fg-dim); }
.gh-ref { font: 12px var(--mono); }
.gh-conv > summary { cursor: pointer; list-style: revert; }
.gh-conv > summary h4 { display: inline; }
.gh-conv { display: flex; flex-direction: column; gap: 8px; }
.gh-back { position: fixed; left: 16px; bottom: 84px; z-index: 30; padding: 6px 12px; border-radius: 18px; background: var(--primary-bg); color: var(--on-primary); border: 1px solid var(--primary-line); box-shadow: 0 6px 20px rgba(0, 0, 0, .35); cursor: pointer; font-size: 12.5px; }
213 changes: 213 additions & 0 deletions cmd/margin-gui/ghstatus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
package main

import (
"errors"
"fmt"
"os"
"regexp"
"strings"

"github.com/redrick/margin/internal/fsx"
"github.com/redrick/margin/internal/github"
"github.com/redrick/margin/internal/review"
"github.com/redrick/margin/internal/text"
)

// The GitHub note is the markdown file, beside the review, where the agent says where the pull
// request's review threads stand: one section per thread, headed by the thread's id. margin writes
// each section with the thread as GitHub has it and "Status: pending"; the agent replaces the status
// and says what it found below it.

// GitHubCheck is what the agent said about one thread.
type GitHubCheck struct {
// Status is done, partly, not done, unclear, or pending while the agent has not answered.
Status string `json:"status"`
Text string `json:"text"`
}

const ghPending = "pending"

var ghHeading = regexp.MustCompile(`(?m)^## +(\S+)`)
var ghStatusLine = regexp.MustCompile(`(?mi)^[*_\s]*status[*_\s]*:[*_\s]*([^\n]*)$`)

// ghSections splits the note into its thread sections, by thread id, in the order they come.
func ghSections(doc string) (ids []string, sections map[string]string) {
sections = map[string]string{}
hs := ghHeading.FindAllStringSubmatchIndex(doc, -1)
for i, h := range hs {
end := len(doc)
if i+1 < len(hs) {
end = hs[i+1][0]
}
id := doc[h[2]:h[3]]
if _, seen := sections[id]; seen {
continue
}
ids = append(ids, id)
sections[id] = doc[h[0]:end]
}
return ids, sections
}

// ghCheck reads the agent's status and answer out of one section: the last status line, and what
// follows it.
func ghCheck(section string) GitHubCheck {
ms := ghStatusLine.FindAllStringSubmatchIndex(section, -1)
if len(ms) == 0 {
return GitHubCheck{Status: ghPending}
}
m := ms[len(ms)-1]
status := strings.ToLower(strings.Trim(section[m[2]:m[3]], "*_ .`"))
switch {
case strings.HasPrefix(status, "pending"):
status = ghPending
case strings.HasPrefix(status, "not done"), strings.HasPrefix(status, "not fixed"), strings.HasPrefix(status, "open"):
status = "not done"
case strings.HasPrefix(status, "partly"), strings.HasPrefix(status, "partial"):
status = "partly"
case strings.HasPrefix(status, "done"), strings.HasPrefix(status, "fixed"), strings.HasPrefix(status, "resolved"):
status = "done"
default:
status = "unclear"
}
return GitHubCheck{Status: status, Text: strings.TrimSpace(section[m[1]:])}
}

// GitHubChecks reads what the agent said about the pull request's threads, by thread id.
func (r *Reviews) GitHubChecks() (map[string]GitHubCheck, error) {
path := r.current()
if path == "" {
return nil, errors.New("no review open")
}
data, err := os.ReadFile(review.GitHubPath(path))
if errors.Is(err, os.ErrNotExist) {
return map[string]GitHubCheck{}, nil
}
if err != nil {
return nil, err
}
_, secs := ghSections(string(data))
out := map[string]GitHubCheck{}
for id, sec := range secs {
out[id] = ghCheck(sec)
}
return out, nil
}

// AskGitHubStatus writes the threads into the GitHub note, waiting for the agent, and asks the agent
// to check each against the code and say where it stands. Sections of other threads stay.
func (r *Reviews) AskGitHubStatus(ids []string, extra string) (map[string]GitHubCheck, error) {
path := r.current()
if path == "" {
return nil, errors.New("no review open")
}
if !r.agents.Running(path) {
return nil, errors.New("start the agent first, so it can check the threads")
}
if len(ids) == 0 {
return nil, errors.New("no threads to check")
}
d, err := r.document(0)
if err != nil {
return nil, err
}
r.gh.mu.Lock()
pr, known := r.gh.pr, r.gh.threads
if r.gh.path != d.Review.Path {
pr = nil
}
r.gh.mu.Unlock()
if pr == nil {
return nil, errors.New("pull from GitHub first")
}
var threads []*github.Thread
for _, id := range ids {
t := known[id]
if t == nil {
return nil, fmt.Errorf("margin does not know thread %s; pull from GitHub again", id)
}
threads = append(threads, t)
}
note := review.GitHubPath(path)
data, err := os.ReadFile(note)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
doc := string(data)
if strings.TrimSpace(doc) == "" {
doc = fmt.Sprintf("# Review threads on PR #%d\n\nmargin writes each thread below; the agent replaces `Status: pending` and says what it found.\n", pr.Number)
}
for _, t := range threads {
doc = putGHSection(doc, t.ID, ghSection(t))
}
if err := fsx.WriteFileAtomic(note, []byte(doc), 0o644); err != nil {
return nil, err
}
if err := r.agents.Send(path, ghStatusMessage(pr, threads, note, extra)); err != nil {
return nil, fmt.Errorf("written to %s, but not sent: %w", note, err)
}
return r.GitHubChecks()
}

// putGHSection replaces the section of thread id, or adds it at the end.
func putGHSection(doc, id, section string) string {
_, secs := ghSections(doc)
if old, ok := secs[id]; ok {
i := strings.Index(doc, old)
return doc[:i] + section + "\n" + doc[i+len(old):]
}
return strings.TrimRight(doc, "\n") + "\n\n" + section
}

func ghSection(t *github.Thread) string {
var b strings.Builder
where := t.Path
switch {
case t.Line > 0:
where += fmt.Sprintf(":%d", t.Line)
case t.OriginalLine > 0:
where += fmt.Sprintf(":%d (outdated, first written on that line)", t.OriginalLine)
}
state := "open"
if t.Resolved {
state = "resolved"
}
starter := "ghost"
if len(t.Comments) > 0 && t.Comments[0].Author != "" {
starter = t.Comments[0].Author
}
fmt.Fprintf(&b, "## %s\n\n`%s` · started by %s · %s on GitHub\n\n", t.ID, where, starter, state)
for i, c := range t.Comments {
if i > 0 {
b.WriteString(">\n")
}
author := c.Author
if author == "" {
author = "ghost"
}
body := text.Sanitize(strings.TrimSpace(c.Body))
fmt.Fprintf(&b, "> **%s:** %s\n", author, strings.ReplaceAll(body, "\n", "\n> "))
}
b.WriteString("\nStatus: pending\n")
return b.String()
}

func ghStatusMessage(pr *github.PR, threads []*github.Thread, note, extra string) string {
list := make([]string, len(threads))
for i, t := range threads {
list[i] = t.ID
}
msg := fmt.Sprintf("[margin github] The reader wants to know where %d review thread(s) on PR #%d stand (head %s). "+
"Each is written out in %s, one section per thread, headed by its id: %s. "+
"For each section read the thread, then check the current code, and any commit a reply names (git show <sha>), "+
"to decide whether what the thread asks for is really done. "+
"Replace `Status: pending` with exactly one of `Status: done`, `Status: partly`, `Status: not done` or `Status: unclear`, "+
"then below it say in a few short sentences what you found: what was fixed, what is still missing or wrong. "+
"Name the code as repo-relative path:line with current line numbers (e.g. `internal/x/y.go:42`), so the reader can jump there; name commits by short sha. "+
"Do not change code, do not post or resolve anything on GitHub, and leave the quoted thread and the other sections as they are.",
len(threads), pr.Number, pr.HeadSHA[:min(9, len(pr.HeadSHA))], note, strings.Join(list, ", "))
if extra = strings.Join(strings.Fields(text.Sanitize(extra)), " "); extra != "" {
msg += " The reader adds: " + extra
}
return msg
}
46 changes: 46 additions & 0 deletions cmd/margin-gui/ghstatus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"strings"
"testing"

"github.com/redrick/margin/internal/github"
)

func TestGitHubNote(t *testing.T) {
t1 := &github.Thread{ID: "PRRT_a", Path: "x.go", Line: 4, Comments: []github.TComment{
{Author: "me", Body: "rename this\n## not a heading"}, {Author: "bob", Body: "fixed in abc123"}}}
t2 := &github.Thread{ID: "PRRT_b", Path: "y.go", OriginalLine: 9, Outdated: true, Resolved: true,
Comments: []github.TComment{{Author: "me", Body: "why?"}}}

doc := putGHSection("# head\n", t1.ID, ghSection(t1))
doc = putGHSection(doc, t2.ID, ghSection(t2))
ids, secs := ghSections(doc)
if strings.Join(ids, ",") != "PRRT_a,PRRT_b" {
t.Fatalf("ids %v in\n%s", ids, doc)
}
if c := ghCheck(secs["PRRT_a"]); c.Status != ghPending || c.Text != "" {
t.Fatalf("fresh section %+v", c)
}
if !strings.Contains(secs["PRRT_a"], "> **bob:** fixed in abc123") || !strings.Contains(secs["PRRT_b"], "`y.go:9 (outdated") {
t.Fatalf("sections\n%s", doc)
}

// The agent answers the first; asking again about the second keeps that answer.
doc = strings.Replace(doc, "Status: pending\n", "**Status:** Partly.\n\nRenamed, but `x.go:12` still uses the old name.\n", 1)
doc = putGHSection(doc, t2.ID, ghSection(t2))
_, secs = ghSections(doc)
c := ghCheck(secs["PRRT_a"])
if c.Status != "partly" || c.Text != "Renamed, but `x.go:12` still uses the old name." {
t.Fatalf("answer %+v", c)
}
if ghCheck(secs["PRRT_b"]).Status != ghPending || strings.Count(doc, "## PRRT_b") != 1 {
t.Fatalf("second ask\n%s", doc)
}

for in, want := range map[string]string{"Status: done": "done", "status: Not done — x": "not done", "Status: fixed": "done", "Status: hmm": "unclear"} {
if got := ghCheck("## T\n\n" + in + "\n").Status; got != want {
t.Errorf("%q: %q, want %q", in, got, want)
}
}
}
Loading
Loading