Skip to content

Commit 46ae078

Browse files
author
chhsia0
committed
Implemented PR creation in Gitea driver.
1 parent f36a1af commit 46ae078

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

scm/driver/gitea/pr.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type pullService struct {
1818

1919
func (s *pullService) Find(ctx context.Context, repo string, index int) (*scm.PullRequest, *scm.Response, error) {
2020
path := fmt.Sprintf("api/v1/repos/%s/pulls/%d", repo, index)
21-
out := new(pullRequest)
21+
out := new(pr)
2222
res, err := s.client.do(ctx, "GET", path, nil, out)
2323
return convertPullRequest(out), res, err
2424
}
@@ -29,7 +29,7 @@ func (s *pullService) FindComment(context.Context, string, int, int) (*scm.Comme
2929

3030
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
3131
path := fmt.Sprintf("api/v1/repos/%s/pulls", repo)
32-
out := []*pullRequest{}
32+
out := []*pr{}
3333
res, err := s.client.do(ctx, "GET", path, nil, &out)
3434
return convertPullRequests(out), res, err
3535
}
@@ -42,6 +42,19 @@ func (s *pullService) ListChanges(context.Context, string, int, scm.ListOptions)
4242
return nil, nil, scm.ErrNotSupported
4343
}
4444

45+
func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
46+
path := fmt.Sprintf("api/v1/repos/%s/pulls", repo)
47+
in := &prInput{
48+
Title: input.Title,
49+
Body: input.Body,
50+
Head: input.Source,
51+
Base: input.Target,
52+
}
53+
out := new(pr)
54+
res, err := s.client.do(ctx, "POST", path, in, out)
55+
return convertPullRequest(out), res, err
56+
}
57+
4558
func (s *pullService) CreateComment(context.Context, string, int, *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
4659
return nil, nil, scm.ErrNotSupported
4760
}
@@ -64,7 +77,7 @@ func (s *pullService) Close(context.Context, string, int) (*scm.Response, error)
6477
// native data structures
6578
//
6679

67-
type pullRequest struct {
80+
type pr struct {
6881
ID int `json:"id"`
6982
Number int `json:"number"`
7083
User user `json:"user"`
@@ -90,19 +103,26 @@ type reference struct {
90103
Sha string `json:"sha"`
91104
}
92105

106+
type prInput struct {
107+
Title string `json:"title"`
108+
Body string `json:"body"`
109+
Head string `json:"head"`
110+
Base string `json:"base"`
111+
}
112+
93113
//
94114
// native data structure conversion
95115
//
96116

97-
func convertPullRequests(src []*pullRequest) []*scm.PullRequest {
117+
func convertPullRequests(src []*pr) []*scm.PullRequest {
98118
dst := []*scm.PullRequest{}
99119
for _, v := range src {
100120
dst = append(dst, convertPullRequest(v))
101121
}
102122
return dst
103123
}
104124

105-
func convertPullRequest(src *pullRequest) *scm.PullRequest {
125+
func convertPullRequest(src *pr) *scm.PullRequest {
106126
return &scm.PullRequest{
107127
Number: src.Number,
108128
Title: src.Title,

scm/driver/gitea/pr_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,38 @@ func TestPullRequestList(t *testing.T) {
6969
}
7070
}
7171

72+
func TestPullRequestCreate(t *testing.T) {
73+
defer gock.Off()
74+
75+
gock.New("https://try.gitea.io").
76+
Post("/api/v1/repos/jcitizen/my-repo/pulls").
77+
Reply(201).
78+
Type("application/json").
79+
File("testdata/pr.json")
80+
81+
input := scm.PullRequestInput{
82+
Title: "Add License File",
83+
Body: "Using a BSD License",
84+
Source: "feature",
85+
Target: "master",
86+
}
87+
88+
client, _ := New("https://try.gitea.io")
89+
got, _, err := client.PullRequests.Create(context.Background(), "jcitizen/my-repo", &input)
90+
if err != nil {
91+
t.Error(err)
92+
}
93+
94+
want := new(scm.PullRequest)
95+
raw, _ := ioutil.ReadFile("testdata/pr.json.golden")
96+
json.Unmarshal(raw, want)
97+
98+
if diff := cmp.Diff(got, want); diff != "" {
99+
t.Errorf("Unexpected Results")
100+
t.Log(diff)
101+
}
102+
}
103+
72104
func TestPullRequestClose(t *testing.T) {
73105
client, _ := New("https://try.gitea.io")
74106
_, err := client.PullRequests.Close(context.Background(), "go-gitea/gitea", 1)

scm/driver/gitea/webhook.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ type (
171171

172172
// gitea pull request webhook payload
173173
pullRequestHook struct {
174-
Action string `json:"action"`
175-
Number int `json:"number"`
176-
PullRequest pullRequest `json:"pull_request"`
177-
Repository repository `json:"repository"`
178-
Sender user `json:"sender"`
174+
Action string `json:"action"`
175+
Number int `json:"number"`
176+
PullRequest pr `json:"pull_request"`
177+
Repository repository `json:"repository"`
178+
Sender user `json:"sender"`
179179
}
180180
)
181181

@@ -209,7 +209,7 @@ func convertBranchHook(dst *createHook, action scm.Action) *scm.BranchHook {
209209
func convertPushHook(dst *pushHook) *scm.PushHook {
210210
if len(dst.Commits) > 0 {
211211
return &scm.PushHook{
212-
Ref: dst.Ref,
212+
Ref: dst.Ref,
213213
Before: dst.Before,
214214
Commit: scm.Commit{
215215
Sha: dst.After,

0 commit comments

Comments
 (0)