From 045725b6c252ea9da20aecd0ffc19a03cef58366 Mon Sep 17 00:00:00 2001 From: sidpremkumar Date: Jul 10 2019 18:09:47 +0000 Subject: [PATCH 1/3] issue matching will always return a value now --- diff --git a/sync2jira/downstream.py b/sync2jira/downstream.py index 3d3313b..22a31c1 100644 --- a/sync2jira/downstream.py +++ b/sync2jira/downstream.py @@ -85,7 +85,7 @@ def _get_jira_client(issue, config): if not isinstance(issue, Issue): log.error("passed in issue is not an Issue instance") - log.error("It is a %s" % (type(issue).__name__)) + log.error("It is a %s" % type(issue).__name__) raise TypeError("Got %s, expected Issue" % type(issue).__name__) # Use the Jira instance set in the issue config. If none then @@ -125,22 +125,44 @@ def _matching_jira_issue_query(client, issue, config, free=False): # Then when that issue is dropped and another one is created is is created with the same # url : pagure.com/something/issue/5. # We need to ensure that we are not catching a dropped issue - # Loop through the results of the query and make sure the ids + # Loop through the results of the query and make sure the ids match final_results = [] for result in results_of_query: # If the queried JIRA issue has the id of the upstream issue or the same title if issue.id in result.fields.description or issue.title == result.fields.summary: - if check_comments_for_duplicate(client, result): + search = check_comments_for_duplicate(client, result) + if search is True: final_results.append(result) + else: + # Else search returned a linked issue + final_results.append(search) # If that's not the case, check if they have the same upstream title # Upstream username/repo can change if repos are merged elif re.search(r"\[[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':\\|,.<>\/?]*\] " + issue.upstream_title, result.fields.summary): - if check_comments_for_duplicate(client, result): + search = check_comments_for_duplicate(client, result) + if search is True: + # We went through all the comments and didn't find anything + # that indicated it was a duplicate log.warning(' Matching downstream issue %s to upstream issue %s' % (result.fields.summary, issue.title)) final_results.append(result) + else: + # Else search returned a linked issue + final_results.append(search) + if not final_results: + # Just return the most updated issue + issue = None + date = datetime(1800, 1, 1) + for result in results_of_query: + result_date = datetime.strptime( + result.fields.updated, '%Y-%m-%dT%H:%M:%S.%f+000)') + if result_date > date: + date = result_date + issue = result + final_results.append(issue) + # Return the final_results log.debug("Found %i results for query %r", len(final_results), query) return final_results @@ -156,11 +178,17 @@ def check_comments_for_duplicate(client, result): client (jira.client.JIRA): JIRA client) result (jira.resource.Issue): JIRA issue Returns: - return (bool): True/False if duplicate comment was found/not found + return (bool): True if duplicate comment was not found + *Or* + return (jira.resource.Issue): JIRA issue if we were able to + find it """ for comment in client.comments(result): - if re.search(r'Marking as duplicate of (\w*)-(\d*)', comment.body): - return False + search = re.search(r'Marking as duplicate of (\w*)-(\d*)', + comment.body) + if search and comment.author.name == 'red-user': + issue_id = search.groups()[0] + '-' + search.groups()[1] + return client.issue(issue_id) return True @@ -790,6 +818,9 @@ def sync_with_jira(issue, config): if existing: # If we found an existing JIRA issue already log.info(" Found existing, matching downstream %r.", existing.key) + if config['sync2jira']['testing']: + log.info(" Testing flag is true. Skipping actual update.") + return # Update relevant metadata (i.e. tags, assignee, etc) _update_jira_issue(existing, issue, client) return diff --git a/sync2jira/main.py b/sync2jira/main.py index 60bc13c..9c3148a 100644 --- a/sync2jira/main.py +++ b/sync2jira/main.py @@ -138,7 +138,6 @@ def listen(config): issue = u.handle_github_message(msg, config) if not issue: - log.warning(" %s, %s yielded no Issue object.", suffix, idx) continue d.sync_with_jira(issue, config) From 8385090875df256ab2fde5e5f058757cfdfb9484 Mon Sep 17 00:00:00 2001 From: sidpremkumar Date: Jul 11 2019 12:46:59 +0000 Subject: [PATCH 2/3] Removing hard-coded user in comment parsing, now pull from config dict --- diff --git a/sync2jira/downstream.py b/sync2jira/downstream.py index 22a31c1..c40fd8e 100644 --- a/sync2jira/downstream.py +++ b/sync2jira/downstream.py @@ -141,7 +141,8 @@ def _matching_jira_issue_query(client, issue, config, free=False): elif re.search(r"\[[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':\\|,.<>\/?]*\] " + issue.upstream_title, result.fields.summary): - search = check_comments_for_duplicate(client, result) + search = check_comments_for_duplicate(client, result, + find_username(issue, config)) if search is True: # We went through all the comments and didn't find anything # that indicated it was a duplicate @@ -170,13 +171,32 @@ def _matching_jira_issue_query(client, issue, config, free=False): return results_of_query -def check_comments_for_duplicate(client, result): +def find_username(issue, config): + """ + Finds JIRA username for an issue object + Args: + issue (sync2jira.intermediary.Issue): Issue object + config (dict): Config dict + Returns: + return (str): Username string + """ + jira_instance = issue.downstream.get('jira_instance', False) + if not jira_instance: + jira_instance = config['sync2jira'].get('default_jira_instance', False) + if not jira_instance: + log.error(" No jira_instance for issue and there is no default in the config") + raise Exception + return config['sync2jira']['jira'][jira_instance]['basic_auth'][0] + + +def check_comments_for_duplicate(client, result, username): """ Checks comment of JIRA issue to see if it has been marked as a duplicate Args: client (jira.client.JIRA): JIRA client) result (jira.resource.Issue): JIRA issue + username (str): Username of JIRA user Returns: return (bool): True if duplicate comment was not found *Or* @@ -186,7 +206,7 @@ def check_comments_for_duplicate(client, result): for comment in client.comments(result): search = re.search(r'Marking as duplicate of (\w*)-(\d*)', comment.body) - if search and comment.author.name == 'red-user': + if search and comment.author.name == username: issue_id = search.groups()[0] + '-' + search.groups()[1] return client.issue(issue_id) return True From d7351ed0a3111e49f72a1227848348979dccc72a Mon Sep 17 00:00:00 2001 From: sidpremkumar Date: Jul 11 2019 13:06:28 +0000 Subject: [PATCH 3/3] condenced function to find recently updated issue in matching --- diff --git a/sync2jira/downstream.py b/sync2jira/downstream.py index c40fd8e..acf6018 100644 --- a/sync2jira/downstream.py +++ b/sync2jira/downstream.py @@ -154,15 +154,9 @@ def _matching_jira_issue_query(client, issue, config, free=False): final_results.append(search) if not final_results: # Just return the most updated issue - issue = None - date = datetime(1800, 1, 1) - for result in results_of_query: - result_date = datetime.strptime( - result.fields.updated, '%Y-%m-%dT%H:%M:%S.%f+000)') - if result_date > date: - date = result_date - issue = result - final_results.append(issue) + results_of_query.sort(key=lambda x: datetime.strptime( + x.fields.updated, '%Y-%m-%dT%H:%M:%S.%f+0000')) + final_results.append(results_of_query[0]) # Return the final_results log.debug("Found %i results for query %r", len(final_results), query)