User Tools

Site Tools


gitlab:tips:api

This is an old revision of the document!


GitLab provides a powerful API that lets you automate a lot of things.

Get the list of users registered in GitLab

#! /usr/bin/env python
#-*- coding: utf-8 -*-
 
import requests
import json
 
# your private token can be found here:
# https://gitlab.example.org/profile/account
TOKEN = "<your-private-token>"
 
r = requests.get("https://gitlab.example.org/api/v3/users.json?per_page=100&private_token="+TOKEN)
 
if r.status_code == 200:
    users = json.loads(r.content)
    for user in users:
        print user["username"], user["email"]

Send an email to the members of a GitLab group

Again, this is pretty easy. We will use the [[http://www.mutt.org/ | Mutt] email client in order to send the email. Mutt needs three parameters:

  • the subject;
  • the list of recipients (member of the GitLab groups);
  • the message to be sent.
$ mutt -s "Subject" `./get_recipents.py <group-id> /dev/null` < message.txt

Below you will find the script *get_recipients.py*:

#! /usr/bin/env python
#-*- coding: utf-8 -*-
 
import requests
import json
 
# your private token can be found here:
# https://gitlab.example.org/profile/account
TOKEN = "<your-private-token>"
GROUP_ID = 21
 
EMAILS = []
 
r = requests.get("https://gitlab.example.org/api/v3/groups/" + str(GROUP_ID) + "/members?private_token=" + TOKEN)
 
if r.status_code == 200:
    members = json.loads(r.content)
    for member in members:
        r = requests.get("https://gitlab.example.org/api/v3/users/" + str(member["id"]) + "?private_token=" + TOKEN)
        if r.status_code == 200:
            user = json.loads(r.content)
            EMAILS.append(user["email"])
gitlab/tips/api.1453842761.txt.gz · Last modified: 2016/01/26 22:12 by cedric