Routed via the feature label. Stages: assess (read-only agent with a structured decide tool — verdict as a tool call, not parsed prose; not-a-fit closes the issue as won't-do with reasons) -> PRD (read-only agent; problem, solution, surface, acceptance criteria, non-goals) -> deliver (PRD posted as the watched issue comment, optional [notify] webhook POST for n8n->email, task -> waiting_human). The daemon's cycle now watches waiting_human tasks: replies after the watch comment (excluding the bot) are judged by an LLM (triage role, APPROVE/REJECT/UNCLEAR + reason — never keyword matching). Approvals requeue under implement (whose planner now treats an existing PRD as prior context); rejections close with the reasoning acknowledged; unclear replies keep waiting. Supporting changes: forge.Comment returns the comment id; forge.RepliesAfter; store watch_comment_id (additive migration), WaitingTasks, Requeue; Route honours a pre-assigned workflow; AgentStage.Extra for stage-specific tools. Live: todo#7 assessed fit -> PRD delivered -> waiting_human.
156 lines
4.9 KiB
Go
156 lines
4.9 KiB
Go
// Package forge wraps the GitHub API surface archied needs: polling
|
|
// labelled issues, accepting collaborator invitations, commenting, and
|
|
// opening pull requests. Gitea support later lands as a second
|
|
// implementation behind the same methods.
|
|
package forge
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
|
|
"github.com/google/go-github/v78/github"
|
|
)
|
|
|
|
type Client struct {
|
|
gh *github.Client
|
|
log *slog.Logger
|
|
}
|
|
|
|
func New(token string, log *slog.Logger) *Client {
|
|
return &Client{gh: github.NewClient(nil).WithAuthToken(token), log: log}
|
|
}
|
|
|
|
// AcceptInvitations auto-accepts pending repository invitations so
|
|
// adding archie as a collaborator is all a human has to do.
|
|
func (c *Client) AcceptInvitations(ctx context.Context) error {
|
|
invites, _, err := c.gh.Users.ListInvitations(ctx, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("list invitations: %w", err)
|
|
}
|
|
for _, inv := range invites {
|
|
if _, err := c.gh.Users.AcceptInvitation(ctx, inv.GetID()); err != nil {
|
|
c.log.Warn("accept invitation failed", "repo", inv.GetRepo().GetFullName(), "err", err)
|
|
continue
|
|
}
|
|
c.log.Info("accepted repository invitation", "repo", inv.GetRepo().GetFullName())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AssignedIssues returns open issues assigned to the given user,
|
|
// excluding PRs. Assigning an issue to the bot is how work is handed to
|
|
// archie (tink-bot style); labels only influence workflow routing.
|
|
func (c *Client) AssignedIssues(ctx context.Context, owner, repo, assignee string) ([]*github.Issue, error) {
|
|
var out []*github.Issue
|
|
opts := &github.IssueListByRepoOptions{
|
|
State: "open",
|
|
Assignee: assignee,
|
|
ListOptions: github.ListOptions{PerPage: 50},
|
|
}
|
|
for {
|
|
issues, resp, err := c.gh.Issues.ListByRepo(ctx, owner, repo, opts)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list issues %s/%s: %w", owner, repo, err)
|
|
}
|
|
for _, is := range issues {
|
|
if !is.IsPullRequest() {
|
|
out = append(out, is)
|
|
}
|
|
}
|
|
if resp.NextPage == 0 {
|
|
break
|
|
}
|
|
opts.ListOptions.Page = resp.NextPage
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// Comment posts an issue (or PR) comment and returns its id, so a
|
|
// workflow can watch for replies that come after it.
|
|
func (c *Client) Comment(ctx context.Context, owner, repo string, number int, body string) (int64, error) {
|
|
cm, _, err := c.gh.Issues.CreateComment(ctx, owner, repo, number,
|
|
&github.IssueComment{Body: github.Ptr(body)})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return cm.GetID(), nil
|
|
}
|
|
|
|
// Reply is a human comment on a watched issue.
|
|
type Reply struct {
|
|
ID int64
|
|
User string
|
|
Body string
|
|
}
|
|
|
|
// RepliesAfter returns comments on the issue with id > afterID that were
|
|
// not written by exclude (the bot) — the human side of waiting_human.
|
|
func (c *Client) RepliesAfter(ctx context.Context, owner, repo string, number int, afterID int64, exclude string) ([]Reply, error) {
|
|
comments, _, err := c.gh.Issues.ListComments(ctx, owner, repo, number,
|
|
&github.IssueListCommentsOptions{ListOptions: github.ListOptions{PerPage: 50}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out []Reply
|
|
for _, cm := range comments {
|
|
if cm.GetID() <= afterID || cm.GetUser().GetLogin() == exclude {
|
|
continue
|
|
}
|
|
out = append(out, Reply{ID: cm.GetID(), User: cm.GetUser().GetLogin(), Body: cm.GetBody()})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// CreatePR opens a pull request and returns its number.
|
|
func (c *Client) CreatePR(ctx context.Context, owner, repo, title, head, base, body string) (int, error) {
|
|
pr, _, err := c.gh.PullRequests.Create(ctx, owner, repo, &github.NewPullRequest{
|
|
Title: github.Ptr(title),
|
|
Head: github.Ptr(head),
|
|
Base: github.Ptr(base),
|
|
Body: github.Ptr(body),
|
|
})
|
|
if err != nil {
|
|
return 0, fmt.Errorf("create PR %s/%s: %w", owner, repo, err)
|
|
}
|
|
return pr.GetNumber(), nil
|
|
}
|
|
|
|
// PRState returns "open", "merged", or "closed" for a PR.
|
|
func (c *Client) PRState(ctx context.Context, owner, repo string, number int) (string, error) {
|
|
pr, _, err := c.gh.PullRequests.Get(ctx, owner, repo, number)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if pr.GetMerged() {
|
|
return "merged", nil
|
|
}
|
|
return pr.GetState(), nil
|
|
}
|
|
|
|
// CloseIssue closes an issue with a final comment (feasibility "won't do").
|
|
func (c *Client) CloseIssue(ctx context.Context, owner, repo string, number int, comment string) error {
|
|
if comment != "" {
|
|
if _, err := c.Comment(ctx, owner, repo, number, comment); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
_, _, err := c.gh.Issues.Edit(ctx, owner, repo, number,
|
|
&github.IssueRequest{State: github.Ptr("closed")})
|
|
return err
|
|
}
|
|
|
|
// VerifyPush confirms the token can push to the repo (permission check
|
|
// at startup so misconfiguration surfaces before any work is claimed).
|
|
func (c *Client) VerifyPush(ctx context.Context, owner, repo string) error {
|
|
r, _, err := c.gh.Repositories.Get(ctx, owner, repo)
|
|
if err != nil {
|
|
return fmt.Errorf("get repo %s/%s: %w", owner, repo, err)
|
|
}
|
|
perms := r.GetPermissions()
|
|
if !perms["push"] {
|
|
return fmt.Errorf("no push permission on %s/%s", owner, repo)
|
|
}
|
|
return nil
|
|
}
|