Skip to content

Commit

Permalink
git-pw: Add a post-result command
Browse files Browse the repository at this point in the history
That command lets authenticated users post test results on revisions.

Signed-off-by: Damien Lespiau <damien.lespiau@intel.com>
  • Loading branch information
Damien Lespiau committed Nov 17, 2015
1 parent a736e47 commit 9a2c3ca
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion git-pw/git-pw
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,29 @@ def die(message):
class HttpError(Exception):
def __init__(self, status_code):
self.status_code = status_code
self.response = None

def set_response(self, response):
self.response = response


class Command(object):
meta = {
'apply': {
'need_git_repo': True,
'need_project' : False,
'need_auth' : False,
},
'apply-patch': {
'need_git_repo': True,
'need_project' : False,
}
'need_auth' : False,
},
'post-result': {
'need_git_repo': True,
'need_project' : False,
'need_auth' : True,
},
}
aliases = {
'as': 'apply',
Expand Down Expand Up @@ -108,6 +119,9 @@ class RestObject(object):
def get(self, url='/'):
return self.pw.get(self.url(url))

def post(self, url='/', data={}):
return self.pw.post(self.url(url), data)

def absolute_url(self, url='/'):
return self.pw.api_base + self.url(url)

Expand Down Expand Up @@ -179,6 +193,17 @@ class Patchwork(object):
self.json_cache[absolute_url] = json
return json

def post(self, url, data):
absolute_url = self.api_base + url
credentials = (self.user.username, self.user.password)

r = requests.post(absolute_url, data=data, auth=credentials)
if r.status_code < 200 or r.status_code >= 300:
e = HttpError(r.status_code)
e.set_response(r)
raise e
return r.json()

def setup(self):
try:
self.api = self.get('/')
Expand Down Expand Up @@ -318,6 +343,35 @@ class GitPatchwork(object):
raise
die('No patch with id %d.' % self.cmd.patch_id)

def do_post_result(self):
(series, revision) = self.cmd_get_series_revision()

try:
data = {
'test_name': self.cmd.test_name,
'state': self.cmd.state,
}
if self.cmd.url:
data['url'] = self.cmd.url
if self.cmd.summary:
data['summary'] = self.cmd.summary
r = revision.post('/test-results/', data=data)
print("Posted result: %s: %s" % (r['test_name'], r['state']))
except HttpError as e:
if e.status_code == 404:
die("Couldn't post test results for series %d" %
self.cmd.series_id)
elif e.status_code == 401 or e.status_code == 403:
die("Not authorized: %s" % e.response.json()['detail'])
elif e.status_code == 400:
data = e.response.json()
for field in data:
print(field + ':')
for reason in data[field]:
print(' ' + reason)
die("Invalid input")
raise

def run(self):
self.setup()
method = 'do_' + self.cmd.method_name()
Expand Down Expand Up @@ -384,6 +438,23 @@ if __name__ == '__main__':
apply_patch_parser.add_argument('patch_id', metavar='patch_id',
type=int, help='the patch id to apply')

# post-result
post_result_parser = subparsers.add_parser('post-result',
help='post test results for a given series revision')
post_result_parser.add_argument('--revision', '-r', metavar='revision',
type=int, help='the revision tested, latest if omitted')
post_result_parser.add_argument('--url', '-u', metavar='revision',
type=str, help='the URL where to get full test results')
post_result_parser.add_argument('--summary', '-s', metavar='state',
type=str, help='a summary of the test results')
post_result_parser.add_argument('series_id', metavar='series_id',
type=int, help='the series id to report test results for')
post_result_parser.add_argument('test_name', metavar='test_name',
type=str, help='the name of the test')
post_result_parser.add_argument('state', metavar='state',
type=str, help='the state of the test. One of pending, success, '
'warning, failure')

git_pw = GitPatchwork()
parser.parse_args(namespace=git_pw.cmd)
ret = git_pw.run()
Expand Down

0 comments on commit 9a2c3ca

Please sign in to comment.