blob: b6664f2dafd5337bbbe231685dfb69320fd9d817 [file] [log] [blame] [view]
iomodo1d173602025-07-26 15:35:57 +04001# Pull Request Capabilities
2
3This 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.
4
5## Features
6
7- **Unified Interface**: Same API for both GitHub and Gerrit
8- **Full CRUD Operations**: Create, read, update, delete pull requests
9- **Advanced Filtering**: List pull requests with various filters
10- **Merge Operations**: Support for different merge strategies
11- **Error Handling**: Comprehensive error handling with detailed messages
12- **Authentication**: Support for token-based and basic authentication
13
14## Supported Platforms
15
16### GitHub
17- Uses GitHub REST API v3
18- Supports personal access tokens for authentication
19- Full support for all pull request operations
20- Handles GitHub-specific features like draft PRs, labels, assignees, and reviewers
21
22### Gerrit
23- Uses Gerrit REST API
24- Supports HTTP password or API token authentication
25- Maps Gerrit "changes" to pull requests
26- Handles Gerrit-specific features like topics and review workflows
27
28## Quick Start
29
30### GitHub Example
31
32```go
33package main
34
35import (
36 "context"
37 "github.com/iomodo/staff/git"
38 "net/http"
39 "time"
40)
41
42func main() {
43 ctx := context.Background()
44
45 // Create GitHub configuration
46 githubConfig := git.GitHubConfig{
47 Token: "your-github-token",
48 BaseURL: "https://api.github.com",
49 HTTPClient: &http.Client{Timeout: 30 * time.Second},
50 }
51
52 // Create GitHub provider
53 githubProvider := git.NewGitHubPullRequestProvider("owner", "repo", githubConfig)
54
55 // Create Git instance with pull request capabilities
56 git := git.NewGitWithPullRequests("/path/to/repo", git.GitConfig{}, githubProvider)
57
58 // Create a pull request
59 prOptions := git.PullRequestOptions{
60 Title: "Add new feature",
61 Description: "This PR adds a new feature to the application.",
62 BaseBranch: "main",
63 HeadBranch: "feature/new-feature",
64 Labels: []string{"enhancement", "feature"},
65 Assignees: []string{"username1", "username2"},
66 Reviewers: []string{"reviewer1", "reviewer2"},
67 Draft: false,
68 }
69
70 pr, err := git.CreatePullRequest(ctx, prOptions)
71 if err != nil {
72 log.Fatal(err)
73 }
74
75 fmt.Printf("Created pull request: %s (#%d)\n", pr.Title, pr.Number)
76}
77```
78
79### Gerrit Example
80
81```go
82package main
83
84import (
85 "context"
86 "github.com/iomodo/staff/git"
87 "net/http"
88 "time"
89)
90
91func main() {
92 ctx := context.Background()
93
94 // Create Gerrit configuration
95 gerritConfig := git.GerritConfig{
96 Username: "your-username",
97 Password: "your-http-password-or-api-token",
98 BaseURL: "https://gerrit.example.com",
99 HTTPClient: &http.Client{Timeout: 30 * time.Second},
100 }
101
102 // Create Gerrit provider
103 gerritProvider := git.NewGerritPullRequestProvider("project-name", gerritConfig)
104
105 // Create Git instance with pull request capabilities
106 git := git.NewGitWithPullRequests("/path/to/repo", git.GitConfig{}, gerritProvider)
107
108 // Create a change (pull request)
109 prOptions := git.PullRequestOptions{
110 Title: "Add new feature",
111 Description: "This change adds a new feature to the application.",
112 BaseBranch: "master",
113 HeadBranch: "feature/new-feature",
114 }
115
116 pr, err := git.CreatePullRequest(ctx, prOptions)
117 if err != nil {
118 log.Fatal(err)
119 }
120
121 fmt.Printf("Created change: %s (#%d)\n", pr.Title, pr.Number)
122}
123```
124
125## API Reference
126
127### Types
128
129#### PullRequest
130Represents a pull request or merge request across platforms.
131
132```go
133type PullRequest struct {
134 ID string
135 Number int
136 Title string
137 Description string
138 State string // "open", "closed", "merged"
139 Author Author
140 CreatedAt time.Time
141 UpdatedAt time.Time
142 BaseBranch string
143 HeadBranch string
144 BaseRepo string
145 HeadRepo string
146 Labels []string
147 Assignees []Author
148 Reviewers []Author
149 Commits []Commit
150 Comments []PullRequestComment
151}
152```
153
154#### PullRequestOptions
155Options for creating or updating pull requests.
156
157```go
158type PullRequestOptions struct {
159 Title string
160 Description string
161 BaseBranch string
162 HeadBranch string
163 BaseRepo string
164 HeadRepo string
165 Labels []string
166 Assignees []string
167 Reviewers []string
168 Draft bool
169}
170```
171
172#### ListPullRequestOptions
173Options for listing pull requests.
174
175```go
176type ListPullRequestOptions struct {
177 State string // "open", "closed", "all"
178 Author string
179 Assignee string
180 BaseBranch string
181 HeadBranch string
182 Labels []string
183 Limit int
184}
185```
186
187#### MergePullRequestOptions
188Options for merging pull requests.
189
190```go
191type MergePullRequestOptions struct {
192 MergeMethod string // "merge", "squash", "rebase"
193 CommitTitle string
194 CommitMsg string
195}
196```
197
198### Methods
199
200#### CreatePullRequest
201Creates a new pull request.
202
203```go
204func (g *Git) CreatePullRequest(ctx context.Context, options PullRequestOptions) (*PullRequest, error)
205```
206
207#### GetPullRequest
208Retrieves a pull request by ID.
209
210```go
211func (g *Git) GetPullRequest(ctx context.Context, id string) (*PullRequest, error)
212```
213
214#### ListPullRequests
215Lists pull requests with optional filtering.
216
217```go
218func (g *Git) ListPullRequests(ctx context.Context, options ListPullRequestOptions) ([]PullRequest, error)
219```
220
221#### UpdatePullRequest
222Updates an existing pull request.
223
224```go
225func (g *Git) UpdatePullRequest(ctx context.Context, id string, options PullRequestOptions) (*PullRequest, error)
226```
227
228#### ClosePullRequest
229Closes a pull request.
230
231```go
232func (g *Git) ClosePullRequest(ctx context.Context, id string) error
233```
234
235#### MergePullRequest
236Merges a pull request.
237
238```go
239func (g *Git) MergePullRequest(ctx context.Context, id string, options MergePullRequestOptions) error
240```
241
242## Configuration
243
244### GitHub Configuration
245
246```go
247type GitHubConfig struct {
248 Token string // GitHub personal access token
249 BaseURL string // GitHub API base URL (default: https://api.github.com)
250 HTTPClient *http.Client // Custom HTTP client (optional)
251}
252```
253
254### Gerrit Configuration
255
256```go
257type GerritConfig struct {
258 Username string // Gerrit username
259 Password string // HTTP password or API token
260 BaseURL string // Gerrit instance URL
261 HTTPClient *http.Client // Custom HTTP client (optional)
262}
263```
264
265## Error Handling
266
267All pull request operations return detailed error information through the `GitError` type:
268
269```go
270type GitError struct {
271 Command string
272 Output string
273 Err error
274}
275```
276
277Common error scenarios:
278- Authentication failures
279- Invalid repository or project names
280- Network connectivity issues
281- API rate limiting
282- Invalid pull request data
283
284## Platform-Specific Notes
285
286### GitHub
287- Requires a personal access token with appropriate permissions
288- Supports draft pull requests
289- Full support for labels, assignees, and reviewers
290- Uses GitHub's REST API v3
291
292### Gerrit
293- Requires HTTP password or API token
294- Uses "changes" instead of "pull requests"
295- Topics are used to group related changes
296- Review workflow is more structured
297- Uses Gerrit's REST API
298
299## Examples
300
301See `pull_request_example.go` for comprehensive examples of using both GitHub and Gerrit providers.
302
303## Testing
304
305Run the tests to ensure everything works correctly:
306
307```bash
308go test ./git/... -v
309```
310
311## Contributing
312
313When adding support for new platforms:
314
3151. Implement the `PullRequestProvider` interface
3162. Add platform-specific configuration types
3173. Create conversion functions to map platform-specific data to our unified types
3184. Add comprehensive tests
3195. Update this documentation
320
321## License
322
323This code is part of the staff project and follows the same licensing terms.