Commit ee4792ba authored by David Foucher's avatar David Foucher

Add method to get file from gitlab with commit_id

parent 5e54663f
......@@ -67,7 +67,7 @@ def test_modification_data_with_new_branch_is_valid():
def test_modification_data_with_existing_branch_is_valid():
data = DATA.copy()
data["last_commit_id"] = "7cfa6c27"
data["commit_id"] = "7cfa6c27"
assert modification_data(data) == {
"branch": f"RULE-modification-test",
"commit_message": 'test',
......
......@@ -265,6 +265,7 @@ async def source_modified(request, response):
gl = gitlab.Gitlab('https://git.beta.pole-emploi.fr', private_token=GITLAB_TOKEN)
project = gl.projects.get('open-source/trefle', lazy=True)
merges = project.mergerequests.list(state="opened", labels=["RULE"])
branch_filter = ''
if(request.query.get('branch', '')):
branch_filter = f"modification-{fold_name(request.query.get('branch','')).lower()}"
modified = {}
......@@ -287,6 +288,17 @@ async def source_modified(request, response):
response.json = modified
@app.route("/source/file")
async def source_file(request, response):
gl = gitlab.Gitlab('https://git.beta.pole-emploi.fr', private_token=GITLAB_TOKEN)
project = gl.projects.get('open-source/trefle', lazy=True)
commit_id = request.query.get('commit_id', '')
file_name = request.query.get('file', '')
if(commit_id and file_name):
response.json = project.files.get(file_path=file_name,
ref=commit_id).decode().decode()
@app.route("/source/save", ['POST'])
async def source_save(request, response):
# TODO log errors
......
......@@ -16,8 +16,9 @@ async def load_project():
async def source_modified(project, data):
filename = data["file_path"]
content = data["content"]
branch = data["start_branch"] if "start_branch" in data.keys() else data["branch"]
original_fingerprint = hash(
project.files.get(filename, ref=data["branch"]).decode().decode()
project.files.get(filename, ref=branch).decode().decode()
)
modified_fingerprint = hash(content)
return original_fingerprint != modified_fingerprint
......@@ -43,8 +44,9 @@ async def submit_modification(data):
async def create_commit(project, data):
branch = data["start_branch"] if "start_branch" in data.keys() else data["branch"]
try:
_file = project.files.get(file_path=data["file_path"], ref=data["branch"])
_file = project.files.get(file_path=data["file_path"], ref=branch)
except Exception as err:
print(f"Source code loading failed on rule {data.get('branch')}: {err!r}")
......@@ -114,7 +116,7 @@ def validate(func):
@validate
def modification_data(data):
branch = f"modification-{fold_name(data.get('title')).lower()}"
last_commit_id = data.get('last_commit_id', {})
last_commit_id = data.get('commit_id', {})
start_branch = {"start_branch": "master"} if not bool(last_commit_id) else {}
data = {
"branch": f"RULE-{branch}",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment