55import toml
66import requests
77import json
8+ import sys
9+ import os
810from git import Repo
911
1012
@@ -13,63 +15,85 @@ def api_connection(host):
1315 return host
1416
1517
16- def find_diff (path , branch , repository ):
17- repo = Repo (path )
18- assert not repo .bare
19- hcommit = repo .iter_commits ("origin/master.." + branch )
20- commits = []
21- for i in hcommit :
22- commits .append (repo .git .show (i ))
23- return commits
24-
25-
26- def send_build (url , patch , branch , treeurl , token ):
18+ def send_checkout (apiurl , repourl , repobranch , repocommit , jobfilter , token ):
2719 headers = {
2820 "Content-Type" : "application/json; charset=utf-8" ,
2921 "Authorization" : "Bearer {}" .format (token ),
3022 }
3123 values = {
32- "treeurl" : treeurl ,
33- "branch" : branch ,
34- "commit" : "example" ,
35- "kbuildname" : "example" ,
36- "testname" : "example" ,
24+ "url" : repourl ,
25+ "branch" : repobranch ,
26+ "commit" : repocommit ,
27+ "jobfilter" : jobfilter ,
3728 }
38- response = requests .post (url , headers = headers , files = {"patch" : patch }, data = values )
29+ print (values )
30+ try :
31+ response = requests .post (apiurl , headers = headers , data = values )
32+ except Exception as e :
33+ click .secho (f"API connection error: { e } " , fg = "red" )
34+ sys .exit (1 )
3935 click .secho (response .status_code , fg = "green" )
4036 click .secho (response .json (), fg = "green" )
4137
4238
43- def load_toml (settings ):
44- with open (settings ) as fp :
45- config = toml .load (fp )
46- return config
39+ def retrieve_repo_data (path ):
40+ try :
41+ repo = Repo (path )
42+ except Exception as e :
43+ click .secho (f"Git repo retrieval error: { e } " , fg = "red" )
44+ sys .exit (1 )
45+ giturl = repo .remotes .origin .url
46+ branch = repo .active_branch .name
47+ commit = repo .head .commit .hexsha
48+ return giturl , branch , commit
4749
4850
49- @click .command (help = "Test commits from a local Kernel repository" )
50- @click .option (
51- "--repository" ,
52- default = "mainline" ,
53- help = "define the kernel upstream repository where to test local changes" ,
54- )
55- @click .option ("--branch" , default = "master" , help = "define the repository branch" )
56- @click .option (
57- "--private" ,
58- default = False ,
59- is_flag = True ,
60- help = "define if the test results will be published" ,
61- )
51+ @click .command (help = "Test current commit from a local Kernel repository" )
6252@click .option (
6353 "--path" ,
64- default = "." ,
65- help = "define the directory of the local tree with local changes" ,
54+ help = "Path to the local kernel repository" ,
6655)
67- @click .option ("--settings" , default = ".kci-dev.toml" , help = "path of toml setting file" )
68- def commit (repository , branch , private , path , settings ):
69- config = load_toml (settings )
70- url = api_connection (config ["connection" ]["host" ])
71- diff = find_diff (path , branch , repository )
72- send_build (url , diff , branch , repository , config ["connection" ]["token" ])
56+ @click .option ("--commit" , help = "Commit hash to test" )
57+ @click .option ("--branch" , help = "Branch to test" )
58+ @click .option ("--jobfilter" , help = "Job filter" )
59+ @click .option ("--repository" , help = "Repository url" )
60+ @click .pass_context
61+ def commit (ctx , ** kwargs ):
62+ cfg = ctx .obj .get ("CONFIG" )
63+ instance = ctx .obj .get ("INSTANCE" )
64+ url = api_connection (cfg [instance ]["host" ])
65+ repourl = None
66+ branch = None
67+ commit = None
68+
69+ # is repo path provided and is it a valid git repository
70+ if 'path' in kwargs and kwargs ["path" ]:
71+ if not os .path .isdir (kwargs ["path" ]):
72+ click .secho ("Invalid git repository path" , fg = "red" )
73+ sys .exit (1 )
74+
75+ # if commit or branch or repository is not provided
76+ # fetch data from local git repository
77+ if not kwargs ["commit" ] or not kwargs ["branch" ]\
78+ or not kwargs ["repository" ] and os .path .isdir (kwargs ["path" ]):
79+ repourl , branch , commit = retrieve_repo_data (kwargs ["path" ])
80+
81+ # If any option specified - override the data from local git repository
82+ if kwargs ["commit" ]:
83+ commit = kwargs ["commit" ]
84+ if kwargs ["branch" ]:
85+ branch = kwargs ["branch" ]
86+ if kwargs ["repository" ]:
87+ repourl = kwargs ["repository" ]
88+
89+ # if commit or branch or repository is not available, exit
90+ if not repourl or not branch or not commit :
91+ click .secho ("Repository url, branch and commit hash must be provided "
92+ "or retrieved from local repo" , fg = "red" )
93+ sys .exit (1 )
94+
95+ send_checkout (url , repourl , branch , commit ,
96+ kwargs ["jobfilter" ], cfg [instance ]["token" ])
7397
7498
7599if __name__ == "__main__" :
0 commit comments