0
Help us understand the problem. What are the problem?

posted at

updated at

Go: GitLab API の使い方

トークンとプロジェクトID の設定

.env
PERSONAL_ACCESS_TOKEN=glpat-xxxxxx
PROJECT_ID="35195199"

プロジェクトのメンバーの取得

get_member.go
// ---------------------------------------------------------------
//
//	get_members.go
//
//					May/07/2022
// ---------------------------------------------------------------
package main

import (
	"os"
	"fmt"
	"io/ioutil"
	"net/http"
	"encoding/json"
	"github.com/joho/godotenv"
)

// ---------------------------------------------------------------
func main() {
	fmt.Printf ("*** 開始 ***\n")
	err := godotenv.Load(".env")
	if err != nil {
		panic(err)
		}

	token := os.Getenv("PERSONAL_ACCESS_TOKEN")
	project_id := os.Getenv("PROJECT_ID")
	limit := "?page=1&per_page=10"
	url_target := "https://gitlab.com/api/v4/projects/" + project_id + "/members" + limit

	req, _ := http.NewRequest("GET", url_target, nil)
	req.Header.Set("PRIVATE-TOKEN", token)
	client := new(http.Client)
	res, err := client.Do(req)
	if err != nil {
		fmt.Println("Request error:", err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println("Request error:", err)
		return
	}

	fmt.Println(len(body))

	str_json := string(body)

	var array_aa [](map[string]interface{})
	json.Unmarshal ([]byte(str_json), &array_aa )
	fmt.Println(len(array_aa))

//
	for  _, vv := range array_aa {
		fmt.Print(vv["id"],"\t")
		fmt.Print(vv["name"],"\t")
		fmt.Print(vv["username"],"\t")
		fmt.Print(vv["web_url"],"\t")
		fmt.Println(vv["created_at"])
		}

	fmt.Printf ("*** 終了 ***\n")
}

// ---------------------------------------------------------------

実行の準備

go mod init member
go mod tidy

実行コマンド

go run get_member.go

プロジェクトのブランチの取得

get_branches.go
// ---------------------------------------------------------------
//
//	get_branches.go
//
//					May/07/2022
// ---------------------------------------------------------------
package main

import (
	"os"
	"fmt"
	"io/ioutil"
	"net/http"
	"encoding/json"
	"github.com/joho/godotenv"
)

// ---------------------------------------------------------------
func main() {
	fmt.Printf ("*** 開始 ***\n")
	err := godotenv.Load(".env")
	if err != nil {
		panic(err)
		}

	token := os.Getenv("PERSONAL_ACCESS_TOKEN")
	project_id := os.Getenv("PROJECT_ID")
	limit := "?page=1&per_page=10"
	url_target := "https://gitlab.com/api/v4/projects/" + project_id + "/repository/branches" + limit

	req, _ := http.NewRequest("GET", url_target, nil)
	req.Header.Set("PRIVATE-TOKEN", token)
	client := new(http.Client)
	res, err := client.Do(req)
	if err != nil {
		fmt.Println("Request error:", err)
		return
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println("Request error:", err)
		return
	}

	fmt.Println(len(body))

	str_json := string(body)

	var array_aa [](map[string]interface{})
	json.Unmarshal ([]byte(str_json), &array_aa )
	fmt.Println(len(array_aa))

//
	for  _, vv := range array_aa {
		fmt.Print(vv["name"],"\t")
		fmt.Print(vv["merged"],"\t")
		commit := vv["commit"].(map[string]interface{})
		fmt.Print(commit["author_name"],"\t")
		fmt.Println(commit["created_at"],"\t")
		}

	fmt.Printf ("*** 終了 ***\n")
}

// ---------------------------------------------------------------

実行コマンド

go run get_branches.go

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?