Exemplo n.º 1
0
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
Exemplo n.º 2
0
def maybe_market_kudos_to_github(kt):
    """Post a Github comment for the specified Kudos.

    Args:
        kt (kudos.models.KudosTransfer): The KudosTransfer to be marketed.

    Returns:
        bool: Whether or not the Github comment was posted successfully.

    """
    if not kt.is_notification_eligible(
            var_to_check=settings.GITHUB_CLIENT_ID) or not kt.github_url:
        return False

    # prepare message
    username = kt.username if '@' in kt.username else f'@{kt.username}'
    _from = f" from @{kt.from_username}" if kt.from_name else ""
    warning = kt.network if kt.network != 'mainnet' else ""
    _comments = "\n\nThe sender had the following public comments: \n> " \
                f"{kt.comments_public}" if kt.comments_public else ""
    if kt.username:
        msg = f"⚡️ A *{kt.kudos_token_cloned_from.humanized_name}* Kudos has been " \
              f"sent to {username} for this issue{_from}. ⚡️ {_comments}\n\nNice work {username}! "
        redeem_instructions = "\nTo redeem your Kudos, login to Gitcoin at https://gitcoin.co/explorer and select " \
                              "'Claim Kudos' from dropdown menu in the top right, or check your email for a " \
                              "link to the Kudos redemption page. "
        if kt.receive_txid:
            redeem_instructions = "\nYour Kudos has automatically been sent in the ETH address we have on file."
        msg += redeem_instructions
    else:
        # TODO: support this once ETH-only sends are done
        return False

    image = f"<a title='{kt.kudos_token_cloned_from.humanized_name}' href='{kt.kudos_token_cloned_from.url}'> " \
            f"<img width='250' src='{kt.kudos_token_cloned_from.img_url}' " \
            f"alt='{kt.kudos_token_cloned_from.humanized_name}'> " \
            f"</a> "
    msg = f"""
<table>
<tr>
<td>
{image}
</td>
<td>
{msg}
</td>
</tr>
</table>"""
    # actually post
    url = kt.github_url
    uri = parse(url).path
    uri_array = uri.split('/')
    try:
        username = uri_array[1]
        repo = uri_array[2]
        issue_num = uri_array[4]
        post_issue_comment(username, repo, issue_num, msg)
    except Exception as e:
        print(e)
        return False
    return True