Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: More verbose message for missing configs on update-queries #230

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions quit/web/modules/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <your 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 <your 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)
Expand Down
16 changes: 15 additions & 1 deletion quit/web/modules/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,21 @@ def merge(refspec):
return response
else:
return "<pre>Unsupported Mimetype: {}</pre>".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 <your 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 <your email>")
logger.error(e)
logger.error(traceback.format_exc())
return "<pre>" + traceback.format_exc() + message + "</pre>", 400
except Exception as e:
logger.error(e)
logger.error(traceback.format_exc())
Expand Down
64 changes: 63 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "<http://ex.org/a> <http://ex.org/b> <http://ex.org/c> ."
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 <http://example.org/> {<http://ex.org/x> <http://ex.org/y> <http://ex.org/z> .}}"
app.post('/sparql/target', data={"update": update})

app = create_app(config).test_client()
# start new app to syncAll()

update = "INSERT DATA {graph <http://example.org/> {<http://ex.org/z> <http://ex.org/y> <http://ex.org/x> .}}"
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 <your 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 <your email>"))
except Exception as e:
gitGlobalConfig.__setitem__("user.name", username)
gitGlobalConfig.__setitem__("user.email", email)
raise e


class FileHandlingTests(unittest.TestCase):
def testNewNamedGraph(self):
Expand Down
49 changes: 49 additions & 0 deletions tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -27,6 +28,7 @@ def testInsertDataNoSnapshotIsolation(self):
args['targetdir'] = repo
app = create_app(args).test_client()


# execute INSERT DATA query
update = """INSERT DATA {
GRAPH <http://example.org/> {
Expand Down Expand Up @@ -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 <http://example.org/> {
<http://ex.org/garbage> a <http://ex.org/Todo> ;
<http://ex.org/task> "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 <your 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 <your email>"))
except Exception as e:
gitGlobalConfig["user.name"] = username
gitGlobalConfig["user.email"] = email
raise e

if __name__ == '__main__':
unittest.main()