获取某个group下的仓库:
https://git.xxxxxx.com/api/v4/groups/group_id
/projects
获取所有仓库:
https://git.xxxxxx.com/api/v4/projects
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"log"
"math"
"net/http"
"os"
"os/exec"
"strconv"
"github.com/bitly/go-simplejson"
)
const (
PRIVATE_TOKEN = "ThmspJQsBdf99nBYEo5Ns-wRxxx"
GET_DMCCA_HEAD = "https://git.xxxxxx.com/api/v4/groups/group_id/projects"
REPOS_FILE = "gitlab-url.txt"
)
var (
client *http.Client = &http.Client{}
repos string
)
func getTotal() {
head, err := http.NewRequest("HEAD", GET_DMCCA_HEAD, nil)
if err != nil {
log.Panicln(err)
}
head.Header.Add("PRIVATE-TOKEN", PRIVATE_TOKEN)
r, err := client.Do(head)
if err != nil {
log.Panicln(err)
}
defer r.Body.Close()
totalNums, _ := strconv.Atoi(r.Header.Get("X-Total"))
fmt.Printf("totalNums: %v\n", totalNums)
page := int(math.Ceil(float64(totalNums) / 100))
for i := 1; i <= page; i++ {
getProjects(i)
}
}
func getProjects(page int) {
GET_DMCCA_PROJECTS := "https://git.xxxxxx.com/api/v4/groups/group_id/projects?per_page=100&page=" + strconv.Itoa(page) + "&simple=true"
req, err := http.NewRequest("GET", GET_DMCCA_PROJECTS, nil)
if err != nil {
log.Panicln(err)
}
req.Header.Add("PRIVATE-TOKEN", PRIVATE_TOKEN)
resp, err := client.Do(req)
if err != nil {
log.Panicln(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Panicln(err)
}
var aa []map[string]interface{}
json.Unmarshal(body, &aa)
// http_url_to_repo
fmt.Printf("len(aa): %v\n", len(aa))
for _, project := range aa {
byt, _ := json.Marshal(project)
json, _ := simplejson.NewJson(byt)
s, _ := json.Get("http_url_to_repo").Interface().(string)
repos = s + "\n" + repos
}
}
func WriteFile() {
fmt.Printf("repos: %v\n", repos)
file, err2 := os.OpenFile(REPOS_FILE, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
defer file.Close()
if err2 != nil {
log.Panicln(err2)
}
w := bufio.NewWriter(file)
w.WriteString(repos)
w.Flush()
}
func main() {
// 获取group_id group下所有代码仓库
getTotal()
// 将git仓库URL地址写入文件
WriteFile()
// 删除最后一行的空行
sed := exec.Command("sed -i ':n;/^\n*$/{$! N;;bn}' ", REPOS_FILE)
sed.Run()
}
gitlab文档地址:
https://docs.gitlab.com/ee/api/groups.html#list-group-hooks
参考链接:
https://ranying666.github.io/2017/06/20/gitlab-api/