def test_patch_issue_comment(self): """Test the github utility patch_issue_comment method.""" comment_id = 1 owner = 'gitcoinco' repo = 'web' url = f'https://api.github.com/repos/{owner}/{repo}/issues/comments/{comment_id}' responses.add(responses.PATCH, url, headers=HEADERS, json={}, status=200) result = patch_issue_comment(comment_id, owner, repo, 'A comment.') assert responses.calls[0].request.url == url assert result == {}
def maybe_market_to_github(bounty, event_name, profile_pairs=None): """Post a Github comment for the specified Bounty. Args: bounty (dashboard.models.Bounty): The Bounty to be marketed. event_name (str): The name of the event. profile_pairs (list of tuples): The list of username and profile page URL tuple pairs. Returns: bool: Whether or not the Github comment was posted successfully. """ if not bounty.is_notification_eligible( var_to_check=settings.GITHUB_CLIENT_ID): return False # Define posting specific variables. comment_id = None url = bounty.github_url uri = parse(url).path uri_array = uri.split('/') # Prepare the comment message string. msg = build_github_notification(bounty, event_name, profile_pairs) if not msg: return False try: username = uri_array[1] repo = uri_array[2] issue_num = uri_array[4] if event_name == 'work_started': comment_id = bounty.interested_comment elif event_name in ['work_done', 'work_submitted']: comment_id = bounty.submissions_comment # Handle creating or updating comments if profiles are provided. if event_name in ['work_started', 'work_submitted'] and profile_pairs: if comment_id is not None: patch_issue_comment(comment_id, username, repo, msg) else: response = post_issue_comment(username, repo, issue_num, msg) if response.get('id'): if event_name == 'work_started': bounty.interested_comment = int(response.get('id')) elif event_name in ['work_done', 'work_submitted']: bounty.submissions_comment = int(response.get('id')) bounty.save() # Handle deleting comments if no profiles are provided. elif event_name in ['work_started'] and not profile_pairs: if comment_id: delete_issue_comment(comment_id, username, repo) if event_name == 'work_started': bounty.interested_comment = None elif event_name == 'work_done': bounty.submissions_comment = None bounty.save() # If this isn't work_started/done, simply post the issue comment. else: post_issue_comment(username, repo, issue_num, msg) except IndexError: return False except Exception as e: extra_data = { 'github_url': url, 'bounty_id': bounty.pk, 'event_name': event_name } logger.error('Failure in marketing to github', exc_info=True, extra=extra_data) print(e) return False return True