Implement Relates-To and Closes tagging

This commit is contained in:
Ian H Pittwood 2020-02-27 10:26:15 -06:00
parent 300ab3ee0d
commit cf6e9a94c1
2 changed files with 82 additions and 41 deletions
gerrit_to_github_issues

@ -30,41 +30,53 @@ def update(gerrit_url: str, gerrit_project_name: str, github_project_name: str,
def process_change(change: dict, repo: Repository, gerrit_url: str): def process_change(change: dict, repo: Repository, gerrit_url: str):
issue_number = github_issues.parse_issue_number(change['commitMessage']) issue_numbers_dict = github_issues.parse_issue_number(change['commitMessage'])
if not issue_number: if not issue_numbers_dict:
LOG.warning(f'No issue tag found for change #{change["number"]}') LOG.warning(f'No issue tag found for change #{change["number"]}')
return return
try: for key, issues_list in issue_numbers_dict.items():
issue = repo.get_issue(issue_number) for issue_number in issues_list:
except github.GithubException: try:
LOG.warning(f'Issue #{issue_number} not found for project') issue = repo.get_issue(issue_number)
return except github.GithubException:
comment_msg = '' LOG.warning(f'Issue #{issue_number} not found for project')
change_url = gerrit.make_gerrit_url(gerrit_url, change['number']) return
link_exists = github_issues.check_issue_for_matching_comments(issue, change_url) comment_msg = ''
if issue.state == 'closed' and not link_exists: change_url = gerrit.make_gerrit_url(gerrit_url, change['number'])
issue.edit(state='open') link_exists = github_issues.check_issue_for_matching_comments(issue, change_url)
comment_msg += 'Issue reopened due to new activity on Gerrit.\n\n' if issue.state == 'closed' and not link_exists:
if 'WIP' in change['commitMessage'] or 'DNM' in change['commitMessage']: LOG.debug(f'Issue #{issue_number} was closed, reopening...')
LOG.debug(f'add `wip` to #{issue_number}') #issue.edit(state='open')
issue.add_to_labels('wip') #issue.create_comment('Issue reopened due to new activity on Gerrit.\n\n')
try: labels = issue.get_labels()
LOG.debug(f'rm `ready for review` to #{issue_number}') if 'WIP' in change['commitMessage'] or 'DNM' in change['commitMessage']:
issue.remove_from_labels('ready for review') if 'wip' not in labels:
except github.GithubException: LOG.debug(f'add `wip` to #{issue_number}')
LOG.debug(f'`ready for review` tag does not exist on issue #{issue_number}') #issue.add_to_labels('wip')
else: if 'ready for review' in labels:
LOG.debug(f'add `ready for review` to #{issue_number}') try:
issue.add_to_labels('ready for review') LOG.debug(f'rm `ready for review` to #{issue_number}')
try: #issue.remove_from_labels('ready for review')
LOG.debug(f'rm `wip` to #{issue_number}') except github.GithubException:
issue.remove_from_labels('wip') LOG.debug(f'`ready for review` tag does not exist on issue #{issue_number}')
except github.GithubException: else:
LOG.debug(f'`wip` tag does not exist on issue #{issue_number}') if 'ready for review' not in labels:
if not link_exists: LOG.debug(f'add `ready for review` to #{issue_number}')
comment_msg += f'New Related Change: {change_url}\n' \ #issue.add_to_labels('ready for review')
f'Authored By: {change["owner"]["name"]} ({change["owner"]["email"]})' if 'wip' in labels:
if comment_msg: try:
issue.create_comment(comment_msg) LOG.debug(f'rm `wip` to #{issue_number}')
LOG.debug(f'Comment to post on #{issue_number}: {comment_msg}') #issue.remove_from_labels('wip')
LOG.info(f'Comment posted to issue #{issue_number}') except github.GithubException:
LOG.debug(f'`wip` tag does not exist on issue #{issue_number}')
if not link_exists:
comment_msg += '### New Related Change\n\n' \
f'**Link:** {change_url}\n' \
f'**Subject:** {change["subject"]}\n' \
f'**Authored By:** {change["owner"]["name"]} ({change["owner"]["email"]})'
if key == 'closes':
comment_msg += '\n\nThis change will close this issue when merged.'
if comment_msg:
LOG.debug(f'Comment to post on #{issue_number}: {comment_msg}')
#issue.create_comment(comment_msg)
LOG.info(f'Comment posted to issue #{issue_number}')

@ -9,6 +9,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging
import re import re
import github import github
@ -17,12 +18,40 @@ from github.Repository import Repository
import errors import errors
LOG = logging.getLogger(__name__)
def parse_issue_number(commit_msg: str) -> int:
match = re.search(r'(?<=\[#)(.*?)(?=\])', commit_msg) def construct_issue_list(match_list: list) -> list:
if not match: new_list = []
return None for issue in match_list:
return int(match.group(0)) try:
new_list.append(int(issue))
except ValueError:
LOG.warning(f'Value {issue} could not be converted to `int` type')
return new_list
def parse_issue_number(commit_msg: str) -> dict:
# Searches for Relates-To or Closes tags first to match and return
LOG.debug(f'Parsing commit message: {commit_msg}')
related = re.findall(r'(?<=Relates-To: #)(.*?)(?=\n)', commit_msg)
LOG.debug(f'Captured related issues: {related}')
closes = re.findall(r'(?<=Closes: #)(.*?)(?=\n)', commit_msg)
LOG.debug(f'Captured closes issues: {closes}')
if related or closes:
return {
'related': construct_issue_list(related),
'closes': construct_issue_list(closes)
}
# If no Relates-To or Closes tags are defined, find legacy [#X] style tags
LOG.debug('Falling back to legacy tags')
legacy_matches = re.findall(r'(?<=\[#)(.*?)(?=\])', commit_msg)
LOG.debug(f'Captured legacy issues: {legacy_matches}')
if not legacy_matches:
return {}
return {
'related': construct_issue_list(legacy_matches)
}
def get_repo(repo_name: str, github_user: str, github_pw: str, github_token: str) -> Repository: def get_repo(repo_name: str, github_user: str, github_pw: str, github_token: str) -> Repository: