| iomodo | 1d17360 | 2025-07-26 15:35:57 +0400 | [diff] [blame] | 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "net/http" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // ExamplePullRequestUsage demonstrates how to use the pull request capabilities |
| 12 | func ExamplePullRequestUsage() { |
| 13 | ctx := context.Background() |
| 14 | |
| 15 | // Example 1: GitHub Pull Requests |
| 16 | exampleGitHubPullRequests(ctx) |
| 17 | |
| 18 | // Example 2: Gerrit Pull Requests |
| 19 | exampleGerritPullRequests(ctx) |
| 20 | } |
| 21 | |
| 22 | func exampleGitHubPullRequests(ctx context.Context) { |
| 23 | fmt.Println("=== GitHub Pull Request Example ===") |
| 24 | |
| 25 | // Create GitHub configuration |
| 26 | githubConfig := GitHubConfig{ |
| 27 | Token: "your-github-token-here", |
| 28 | BaseURL: "https://api.github.com", |
| 29 | HTTPClient: &http.Client{Timeout: 30 * time.Second}, |
| 30 | } |
| 31 | |
| 32 | // Create GitHub pull request provider |
| 33 | githubProvider := NewGitHubPullRequestProvider("owner", "repo", githubConfig) |
| 34 | |
| 35 | // Create Git instance with GitHub pull request capabilities |
| 36 | git := NewGitWithPullRequests("/path/to/repo", GitConfig{ |
| 37 | Timeout: 30 * time.Second, |
| 38 | }, githubProvider) |
| 39 | |
| 40 | // Create a new pull request |
| 41 | prOptions := PullRequestOptions{ |
| 42 | Title: "Add new feature", |
| 43 | Description: "This PR adds a new feature to the application.", |
| 44 | BaseBranch: "main", |
| 45 | HeadBranch: "feature/new-feature", |
| 46 | Labels: []string{"enhancement", "feature"}, |
| 47 | Assignees: []string{"username1", "username2"}, |
| 48 | Reviewers: []string{"reviewer1", "reviewer2"}, |
| 49 | Draft: false, |
| 50 | } |
| 51 | |
| 52 | pr, err := git.CreatePullRequest(ctx, prOptions) |
| 53 | if err != nil { |
| 54 | log.Printf("Failed to create pull request: %v", err) |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | fmt.Printf("Created pull request: %s (#%d)\n", pr.Title, pr.Number) |
| 59 | |
| 60 | // List pull requests |
| 61 | listOptions := ListPullRequestOptions{ |
| 62 | State: "open", |
| 63 | Author: "username", |
| 64 | BaseBranch: "main", |
| 65 | Limit: 10, |
| 66 | } |
| 67 | |
| 68 | prs, err := git.ListPullRequests(ctx, listOptions) |
| 69 | if err != nil { |
| 70 | log.Printf("Failed to list pull requests: %v", err) |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | fmt.Printf("Found %d pull requests\n", len(prs)) |
| 75 | |
| 76 | // Get a specific pull request |
| 77 | pr, err = git.GetPullRequest(ctx, pr.ID) |
| 78 | if err != nil { |
| 79 | log.Printf("Failed to get pull request: %v", err) |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | fmt.Printf("Pull request status: %s\n", pr.State) |
| 84 | |
| 85 | // Update a pull request |
| 86 | updateOptions := PullRequestOptions{ |
| 87 | Title: "Updated title", |
| 88 | Description: "Updated description", |
| 89 | Labels: []string{"bug", "urgent"}, |
| 90 | } |
| 91 | |
| 92 | updatedPR, err := git.UpdatePullRequest(ctx, pr.ID, updateOptions) |
| 93 | if err != nil { |
| 94 | log.Printf("Failed to update pull request: %v", err) |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | fmt.Printf("Updated pull request: %s\n", updatedPR.Title) |
| 99 | |
| 100 | // Merge a pull request |
| 101 | mergeOptions := MergePullRequestOptions{ |
| 102 | MergeMethod: "squash", |
| 103 | CommitTitle: "Merge pull request #123", |
| 104 | CommitMsg: "This merges the feature branch into main", |
| 105 | } |
| 106 | |
| 107 | err = git.MergePullRequest(ctx, pr.ID, mergeOptions) |
| 108 | if err != nil { |
| 109 | log.Printf("Failed to merge pull request: %v", err) |
| 110 | return |
| 111 | } |
| 112 | |
| 113 | fmt.Println("Pull request merged successfully") |
| 114 | } |
| 115 | |
| 116 | func exampleGerritPullRequests(ctx context.Context) { |
| 117 | fmt.Println("=== Gerrit Pull Request Example ===") |
| 118 | |
| 119 | // Create Gerrit configuration |
| 120 | gerritConfig := GerritConfig{ |
| 121 | Username: "your-username", |
| 122 | Password: "your-http-password-or-api-token", |
| 123 | BaseURL: "https://gerrit.example.com", |
| 124 | HTTPClient: &http.Client{Timeout: 30 * time.Second}, |
| 125 | } |
| 126 | |
| 127 | // Create Gerrit pull request provider |
| 128 | gerritProvider := NewGerritPullRequestProvider("project-name", gerritConfig) |
| 129 | |
| 130 | // Create Git instance with Gerrit pull request capabilities |
| 131 | git := NewGitWithPullRequests("/path/to/repo", GitConfig{ |
| 132 | Timeout: 30 * time.Second, |
| 133 | }, gerritProvider) |
| 134 | |
| 135 | // Create a new change (pull request) |
| 136 | prOptions := PullRequestOptions{ |
| 137 | Title: "Add new feature", |
| 138 | Description: "This change adds a new feature to the application.", |
| 139 | BaseBranch: "master", |
| 140 | HeadBranch: "feature/new-feature", |
| 141 | } |
| 142 | |
| 143 | pr, err := git.CreatePullRequest(ctx, prOptions) |
| 144 | if err != nil { |
| 145 | log.Printf("Failed to create change: %v", err) |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | fmt.Printf("Created change: %s (#%d)\n", pr.Title, pr.Number) |
| 150 | |
| 151 | // List changes |
| 152 | listOptions := ListPullRequestOptions{ |
| 153 | State: "open", |
| 154 | Author: "username", |
| 155 | BaseBranch: "master", |
| 156 | Limit: 10, |
| 157 | } |
| 158 | |
| 159 | prs, err := git.ListPullRequests(ctx, listOptions) |
| 160 | if err != nil { |
| 161 | log.Printf("Failed to list changes: %v", err) |
| 162 | return |
| 163 | } |
| 164 | |
| 165 | fmt.Printf("Found %d changes\n", len(prs)) |
| 166 | |
| 167 | // Get a specific change |
| 168 | pr, err = git.GetPullRequest(ctx, pr.ID) |
| 169 | if err != nil { |
| 170 | log.Printf("Failed to get change: %v", err) |
| 171 | return |
| 172 | } |
| 173 | |
| 174 | fmt.Printf("Change status: %s\n", pr.State) |
| 175 | |
| 176 | // Update a change |
| 177 | updateOptions := PullRequestOptions{ |
| 178 | Title: "Updated title", |
| 179 | Description: "Updated description", |
| 180 | } |
| 181 | |
| 182 | updatedPR, err := git.UpdatePullRequest(ctx, pr.ID, updateOptions) |
| 183 | if err != nil { |
| 184 | log.Printf("Failed to update change: %v", err) |
| 185 | return |
| 186 | } |
| 187 | |
| 188 | fmt.Printf("Updated change: %s\n", updatedPR.Title) |
| 189 | |
| 190 | // Submit a change (merge) |
| 191 | mergeOptions := MergePullRequestOptions{ |
| 192 | CommitTitle: "Submit change", |
| 193 | CommitMsg: "This submits the change to master", |
| 194 | } |
| 195 | |
| 196 | err = git.MergePullRequest(ctx, pr.ID, mergeOptions) |
| 197 | if err != nil { |
| 198 | log.Printf("Failed to submit change: %v", err) |
| 199 | return |
| 200 | } |
| 201 | |
| 202 | fmt.Println("Change submitted successfully") |
| 203 | } |
| 204 | |
| 205 | // Example of using both providers in the same application |
| 206 | func ExampleMultiProviderUsage() { |
| 207 | ctx := context.Background() |
| 208 | |
| 209 | // Determine which provider to use based on configuration |
| 210 | useGitHub := true // This could come from config |
| 211 | |
| 212 | var git GitInterface |
| 213 | |
| 214 | if useGitHub { |
| 215 | // Use GitHub |
| 216 | githubConfig := GitHubConfig{ |
| 217 | Token: "github-token", |
| 218 | BaseURL: "https://api.github.com", |
| 219 | } |
| 220 | githubProvider := NewGitHubPullRequestProvider("owner", "repo", githubConfig) |
| 221 | git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, githubProvider) |
| 222 | } else { |
| 223 | // Use Gerrit |
| 224 | gerritConfig := GerritConfig{ |
| 225 | Username: "gerrit-username", |
| 226 | Password: "gerrit-password", |
| 227 | BaseURL: "https://gerrit.example.com", |
| 228 | } |
| 229 | gerritProvider := NewGerritPullRequestProvider("project", gerritConfig) |
| 230 | git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, gerritProvider) |
| 231 | } |
| 232 | |
| 233 | // Use the same interface regardless of provider |
| 234 | prOptions := PullRequestOptions{ |
| 235 | Title: "Cross-platform PR", |
| 236 | Description: "This works with both GitHub and Gerrit", |
| 237 | BaseBranch: "main", |
| 238 | HeadBranch: "feature/cross-platform", |
| 239 | } |
| 240 | |
| 241 | pr, err := git.CreatePullRequest(ctx, prOptions) |
| 242 | if err != nil { |
| 243 | log.Printf("Failed to create pull request: %v", err) |
| 244 | return |
| 245 | } |
| 246 | |
| 247 | fmt.Printf("Created pull request: %s\n", pr.Title) |
| 248 | } |