Exemple #1
0
 def github_url(self):
     """Return the Github repository URL."""
     if "https://github.com" not in self.action_url:
         raise Exception("not a github url")
     _repo_name = repo_name(self.action_url)
     _org_name = org_name(self.action_url)
     return f"https://github.com/{_org_name}/{_repo_name}"
    def handle(self, *args, **options):

        notifications = get_notifications()
        print(len(notifications))
        for notification in notifications:
            process_me = notification['reason'] == 'mention'
            if process_me:
                try:
                    url = notification['subject']['url']
                    url = url.replace('/repos', '')
                    url = url.replace('//api.github', '//github')
                    latest_comment_url = notification['subject']['latest_comment_url']
                    _org_name = org_name(url)
                    _repo_name = repo_name(url)
                    _issue_number = issue_number(url)
                    _comment_id = latest_comment_url.split('/')[-1]
                    comment = get_issue_comments(_org_name, _repo_name, _issue_number, _comment_id)
                    does_mention_gitcoinbot = settings.GITHUB_API_USER in comment.get('body','')
                    if comment.get('message','') == "Not Found":
                        print("comment was not found")
                    elif not does_mention_gitcoinbot:
                        print("does not mention gitcoinbot")
                    else:
                        comment_from = comment['user']['login']
                        num_reactions = comment['reactions']['total_count']
                        print(_org_name, _repo_name, _issue_number, _comment_id, num_reactions, comment_from)
                        is_from_gitcoinbot = settings.GITHUB_API_USER in comment_from
                        if num_reactions == 0 and not is_from_gitcoinbot:
                            print("unprocessed")
                            post_issue_comment_reaction(_org_name, _repo_name, _comment_id, 'heart')
                            comment = f"@{comment_from}. :wave: thanks for the atMention, but you need to [install @gitcoinbot on this repo for me to be able to respond](https://github.com/apps/gitcoinbot).  More details [in the documentation](https://github.com/gitcoinco/web/tree/master/app/gitcoinbot).\n\n:v:\n@gitcoinbot"
                            post_issue_comment(_org_name, _repo_name, _issue_number, comment)
                except Exception as e:
                    logging.exception(e)
                    print(e)
Exemple #3
0
    def handle(self, *args, **options):
        num_days_back_to_warn = 3
        num_days_back_to_delete_interest = 7

        days = [i * 3 for i in range(1, 15)]
        if settings.DEBUG:
            days = range(1, 1000)
        for day in days:
            interests = Interest.objects.filter(
                created__gte=(timezone.now() - timezone.timedelta(days=(day+1))),
                created__lt=(timezone.now() - timezone.timedelta(days=day)),
            ).all()
            print('day {} got {} interests'.format(day, interests.count()))
            for interest in interests:
                for bounty in Bounty.objects.filter(interested=interest, current_bounty=True, idx_status__in=['open', 'started', 'submitted']):
                    print("{} is interested in {}".format(interest, bounty))
                    try:
                        owner = org_name(bounty.github_url)
                        repo = repo_name(bounty.github_url)
                        issue_num = issue_number(bounty.github_url)
                        comments = get_issue_comments(owner, repo, issue_num)
                        comments_by_interested_party = [comment for comment in comments if comment['user']['login'] == interest.profile.handle]
                        should_warn_user = False
                        should_delete_interest = False
                        last_heard_from_user_days = None
                        
                        if len(comments_by_interested_party) == 0:
                            should_warn_user = True
                            should_delete_interest = False
                        else:
                            # example format: 2018-01-26T17:56:31Z'
                            time_format = '%Y-%m-%dT%H:%M:%SZ'
                            last_comment_by_user = datetime.strptime(comments_by_interested_party[0]['created_at'], time_format)
                            delta_now_vs_last_comment = datetime.now() - last_comment_by_user
                            last_heard_from_user_days = delta_now_vs_last_comment.days
                            should_warn_user = last_heard_from_user_days >= num_days_back_to_warn
                            should_delete_interest = last_heard_from_user_days >= num_days_back_to_delete_interest
                        
                        if should_delete_interest:
                            print('executing should_delete_interest for {}'.format(interest.pk))
                            bounty_startwork_expired(interest.profile.email, bounty, interest, last_heard_from_user_days)
                            interest.delete()

                        elif should_warn_user:
                            print('executing should_warn_user for {}'.format(interest.pk))
                            bounty_startwork_expire_warning(interest.profile.email, bounty, interest, last_heard_from_user_days)

                    except Exception as e:
                        print(e)
