-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathclient_repository_commit.go
99 lines (80 loc) · 2.7 KB
/
client_repository_commit.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
/*
Copyright 2020 The Flux CD contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gitlab
import (
"context"
"fmt"
"github.com/fluxcd/go-git-providers/gitprovider"
"gitlab.com/gitlab-org/api/client-go"
)
// CommitClient implements the gitprovider.CommitClient interface.
var _ gitprovider.CommitClient = &CommitClient{}
// CommitClient operates on the commits for a specific repository.
type CommitClient struct {
*clientContext
ref gitprovider.RepositoryRef
}
// ListPage lists repository commits of the given page and page size.
func (c *CommitClient) ListPage(_ context.Context, branch string, perPage, page int) ([]gitprovider.Commit, error) {
dks, err := c.listPage(branch, perPage, page)
if err != nil {
return nil, err
}
// Cast to the generic []gitprovider.Commit
commits := make([]gitprovider.Commit, 0, len(dks))
for _, dk := range dks {
commits = append(commits, dk)
}
return commits, nil
}
func (c *CommitClient) listPage(branch string, perPage, page int) ([]*commitType, error) {
// GET /repos/{owner}/{repo}/commits
apiObjs, err := c.c.ListCommitsPage(getRepoPath(c.ref), branch, perPage, page)
if err != nil {
return nil, err
}
// Map the api object to our CommitType type
keys := make([]*commitType, 0, len(apiObjs))
for _, apiObj := range apiObjs {
keys = append(keys, newCommit(c, apiObj))
}
return keys, nil
}
// Create creates a commit with the given specifications.
func (c *CommitClient) Create(_ context.Context, branch string, message string, files []gitprovider.CommitFile) (gitprovider.Commit, error) {
if len(files) == 0 {
return nil, fmt.Errorf("no files added")
}
commitActions := make([]*gitlab.CommitActionOptions, 0)
for _, file := range files {
fileAction := gitlab.FileCreate
if file.Content == nil {
fileAction = gitlab.FileDelete
}
commitActions = append(commitActions, &gitlab.CommitActionOptions{
Action: &fileAction,
FilePath: file.Path,
Content: file.Content,
})
}
opts := &gitlab.CreateCommitOptions{
Branch: &branch,
CommitMessage: &message,
Actions: commitActions,
}
commit, _, err := c.c.Client().Commits.CreateCommit(getRepoPath(c.ref), opts)
if err != nil {
return nil, err
}
return newCommit(c, commit), nil
}