forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
git.go
159 lines (133 loc) · 4.13 KB
/
git.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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 gitService struct {
client *wrapper
}
func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *scm.Response, error) {
return "", nil, scm.ErrNotSupported
}
func (s *gitService) CreateRef(ctx context.Context, repo, ref, sha string) (*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) DeleteRef(ctx context.Context, repo, ref string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *gitService) FindBranch(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/branches/%s", repo, name)
out := new(branch)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertBranch(out), res, err
}
func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Commit, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/git/commits/%s", repo, ref)
out := new(commitInfo)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertCommitInfo(out), res, err
}
func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) ListBranches(ctx context.Context, repo string, _ scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/branches", repo)
out := []*branch{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertBranchList(out), res, err
}
func (s *gitService) ListCommits(ctx context.Context, repo string, _ scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) ListTags(ctx context.Context, repo string, _ scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
//
// native data structures
//
type (
// gitea branch object.
branch struct {
Name string `json:"name"`
Commit commit `json:"commit"`
}
// gitea commit object.
commit struct {
ID string `json:"id"`
Sha string `json:"sha"`
Message string `json:"message"`
URL string `json:"url"`
Author signature `json:"author"`
Committer signature `json:"committer"`
Timestamp time.Time `json:"timestamp"`
}
// gitea commit info object.
commitInfo struct {
Sha string `json:"sha"`
Commit commit `json:"commit"`
Author user `json:"author"`
Committer user `json:"committer"`
}
// gitea signature object.
signature struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
}
)
//
// native data structure conversion
//
func convertBranchList(src []*branch) []*scm.Reference {
dst := []*scm.Reference{}
for _, v := range src {
dst = append(dst, convertBranch(v))
}
return dst
}
func convertBranch(src *branch) *scm.Reference {
return &scm.Reference{
Name: scm.TrimRef(src.Name),
Path: scm.ExpandRef(src.Name, "refs/heads/"),
Sha: src.Commit.ID,
}
}
// func convertCommitList(src []*commit) []*scm.Commit {
// dst := []*scm.Commit{}
// for _, v := range src {
// dst = append(dst, convertCommitInfo(v))
// }
// return dst
// }
func convertCommitInfo(src *commitInfo) *scm.Commit {
return &scm.Commit{
Sha: src.Sha,
Link: src.Commit.URL,
Message: src.Commit.Message,
Author: convertUserSignature(src.Author),
Committer: convertUserSignature(src.Committer),
}
}
func convertSignature(src signature) scm.Signature {
return scm.Signature{
Login: src.Username,
Email: src.Email,
Name: src.Name,
}
}
func convertUserSignature(src user) scm.Signature {
return scm.Signature{
Login: userLogin(&src),
Email: src.Email,
Name: src.Fullname,
Avatar: src.Avatar,
}
}