Files

91 lines
3.0 KiB
Go

// Package forge talks to the code host a repo lives on, Gitea or GitHub, through one [Forge] interface, so spx
// commands work the same on either.
package forge
import "strings"
// Kind names a forge flavor.
type Kind string
const (
KindGitea Kind = "gitea"
KindGitHub Kind = "github"
)
// KindForHost tells which forge serves `host`: only `github.com` is GitHub, every other host is taken as Gitea.
func KindForHost(host string) Kind {
if strings.EqualFold(host, "github.com") {
return KindGitHub
}
return KindGitea
}
// Issue is the part of an issue spx reads; its JSON form is what `spx issue view --json` prints.
type Issue struct {
Number int `json:"number"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
Author string `json:"author"`
Assignees []string `json:"assignees"`
HTMLURL string `json:"html_url"`
}
// Comment is one comment on an issue or pull request.
type Comment struct {
ID int64 `json:"id"`
Author string `json:"author"`
Body string `json:"body"`
CreatedAt string `json:"created_at"`
HTMLURL string `json:"html_url"`
}
// BranchRef is one side of a pull request: the branch and the commit it pointed at.
type BranchRef struct {
Branch string `json:"branch"`
SHA string `json:"sha"`
}
// PullRequest is the part of a pull request spx reads; its JSON form is what `spx pr view --json` prints.
type PullRequest struct {
Number int `json:"number"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
Merged bool `json:"merged"`
Author string `json:"author"`
Head BranchRef `json:"head"`
Base BranchRef `json:"base"`
HTMLURL string `json:"html_url"`
}
// NewPullRequest is what it takes to open a pull request from branch `Head` into branch `Base`.
type NewPullRequest struct {
Title string
Body string
Head string
Base string
}
// Forge is one repo on a code host, with the few calls spx makes against it.
//
// Pull requests share numbering with issues on both forges, so the issue and comment calls also work on a pull
// request number; that is how pull request comments are read and posted.
type Forge interface {
// Kind tells which forge this is.
Kind() Kind
// CurrentUserLogin returns the login of the user the token belongs to.
CurrentUserLogin() (string, error)
// CreateIssue opens an issue assigned to `assignees`.
CreateIssue(title, body string, assignees []string) (*Issue, error)
GetIssue(number int) (*Issue, error)
UpdateIssueBody(number int, body string) error
// ListIssueComments returns every comment, oldest first, reading all pages.
ListIssueComments(number int) ([]Comment, error)
CreateIssueComment(number int, body string) (*Comment, error)
GetPullRequest(number int) (*PullRequest, error)
CreatePullRequest(pr NewPullRequest) (*PullRequest, error)
// DefaultBranch returns the repo's default branch, the usual base of a new pull request.
DefaultBranch() (string, error)
}