| iomodo | 1d17360 | 2025-07-26 15:35:57 +0400 | [diff] [blame^] | 1 | # Pull Request Capabilities |
| 2 | |
| 3 | 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. |
| 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 |
| 33 | package main |
| 34 | |
| 35 | import ( |
| 36 | "context" |
| 37 | "github.com/iomodo/staff/git" |
| 38 | "net/http" |
| 39 | "time" |
| 40 | ) |
| 41 | |
| 42 | func 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 |
| 82 | package main |
| 83 | |
| 84 | import ( |
| 85 | "context" |
| 86 | "github.com/iomodo/staff/git" |
| 87 | "net/http" |
| 88 | "time" |
| 89 | ) |
| 90 | |
| 91 | func 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 |
| 130 | Represents a pull request or merge request across platforms. |
| 131 | |
| 132 | ```go |
| 133 | type 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 |
| 155 | Options for creating or updating pull requests. |
| 156 | |
| 157 | ```go |
| 158 | type 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 |
| 173 | Options for listing pull requests. |
| 174 | |
| 175 | ```go |
| 176 | type 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 |
| 188 | Options for merging pull requests. |
| 189 | |
| 190 | ```go |
| 191 | type MergePullRequestOptions struct { |
| 192 | MergeMethod string // "merge", "squash", "rebase" |
| 193 | CommitTitle string |
| 194 | CommitMsg string |
| 195 | } |
| 196 | ``` |
| 197 | |
| 198 | ### Methods |
| 199 | |
| 200 | #### CreatePullRequest |
| 201 | Creates a new pull request. |
| 202 | |
| 203 | ```go |
| 204 | func (g *Git) CreatePullRequest(ctx context.Context, options PullRequestOptions) (*PullRequest, error) |
| 205 | ``` |
| 206 | |
| 207 | #### GetPullRequest |
| 208 | Retrieves a pull request by ID. |
| 209 | |
| 210 | ```go |
| 211 | func (g *Git) GetPullRequest(ctx context.Context, id string) (*PullRequest, error) |
| 212 | ``` |
| 213 | |
| 214 | #### ListPullRequests |
| 215 | Lists pull requests with optional filtering. |
| 216 | |
| 217 | ```go |
| 218 | func (g *Git) ListPullRequests(ctx context.Context, options ListPullRequestOptions) ([]PullRequest, error) |
| 219 | ``` |
| 220 | |
| 221 | #### UpdatePullRequest |
| 222 | Updates an existing pull request. |
| 223 | |
| 224 | ```go |
| 225 | func (g *Git) UpdatePullRequest(ctx context.Context, id string, options PullRequestOptions) (*PullRequest, error) |
| 226 | ``` |
| 227 | |
| 228 | #### ClosePullRequest |
| 229 | Closes a pull request. |
| 230 | |
| 231 | ```go |
| 232 | func (g *Git) ClosePullRequest(ctx context.Context, id string) error |
| 233 | ``` |
| 234 | |
| 235 | #### MergePullRequest |
| 236 | Merges a pull request. |
| 237 | |
| 238 | ```go |
| 239 | func (g *Git) MergePullRequest(ctx context.Context, id string, options MergePullRequestOptions) error |
| 240 | ``` |
| 241 | |
| 242 | ## Configuration |
| 243 | |
| 244 | ### GitHub Configuration |
| 245 | |
| 246 | ```go |
| 247 | type 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 |
| 257 | type 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 | |
| 267 | All pull request operations return detailed error information through the `GitError` type: |
| 268 | |
| 269 | ```go |
| 270 | type GitError struct { |
| 271 | Command string |
| 272 | Output string |
| 273 | Err error |
| 274 | } |
| 275 | ``` |
| 276 | |
| 277 | Common 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 | |
| 301 | See `pull_request_example.go` for comprehensive examples of using both GitHub and Gerrit providers. |
| 302 | |
| 303 | ## Testing |
| 304 | |
| 305 | Run the tests to ensure everything works correctly: |
| 306 | |
| 307 | ```bash |
| 308 | go test ./git/... -v |
| 309 | ``` |
| 310 | |
| 311 | ## Contributing |
| 312 | |
| 313 | When adding support for new platforms: |
| 314 | |
| 315 | 1. Implement the `PullRequestProvider` interface |
| 316 | 2. Add platform-specific configuration types |
| 317 | 3. Create conversion functions to map platform-specific data to our unified types |
| 318 | 4. Add comprehensive tests |
| 319 | 5. Update this documentation |
| 320 | |
| 321 | ## License |
| 322 | |
| 323 | This code is part of the staff project and follows the same licensing terms. |