Skip to content

Commit

Permalink
Provides support to allow skipping pre-migrated bugs from bugzilla to…
Browse files Browse the repository at this point in the history
… gitlab, this closes xmunoz#37
  • Loading branch information
bRitch022 committed May 20, 2022
1 parent 50862f2 commit e0ee04d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
1 change: 1 addition & 0 deletions bugzilla2gitlab/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"include_bugzilla_link",
"use_bugzilla_id",
"verify",
"gitlab_skip_pre_migrated_issues",
],
)

Expand Down
14 changes: 12 additions & 2 deletions bugzilla2gitlab/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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
33 changes: 33 additions & 0 deletions bugzilla2gitlab/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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"]]]
Expand Down
10 changes: 9 additions & 1 deletion tests/test_bugzilla2gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]))
7 changes: 5 additions & 2 deletions tests/test_data/config/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

0 comments on commit e0ee04d

Please sign in to comment.