Exemple #4
0
    def get_relative_url(self, preceding_slash=True):
        """Get the relative URL for the Bounty.

        Attributes:
            preceding_slash (bool): Whether or not to include a preceding slash.

        Returns:
            str: The relative URL for the Bounty.

        """
        try:
            _org_name = org_name(self.github_url)
            _issue_num = int(issue_number(self.github_url))
            _repo_name = repo_name(self.github_url)
            return f"{'/' if preceding_slash else ''}issue/{_org_name}/{_repo_name}/{_issue_num}"
        except Exception:
            return f"{'/' if preceding_slash else ''}funding/details?url={self.github_url}"
Exemple #5
0
def maybe_market_to_user_discord(bounty, event_name):
    """Send a Discord message to the user's discord channel for the specified Bounty.

    Args:
        bounty (dashboard.models.Bounty): The Bounty to be marketed.
        event_name (str): The name of the event.

    Returns:
        bool: Whether or not the Discord notification was sent successfully.

    """
    from dashboard.models import Profile
    if bounty.get_natural_value() < 0.0001:
        return False
    if bounty.network != settings.ENABLE_NOTIFICATIONS_ON_NETWORK:
        return False

    msg = build_message_for_integration(bounty, event_name)
    if not msg:
        return False

    url = bounty.github_url
    sent = False
    try:
        repo = org_name(url) + '/' + repo_name(url)
        subscribers = Profile.objects.filter(discord_repos__contains=[repo])
        subscribers = subscribers & Profile.objects.exclude(
            discord_webhook_url='')
        for subscriber in subscribers:
            try:
                headers = {'Content-Type': 'application/json'}
                body = {
                    "content": msg,
                    "avatar_url":
                    "https://gitcoin.co/static/v2/images/helmet.png"
                }
                discord_response = requests.post(
                    subscriber.discord_webhook_url, headers=headers, json=body)
                if discord_response.status_code == 204:
                    sent = True
            except Exception as e:
                print(e)
    except Exception as e:
        print(e)

    return sent
Exemple #6
0
    def all_bountied_repos(self):
        if self.all_bountied_repos_cache:
            return self.all_bountied_repos_cache

        bounties = Bounty.objects.filter(current_bounty=True)
        return_me = []
        for bounty in bounties:
            try:
                org_name = bounty.org_name
                rn = repo_name(bounty.github_url)
                this_repo = f"{org_name}/{rn}".lower()
                if this_repo not in return_me:
                    return_me.append(this_repo)
            except Exception as e:
                print(e)
        self.all_bountied_repos_cache = return_me
        return return_me
Exemple #7
0
def maybe_market_to_user_slack(bounty, event_name):
    """Send a Slack message to the user's slack channel for the specified Bounty.

    Args:
        bounty (dashboard.models.Bounty): The Bounty to be marketed.
        event_name (str): The name of the event.

    Returns:
        bool: Whether or not the Slack notification was sent successfully.

    """
    from dashboard.models import Profile
    if bounty.get_natural_value() < 0.0001:
        return False
    if bounty.network != settings.ENABLE_NOTIFICATIONS_ON_NETWORK:
        return False

    msg = build_message_for_slack(bounty, event_name)
    if not msg:
        return False

    url = bounty.github_url
    sent = False
    try:
        repo = org_name(url) + '/' + repo_name(url)
        subscribers = Profile.objects.filter(slack_repos__contains=[repo])
        subscribers = subscribers & Profile.objects.exclude(slack_token='',
                                                            slack_channel='')
        for subscriber in subscribers:
            try:
                sc = SlackClient(subscriber.slack_token)
                sc.api_call(
                    "chat.postMessage",
                    channel=subscriber.slack_channel,
                    text=msg,
                    icon_url=settings.GITCOIN_SLACK_ICON_URL,
                )
                sent = True
            except Exception as e:
                print(e)
    except Exception as e:
        print(e)

    return sent
