From f027b0e0066f74a448a7664cf43572a30037b023 Mon Sep 17 00:00:00 2001 From: Richard Purdie Date: Sat, 10 Apr 2021 16:04:49 +0100 Subject: [PATCH] reporters/swatbot: If we see a 302 response, attempt a new login Signed-off-by: Richard Purdie --- reporters/swatbot.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/reporters/swatbot.py b/reporters/swatbot.py index dfaa48a..4a5a04e 100644 --- a/reporters/swatbot.py +++ b/reporters/swatbot.py @@ -40,6 +40,15 @@ class SwatBotURI(object): csrftoken = req2.cookies['csrftoken'] self.headers = {'Content-type':'application/vnd.api+json', 'X-CSRFToken': csrftoken} + def query_with_login(self, query): + req = self.client.get(query, timeout=self.TIMEOUT, headers=self.headers, allow_redirects=False) + # A 302 status means we probably need to renew the login + if req.status_code == requests.codes.found and "login" in req.headers['Location']: + log.err("SwatBot: Attempting to renew login") + self.login() + return self.client.get(query, timeout=self.TIMEOUT, headers=self.headers, allow_redirects=False) + return req + def find_build_collection(self, build): dbid = None @@ -49,7 +58,7 @@ class SwatBotURI(object): if build['buildset']['parent_buildid']: collection_build_id = build['buildset']['parent_buildid'] - req = self.client.get(url + "?buildid=" + str(collection_build_id), timeout=self.TIMEOUT, headers=self.headers) + req = self.query_with_login(url + "?buildid=" + str(collection_build_id)) if req.status_code == requests.codes.ok: try: data = req.json()['data'] @@ -109,7 +118,7 @@ class SwatBotURI(object): log.err("SwatBot: Couldn't find BuildCollection database ID") return False - req = self.client.get(url + "?buildid=" + str(build['buildid']), timeout=self.TIMEOUT, headers=self.headers) + req = self.query_with_login(url + "?buildid=" + str(build['buildid'])) if req.status_code == requests.codes.ok: data = req.json()['data'] if len(data) > 1: @@ -144,7 +153,7 @@ class SwatBotURI(object): @defer.inlineCallbacks def update_build(self, build, master): - req = self.client.get(self.uri + "rest/build/?buildid=" + str(build['buildid']), timeout=self.TIMEOUT, headers=self.headers) + req = self.query_with_login(self.uri + "rest/build/?buildid=" + str(build['buildid'])) if req.status_code != requests.codes.ok: log.err("SwatBot: Couldn't find build to update: %s (%s)" % (str(req.status_code), str(req.json()))) return False