gitlab:tips:api
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
gitlab:tips:api [2016/01/26 22:08] – cedric | gitlab:tips:api [2016/01/27 07:35] (current) – cedric | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | GitLab provides [[http:// | + | GitLab provides [[http:// |
+ | This tutorial presents how to use the API of GitLab with [[https:// | ||
+ | > For the examples below we will use the instance https:// | ||
====== 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:// | ||
<code python> | <code python> | ||
Line 17: | Line 21: | ||
TOKEN = "< | TOKEN = "< | ||
- | r = requests.get(" | + | r = requests.get(" |
if r.status_code == 200: | if r.status_code == 200: | ||
Line 24: | Line 28: | ||
print user[" | print user[" | ||
</ | </ | ||
+ | |||
+ | The // | ||
+ | |||
====== 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:// | ||
+ | |||
+ | In order to send the email, we will simply use the [[http:// | ||
+ | |||
+ | The Mutt command will look like this: | ||
<code bash> | <code bash> | ||
- | $ mutt -s "Subject" `./ | + | $ mutt -s "The subject" `./ |
</ | </ | ||
- | Below you will find the script | + | As you can see, Mutt needs three parameters: |
+ | * the subject of the email. Will be given in parameter; | ||
+ | | ||
+ | | ||
+ | |||
+ | 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> | ||
#! / | #! / | ||
#-*- 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 71: | ||
members = json.loads(r.content) | members = json.loads(r.content) | ||
for member in members: | for member in members: | ||
- | r = requests.get(" | + | r = requests.get(" |
if r.status_code == 200: | if r.status_code == 200: | ||
user = json.loads(r.content) | user = json.loads(r.content) | ||
- | EMAILS.append(user[" | + | |
+ | | ||
</ | </ | ||
+ | |||
+ | A first request to the //Groups// resource returns the list of members in the group. The '' | ||
+ | |||
+ | Only active users (// |
gitlab/tips/api.txt · Last modified: 2016/01/27 07:35 by cedric