diff --git a/bugzilla2gitlab/config.py b/bugzilla2gitlab/config.py index 44cdd32..000ebc1 100644 --- a/bugzilla2gitlab/config.py +++ b/bugzilla2gitlab/config.py @@ -32,6 +32,7 @@ "include_bugzilla_link", "use_bugzilla_id", "verify", + "gitlab_skip_pre_migrated_issues", ], ) diff --git a/bugzilla2gitlab/migrator.py b/bugzilla2gitlab/migrator.py index 497570a..a141bc4 100644 --- a/bugzilla2gitlab/migrator.py +++ b/bugzilla2gitlab/migrator.py @@ -18,8 +18,12 @@ def migrate(self, bug_list): self.conf.bugzilla_user, self.conf.bugzilla_password, ) + + bug_migrated = True # assume true for now for bug in bug_list: - self.migrate_one(bug) + bug_migrated = self.migrate_one(bug) + + return bug_migrated def migrate_one(self, bugzilla_bug_id): """ @@ -28,4 +32,10 @@ def migrate_one(self, bugzilla_bug_id): print("Migrating bug {}".format(bugzilla_bug_id)) fields = get_bugzilla_bug(self.conf.bugzilla_base_url, bugzilla_bug_id) issue_thread = IssueThread(self.conf, fields) - issue_thread.save() + + if(self.conf.gitlab_skip_pre_migrated_issues and issue_thread.preexists): + print("Bug {} already migrated".format(bugzilla_bug_id)) + return False + else: + issue_thread.save() + return True diff --git a/bugzilla2gitlab/models.py b/bugzilla2gitlab/models.py index bfa33aa..bb62df5 100644 --- a/bugzilla2gitlab/models.py +++ b/bugzilla2gitlab/models.py @@ -13,6 +13,7 @@ class IssueThread: def __init__(self, config, fields): global CONF CONF = config + self.preexists = False self.load_objects(fields) def load_objects(self, fields): @@ -21,6 +22,14 @@ def load_objects(self, fields): If CONF.dry_run=False, then Attachments are created in GitLab in this step. """ self.issue = Issue(fields) + + """ + If configured, check if the issue already exists + """ + if(CONF.gitlab_skip_pre_migrated_issues is True and self.issue.find_issue()): + self.preexists = True + return + self.comments = [] """ fields["long_desc"] gets peared down in Issue creation (above). This is because bugzilla @@ -71,6 +80,30 @@ def __init__(self, bugzilla_fields): validate_user(bugzilla_fields["assigned_to"]) self.load_fields(bugzilla_fields) + def find_issue(self): + url = "{}/issues?search{}&in=title".format( + CONF.gitlab_base_url, self.title, + ) + + data = {k: v for k, v in self.__dict__.items() if k in self.data_fields} + + self.headers["sudo"] = self.sudo + + response = _perform_request( + url, + "get", + headers=self.headers, + data=data, + json=True, + dry_run=CONF.dry_run, + verify=CONF.verify, + ) + + if(response and isinstance(response, list) and response[0] is not ""): + return True + else: + return False + def load_fields(self, fields): self.title = fields["short_desc"] self.sudo = CONF.gitlab_users[CONF.bugzilla_users[fields["reporter"]]] diff --git a/tests/test_bugzilla2gitlab.py b/tests/test_bugzilla2gitlab.py index 3d1b7b1..2eb71bc 100755 --- a/tests/test_bugzilla2gitlab.py +++ b/tests/test_bugzilla2gitlab.py @@ -64,6 +64,9 @@ def mockmilestones(project_id, gitlab_url, headers, verify): # conf.gitlab_milestones is a dictionary assert isinstance(conf.gitlab_milestones, dict) + #conf.gitlab_skip_pre_migrated_issues is a boolean value + assert isinstance(conf.gitlab_skip_pre_migrated_issues, bool) + def test_Migrator(monkeypatch): bug_id = 103 @@ -88,4 +91,9 @@ def mock_fetchbugcontent(url, bug_id): # just test that it works without throwing any exceptions client = Migrator(os.path.join(TEST_DATA_PATH, "config")) - client.migrate([bug_id]) + assert (True, client.migrate([bug_id])) + + # Test that the same bug is skipped if it's attempted to migrate again + conf = bugzilla2gitlab.config.get_config(os.path.join(TEST_DATA_PATH, "config")) + if(conf.gitlab_skip_pre_migrated_issues is True): + assert (False, client.migrate([bug_id])) diff --git a/tests/test_data/config/defaults.yml b/tests/test_data/config/defaults.yml index c9957d7..6d8c1cc 100755 --- a/tests/test_data/config/defaults.yml +++ b/tests/test_data/config/defaults.yml @@ -11,9 +11,9 @@ gitlab_base_url: "https://git.example.com/api/v4" verify: true # Auto-generate the issue id in Gitlab. If set to `true`, create a Gitlab issue with -# the same id as the Bugzilla ticket number. The group members which are +# the same id as the Bugzilla ticket number. The group members which are # mapped in the user mappings need to be defined as project owners for this -# to work properly. Otherwise the Gitlab API will silently ignore the `iid` setting +# to work properly. Otherwise the Gitlab API will silently ignore the `iid` setting # and fallback to the default behavior (i.e. auto-generate the issue id). use_bugzilla_id: false @@ -76,3 +76,6 @@ map_milestones: true milestones_to_skip: - "---" - "UNKNOWN" + +# Set to true to skip pre-migrated issues +gitlab_skip_pre_migrated_issues: true