Exemple #8
0
    def handle(self, *args, **options):

        notifications = get_notifications()
        print(len(notifications))
        for notification in notifications:
            process_me = notification['reason'] == 'mention'
            if process_me:
                try:
                    url = notification['subject']['url']
                    url = url.replace('/repos', '')
                    url = url.replace('//api.github', '//github')
                    latest_comment_url = notification['subject'][
                        'latest_comment_url']
                    _org_name = org_name(url)
                    _repo_name = repo_name(url)
                    _issue_number = issue_number(url)
                    _comment_id = latest_comment_url.split('/')[-1]
                    comment = get_issue_comments(_org_name, _repo_name,
                                                 _issue_number, _comment_id)
                    does_mention_gitcoinbot = settings.GITHUB_API_USER in comment.get(
                        'body', '')
                    if comment.get('message', '') == "Not Found":
                        print("comment was not found")
                    elif not does_mention_gitcoinbot:
                        print("does not mention gitcoinbot")
                    else:
                        comment_from = comment['user']['login']
                        num_reactions = comment['reactions']['total_count']
                        print(_org_name, _repo_name, _issue_number,
                              _comment_id, num_reactions, comment_from)
                        is_from_gitcoinbot = settings.GITHUB_API_USER in comment_from
                        if num_reactions == 0 and not is_from_gitcoinbot:
                            print("unprocessed")
                            post_issue_comment_reaction(
                                _org_name, _repo_name, _comment_id, 'heart')
                except Exception as e:
                    logging.exception(e)
                    print(e)
Exemple #9
0
 def github_repo_name(self):
     try:
         return repo_name(self.github_url)
     except Exception:
         return None
Exemple #10
0
    def handle(self, *args, **options):
        num_days_back_to_warn = 3
        num_days_back_to_delete_interest = 7

        days = [i * 3 for i in range(1, 15)]
        if settings.DEBUG:
            days = range(1, 1000)
        for day in days:
            interests = Interest.objects.filter(
                created__gte=(timezone.now() -
                              timezone.timedelta(days=(day + 1))),
                created__lt=(timezone.now() - timezone.timedelta(days=day)),
            ).all()
            print('day {} got {} interests'.format(day, interests.count()))
            for interest in interests:
                for bounty in Bounty.objects.filter(
                        interested=interest,
                        current_bounty=True,
                        idx_status__in=['open', 'started']):
                    print("{} is interested in {}".format(interest, bounty))
                    try:
                        owner = org_name(bounty.github_url)
                        repo = repo_name(bounty.github_url)
                        issue_num = issue_number(bounty.github_url)
                        comments = get_issue_comments(owner, repo, issue_num)
                        comments_by_interested_party = [
                            comment for comment in comments if comment['user']
                            ['login'] == interest.profile.handle
                        ]
                        should_warn_user = False
                        should_delete_interest = False
                        last_heard_from_user_days = None

                        if len(comments_by_interested_party) == 0:
                            should_warn_user = True
                            should_delete_interest = False
                        else:
                            # example format: 2018-01-26T17:56:31Z'
                            comment_times = [
                                datetime.strptime(comment['created_at'],
                                                  '%Y-%m-%dT%H:%M:%SZ')
                                for comment in comments_by_interested_party
                            ]
                            last_comment_by_user = max(comment_times)

                            # if user hasn't commented since they expressed interest, handled this condition
                            # per https://github.com/gitcoinco/web/issues/462#issuecomment-368384384
                            if last_comment_by_user < interest.created_at.replace(
                                    tzinfo=None):
                                last_comment_by_user = interest.created_at.replace(
                                    tzinfo=None)

                            # some small calcs
                            delta_now_vs_last_comment = datetime.now(
                            ) - last_comment_by_user
                            last_heard_from_user_days = delta_now_vs_last_comment.days

                            # decide action params
                            should_warn_user = last_heard_from_user_days >= num_days_back_to_warn
                            should_delete_interest = last_heard_from_user_days >= num_days_back_to_delete_interest

                            print(
                                f"- its been {last_heard_from_user_days} days since we heard from the user"
                            )

                        if should_delete_interest:
                            print('executing should_delete_interest for {}'.
                                  format(interest.pk))
                            bounty_startwork_expired(
                                interest.profile.email, bounty, interest,
                                last_heard_from_user_days)
                            interest.delete()

                        elif should_warn_user:
                            print('executing should_warn_user for {}'.format(
                                interest.pk))
                            bounty_startwork_expire_warning(
                                interest.profile.email, bounty, interest,
                                last_heard_from_user_days)

                    except Exception as e:
                        print(e)