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_gh_notifications() print('Notifications Count: ', notifications.totalCount) for notification in notifications: if notification.reason == 'mention': 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)
def handle(self, *args, **options): notifications = get_notifications() try: print('Notifications Count: ', notifications.totalCount) for notification in notifications: if hasattr(notification, '_subject') and notification.reason == 'mention': try: url = notification.subject.url url = url.replace('/repos', '') url = url.replace('//api.github', '//github') latest_comment_url = notification.subject.latest_comment_url if latest_comment_url is None: print("no latest comment url") continue _org_name = org_name(url) _repo_name = repo_name(url) _issue_number = issue_number(url) if not latest_comment_url: continue _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.body if isinstance(comment, dict) and 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.get_reactions().totalCount 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 RateLimitExceededException as e: logging.debug(e) print(e) time.sleep(60) except Exception as e: logging.exception(e) print(e) except RateLimitExceededException as e: logging.debug(e) print(e) except AttributeError as e: logging.debug(e) print(e)
def all_bountied_repos(self): if self.all_bountied_repos_cache: return self.all_bountied_repos_cache bounties = Bounty.objects.current() 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
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
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_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(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