Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion configs/config.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ custom_template=Custom
GITLAB_TOKEN=
squash_commits=true
delete_branch_after_merge=true
auto_fetch_after_merge_request=false
developer_email="<someone@mail.com>"
OPENAI_API_KEY=<your_openai_api_key>
incident_project_id=<your_incident_project_id>
production_pipeline_name=deploy
production_job_name=
production_ref=
production_ref=
6 changes: 5 additions & 1 deletion gitHappens.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
DELETE_BRANCH = config.get('DEFAULT', 'delete_branch_after_merge').lower() == 'true'
DEVELOPER_EMAIL = config.get('DEFAULT', 'developer_email', fallback=None)
SQUASH_COMMITS = config.get('DEFAULT', 'squash_commits').lower() == 'true'
AUTO_FETCH_AFTER_MERGE_REQUEST = config.get('DEFAULT', 'auto_fetch_after_merge_request', fallback='false').lower() == 'true'
PRODUCTION_PIPELINE_NAME = config.get('DEFAULT', 'production_pipeline_name', fallback='deploy')
PRODUCTION_JOB_NAME = config.get('DEFAULT', 'production_job_name', fallback=None)
PRODUCTION_REF = config.get('DEFAULT', 'production_ref', fallback=None)
Expand Down Expand Up @@ -336,6 +337,9 @@ def startIssueCreation(project_id, title, milestone, epic, iteration, selectedSe
createdMergeRequest = create_merge_request(project_id, createdBranch, createdIssue, selectedSettings.get('labels'), milestone)
print(f"Merge request #{createdMergeRequest['iid']}: {createdMergeRequest['title']} created.")

if AUTO_FETCH_AFTER_MERGE_REQUEST:
subprocess.check_call(["git", "fetch", "origin"])

print("Run:")
print(" git fetch origin")
print(f" git checkout -b '{createdMergeRequest['source_branch']}' 'origin/{createdMergeRequest['source_branch']}'")
Expand Down Expand Up @@ -842,4 +846,4 @@ def main():
startIssueCreation(project_id, title, milestone, epic, iteration, selectedSettings, onlyIssue)

if __name__ == '__main__':
main()
main()
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

73 changes: 73 additions & 0 deletions tests/test_auto_fetch_after_merge_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import importlib.util
import sys
import types
import unittest
from pathlib import Path
from unittest import mock


def load_githappens_module(auto_fetch_after_merge_request):
root = Path(__file__).resolve().parents[1]
config_dir = root / "configs"
config_dir.mkdir(exist_ok=True)
(config_dir / "config.ini").write_text(
"[DEFAULT]\n"
"base_url=https://gitlab.example\n"
Comment on lines +11 to +20
"group_id=1\n"
"custom_template=Custom\n"
"GITLAB_TOKEN=test-token\n"
"squash_commits=true\n"
"delete_branch_after_merge=true\n"
f"auto_fetch_after_merge_request={str(auto_fetch_after_merge_request).lower()}\n",
encoding="utf-8",
)
(config_dir / "templates.json").write_text(
'{"templates": [], "reviewers": []}',
encoding="utf-8",
)

inquirer_stub = types.SimpleNamespace(
prompt=mock.Mock(return_value={"estimated_time": ""}),
Text=lambda *args, **kwargs: ("Text", args, kwargs),
List=lambda *args, **kwargs: ("List", args, kwargs),
Checkbox=lambda *args, **kwargs: ("Checkbox", args, kwargs),
)
sys.modules["inquirer"] = inquirer_stub

spec = importlib.util.spec_from_file_location("gitHappens_under_test", root / "gitHappens.py")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module


class AutoFetchAfterMergeRequestTest(unittest.TestCase):
def test_fetches_origin_after_merge_request_when_setting_is_enabled(self):
git_happens = load_githappens_module(auto_fetch_after_merge_request=True)
created_issue = {"iid": 12, "title": "Fix broken thing"}
Comment on lines +58 to +61
created_branch = {"name": "12-fix-broken-thing"}
created_merge_request = {
"iid": 34,
"title": "Fix broken thing",
"source_branch": "12-fix-broken-thing",
}

with mock.patch.object(git_happens, "createIssue", return_value=created_issue), \
mock.patch.object(git_happens, "create_branch", return_value=created_branch), \
mock.patch.object(git_happens, "create_merge_request", return_value=created_merge_request), \
mock.patch.object(git_happens.subprocess, "check_call") as check_call:
result = git_happens.startIssueCreation(
99,
"Fix broken thing",
False,
False,
False,
{"labels": ["Bug"]},
False,
)

self.assertEqual(result, created_issue)
check_call.assert_called_once_with(["git", "fetch", "origin"])


if __name__ == "__main__":
unittest.main()
Loading