This package now includes comprehensive pull request (PR) capabilities that support both GitHub and Gerrit platforms. The implementation provides a unified interface for managing pull requests across different code hosting platforms.
package main import ( "context" "github.com/iomodo/staff/git" "net/http" "time" ) func main() { ctx := context.Background() // Create GitHub configuration githubConfig := git.GitHubConfig{ Token: "your-github-token", BaseURL: "https://api.github.com", HTTPClient: &http.Client{Timeout: 30 * time.Second}, } // Create GitHub provider githubProvider := git.NewGitHubPullRequestProvider("owner", "repo", githubConfig) // Create Git instance with pull request capabilities git := git.NewGitWithPullRequests("/path/to/repo", git.GitConfig{}, githubProvider) // Create a pull request prOptions := git.PullRequestOptions{ Title: "Add new feature", Description: "This PR adds a new feature to the application.", BaseBranch: "main", HeadBranch: "feature/new-feature", Labels: []string{"enhancement", "feature"}, Assignees: []string{"username1", "username2"}, Reviewers: []string{"reviewer1", "reviewer2"}, Draft: false, } pr, err := git.CreatePullRequest(ctx, prOptions) if err != nil { log.Fatal(err) } fmt.Printf("Created pull request: %s (#%d)\n", pr.Title, pr.Number) }
package main import ( "context" "github.com/iomodo/staff/git" "net/http" "time" ) func main() { ctx := context.Background() // Create Gerrit configuration gerritConfig := git.GerritConfig{ Username: "your-username", Password: "your-http-password-or-api-token", BaseURL: "https://gerrit.example.com", HTTPClient: &http.Client{Timeout: 30 * time.Second}, } // Create Gerrit provider gerritProvider := git.NewGerritPullRequestProvider("project-name", gerritConfig) // Create Git instance with pull request capabilities git := git.NewGitWithPullRequests("/path/to/repo", git.GitConfig{}, gerritProvider) // Create a change (pull request) prOptions := git.PullRequestOptions{ Title: "Add new feature", Description: "This change adds a new feature to the application.", BaseBranch: "master", HeadBranch: "feature/new-feature", } pr, err := git.CreatePullRequest(ctx, prOptions) if err != nil { log.Fatal(err) } fmt.Printf("Created change: %s (#%d)\n", pr.Title, pr.Number) }
Represents a pull request or merge request across platforms.
type PullRequest struct { ID string Number int Title string Description string State string // "open", "closed", "merged" Author Author CreatedAt time.Time UpdatedAt time.Time BaseBranch string HeadBranch string BaseRepo string HeadRepo string Labels []string Assignees []Author Reviewers []Author Commits []Commit Comments []PullRequestComment }
Options for creating or updating pull requests.
type PullRequestOptions struct {
Title string
Description string
BaseBranch string
HeadBranch string
BaseRepo string
HeadRepo string
Labels []string
Assignees []string
Reviewers []string
Draft bool
}
Options for listing pull requests.
type ListPullRequestOptions struct { State string // "open", "closed", "all" Author string Assignee string BaseBranch string HeadBranch string Labels []string Limit int }
Options for merging pull requests.
type MergePullRequestOptions struct { MergeMethod string // "merge", "squash", "rebase" CommitTitle string CommitMsg string }
Creates a new pull request.
func (g *Git) CreatePullRequest(ctx context.Context, options PullRequestOptions) (*PullRequest, error)
Retrieves a pull request by ID.
func (g *Git) GetPullRequest(ctx context.Context, id string) (*PullRequest, error)
Lists pull requests with optional filtering.
func (g *Git) ListPullRequests(ctx context.Context, options ListPullRequestOptions) ([]PullRequest, error)
Updates an existing pull request.
func (g *Git) UpdatePullRequest(ctx context.Context, id string, options PullRequestOptions) (*PullRequest, error)
Closes a pull request.
func (g *Git) ClosePullRequest(ctx context.Context, id string) error
Merges a pull request.
func (g *Git) MergePullRequest(ctx context.Context, id string, options MergePullRequestOptions) error
type GitHubConfig struct { Token string // GitHub personal access token BaseURL string // GitHub API base URL (default: https://api.github.com) HTTPClient *http.Client // Custom HTTP client (optional) }
type GerritConfig struct { Username string // Gerrit username Password string // HTTP password or API token BaseURL string // Gerrit instance URL HTTPClient *http.Client // Custom HTTP client (optional) }
All pull request operations return detailed error information through the GitError type:
type GitError struct {
Command string
Output string
Err error
}
Common error scenarios:
See pull_request_example.go for comprehensive examples of using both GitHub and Gerrit providers.
Run the tests to ensure everything works correctly:
go test ./git/... -v
When adding support for new platforms:
PullRequestProvider interfaceThis code is part of the staff project and follows the same licensing terms.