Skip to content

Commit 4b08724

Browse files
authored
Merge pull request #169 from fluxcd/edit-prs
Add API for editing pull requests
2 parents fe9beee + 9dffaae commit 4b08724

21 files changed

+174
-49
lines changed

github/client_repository_file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package github
1919
import (
2020
"context"
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323

2424
"github.com/fluxcd/go-git-providers/gitprovider"
2525
"github.com/google/go-github/v47/github"
@@ -65,7 +65,7 @@ func (c *FileClient) Get(ctx context.Context, path, branch string, optFns ...git
6565
if err != nil {
6666
return nil, err
6767
}
68-
content, err := ioutil.ReadAll(output)
68+
content, err := io.ReadAll(output)
6969
if err != nil {
7070
return nil, err
7171
}

github/client_repository_pullrequest.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ func (c *PullRequestClient) Create(ctx context.Context, title, branch, baseBranc
6666
return newPullRequest(c.clientContext, pr), nil
6767
}
6868

69+
// Edit modifies an existing PR. Please refer to "EditOptions" for details on which data can be edited.
70+
func (c *PullRequestClient) Edit(ctx context.Context, number int, opts gitprovider.EditOptions) (gitprovider.PullRequest, error) {
71+
editPR := &github.PullRequest{}
72+
editPR.Title = opts.Title
73+
editedPR, _, err := c.c.Client().PullRequests.Edit(ctx, c.ref.GetIdentity(), c.ref.GetRepository(), number, editPR)
74+
if err != nil {
75+
return nil, err
76+
}
77+
return newPullRequest(c.clientContext, editedPR), nil
78+
}
79+
6980
// Get retrieves an existing pull request by number
7081
func (c *PullRequestClient) Get(ctx context.Context, number int) (gitprovider.PullRequest, error) {
7182

github/integration_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"math/rand"
2424
"net/http"
2525
"os"
26+
"reflect"
2627
"strings"
2728
"sync"
2829
"testing"
@@ -427,7 +428,7 @@ var _ = Describe("GitHub Provider", func() {
427428
Expect(hasPermission).To(Equal(true))
428429
})
429430

430-
It("should be possible to create a pr for a user repository", func() {
431+
It("should be possible to create and edit a pr for a user repository", func() {
431432

432433
userRepoRef := newUserRepoRef(testUser, testUserRepoName)
433434

@@ -480,6 +481,7 @@ var _ = Describe("GitHub Provider", func() {
480481
Expect(pr.Get().Merged).To(BeFalse())
481482

482483
prs, err := userRepo.PullRequests().List(ctx)
484+
Expect(err).NotTo(HaveOccurred())
483485
Expect(len(prs)).To(Equal(1))
484486
Expect(prs[0].Get().WebURL).To(Equal(pr.Get().WebURL))
485487

@@ -508,13 +510,23 @@ var _ = Describe("GitHub Provider", func() {
508510
Expect(pr.Get().WebURL).ToNot(BeEmpty())
509511
Expect(pr.Get().Merged).To(BeFalse())
510512

511-
err = userRepo.PullRequests().Merge(ctx, pr.Get().Number, gitprovider.MergeMethodMerge, "merged")
513+
editedPR, err := userRepo.PullRequests().Edit(ctx, pr.Get().Number, gitprovider.EditOptions{
514+
Title: gitprovider.StringVar("a new title"),
515+
})
516+
Expect(err).ToNot(HaveOccurred())
517+
518+
err = userRepo.PullRequests().Merge(ctx, editedPR.Get().Number, gitprovider.MergeMethodMerge, "merged")
512519
Expect(err).ToNot(HaveOccurred())
513520

514-
getPR, err = userRepo.PullRequests().Get(ctx, pr.Get().Number)
521+
getPR, err = userRepo.PullRequests().Get(ctx, editedPR.Get().Number)
515522
Expect(err).ToNot(HaveOccurred())
516523

517524
Expect(getPR.Get().Merged).To(BeTrue())
525+
apiObject := getPR.APIObject()
526+
githubPR, ok := apiObject.(*github.PullRequest)
527+
Expect(ok).To(BeTrue(), "API object of PullRequest has unexpected type %q", reflect.TypeOf(apiObject))
528+
Expect(githubPR.Title).ToNot(BeNil())
529+
Expect(*githubPR.Title).To(Equal("a new title"))
518530
})
519531

520532
It("should be possible to download files from path and branch specified", func() {

gitlab/client_repositories_org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (c *OrgRepositoriesClient) Reconcile(ctx context.Context, ref gitprovider.O
118118
return actual, actionTaken, err
119119
}
120120

121-
//nolint
121+
// nolint
122122
func createProject(ctx context.Context, c gitlabClient, ref gitprovider.RepositoryRef, groupName string, req gitprovider.RepositoryInfo, opts ...gitprovider.RepositoryCreateOption) (*gitlab.Project, error) {
123123
// First thing, validate and default the request to ensure a valid and fully-populated object
124124
// (to minimize any possible diffs between desired and actual state)

gitlab/client_repository_file.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package gitlab
1919
import (
2020
"context"
2121
"encoding/base64"
22-
"io/ioutil"
22+
"io"
2323
"strings"
2424

2525
"github.com/fluxcd/go-git-providers/gitprovider"
@@ -72,7 +72,7 @@ func (c *FileClient) Get(ctx context.Context, path, branch string, optFns ...git
7272
}
7373
filePath := fileDownloaded.FilePath
7474
fileContentDecoded := base64.NewDecoder(base64.RawStdEncoding, strings.NewReader(fileDownloaded.Content))
75-
fileBytes, err := ioutil.ReadAll(fileContentDecoded)
75+
fileBytes, err := io.ReadAll(fileContentDecoded)
7676
if err != nil {
7777
return nil, err
7878
}

gitlab/client_repository_pullrequest.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ func (c *PullRequestClient) Create(_ context.Context, title, branch, baseBranch,
7171
return newPullRequest(c.clientContext, mr), nil
7272
}
7373

74+
// Edit modifies an existing MR. Please refer to "EditOptions" for details on which data can be edited.
75+
func (c *PullRequestClient) Edit(ctx context.Context, number int, opts gitprovider.EditOptions) (gitprovider.PullRequest, error) {
76+
mrUpdate := &gitlab.UpdateMergeRequestOptions{
77+
Title: opts.Title,
78+
}
79+
editedMR, _, err := c.c.Client().MergeRequests.UpdateMergeRequest(getRepoPath(c.ref), number, mrUpdate)
80+
if err != nil {
81+
return nil, err
82+
}
83+
return newPullRequest(c.clientContext, editedMR), nil
84+
}
85+
7486
// Get retrieves an existing pull request by number
7587
func (c *PullRequestClient) Get(_ context.Context, number int) (gitprovider.PullRequest, error) {
7688

gitlab/integration_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"math/rand"
2727
"net/http"
2828
"os"
29+
"reflect"
2930
"strings"
3031
"sync"
3132
"testing"
@@ -548,7 +549,7 @@ var _ = Describe("GitLab Provider", func() {
548549
Permission: &pushPermission,
549550
}
550551

551-
ta, actionTaken, err = repo.TeamAccess().Reconcile(ctx, teamInfo)
552+
_, actionTaken, err = repo.TeamAccess().Reconcile(ctx, teamInfo)
552553
Expect(err).ToNot(HaveOccurred())
553554
Expect(actionTaken).To(Equal(false))
554555
})
@@ -752,7 +753,7 @@ var _ = Describe("GitLab Provider", func() {
752753
validateUserRepo(newRepo, repoRef)
753754
})
754755

755-
It("should be possible to create a pr for a user repository", func() {
756+
It("should be possible to create and edit a pr for a user repository", func() {
756757

757758
testRepoName = fmt.Sprintf("test-repo2-%03d", rand.Intn(1000))
758759
repoRef := newUserRepoRef(testUserName, testRepoName)
@@ -865,8 +866,20 @@ var _ = Describe("GitLab Provider", func() {
865866
Expect(pr.Get().WebURL).ToNot(BeEmpty())
866867
Expect(pr.Get().Merged).To(BeFalse())
867868

868-
err = userRepo.PullRequests().Merge(ctx, pr.Get().Number, gitprovider.MergeMethodMerge, "merged")
869+
editedPR, err := userRepo.PullRequests().Edit(ctx, pr.Get().Number, gitprovider.EditOptions{
870+
Title: gitprovider.StringVar("a new title"),
871+
})
872+
Expect(err).ToNot(HaveOccurred())
873+
874+
err = userRepo.PullRequests().Merge(ctx, editedPR.Get().Number, gitprovider.MergeMethodMerge, "merged")
875+
Expect(err).ToNot(HaveOccurred())
876+
877+
getPR, err := userRepo.PullRequests().Get(ctx, editedPR.Get().Number)
869878
Expect(err).ToNot(HaveOccurred())
879+
apiObject := getPR.APIObject()
880+
gitlabMR, ok := apiObject.(*gitlab.MergeRequest)
881+
Expect(ok).To(BeTrue(), "API object of PullRequest has unexpected type %q", reflect.TypeOf(apiObject))
882+
Expect(gitlabMR.Title).To(Equal("a new title"))
870883

871884
expectPRToBeMerged(ctx, userRepo, pr.Get().Number)
872885

gitlab/resource_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func (s *gitlabProjectSpec) Equals(other *gitlabProjectSpec) bool {
296296
return cmp.Equal(s, other)
297297
}
298298

299-
//nolint
299+
// nolint
300300
var gitlabVisibilityMap = map[gitprovider.RepositoryVisibility]gogitlab.VisibilityValue{
301301
gitprovider.RepositoryVisibilityInternal: gogitlab.InternalVisibility,
302302
gitprovider.RepositoryVisibilityPrivate: gogitlab.PrivateVisibility,

gitlab/resource_teamaccess.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (ta *teamAccess) Reconcile(ctx context.Context) (bool, error) {
119119
return true, ta.Update(ctx)
120120
}
121121

122-
//nolint
122+
// nolint
123123
var permissionPriority = map[int]gitprovider.RepositoryPermission{
124124
10: gitprovider.RepositoryPermissionPull,
125125
20: gitprovider.RepositoryPermissionTriage,

gitprovider/client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,21 @@ type PullRequestClient interface {
231231
List(ctx context.Context) ([]PullRequest, error)
232232
// Create creates a pull request with the given specifications.
233233
Create(ctx context.Context, title, branch, baseBranch, description string) (PullRequest, error)
234+
// Edit allows for changing an existing pull request using the given options. Please refer to "EditOptions" for details on which data can be
235+
// edited.
236+
Edit(ctx context.Context, number int, opts EditOptions) (PullRequest, error)
234237
// Get retrieves an existing pull request by number
235238
Get(ctx context.Context, number int) (PullRequest, error)
236239
// Merge merges a pull request with via either the "Squash" or "Merge" method
237240
Merge(ctx context.Context, number int, mergeMethod MergeMethod, message string) error
238241
}
239242

243+
// EditOptions is provided to a PullRequestClient's "Edit" method for updating an existing pull request.
244+
type EditOptions struct {
245+
// Title is set to a non-nil value to request a pull request's title to be changed.
246+
Title *string
247+
}
248+
240249
// FileClient operates on the branches for a specific repository.
241250
// This client can be accessed through Repository.Branches().
242251
type FileClient interface {

0 commit comments

Comments
 (0)