LoginSignup
0
0

More than 1 year has passed since last update.

Go: GitLab API の使い方

Last updated at Posted at 2022-05-07

トークンとプロジェクト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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0