Go操作Gitlab

环境准备

需要有一个Gitlab环境,可以使用下面的命令快速运行一个Gitlab

1 sudo docker run --detach \
2  --hostname gitlab.example.com \
3  --publish 443:443 --publish 80:80 --publish 22:22 \
4  --name gitlab \
5  --restart always \
6  --volume /srv/gitlab/config:/etc/gitlab \
7  --volume /srv/gitlab/logs:/var/log/gitlab \
8  --volume /srv/gitlab/data:/var/opt/gitlab \
9  gitlab/gitlab-ce:latest

go-gitlab简介

go-gitlab几乎包含了gitlab上全部的api功能,这也是我们选择它的其中一个很重要的原因。

安装go-gitlab

1go get -u github.com/xanzy/go-gitlab

基本使用

连接gitlab

在连接gitlab之前你需要在gitlab上面登录账号,并在profile-Access Tokens页面设置token信息。

gitlab-token

默认情况下连接的是gitlab.com上的API地址,你可以通过下面的方法进行修改。

 1package main
 2
 3import (
 4	"fmt"
 5	"github.com/xanzy/go-gitlab"
 6	"net/http"
 7	"net/http/httptest"
 8)
 9
10var (
11	token = "xxxxxx" // token信息
12	url = "https://xxxxx" // gitlab URL
13)
14func setup() (*http.ServeMux, *httptest.Server, *gitlab.Client) {
15	// mux is the HTTP request multiplexer used with the test server.
16	mux := http.NewServeMux()
17
18	// server is a test HTTP server used to provide mock API responses.
19	server := httptest.NewServer(mux)
20
21	// client is the Gitlab client being tested.
22	client := gitlab.NewClient(nil, token)
23	client.SetBaseURL(url)
24
25	return mux, server, client
26}
27
28func main(){
29	_, _, client := setup()
30
31	users, _, err := client.Users.ListUsers(&gitlab.ListUsersOptions{})
32	if err != nil {
33		panic(err)
34	}
35	for _, user := range users {
36		log.Println(user.Username, user.Name, user.CreatedAt.Format("2006-01-02"))
37	}
38}

输出结果

12020/03/19 12:30:19 root Administrator 2020-03-19

新建项目

 1package main
 2
 3import (
 4	"github.com/xanzy/go-gitlab"
 5	"log"
 6	"net/http"
 7	"net/http/httptest"
 8)
 9
10var (
11	token = "xxxxxx"
12	url = "https://x x x x x x"
13)
14func setup() (*http.ServeMux, *httptest.Server, *gitlab.Client) {
15	// mux is the HTTP request multiplexer used with the test server.
16	mux := http.NewServeMux()
17
18	// server is a test HTTP server used to provide mock API responses.
19	server := httptest.NewServer(mux)
20
21	// client is the Gitlab client being tested.
22	client := gitlab.NewClient(nil, token)
23	client.SetBaseURL(url)
24
25	return mux, server, client
26}
27
28func main(){
29	_, _, client := setup()
30
31	// Create new project
32	p := &gitlab.CreateProjectOptions{
33		Name:                 gitlab.String("demo_project"),
34		Description:          gitlab.String("Just a test project to play with"),
35		MergeRequestsEnabled: gitlab.Bool(true),
36		SnippetsEnabled:      gitlab.Bool(true),
37		Visibility:           gitlab.Visibility(gitlab.PublicVisibility),
38	}
39
40	_, _, err := client.Projects.CreateProject(p)
41	if err != nil {
42		log.Fatal(err)
43	}
44	log.Println("创建project成功")
45}

输出结果

12020/03/19 12:43:37 创建project成功

gitlab-project

可以看到刚创建的project。

新建组

 1package main
 2
 3import (
 4	"github.com/xanzy/go-gitlab"
 5	"log"
 6	"net/http"
 7	"net/http/httptest"
 8)
 9
10var (
11	token = "xxxxxxxx"
12	url = "https://xxxxxxxx"
13)
14func setup() (*http.ServeMux, *httptest.Server, *gitlab.Client) {
15	// mux is the HTTP request multiplexer used with the test server.
16	mux := http.NewServeMux()
17
18	// server is a test HTTP server used to provide mock API responses.
19	server := httptest.NewServer(mux)
20
21	// client is the Gitlab client being tested.
22	client := gitlab.NewClient(nil, token)
23	client.SetBaseURL(url)
24
25	return mux, server, client
26}
27
28func main(){
29	_, _, client := setup()
30
31	// Create new group
32	g := gitlab.CreateGroupOptions{
33		Name:gitlab.String("demo_group"),
34		Description:gitlab.String("demo test group to play with"),
35		Path:gitlab.String("demo_group"),
36		Visibility: gitlab.Visibility(gitlab.PrivateVisibility),
37		}
38
39	group, response, err := client.Groups.CreateGroup(&g)
40	if err != nil {
41		log.Fatal(err)
42	}
43	log.Printf("Group Name: %s, Group Path: %s, Status: %s\n",group.Name, group.Path, response.Status)
44
45}

输出结果

12020/03/19 12:53:56 Group Name: demo_group, Group Path: demo_group, Status: 201 Created

gitlab-group

添加用户

 1package main
 2
 3import (
 4	"github.com/xanzy/go-gitlab"
 5	"log"
 6	"net/http"
 7	"net/http/httptest"
 8)
 9
10var (
11	token = "xxxxxxxxxxx"
12	url = "https://xxxxxxxxxxx"
13)
14func setup() (*http.ServeMux, *httptest.Server, *gitlab.Client) {
15	// mux is the HTTP request multiplexer used with the test server.
16	mux := http.NewServeMux()
17
18	// server is a test HTTP server used to provide mock API responses.
19	server := httptest.NewServer(mux)
20
21	// client is the Gitlab client being tested.
22	client := gitlab.NewClient(nil, token)
23	client.SetBaseURL(url)
24
25	return mux, server, client
26}
27
28func main(){
29	_, _, client := setup()
30
31	u := &gitlab.CreateUserOptions{
32		Email:               gitlab.String("hzde0128@live.cn"),
33		Password:            gitlab.String("12345678"),
34		Username:            gitlab.String("hzde0128"),
35		Name:                gitlab.String("hzde0128"),
36		Admin:               gitlab.Bool(true),
37		CanCreateGroup:      gitlab.Bool(true),
38	}
39	user, r, err := client.Users.CreateUser(u)
40	if err != nil {
41		log.Fatalf("创建用户失败,错误信息:%v\n", err)
42		return
43	}
44	log.Printf("Name :%s,Username: %s, Status: %d\n", user.Name, user.Username, r.StatusCode)
45
46}

输出结果

12020/03/19 13:13:59 Name :hzde0128,Username: hzde0128, Status: 201

gitlab-user

其它高级用法

更多的功能还是参考GitHub上的test文件或者上go.dev去查找。