11import json
2+ import os
23import re
4+
35import requests
46import yaml
57
8+ current_dir = os .path .dirname (os .path .abspath (__file__ ))
9+ parent_dir = os .path .dirname (current_dir )
10+ repos_file = os .path .join (parent_dir , "main.yaml" )
11+
612
713class Callback :
814 def __init__ (self , token : str ):
@@ -16,17 +22,17 @@ def __init__(self, token: str):
1622 self .test_name = None
1723 self .status = None
1824 self .available_statuses = [
19- ' started' ,
20- ' success' ,
21- ' canceled' ,
22- ' failed' ,
25+ " started" ,
26+ " success" ,
27+ " canceled" ,
28+ " failed" ,
2329 ]
2430 self .commit = None
2531 self .required_args = [
26- ' repository' ,
27- ' commit' ,
28- ' test_name' ,
29- ' status' ,
32+ " repository" ,
33+ " commit" ,
34+ " test_name" ,
35+ " status" ,
3036 ]
3137 self .populateBranches ()
3238
@@ -38,16 +44,18 @@ def checkToken(self) -> bool:
3844 return False
3945
4046 resp_user = requests .get (
41- ' https://api.github.com/user/memberships/orgs' ,
47+ " https://api.github.com/user/memberships/orgs" ,
4248 headers = {
43- ' Authorization' : f"Bearer { self .token } " ,
44- ' Accept' : ' application/vnd.github+json' ,
45- }
49+ " Authorization" : f"Bearer { self .token } " ,
50+ " Accept" : " application/vnd.github+json" ,
51+ },
4652 )
4753 if resp_user .status_code != 200 :
4854 return False
4955 resp_user_json = json .loads (resp_user .content )
50- self .user_orgs = [x ['organization' ]['login' ] for x in resp_user_json if x ['state' ] == 'active' ]
56+ self .user_orgs = [
57+ x ["organization" ]["login" ] for x in resp_user_json if x ["state" ] == "active"
58+ ]
5159 return True
5260
5361 def checkOrgPermissions (self , org : str ) -> bool :
@@ -58,32 +66,34 @@ def checkOrgPermissions(self, org: str) -> bool:
5866 return False
5967
6068 def populateBranches (self ):
61- with open ("main.yaml" , "r" ) as stream :
69+ with open (repos_file ) as stream :
6270 try :
6371 yaml_object = yaml .load (stream , Loader = yaml .BaseLoader )
6472 except yaml .YAMLError as exc :
6573 print (exc )
6674 exit (2 )
67- repos = yaml_object [' repos' ]
75+ repos = yaml_object [" repos" ]
6876 for project in repos :
6977 project_obj = repos [project ]
70- project_name = project .split ('/' )[- 1 ]
78+ project_name = project .split ("/" )[- 1 ]
7179 self .repos .append (project_name )
72- self .repos_branches [project_name ] = [str (x ) for x in project_obj ['branches' ]]
80+ self .repos_branches [project_name ] = [
81+ str (x ) for x in project_obj ["branches" ]
82+ ]
7383
7484 def argsCheck (self , args : dict ) -> list :
7585 for key , value in args .items ():
7686 # prepare list of missed arguments
7787 if key in self .required_args :
7888 self .required_args .remove (key )
7989 # populate object variables
80- if key == ' status' :
90+ if key == " status" :
8191 self .status = value
82- if key == ' test_name' :
92+ if key == " test_name" :
8393 self .test_name = value
84- if key == ' repository' :
94+ if key == " repository" :
8595 self .repository = value
86- if key == ' commit' :
96+ if key == " commit" :
8797 self .commit = value
8898 return self .required_args
8999
@@ -106,19 +116,21 @@ def pushToDB(self, dbObj, benchmark):
106116 dbObj .session .add (new_bench )
107117 dbObj .session .commit ()
108118 else :
109- dbObj .session .query (benchmark ). \
110- filter (
119+ dbObj .session .query (benchmark ).filter (
111120 benchmark .commit == self .commit ,
112121 benchmark .testName == self .test_name ,
113- ).update ({' status' : self .status })
122+ ).update ({" status" : self .status })
114123 dbObj .session .commit ()
115124
116125 def DBRecordExists (self , dbObj , benchmark ):
117- benchmarkId = dbObj .session .query (
118- benchmark .id
119- ).filter (
120- benchmark .commit == self .commit ,
121- ).order_by (benchmark .id .desc ()).first ()
126+ benchmarkId = (
127+ dbObj .session .query (benchmark .id )
128+ .filter (
129+ benchmark .commit == self .commit ,
130+ )
131+ .order_by (benchmark .id .desc ())
132+ .first ()
133+ )
122134 if benchmarkId is not None :
123135 return True
124136 return False
0 commit comments