User Tools

Site Tools


gitlab:tips:api

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
gitlab:tips:api [2016/01/26 22:08] cedricgitlab:tips:api [2016/01/27 07:32] cedric
Line 1: Line 1:
  
-GitLab provides [[http://doc.gitlab.com/ce/api/ | a powerful API]] that lets you automate a lot of things.+GitLab provides [[http://doc.gitlab.com/ce/api/ | a powerful API]] that lets you interact programmatically with a GitLab instance in order to automate a lot of things.
  
 +This tutorial presents how to use the API of GitLab with [[https://www.python.org | Python]]. Of course you can use your favorite language.
  
 +> For the examples below we will use the instance https://gitlab.example.org. Of course you can use https://gitlab.com. 
  
 ====== Get the list of users registered in GitLab ====== ====== Get the list of users registered in GitLab ======
 +
 +This first example is fairly easy and takes advantage of the [[http://doc.gitlab.com/ce/api/users.html | Users resource]].
  
 <code python> <code python>
Line 17: Line 21:
 TOKEN = "<your-private-token>" TOKEN = "<your-private-token>"
  
-r = requests.get("https://gitlab.example.org/api/v3/users.json?per_page=100&private_token="+TOKEN)+r = requests.get("https://gitlab.example.org/api/v3/users?per_page=100&private_token="+TOKEN)
  
 if r.status_code == 200: if r.status_code == 200:
Line 24: Line 28:
         print user["username"], user["email"]         print user["username"], user["email"]
 </code> </code>
 +
 +
  
 ====== Send an email to the members of a GitLab group ====== ====== Send an email to the members of a GitLab group ======
 +
 +Again, this is pretty easy. We will need the [[http://doc.gitlab.com/ce/api/groups.html | Groups]] resource and the [[http://doc.gitlab.com/ce/api/users.html | Users]] resource.
 +
 +In order to send the email, we will simply use the [[http://www.mutt.org/ | Mutt]] email client.
 +
 +The Mutt command will look like this:
  
 <code bash> <code bash>
-$ mutt -s "Subject" `./get_recipents.py <group-id> /dev/null` < message.txt+$ mutt -s "The subject" `./get_recipents.py <group-id> /dev/null` < message.txt
 </code> </code>
  
-Below you will find the script *get_recipients.py*:+As you can see, Mutt needs three parameters: 
 +  * the subject of the email. Will be given in parameter; 
 +  the message to be sentWill be given through a Unix pipeline (//message.txt//); 
 +  the list of recipients (members of the GitLab group). Will be given in parameter as the result of a Python script. 
 + 
 +As you can expect, the Python script will use the API of GitLab in order to get the list of recipients. 
 +Below you will find a working script:
  
 <code python> <code python>
 #! /usr/bin/env python #! /usr/bin/env python
 #-*- coding: utf-8 -*- #-*- coding: utf-8 -*-
- 
-# 
-# If you want to send a message to the members of a 
-# GitLab groups: 
- 
-# 
  
 import requests import requests
Line 58: Line 70:
     members = json.loads(r.content)     members = json.loads(r.content)
     for member in members:     for member in members:
-        r = requests.get("https://gitlab.example.org/api/v3/users/" + str(member["id"]) + "?private_token=" + TOKEN, verify=False)+        r = requests.get("https://gitlab.example.org/api/v3/users/" + str(member["id"]) + "?private_token=" + TOKEN)
         if r.status_code == 200:         if r.status_code == 200:
             user = json.loads(r.content)             user = json.loads(r.content)
-            EMAILS.append(user["email"])+            if user["state"] == "active": 
 +                EMAILS.append(user["email"])
 </code> </code>
 +
 +A first request to the //Groups// resource returns the list of members in the group. The ''for'' loop iterates through these members in order to get their email.
 +
 +Only active users (//user["state"] == "active"//) will receive the email.
gitlab/tips/api.txt · Last modified: 2016/01/27 07:35 by cedric