Add pull request capability
Change-Id: Ib54054cc9b32930764cc2110203742c3948f9ea3
diff --git a/server/git/pull_request_example.go b/server/git/pull_request_example.go
new file mode 100644
index 0000000..5e34dc4
--- /dev/null
+++ b/server/git/pull_request_example.go
@@ -0,0 +1,248 @@
+package git
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "net/http"
+ "time"
+)
+
+// ExamplePullRequestUsage demonstrates how to use the pull request capabilities
+func ExamplePullRequestUsage() {
+ ctx := context.Background()
+
+ // Example 1: GitHub Pull Requests
+ exampleGitHubPullRequests(ctx)
+
+ // Example 2: Gerrit Pull Requests
+ exampleGerritPullRequests(ctx)
+}
+
+func exampleGitHubPullRequests(ctx context.Context) {
+ fmt.Println("=== GitHub Pull Request Example ===")
+
+ // Create GitHub configuration
+ githubConfig := GitHubConfig{
+ Token: "your-github-token-here",
+ BaseURL: "https://api.github.com",
+ HTTPClient: &http.Client{Timeout: 30 * time.Second},
+ }
+
+ // Create GitHub pull request provider
+ githubProvider := NewGitHubPullRequestProvider("owner", "repo", githubConfig)
+
+ // Create Git instance with GitHub pull request capabilities
+ git := NewGitWithPullRequests("/path/to/repo", GitConfig{
+ Timeout: 30 * time.Second,
+ }, githubProvider)
+
+ // Create a new pull request
+ prOptions := 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.Printf("Failed to create pull request: %v", err)
+ return
+ }
+
+ fmt.Printf("Created pull request: %s (#%d)\n", pr.Title, pr.Number)
+
+ // List pull requests
+ listOptions := ListPullRequestOptions{
+ State: "open",
+ Author: "username",
+ BaseBranch: "main",
+ Limit: 10,
+ }
+
+ prs, err := git.ListPullRequests(ctx, listOptions)
+ if err != nil {
+ log.Printf("Failed to list pull requests: %v", err)
+ return
+ }
+
+ fmt.Printf("Found %d pull requests\n", len(prs))
+
+ // Get a specific pull request
+ pr, err = git.GetPullRequest(ctx, pr.ID)
+ if err != nil {
+ log.Printf("Failed to get pull request: %v", err)
+ return
+ }
+
+ fmt.Printf("Pull request status: %s\n", pr.State)
+
+ // Update a pull request
+ updateOptions := PullRequestOptions{
+ Title: "Updated title",
+ Description: "Updated description",
+ Labels: []string{"bug", "urgent"},
+ }
+
+ updatedPR, err := git.UpdatePullRequest(ctx, pr.ID, updateOptions)
+ if err != nil {
+ log.Printf("Failed to update pull request: %v", err)
+ return
+ }
+
+ fmt.Printf("Updated pull request: %s\n", updatedPR.Title)
+
+ // Merge a pull request
+ mergeOptions := MergePullRequestOptions{
+ MergeMethod: "squash",
+ CommitTitle: "Merge pull request #123",
+ CommitMsg: "This merges the feature branch into main",
+ }
+
+ err = git.MergePullRequest(ctx, pr.ID, mergeOptions)
+ if err != nil {
+ log.Printf("Failed to merge pull request: %v", err)
+ return
+ }
+
+ fmt.Println("Pull request merged successfully")
+}
+
+func exampleGerritPullRequests(ctx context.Context) {
+ fmt.Println("=== Gerrit Pull Request Example ===")
+
+ // Create Gerrit configuration
+ gerritConfig := 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 pull request provider
+ gerritProvider := NewGerritPullRequestProvider("project-name", gerritConfig)
+
+ // Create Git instance with Gerrit pull request capabilities
+ git := NewGitWithPullRequests("/path/to/repo", GitConfig{
+ Timeout: 30 * time.Second,
+ }, gerritProvider)
+
+ // Create a new change (pull request)
+ prOptions := 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.Printf("Failed to create change: %v", err)
+ return
+ }
+
+ fmt.Printf("Created change: %s (#%d)\n", pr.Title, pr.Number)
+
+ // List changes
+ listOptions := ListPullRequestOptions{
+ State: "open",
+ Author: "username",
+ BaseBranch: "master",
+ Limit: 10,
+ }
+
+ prs, err := git.ListPullRequests(ctx, listOptions)
+ if err != nil {
+ log.Printf("Failed to list changes: %v", err)
+ return
+ }
+
+ fmt.Printf("Found %d changes\n", len(prs))
+
+ // Get a specific change
+ pr, err = git.GetPullRequest(ctx, pr.ID)
+ if err != nil {
+ log.Printf("Failed to get change: %v", err)
+ return
+ }
+
+ fmt.Printf("Change status: %s\n", pr.State)
+
+ // Update a change
+ updateOptions := PullRequestOptions{
+ Title: "Updated title",
+ Description: "Updated description",
+ }
+
+ updatedPR, err := git.UpdatePullRequest(ctx, pr.ID, updateOptions)
+ if err != nil {
+ log.Printf("Failed to update change: %v", err)
+ return
+ }
+
+ fmt.Printf("Updated change: %s\n", updatedPR.Title)
+
+ // Submit a change (merge)
+ mergeOptions := MergePullRequestOptions{
+ CommitTitle: "Submit change",
+ CommitMsg: "This submits the change to master",
+ }
+
+ err = git.MergePullRequest(ctx, pr.ID, mergeOptions)
+ if err != nil {
+ log.Printf("Failed to submit change: %v", err)
+ return
+ }
+
+ fmt.Println("Change submitted successfully")
+}
+
+// Example of using both providers in the same application
+func ExampleMultiProviderUsage() {
+ ctx := context.Background()
+
+ // Determine which provider to use based on configuration
+ useGitHub := true // This could come from config
+
+ var git GitInterface
+
+ if useGitHub {
+ // Use GitHub
+ githubConfig := GitHubConfig{
+ Token: "github-token",
+ BaseURL: "https://api.github.com",
+ }
+ githubProvider := NewGitHubPullRequestProvider("owner", "repo", githubConfig)
+ git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, githubProvider)
+ } else {
+ // Use Gerrit
+ gerritConfig := GerritConfig{
+ Username: "gerrit-username",
+ Password: "gerrit-password",
+ BaseURL: "https://gerrit.example.com",
+ }
+ gerritProvider := NewGerritPullRequestProvider("project", gerritConfig)
+ git = NewGitWithPullRequests("/path/to/repo", GitConfig{}, gerritProvider)
+ }
+
+ // Use the same interface regardless of provider
+ prOptions := PullRequestOptions{
+ Title: "Cross-platform PR",
+ Description: "This works with both GitHub and Gerrit",
+ BaseBranch: "main",
+ HeadBranch: "feature/cross-platform",
+ }
+
+ pr, err := git.CreatePullRequest(ctx, prOptions)
+ if err != nil {
+ log.Printf("Failed to create pull request: %v", err)
+ return
+ }
+
+ fmt.Printf("Created pull request: %s\n", pr.Title)
+}