Skip to content

Commit 0155ef7

Browse files
authored
Merge pull request #83 from fluxcd/pr-url
Return WebURL of newly created pull request
2 parents 1e9523c + c1841e1 commit 0155ef7

File tree

10 files changed

+144
-13
lines changed

10 files changed

+144
-13
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ See the following (automatically tested) examples:
172172
- [github/example_organization_test.go](github/example_organization_test.go)
173173
- [github/example_repository_test.go](github/example_repository_test.go)
174174

175+
176+
If you need to run `make test` for your fork/branch you may need to supply the following environment variables:
177+
- GIT_PROVIDER_ORGANIZATION : For GitHub this should be an organization whereas for GitLab this should be a top-level group. As this environment variable is used for both test suites, the name of the GitHub organization must match the name of the GitLab top-level group. Also, this organization needs to have its repository default branch set to `main`.
178+
- GIT_PROVIDER_USER : This should be the same username for both GitHub and GitLab.
179+
- GITLAB_TEST_TEAM_NAME : An existing GitLab group.
180+
- GITLAB_TEST_SUBGROUP : An existing GitLab subgroup of the GIT_PROVIDER_ORGANIZATION top-level group.
181+
- GITHUB_TOKEN : A GitHub token with `repo`, `admin:org` and `delete_repo` permissions.
182+
- GITLAB_TOKEN: A GitLab token with `api` scope.
183+
175184
## Maintainers
176185

