diff --git a/quit/web/modules/endpoint.py b/quit/web/modules/endpoint.py index 9783cb8c..8d858d8d 100644 --- a/quit/web/modules/endpoint.py +++ b/quit/web/modules/endpoint.py @@ -145,6 +145,23 @@ def sparql(branch_or_ref): else: response.headers["X-CurrentCommit"] = commitid return response + except KeyError as e: + logger.exception(e) + print(e.args) + if "config value 'user.name' was not found" in e.args: + message = ("Unable to process query: " + "git config value 'user.name' was not found.\n" + "Please use the following command in your data repo:" + "\n\n git config user.name ") + return make_response(message, 400) + if "config value 'user.email' was not found" in e.args: + message = ("Unable to process query: " + "git config value 'user.email' was not found.\n" + "Please use the following command in your data repo:" + "\n\n git config user.email ") + return make_response(message, 400) + # KeyError has many sources -> it could be caused by other problems + return make_response('Error after executing the update query.', 400) except Exception as e: # query ok, but unsupported query type or other problem during commit logger.exception(e) diff --git a/quit/web/modules/git.py b/quit/web/modules/git.py index b59928ee..62c56648 100644 --- a/quit/web/modules/git.py +++ b/quit/web/modules/git.py @@ -258,7 +258,21 @@ def merge(refspec): return response else: return "
Unsupported Mimetype: {}
".format(mimetype), 406 - + except KeyError as e: + message = "\n" + if "config value 'user.name' was not found" in e.args: + message += ("Unable to process query: " + "git config value 'user.name' was not found.\n" + "Please use the following command in your data repo:" + "\n\n git config user.name ") + if "config value 'user.email' was not found" in e.args: + message += ("Unable to process query: " + "git config value 'user.email' was not found.\n" + "Please use the following command in your data repo:" + "\n\n git config user.email ") + logger.error(e) + logger.error(traceback.format_exc()) + return "
" + traceback.format_exc() + message + "
", 400 except Exception as e: logger.error(e) logger.error(traceback.format_exc()) diff --git a/tests/test_app.py b/tests/test_app.py index 6b88c23d..f61ec8e3 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -4,7 +4,8 @@ from urllib.parse import quote_plus from datetime import datetime -from pygit2 import GIT_SORT_TOPOLOGICAL, Signature, GIT_OBJ_BLOB +from pygit2 import GIT_SORT_TOPOLOGICAL, Signature +from pygit2 import config as gitconfig from quit.conf import Feature import quit.application as quitApp from quit.web.app import create_app @@ -3580,6 +3581,67 @@ def testWithOnInsertUsing(self): with open(path.join(repo.workdir, 'graph_1.nt'), 'r') as f: self.assertEqual('\n', f.read()) + def testMergeFailureByUnconfiguredAuthorName(self): + """Test merging two commits without a set git config user.name """ + # Prepate a git Repository + content = " ." + with TemporaryRepositoryFactory().withGraph("http://example.org/", content) as repo: + gitGlobalConfig = gitconfig.Config.get_global_config() + username = gitGlobalConfig.__getitem__("user.name") + email = gitGlobalConfig.__getitem__("user.email") + # Start Quit + args = quitApp.parseArgs(['-t', repo.workdir]) + objects = quitApp.initialize(args) + config = objects['config'] + app = create_app(config).test_client() + + app.post("/branch", data={"oldbranch": "master", "newbranch": "develop"}) + app.post("/branch", data={"oldbranch": "master", "newbranch": "target"}) + + # execute INSERT DATA query + update = "INSERT DATA {graph { .}}" + app.post('/sparql/target', data={"update": update}) + + app = create_app(config).test_client() + # start new app to syncAll() + + update = "INSERT DATA {graph { .}}" + app.post('/sparql/develop', data={"update": update}) + try: + gitGlobalConfig.__delitem__("user.name") + # execute INSERT DATA query + # execute merge + app = create_app(config).test_client() + response = app.post("/merge", data={"target": "target", "branch": "develop"}) + message = response.get_data().decode("utf-8") + self.assertEqual( + message, + ("Unable to process query: git config value 'user.name' " + "was not found.\nPlease use the following command in your" + " data repo:\n\n git config user.name ")) + # Uncommenting the second line below and commenting the first + # causes the test to fail with an KeyError. + # "username" and username are both str s + # Even more, the same code line still correctly reset user.name + # and user.email + gitGlobalConfig.__setitem__("user.name", "username") + # gitGlob alConfig.__setitem__("user.name", username) + gitGlobalConfig = gitconfig.Config.get_xdg_config() + gitGlobalConfig.__delitem__("user.email") + response = app.post("/merge", data={"target": "master", "branch": "develop", "method": "context"}) + gitGlobalConfig.__setitem__("user.name", username) + gitGlobalConfig.__setitem__("user.email", email) + message = response.get_data().decode("utf-8") + self.assertEqual( + message, + ("Unable to process query: git config value 'user.email' " + "was not found.\nPlease use the following command in your" + " data repo:\n\n git config user.email ")) + except Exception as e: + gitGlobalConfig.__setitem__("user.name", username) + gitGlobalConfig.__setitem__("user.email", email) + raise e + class FileHandlingTests(unittest.TestCase): def testNewNamedGraph(self): diff --git a/tests/test_endpoint.py b/tests/test_endpoint.py index 83cbcdbc..f74cda58 100644 --- a/tests/test_endpoint.py +++ b/tests/test_endpoint.py @@ -5,6 +5,7 @@ import quit.application as quitApp from quit.web.app import create_app from tempfile import TemporaryDirectory +from pygit2 import config as gitconfig import json class EndpointTests(unittest.TestCase): @@ -27,6 +28,7 @@ def testInsertDataNoSnapshotIsolation(self): args['targetdir'] = repo app = create_app(args).test_client() + # execute INSERT DATA query update = """INSERT DATA { GRAPH { @@ -408,6 +410,53 @@ def testInsertDataOverlappingWithMerge(self): "p": {'type': 'uri', 'value': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'}, "o": {'type': 'uri', 'value': 'http://ex.org/Todo'}}) + def testInsertDataWithNoGitConfigUser(self): + with TemporaryDirectory() as repo: + + # Start Quit + args = quitApp.parseArgs(['-t', repo]) + objects = quitApp.initialize(args) + config = objects['config'] + app = create_app(config).test_client() + gitGlobalConfig = gitconfig.Config.get_global_config() + username = gitGlobalConfig["user.name"] + email = gitGlobalConfig["user.email"] + try: + del gitGlobalConfig["user.name"] + # execute INSERT DATA query + update = """INSERT DATA { + GRAPH { + a ; + "Take out the organic waste" . + }} + """ + response = app.post('/sparql', data=dict(update=update)) + message = response.get_data().decode("utf-8") + self.assertEqual( + message, + ("Unable to process query: git config value 'user.name' " + "was not found.\nPlease use the following command in your" + " data repo:\n\n git config user.name ")) + # Uncommenting the second line below and commenting the first + # causes the test to fail with an KeyError. + # "username" and username are both str s + # Even more, the same code line still correctly reset user.name + # and user.email + gitGlobalConfig["user.name"] = username + del gitGlobalConfig["user.email"] + response = app.post('/sparql', data=dict(update=update)) + gitGlobalConfig["user.name"] = username + gitGlobalConfig["user.email"] = email + message = response.get_data().decode("utf-8") + self.assertEqual( + message, + ("Unable to process query: git config value 'user.email' " + "was not found.\nPlease use the following command in your" + " data repo:\n\n git config user.email ")) + except Exception as e: + gitGlobalConfig["user.name"] = username + gitGlobalConfig["user.email"] = email + raise e if __name__ == '__main__': unittest.main()