Skip to content
Open
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
15 changes: 10 additions & 5 deletions gitHappens.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,13 +336,18 @@ 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.")

print("Run:")
print(" git fetch origin")
print(f" git checkout -b '{createdMergeRequest['source_branch']}' 'origin/{createdMergeRequest['source_branch']}'")
print("to switch to new branch.")
checkoutBranch(createdMergeRequest['source_branch'])
print(f"Switched to branch {createdMergeRequest['source_branch']}.")
Comment on lines +339 to +340

return createdIssue

def checkoutBranch(branchName):
subprocess.check_call(['git', 'fetch', 'origin'])
try:
subprocess.check_call(['git', 'checkout', branchName])
except subprocess.CalledProcessError:
subprocess.check_call(['git', 'checkout', '-b', branchName, f'origin/{branchName}'])
Comment on lines +344 to +349

def getCurrentBranch():
return subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'], text=True).strip()

Expand Down Expand Up @@ -842,4 +847,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_start_issue_creation.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():
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"
"group_id=1\n"
"custom_template=Custom\n"
"GITLAB_TOKEN=test-token\n"
"squash_commits=true\n"
"delete_branch_after_merge=true\n",
encoding="utf-8",
)
(config_dir / "templates.json").write_text(
'{"templates": [], "reviewers": []}',
encoding="utf-8",
)
Comment on lines +10 to +26

inquirer_stub = types.SimpleNamespace(
prompt=mock.Mock(),
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)
Comment on lines +34 to +38
return module


class StartIssueCreationTest(unittest.TestCase):
def test_switches_to_created_merge_request_branch(self):
git_happens = load_githappens_module()
created_issue = {"iid": 12, "title": "Fix broken thing"}
created_branch = {"name": "12-fix-broken-thing"}
created_merge_request = {
"iid": 34,
"title": "Fix broken thing",
"source_branch": "12-fix-broken-thing",
}

git_happens.inquirer.prompt.return_value = {"estimated_time": ""}
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, "checkoutBranch", create=True) as checkout_branch:
result = git_happens.startIssueCreation(
99,
"Fix broken thing",
False,
False,
False,
{"labels": ["Bug"]},
False,
)

self.assertEqual(result, created_issue)
checkout_branch.assert_called_once_with("12-fix-broken-thing")


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