-
Notifications
You must be signed in to change notification settings - Fork 0
/
failed-jenkins-jobs.py
48 lines (34 loc) · 1.18 KB
/
failed-jenkins-jobs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
from requests.auth import AuthBase
from base64 import b64encode
import sys
import os
from dotenv import load_dotenv
load_dotenv()
class BasicAuthToken(requests.auth.AuthBase):
def __init__(self, token):
self.token = token
def __call__(self, r):
authstr = 'Basic ' + b64encode(self.token.encode('utf-8')).decode('utf-8')
r.headers['Authorization'] = authstr
return r
def main():
url = 'https://ci.cloud.cnaf.infn.it/view/Failed/api/json'
query = {'pretty': 'true'}
jenkins_user = os.environ.get('USERNAME')
jenkins_api_token = os.environ.get('API_TOKEN')
credentials = jenkins_user+':'+jenkins_api_token
response = requests.get(url, params=query, auth=BasicAuthToken(credentials))
if response.status_code == 200:
data = response.json()['jobs']
print('List of failed jenkins jobs:')
for i in range(len(data)):
print(i+1, data[i]['url'])
elif response.status_code == 401:
print('Unauthorized user')
else :
print('Exit with HTTP response status code',response.status_code)
if __name__ == '__main__':
main()