-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab.go
70 lines (62 loc) · 1.68 KB
/
gitlab.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
// Copyright 2018 Julien Schmidt. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be found
// in the LICENSE file.
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"strconv"
)
const (
visibilityPrivate = 0
visibilityInternal = 10
visibilityPublic = 20
)
type gitlabRepo struct {
ID int `json:"id"`
Name string `json:"name"`
NameWithNamespace string `json:"name_with_namespace"`
Description string `json:"description"`
Path string `json:"path"`
}
type gitlabRepos []gitlabRepo
func gitlabRepoPath(id int) string {
bs := strconv.FormatInt(int64(id), 10)
hash := sha256.Sum256([]byte(bs))
hexDigest := hex.EncodeToString(hash[:])
return "/var/opt/gitlab/git-data/repositories/@hashed/" + hexDigest[0:2] + "/" + hexDigest[2:4] + "/" + hexDigest + ".git/"
}
func getGitlabRepos() (repos gitlabRepos, err error) {
page := 1
for {
var newRepos gitlabRepos
resp, err := http.Get(cfg.GitlabURL + "api/v4/groups/" + cfg.OrgName +
"/projects?visibility=public&simple=1&per_page=100&page=" + strconv.Itoa(page))
if err != nil {
return nil, err
}
if err = json.NewDecoder(resp.Body).Decode(&newRepos); err != nil {
return nil, err
}
if len(newRepos) < 100 {
if page == 1 {
return newRepos, nil
} else {
return append(repos, newRepos...), nil
}
} else {
repos = append(repos, newRepos...)
page++
}
}
}
func getGitlabRepo(id int) (repo gitlabRepo, err error) {
resp, err := http.Get(cfg.GitlabURL + "api/v4/projects/" + strconv.Itoa(id))
if err != nil {
return
}
err = json.NewDecoder(resp.Body).Decode(&repo)
return
}