/
getGitlabProjectRefs.go
129 lines (109 loc) · 3.01 KB
/
getGitlabProjectRefs.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
/*
Copyright paskal.maksim@gmail.com
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 api
import (
"context"
"regexp"
"sort"
"strings"
"time"
"github.com/maksim-paskal/kubernetes-manager/pkg/client"
"github.com/maksim-paskal/kubernetes-manager/pkg/telemetry"
"github.com/pkg/errors"
"github.com/xanzy/go-gitlab"
)
type GetGitlabProjectBranchItem struct {
Name string
Slug string
updated *time.Time
}
func GetGitlabProjectRefs(ctx context.Context, projectID string) ([]*GetGitlabProjectBranchItem, error) {
ctx, span := telemetry.Start(ctx, "api.GetGitlabProjectRefs")
defer span.End()
gitlabClient := client.GetGitlabClient()
if gitlabClient == nil {
return nil, errNoGitlabClient
}
// to slug ref name - use simple logic
// replace all unknown symbols to '-'
slugRegexp := regexp.MustCompile(`[^a-zA-Z0-9]`)
const (
gitlabListPerPage = 100
maxBranches = 30
maxTags = 10
)
result := make([]*GetGitlabProjectBranchItem, 0)
currentPage := 0
// add all project branches
for {
if ctx.Err() != nil {
return nil, ctx.Err()
}
currentPage++
gitBranches, _, err := gitlabClient.Branches.ListBranches(
projectID,
&gitlab.ListBranchesOptions{
ListOptions: gitlab.ListOptions{
Page: currentPage,
PerPage: gitlabListPerPage,
},
},
gitlab.WithContext(ctx),
)
if err != nil {
return nil, errors.Wrap(err, "can not list branches")
}
if len(gitBranches) == 0 {
break
}
for _, gitBranch := range gitBranches {
result = append(result, &GetGitlabProjectBranchItem{
Name: gitBranch.Name,
Slug: strings.ToLower(slugRegexp.ReplaceAllString(gitBranch.Name, "-")),
updated: gitBranch.Commit.CommittedDate,
})
}
}
// sort branches by updated date
sort.Slice(result, func(i, j int) bool {
return result[i].updated.After(*result[j].updated)
})
// return only specified number of branches
if len(result) > maxBranches {
result = result[:maxBranches]
}
// add project tags
orderBy := "updated"
gitTags, _, err := gitlabClient.Tags.ListTags(
projectID,
&gitlab.ListTagsOptions{
ListOptions: gitlab.ListOptions{
Page: 0,
PerPage: maxTags,
},
OrderBy: &orderBy,
},
gitlab.WithContext(ctx),
)
if err != nil {
return nil, errors.Wrap(err, "can not list tags")
}
for _, gitTag := range gitTags {
result = append(result, &GetGitlabProjectBranchItem{
Name: gitTag.Name,
Slug: strings.ToLower(slugRegexp.ReplaceAllString(gitTag.Name, "-")),
updated: gitTag.Commit.CommittedDate,
})
}
return result, nil
}