forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 84
/
pr.go
135 lines (115 loc) · 3.84 KB
/
pr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gitea
import (
"context"
"fmt"
"time"
"github.com/jenkins-x/go-scm/scm"
)
type pullService struct {
client *wrapper
}
func (s *pullService) Find(ctx context.Context, repo string, index int) (*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/pulls/%d", repo, index)
out := new(pullRequest)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertPullRequest(out), res, err
}
func (s *pullService) FindComment(context.Context, string, int, int) (*scm.Comment, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/pulls", repo)
out := []*pullRequest{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertPullRequests(out), res, err
}
func (s *pullService) ListComments(context.Context, string, int, scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *pullService) ListChanges(context.Context, string, int, scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *pullService) CreateComment(context.Context, string, int, *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *pullService) DeleteComment(context.Context, string, int, int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *pullService) Merge(ctx context.Context, repo string, index int) (*scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/pulls/%d/merge", repo, index)
res, err := s.client.do(ctx, "POST", path, nil, nil)
return res, err
}
func (s *pullService) Close(context.Context, string, int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
//
// native data structures
//
type pullRequest struct {
ID int `json:"id"`
Number int `json:"number"`
User user `json:"user"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
HeadBranch string `json:"head_branch"`
HeadRepo repository `json:"head_repo"`
Head reference `json:"head"`
BaseBranch string `json:"base_branch"`
BaseRepo repository `json:"base_repo"`
Base reference `json:"base"`
HTMLURL string `json:"html_url"`
Mergeable bool `json:"mergeable"`
Merged bool `json:"merged"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
}
type reference struct {
Repo repository `json:"repo"`
Name string `json:"ref"`
Sha string `json:"sha"`
}
//
// native data structure conversion
//
func convertPullRequests(src []*pullRequest) []*scm.PullRequest {
dst := []*scm.PullRequest{}
for _, v := range src {
dst = append(dst, convertPullRequest(v))
}
return dst
}
func convertPullRequest(src *pullRequest) *scm.PullRequest {
return &scm.PullRequest{
Number: src.Number,
Title: src.Title,
Body: src.Body,
Sha: src.Head.Sha,
Source: src.Head.Name,
Target: src.Base.Name,
Link: src.HTMLURL,
Fork: src.Base.Repo.FullName,
Ref: fmt.Sprintf("refs/pull/%d/head", src.Number),
Closed: src.State == "closed",
Author: *convertUser(&src.User),
Merged: src.Merged,
Created: src.Created,
Updated: src.Updated,
}
}
func convertPullRequestFromIssue(src *issue) *scm.PullRequest {
return &scm.PullRequest{
Number: src.Number,
Title: src.Title,
Body: src.Body,
Closed: src.State == "closed",
Author: *convertUser(&src.User),
Merged: src.PullRequest.Merged,
Created: src.Created,
Updated: src.Updated,
}
}