LoginSignup
0
0

More than 1 year has passed since last update.

Node.js: GitLab API の使い方

Posted at
package.json
{
  "type": "module"
}

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

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

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

get_members.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	get_members.js
//
//					May/06/2022
//
// ---------------------------------------------------------------
'use strict'

import fetch from 'node-fetch'
import 'dotenv/config'

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const token = `${process.env.PERSONAL_ACCESS_TOKEN}`
const project_id = `${process.env.PROJECT_ID}`
const limit="?page=1&per_page=10"
const url="https://gitlab.com/api/v4/projects/" + project_id + "/members" + limit

const options = {
	method: "GET",
	headers: {
		"Content-Type": "application/json",
		"PRIVATE-TOKEN": token
		}
	}

const response = await fetch(url,options)
const body = await response.text()

const array_aa = JSON.parse(body)

console.log(array_aa.length)

for (var it in array_aa)
	{
	const unit_aa = array_aa[it]
	var str_out = unit_aa.id + "\t"
	str_out += unit_aa.username + "\t"
	str_out += unit_aa.name + "\t"
	str_out += unit_aa.web_url + "\t"
	str_out += unit_aa.created_at
	console.log(str_out)
	}
console.error ("*** 終了 ***")

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

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

get_branches.js
#! /usr/bin/node
// ---------------------------------------------------------------
//	get_branches.js
//
//					May/06/2022
//
// ---------------------------------------------------------------
'use strict'

import fetch from 'node-fetch'
import 'dotenv/config'

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const token = `${process.env.PERSONAL_ACCESS_TOKEN}`
const project_id = `${process.env.PROJECT_ID}`
const limit="?page=1&per_page=10"
const url="https://gitlab.com/api/v4/projects/" + project_id + "/repository/branches" + limit

const options = {
	method: "GET",
	headers: {
		"Content-Type": "application/json",
		"PRIVATE-TOKEN": token
		}
	}

const response = await fetch(url,options)
const body = await response.text()

const array_aa = JSON.parse(body)

console.log(array_aa.length)

for (var it in array_aa)
	{
	const unit_aa = array_aa[it]
	var str_out = unit_aa.name + "\t"
	str_out += unit_aa.merged + "\t"
	str_out += unit_aa.commit.author_name + "\t"
	str_out += unit_aa.commit.created_at
	console.log(str_out)
	}
console.error ("*** 終了 ***")

// ---------------------------------------------------------------
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