Repository navigation
Expand file tree
/
Copy pathclient_commit.go
More file actions
107 lines (88 loc) · 2.45 KB
/
Copy pathclient_commit.go
File metadata and controls
107 lines (88 loc) · 2.45 KB
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
package gitlab
import (
"context"
"time"
"github.com/xanzy/go-gitlab"
"github.com/ovh/cds/sdk"
)
//Commits returns commit data from a given starting commit, between two commits
//The commits may be identified by branch or tag name or by hash.
func (c *gitlabClient) Commits(ctx context.Context, repo, branch, since, until string) ([]sdk.VCSCommit, error) {
// Gitlab commit listing only allow 'since' and 'until' parameter as dates
// Need to fetch commit date, then use it to filter
opt := &gitlab.ListCommitsOptions{
RefName: &branch,
}
commit, err := c.Commit(ctx, repo, since)
if err == nil {
since := time.Unix(commit.Timestamp, 0)
opt.Since = &since
}
commit, err = c.Commit(ctx, repo, until)
if err == nil {
since := time.Unix(commit.Timestamp, 0)
opt.Since = &since
}
commits, _, err := c.client.Commits.ListCommits(repo, opt)
if err != nil {
return nil, err
}
vcscommits := make([]sdk.VCSCommit, len(commits))
for i, c := range commits {
vcscommits[i] = sdk.VCSCommit{
Hash: c.ID,
Author: sdk.VCSAuthor{
Name: c.AuthorName,
DisplayName: c.AuthorName,
Email: c.AuthorEmail,
},
Timestamp: c.AuthoredDate.Unix() * 1000,
Message: c.Message,
}
}
return vcscommits, nil
}
//Commit retrieves a specific according to a hash
func (c *gitlabClient) Commit(ctx context.Context, repo, hash string) (sdk.VCSCommit, error) {
commit := sdk.VCSCommit{}
gc, _, err := c.client.Commits.GetCommit(repo, hash)
if err != nil {
return commit, err
}
commit.Hash = hash
commit.Author = sdk.VCSAuthor{
Name: gc.AuthorName,
DisplayName: gc.AuthorName,
Email: gc.AuthorEmail,
}
commit.Timestamp = gc.AuthoredDate.Unix() * 1000
commit.Message = gc.Message
return commit, nil
}
func (c *gitlabClient) CommitsBetweenRefs(ctx context.Context, repo, base, head string) ([]sdk.VCSCommit, error) {
opt := &gitlab.CompareOptions{
From: &base,
To: &head,
}
compare, _, err := c.client.Repositories.Compare(repo, opt)
if err != nil {
return nil, err
}
if compare == nil || compare.Commits == nil {
return nil, nil
}
vcscommits := make([]sdk.VCSCommit, len(compare.Commits))
for i, c := range compare.Commits {
vcscommits[i] = sdk.VCSCommit{
Hash: c.ID,
Author: sdk.VCSAuthor{
Name: c.AuthorName,
DisplayName: c.AuthorName,
Email: c.AuthorEmail,
},
Timestamp: c.AuthoredDate.Unix() * 1000,
Message: c.Message,
}
}
return vcscommits, nil
}