177186
In alphabetical order:
@@ -181,6 +190,7 @@ In alphabetical order:
181190
- Simon Howe, [@foot](https://github.com/foot)
182191
- Dinos Kousidis, [@dinosk](https://github.com/dinosk)
183192
- Stefan Prodan, [@stefanprodan](https://github.com/stefanprodan)
193+
- Yiannis Triantafyllopoulos, [@yiannistri](https://github.com/yiannistri)
184194

185195
## Getting Help
186196

github/client_repository_pullrequest.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package github
1818

1919
import (
2020
"context"
21+
2122
"github.com/fluxcd/go-git-providers/gitprovider"
2223
"github.com/google/go-github/v32/github"
2324
)
@@ -32,7 +33,7 @@ type PullRequestClient struct {
3233
}
3334

3435
// Create creates a pull request with the given specifications.
35-
func (c *PullRequestClient) Create(ctx context.Context, title, branch, baseBranch, description string) error {
36+
func (c *PullRequestClient) Create(ctx context.Context, title, branch, baseBranch, description string) (gitprovider.PullRequest, error) {
3637

3738
prOpts := &github.NewPullRequest{
3839
Title: &title,
@@ -41,9 +42,10 @@ func (c *PullRequestClient) Create(ctx context.Context, title, branch, baseBranc
4142
Body: &description,
4243
}
4344

44-
if _, _, err := c.c.Client().PullRequests.Create(ctx, c.ref.GetIdentity(), c.ref.GetRepository(), prOpts); err != nil {
45-
return err
45+
pr, _, err := c.c.Client().PullRequests.Create(ctx, c.ref.GetIdentity(), c.ref.GetRepository(), prOpts)
46+
if err != nil {
47+
return nil, err
4648
}
4749

48-
return nil
50+
return newPullRequest(c.clientContext, pr), nil
4951
}

github/integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ var _ = Describe("GitHub Provider", func() {
408408
_, err = userRepo.Commits().Create(ctx, branchName, "added config file", files)
409409
Expect(err).ToNot(HaveOccurred())
410410

411-
err = userRepo.PullRequests().Create(ctx, "Added config file", branchName, *defaultBranch, "added config file")
411+
pr, err := userRepo.PullRequests().Create(ctx, "Added config file", branchName, *defaultBranch, "added config file")
412412
Expect(err).ToNot(HaveOccurred())
413-
413+
Expect(pr.Get().WebURL).ToNot(BeEmpty())
414414
})
415415

416416
AfterSuite(func() {

github/resource_pullrequest.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2021 The Flux CD contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package github
18+
19+
import (
20+
"github.com/fluxcd/go-git-providers/gitprovider"
21+
"github.com/google/go-github/v32/github"
22+
)
23+
24+
func newPullRequest(ctx *clientContext, apiObj *github.PullRequest) *pullrequest {
25+
return &pullrequest{
26+
clientContext: ctx,
27+
pr: *apiObj,
28+
}
29+
}
30+
31+
var _ gitprovider.PullRequest = &pullrequest{}
32+
33+
type pullrequest struct {
34+
*clientContext
35+
36+
pr github.PullRequest
37+
}
38+
39+
func (pr *pullrequest) Get() gitprovider.PullRequestInfo {
40+
return pullrequestFromAPI(&pr.pr)
41+
}
42+
43+
func (pr *pullrequest) APIObject() interface{} {
44+
return &pr.pr
45+
}
46+
47+
func pullrequestFromAPI(apiObj *github.PullRequest) gitprovider.PullRequestInfo {
48+
return gitprovider.PullRequestInfo{
49+
WebURL: apiObj.GetHTMLURL(),
50+
}
51+
}

gitlab/client_repository_pullrequest.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package gitlab
1818

1919
import (
2020
"context"
21+
2122
"github.com/fluxcd/go-git-providers/gitprovider"
2223
"github.com/xanzy/go-gitlab"
2324
)
@@ -32,7 +33,7 @@ type PullRequestClient struct {
3233
}
3334

3435
// Create creates a pull request with the given specifications.
35-
func (c *PullRequestClient) Create(ctx context.Context, title, branch, baseBranch, description string) error {
36+
func (c *PullRequestClient) Create(ctx context.Context, title, branch, baseBranch, description string) (gitprovider.PullRequest, error) {
3637

3738
prOpts := &gitlab.CreateMergeRequestOptions{
3839
Title: &title,
@@ -41,9 +42,10 @@ func (c *PullRequestClient) Create(ctx context.Context, title, branch, baseBranc
4142
Description: &description,
4243
}
4344

44-
if _, _, err := c.c.Client().MergeRequests.CreateMergeRequest(getRepoPath(c.ref), prOpts); err != nil {
45-
return err
45+
mr, _, err := c.c.Client().MergeRequests.CreateMergeRequest(getRepoPath(c.ref), prOpts)
46+
if err != nil {
47+
return nil, err
4648
}
4749

48-
return nil
50+
return newPullRequest(c.clientContext, mr), nil
4951
}

gitlab/integration_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,9 +729,9 @@ var _ = Describe("GitLab Provider", func() {
729729
_, err = userRepo.Commits().Create(ctx, branchName, "added config file", files)
730730
Expect(err).ToNot(HaveOccurred())
731731

732-
err = userRepo.PullRequests().Create(ctx, "Added config file", branchName, defaultBranch, "added config file")
732+
pr, err := userRepo.PullRequests().Create(ctx, "Added config file", branchName, defaultBranch, "added config file")
733733
Expect(err).ToNot(HaveOccurred())
734-
734+
Expect(pr.Get().WebURL).ToNot(BeEmpty())
735735
})
736736

737737
AfterSuite(func() {

gitlab/resource_pullrequest.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2021 The Flux CD contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gitlab
18+
19+
import (
20+
"github.com/fluxcd/go-git-providers/gitprovider"
21+
"github.com/xanzy/go-gitlab"
22+
)
23+
24+
func newPullRequest(ctx *clientContext, apiObj *gitlab.MergeRequest) *pullrequest {
25+
return &pullrequest{
26+
clientContext: ctx,
27+
pr: *apiObj,
28+
}
29+
}
30+
31+
var _ gitprovider.PullRequest = &pullrequest{}
32+
33+
type pullrequest struct {
34+
*clientContext
35+
36+
pr gitlab.MergeRequest
37+
}
38+
39+
func (pr *pullrequest) Get() gitprovider.PullRequestInfo {
40+
return pullrequestFromAPI(&pr.pr)
41+
}
42+
43+
func (pr *pullrequest) APIObject() interface{} {
44+
return &pr.pr
45+
}
46+
47+
func pullrequestFromAPI(apiObj *gitlab.MergeRequest) gitprovider.PullRequestInfo {
48+
return gitprovider.PullRequestInfo{
49+
WebURL: apiObj.WebURL,
50+
}
51+
}

gitprovider/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,5 @@ type BranchClient interface {
228228
// This client can be accessed through Repository.PullRequests().
229229
type PullRequestClient interface {
230230
// Create creates a pull request with the given specifications.
231-
Create(ctx context.Context, title, branch, baseBranch, description string) error
231+
Create(ctx context.Context, title, branch, baseBranch, description string) (PullRequest, error)
232232
}

gitprovider/resources.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,12 @@ type Commit interface {
138138
// Get returns high-level information about this commit.
139139
Get() CommitInfo
140140
}
141+
142+
type PullRequest interface {
143+
// Object implements the Object interface,
144+
// allowing access to the underlying object returned from the API.
145+
Object
146+
147+
// Get returns high-level information about this pull request.
148+
Get() PullRequestInfo
149+
}

gitprovider/types_repository.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,9 @@ type CommitFile struct {
199199
// +required
200200
Content *string `json:"content"`
201201
}
202+
203+
type PullRequestInfo struct {
204+
// WebURL is the URL of the pull request in the git provider web interface.
205+
// +required
206+
WebURL string `json:"web_url"`
207+
}

0 commit comments

Comments
 (0)