当前位置:   article > 正文

golang操作gitlab API:获取某个group下的所有仓库并写入文件 && 解决gitpab限制单次查询100条的限制_golang的git-api

golang的git-api

获取某个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()

}
  • 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

gitlab文档地址:
https://docs.gitlab.com/ee/api/groups.html#list-group-hooks

参考链接:
https://ranying666.github.io/2017/06/20/gitlab-api/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/486574
推荐阅读
相关标签
  

闽ICP备14008679号