Commit ec69f3a4 authored by David Foucher's avatar David Foucher

Updt change branch naming (no more date)

parent 836ba585
import pytest
import datetime
from trefle.source import modification_data
DATA = {
......@@ -12,8 +10,6 @@ DATA = {
'author_email': 'test@test.com',
'author_name': 'test',
}
# TODO: minutes can be different so fix minute to 00 (mock)
NOW = datetime.datetime.today().strftime("%y%m%d%H%M")
def test_modification_data_mail_error():
......@@ -24,6 +20,15 @@ def test_modification_data_mail_error():
assert "pas un mail valide" in str(err.value)
# TODO: mok gitlab api
# def test_modification_with_last_commit_id():
# data = DATA.copy()
# data["last_commit_id"] = 'test'
# with pytest.raises(ValueError) as err:
# modification_data(data)
# assert "pas un identifiant de commit valide" in str(err.value)
def test_modification_data_filename_error():
data = DATA.copy()
data["filename"] = 'test'
......@@ -48,18 +53,27 @@ def test_modification_data_title_not_provided():
assert "doit être renseigné" in str(err.value)
def test_modification_data():
def test_modification_data_with_new_branch_is_valid():
assert modification_data(DATA) == {
"branch": f"RULE-modification-test-{NOW}",
"branch": f"RULE-modification-test",
"start_branch": "master",
"commit_message": 'test',
"author_email": 'test@test.com',
"author_name": 'test',
"actions": [
{
"action": "update",
"file_path": 'test.rules',
"content": 'test',
}
],
"file_path": "test.rules",
"content": "test",
}
def test_modification_data_with_existing_branch_is_valid():
data = DATA.copy()
data["last_commit_id"] = "7cfa6c27"
assert modification_data(data) == {
"branch": f"RULE-modification-test",
"commit_message": 'test',
"author_email": 'test@test.com',
"author_name": 'test',
"file_path": "test.rules",
"content": "test",
"last_commit_id": "7cfa6c27"
}
import datetime
import re
import gitlab
......@@ -15,17 +14,18 @@ async def load_project():
async def source_modified(project, data):
filename = data['actions'][0]['file_path']
content = data['actions'][0]['content']
original_fingerprint = hash(project.files.get(filename,
ref='master').decode().decode())
filename = data["file_path"]
content = data["content"]
original_fingerprint = hash(
project.files.get(filename, ref=data["branch"]).decode().decode()
)
modified_fingerprint = hash(content)
return original_fingerprint != modified_fingerprint
async def submit_modification(data):
project = await load_project()
mr_title = data.get('title')
mr_title = data.get("title")
data = modification_data(data)
if not await source_modified(project, data):
......@@ -36,25 +36,46 @@ async def submit_modification(data):
)
else:
commit = await create_commit(project, data)
mr = await create_mr(project, data.get("branch"), mr_title)
commit["merge_request"] = mr
if "start_branch" in data:
mr = await create_mr(project, data.get("branch"), mr_title)
commit["merge_request"] = mr
return commit
async def create_commit(project, data):
try:
commit = project.commits.create(data)
return commit.attributes
_file = project.files.get(file_path=data["file_path"], ref=data["branch"])
except Exception as err:
print(f"Source code commit failed on rule {data.get('branch')}: {err!r}")
print(f"Source code loading failed on rule {data.get('branch')}: {err!r}")
_file.content = data["content"]
if "start_branch" in data:
try:
return _file.save(branch=data["branch"],
commit_message=data["commit_message"],
start_branch=data["start_branch"])
except Exception as err:
print(f"Source code first modification failed on rule {data.get('branch')}: {err!r}")
else:
try:
return _file.save(branch=data["branch"],
commit_message=data["commit_message"],
last_commit_id=data["last_commit_id"])
except Exception as err:
print(f"Source code modification failed on rule {data.get('branch')}: {err!r}")
async def create_mr(project, source_branch, title):
try:
mr = project.mergerequests.create({'source_branch': source_branch,
'target_branch': 'master',
'title': title,
'labels': ['RULE']})
mr = project.mergerequests.create(
{
"source_branch": source_branch,
"target_branch": "master",
"title": title,
"labels": ["RULE", "gui-modified"],
}
)
return mr.attributes
except Exception as err:
print(f"Merge request creation failed on rule {source_branch}: {err!r}")
......@@ -92,19 +113,18 @@ def validate(func):
@validate
def modification_data(data):
now = datetime.datetime.today().strftime("%y%m%d%H%M")
branch = f"modification-{fold_name(data.get('title')).lower()}"
return {
"branch": f"RULE-{branch}-{now}",
"start_branch": "master",
last_commit_id = data.get('last_commit_id', {})
start_branch = {"start_branch": "master"} if not bool(last_commit_id) else {}
data = {
"branch": f"RULE-{branch}",
"commit_message": data.get("comment"),
"author_email": data.get("author_email"),
"author_name": data.get("author_name"),
"actions": [
{
"action": "update",
"file_path": data.get("filename"),
"content": data.get("content"),
}
],
"file_path": data.get("filename"),
"content": data.get("content"),
}
data.update({"last_commit_id": last_commit_id} if bool(last_commit_id) else {})
data.update(start_branch)
return data
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