blob: 5c70d2184ef4c98a422ef1355d920cc1298151a5 [file] [log] [blame]
iomodo1d173602025-07-26 15:35:57 +04001package git
2
3import (
4 "context"
iomodo0c203b12025-07-26 19:44:57 +04005 "log/slog"
iomodo1d173602025-07-26 15:35:57 +04006 "net/http"
iomodo0c203b12025-07-26 19:44:57 +04007 "os"
iomodo1d173602025-07-26 15:35:57 +04008 "time"
9)
10
iomodo0c203b12025-07-26 19:44:57 +040011// ExamplePullRequestUsage demonstrates how to use pull request functionality
iomodo1d173602025-07-26 15:35:57 +040012func ExamplePullRequestUsage() {
13 ctx := context.Background()
14
iomodo0c203b12025-07-26 19:44:57 +040015 // Create logger
16 logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
iomodo1d173602025-07-26 15:35:57 +040017
iomodo0c203b12025-07-26 19:44:57 +040018 // Example with GitHub
19 exampleGitHubPullRequests(ctx, logger)
20
21 // Example with Gerrit
22 exampleGerritPullRequests(ctx, logger)
iomodo1d173602025-07-26 15:35:57 +040023}
24
iomodo0c203b12025-07-26 19:44:57 +040025func exampleGitHubPullRequests(ctx context.Context, logger *slog.Logger) {
26 logger.Info("=== GitHub Pull Request Example ===")
iomodo1d173602025-07-26 15:35:57 +040027
28 // Create GitHub configuration
29 githubConfig := GitHubConfig{
30 Token: "your-github-token-here",
31 BaseURL: "https://api.github.com",
32 HTTPClient: &http.Client{Timeout: 30 * time.Second},
33 }
34
35 // Create GitHub pull request provider
36 githubProvider := NewGitHubPullRequestProvider("owner", "repo", githubConfig)
37
38 // Create Git instance with GitHub pull request capabilities
39 git := NewGitWithPullRequests("/path/to/repo", GitConfig{
40 Timeout: 30 * time.Second,
iomodo0c203b12025-07-26 19:44:57 +040041 }, githubProvider, logger)
iomodo1d173602025-07-26 15:35:57 +040042
43 // Create a new pull request
44 prOptions := PullRequestOptions{
45 Title: "Add new feature",
46 Description: "This PR adds a new feature to the application.",
47 BaseBranch: "main",
48 HeadBranch: "feature/new-feature",
49 Labels: []string{"enhancement", "feature"},
50 Assignees: []string{"username1", "username2"},
51 Reviewers: []string{"reviewer1", "reviewer2"},
52 Draft: false,
53 }
54
55 pr, err := git.CreatePullRequest(ctx, prOptions)
56 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +040057 logger.Error("Failed to create pull request", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +040058 return
59 }
60
iomodo0c203b12025-07-26 19:44:57 +040061 logger.Info("Created pull request", slog.String("title", pr.Title), slog.Int("number", pr.Number))
iomodo1d173602025-07-26 15:35:57 +040062
63 // List pull requests
64 listOptions := ListPullRequestOptions{
65 State: "open",
66 Author: "username",
67 BaseBranch: "main",
68 Limit: 10,
69 }
70
71 prs, err := git.ListPullRequests(ctx, listOptions)
72 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +040073 logger.Error("Failed to list pull requests", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +040074 return
75 }
76
iomodo0c203b12025-07-26 19:44:57 +040077 logger.Info("Found pull requests", slog.Int("count", len(prs)))
iomodo1d173602025-07-26 15:35:57 +040078
79 // Get a specific pull request
80 pr, err = git.GetPullRequest(ctx, pr.ID)
81 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +040082 logger.Error("Failed to get pull request", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +040083 return
84 }
85
iomodo0c203b12025-07-26 19:44:57 +040086 logger.Info("Pull request status", slog.String("state", pr.State))
iomodo1d173602025-07-26 15:35:57 +040087
88 // Update a pull request
89 updateOptions := PullRequestOptions{
90 Title: "Updated title",
91 Description: "Updated description",
92 Labels: []string{"bug", "urgent"},
93 }
94
95 updatedPR, err := git.UpdatePullRequest(ctx, pr.ID, updateOptions)
96 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +040097 logger.Error("Failed to update pull request", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +040098 return
99 }
100
iomodo0c203b12025-07-26 19:44:57 +0400101 logger.Info("Updated pull request", slog.String("title", updatedPR.Title))
iomodo1d173602025-07-26 15:35:57 +0400102
103 // Merge a pull request
104 mergeOptions := MergePullRequestOptions{
105 MergeMethod: "squash",
106 CommitTitle: "Merge pull request #123",
107 CommitMsg: "This merges the feature branch into main",
108 }
109
110 err = git.MergePullRequest(ctx, pr.ID, mergeOptions)
111 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400112 logger.Error("Failed to merge pull request", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +0400113 return
114 }
115
iomodo0c203b12025-07-26 19:44:57 +0400116 logger.Info("Pull request merged successfully")
iomodo1d173602025-07-26 15:35:57 +0400117}
118
iomodo0c203b12025-07-26 19:44:57 +0400119func exampleGerritPullRequests(ctx context.Context, logger *slog.Logger) {
120 logger.Info("=== Gerrit Pull Request Example ===")
iomodo1d173602025-07-26 15:35:57 +0400121
122 // Create Gerrit configuration
123 gerritConfig := GerritConfig{
124 Username: "your-username",
125 Password: "your-http-password-or-api-token",
126 BaseURL: "https://gerrit.example.com",
127 HTTPClient: &http.Client{Timeout: 30 * time.Second},
128 }
129
130 // Create Gerrit pull request provider
131 gerritProvider := NewGerritPullRequestProvider("project-name", gerritConfig)
132
133 // Create Git instance with Gerrit pull request capabilities
134 git := NewGitWithPullRequests("/path/to/repo", GitConfig{
135 Timeout: 30 * time.Second,
iomodo0c203b12025-07-26 19:44:57 +0400136 }, gerritProvider, logger)
iomodo1d173602025-07-26 15:35:57 +0400137
138 // Create a new change (pull request)
139 prOptions := PullRequestOptions{
140 Title: "Add new feature",
141 Description: "This change adds a new feature to the application.",
142 BaseBranch: "master",
143 HeadBranch: "feature/new-feature",
144 }
145
146 pr, err := git.CreatePullRequest(ctx, prOptions)
147 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400148 logger.Error("Failed to create change", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +0400149 return
150 }
151
iomodo0c203b12025-07-26 19:44:57 +0400152 logger.Info("Created change", slog.String("title", pr.Title), slog.Int("number", pr.Number))
iomodo1d173602025-07-26 15:35:57 +0400153
154 // List changes
155 listOptions := ListPullRequestOptions{
156 State: "open",
157 Author: "username",
158 BaseBranch: "master",
159 Limit: 10,
160 }
161
162 prs, err := git.ListPullRequests(ctx, listOptions)
163 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400164 logger.Error("Failed to list changes", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +0400165 return
166 }
167
iomodo0c203b12025-07-26 19:44:57 +0400168 logger.Info("Found changes", slog.Int("count", len(prs)))
iomodo1d173602025-07-26 15:35:57 +0400169
170 // Get a specific change
171 pr, err = git.GetPullRequest(ctx, pr.ID)
172 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400173 logger.Error("Failed to get change", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +0400174 return
175 }
176
iomodo0c203b12025-07-26 19:44:57 +0400177 logger.Info("Change status", slog.String("state", pr.State))
iomodo1d173602025-07-26 15:35:57 +0400178
179 // Update a change
180 updateOptions := PullRequestOptions{
181 Title: "Updated title",
182 Description: "Updated description",
183 }
184
185 updatedPR, err := git.UpdatePullRequest(ctx, pr.ID, updateOptions)
186 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400187 logger.Error("Failed to update change", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +0400188 return
189 }
190
iomodo0c203b12025-07-26 19:44:57 +0400191 logger.Info("Updated change", slog.String("title", updatedPR.Title))
iomodo1d173602025-07-26 15:35:57 +0400192
193 // Submit a change (merge)
194 mergeOptions := MergePullRequestOptions{
195 CommitTitle: "Submit change",
196 CommitMsg: "This submits the change to master",
197 }
198
199 err = git.MergePullRequest(ctx, pr.ID, mergeOptions)
200 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400201 logger.Error("Failed to submit change", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +0400202 return
203 }
204
iomodo0c203b12025-07-26 19:44:57 +0400205 logger.Info("Change submitted successfully")
iomodo1d173602025-07-26 15:35:57 +0400206}
207
208// Example of using both providers in the same application
209func ExampleMultiProviderUsage() {
210 ctx := context.Background()
211
212 // Determine which provider to use based on configuration
213 useGitHub := true // This could come from config
214
215 var git GitInterface
216
217 if useGitHub {
218 // Use GitHub
219 githubConfig := GitHubConfig{
220 Token: "github-token",
221 BaseURL: "https://api.github.com",
222 }
223 githubProvider := NewGitHubPullRequestProvider("owner", "repo", githubConfig)
iomodo0c203b12025-07-26 19:44:57 +0400224 git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, githubProvider, nil) // Pass nil for logger as it's not used in this example
iomodo1d173602025-07-26 15:35:57 +0400225 } else {
226 // Use Gerrit
227 gerritConfig := GerritConfig{
228 Username: "gerrit-username",
229 Password: "gerrit-password",
230 BaseURL: "https://gerrit.example.com",
231 }
232 gerritProvider := NewGerritPullRequestProvider("project", gerritConfig)
iomodo0c203b12025-07-26 19:44:57 +0400233 git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, gerritProvider, nil) // Pass nil for logger as it's not used in this example
iomodo1d173602025-07-26 15:35:57 +0400234 }
235
236 // Use the same interface regardless of provider
237 prOptions := PullRequestOptions{
238 Title: "Cross-platform PR",
239 Description: "This works with both GitHub and Gerrit",
240 BaseBranch: "main",
241 HeadBranch: "feature/cross-platform",
242 }
243
244 pr, err := git.CreatePullRequest(ctx, prOptions)
245 if err != nil {
iomodo0c203b12025-07-26 19:44:57 +0400246 slog.Error("Failed to create pull request", slog.String("error", err.Error()))
iomodo1d173602025-07-26 15:35:57 +0400247 return
248 }
249
iomodo0c203b12025-07-26 19:44:57 +0400250 slog.Info("Created pull request", slog.String("title", pr.Title))
iomodo1d173602025-07-26 15:35:57 +0400251}