From 8d20b3f2a51afc509a82553adc12ed450ea0b55d Mon Sep 17 00:00:00 2001 From: Daniel Augusto Date: Fri, 5 Oct 2018 23:42:42 -0300 Subject: :sparkles: Added bug and feature request templates --- .github/ISSUE_TEMPLATE/bug-report-template.md | 36 +++++++++++++++++++++++++++ .github/ISSUE_TEMPLATE/feature_request.md | 24 ++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/bug-report-template.md create mode 100644 .github/ISSUE_TEMPLATE/feature_request.md diff --git a/.github/ISSUE_TEMPLATE/bug-report-template.md b/.github/ISSUE_TEMPLATE/bug-report-template.md new file mode 100644 index 00000000..bcce2b6d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report-template.md @@ -0,0 +1,36 @@ +--- +name: Bug Report Template +about: A simple bug report template. + +--- + +## Description + +Please, provide here a simple description about the bug. + +## Steps + +### Step 1 +Please, provide here a detailed description about the step 1. + +### Step 2 +Please, provide here a detailed description about the step 2. + +### Step 3 +Please, provide here a detailed description about the step 3. + +## Expected Behavior +The expected behaviour is . + +## Actual Behavior +The actual behaviour is . + +## Environment +- Device Type: (e.g.: Desktop or Mobile) +- Operating System: (e.g.: Microsoft Windows or Ubuntu Linux) + - Version: (e.g.: 10 or 18.04) +- Browser: (e.g.: Mozilla Firefox or Google Chrome) + - Version: (e.g.: 1, 2, 3) + +## Other +Please, provide here additional details that can be helpful. diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 00000000..30606299 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,24 @@ +--- +name: Feature request +about: A simple feature request template. + +--- + +## Description + +Please, provide here a simple description about the feature request. + +## Reason + +- Reason 1 +- Reason 2 +- Reason 3 + +## Required Changes + +- Change 1 +- Change 2 +- Change 3 + +## Other +Please, provide here additional details that can be helpful. -- cgit v1.2.3 From 929a46dc6c46df731c275c584b1a0f163bd0a881 Mon Sep 17 00:00:00 2001 From: Daniel Augusto Date: Sat, 6 Oct 2018 00:48:03 -0300 Subject: :memo: Improved grammar and replaced numbered items. --- .github/ISSUE_TEMPLATE/bug-report-template.md | 23 ++++++++--------------- .github/ISSUE_TEMPLATE/feature_request.md | 16 ++++++++-------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/bug-report-template.md b/.github/ISSUE_TEMPLATE/bug-report-template.md index bcce2b6d..6c03fc5a 100644 --- a/.github/ISSUE_TEMPLATE/bug-report-template.md +++ b/.github/ISSUE_TEMPLATE/bug-report-template.md @@ -6,31 +6,24 @@ about: A simple bug report template. ## Description -Please, provide here a simple description about the bug. +[provide a simple description of the bug] -## Steps +## Steps to Reproduce -### Step 1 -Please, provide here a detailed description about the step 1. - -### Step 2 -Please, provide here a detailed description about the step 2. - -### Step 3 -Please, provide here a detailed description about the step 3. +- [replace with a description of a step to reproduce the issue] +- [replace with a description of a step to reproduce the issue] +- [replace with a description of a step to reproduce the issue] ## Expected Behavior -The expected behaviour is . +The expected behaviour is [provide a detailed description and, if necessary, screenshots]. ## Actual Behavior -The actual behaviour is . +The actual behaviour is [provide a detailed description and, if necessary, screenshots]. ## Environment - Device Type: (e.g.: Desktop or Mobile) - Operating System: (e.g.: Microsoft Windows or Ubuntu Linux) - Version: (e.g.: 10 or 18.04) -- Browser: (e.g.: Mozilla Firefox or Google Chrome) - - Version: (e.g.: 1, 2, 3) ## Other -Please, provide here additional details that can be helpful. +[provide any additional details that can be helpful] diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index 30606299..f0d1d9c0 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -6,19 +6,19 @@ about: A simple feature request template. ## Description -Please, provide here a simple description about the feature request. +[provide a simple description of the feature request] ## Reason -- Reason 1 -- Reason 2 -- Reason 3 +- [replace with a reason] +- [replace with a reason] +- [replace with a reason] ## Required Changes -- Change 1 -- Change 2 -- Change 3 +- [replace with a change] +- [replace with a change] +- [replace with a change] ## Other -Please, provide here additional details that can be helpful. +[provide any additional details that can be helpful] -- cgit v1.2.3 From bee600fa2a94bdf0f4fc826c0914d012f28e9988 Mon Sep 17 00:00:00 2001 From: sco1 Date: Sun, 7 Oct 2018 15:26:06 -0700 Subject: Add Hacktoberfest user stats cog (#7) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Initialize Hacktoberfest stats cog * Fix generated grammar, add temporary variables to shorten string gen * Code review modifications Adjust variable & function names to be more in line with PEP8's recommendations. Break API query URL into multiple parts to aid readability and customization Adjust command input to explicitly accept a username input rather than star packing & unpacking. d.py guards against too many inputs for us Change _getURL to _get_shortname and adjust documentation because we're not even doing what the function name and documentation said we were * Remove hardcoded response channel * Update prefixes Spoooooky * Swap emoji for unicode string 👻👻👻 * Change unicode ghost string to a friendlier ghost string See: Casper, the Friendly Ghost --- bot/bot.py | 7 +- bot/cogs/hacktoberstats.py | 186 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 bot/cogs/hacktoberstats.py diff --git a/bot/bot.py b/bot/bot.py index a67fcdab..f2a857bc 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -1,13 +1,14 @@ +from os import environ from pathlib import Path from sys import stderr from traceback import print_exc -from os import environ from discord.ext import commands - HACKTOBERBOT_TOKEN = environ.get('HACKTOBERBOT_TOKEN') -bot = commands.Bot(command_prefix=commands.when_mentioned_or('!')) + +ghost_unicode = "\N{GHOST}" +bot = commands.Bot(command_prefix=commands.when_mentioned_or(".", f"{ghost_unicode} ", ghost_unicode)) if __name__ == '__main__': # Scan for files in the /cogs/ directory and make a list of the file names. diff --git a/bot/cogs/hacktoberstats.py b/bot/cogs/hacktoberstats.py new file mode 100644 index 00000000..4e896ae9 --- /dev/null +++ b/bot/cogs/hacktoberstats.py @@ -0,0 +1,186 @@ +import re +import typing +from collections import Counter +from datetime import datetime + +import aiohttp +import discord +from discord.ext import commands + + +class Stats: + def __init__(self, bot): + self.bot = bot + + @commands.command( + name="stats", + aliases=["getstats", "userstats"], + brief="Get a user's Hacktoberfest contribution stats", + ) + async def get_stats(self, ctx, username: str): + """ + Query GitHub's API for PRs created by a GitHub user during the month of October that + do not have an 'invalid' tag + + For example: + !getstats heavysaturn + + If a valid username is provided, an embed is generated and posted to the channel + + Otherwise, post a helpful error message + + The first input argument is treated as the username, any additional inputs are discarded + """ + prs = await self.get_october_prs(username) + + if prs: + stats_embed = self.build_embed(username, prs) + await ctx.send('Here are some stats!', embed=stats_embed) + else: + await ctx.send(f"No October GitHub contributions found for '{username}'") + + def build_embed(self, username: str, prs: typing.List[dict]) -> discord.Embed: + """ + Return a stats embed built from username's PRs + """ + pr_stats = self._summarize_prs(prs) + + n = pr_stats['n_prs'] + if n >= 5: + shirtstr = f"**{username} has earned a tshirt!**" + elif n == 4: + shirtstr = f"**{username} is 1 PR away from a tshirt!**" + else: + shirtstr = f"**{username} is {5 - n} PRs away from a tshirt!**" + + stats_embed = discord.Embed( + title=f"{username}'s Hacktoberfest", + color=discord.Color(0x9c4af7), + description=f"{username} has made {n} {Stats._contributionator(n)} in October\n\n{shirtstr}\n\n" + ) + + stats_embed.set_thumbnail(url=f"https://www.github.com/{username}.png") + stats_embed.set_author( + name="Hacktoberfest", + url="https://hacktoberfest.digitalocean.com", + icon_url="https://hacktoberfest.digitalocean.com/assets/logo-hacktoberfest.png" + ) + stats_embed.add_field( + name="Top 5 Repositories:", + value=self._build_top5str(pr_stats) + ) + + return stats_embed + + @staticmethod + async def get_october_prs(username: str) -> typing.List[dict]: + """ + Query GitHub's API for PRs created during the month of October by username that do + not have an 'invalid' tag + + If PRs are found, return a list of dicts with basic PR information + + For each PR: + { + "repo_url": str + "repo_shortname": str (e.g. "discord-python/hacktoberbot") + "created_at": datetime.datetime + } + + Otherwise, return None + """ + base_url = "https://api.github.com/search/issues?q=" + not_label = "invalid" + action_type = "pr" + is_query = f"public+author:{username}" + date_range = "2018-10-01..2018-10-31" + per_page = "300" + query_url = f"{base_url}-label:{not_label}+type:{action_type}+is:{is_query}+created:{date_range}&per_page={per_page}" + + headers = {"user-agent": "Discord Python Hactoberbot"} + async with aiohttp.ClientSession() as session: + async with session.get(query_url, headers=headers) as resp: + jsonresp = await resp.json() + + if "message" in jsonresp.keys(): + # One of the parameters is invalid, short circuit for now + # In the future, log: jsonresp["errors"][0]["message"] + return + else: + if jsonresp["total_count"] == 0: + # Short circuit if there aren't any PRs + return + else: + outlist = [] + for item in jsonresp["items"]: + shortname = Stats._get_shortname(item["repository_url"]) + itemdict = { + "repo_url": f"https://www.github.com/{shortname}", + "repo_shortname": shortname, + "created_at": datetime.strptime( + item["created_at"], r"%Y-%m-%dT%H:%M:%SZ" + ), + } + outlist.append(itemdict) + return outlist + + @staticmethod + def _get_shortname(in_url: str) -> str: + """ + Extract shortname from https://api.github.com/repos/* URL + + e.g. "https://api.github.com/repos/discord-python/hacktoberbot" + | + V + "discord-python/hacktoberbot" + """ + exp = r"https?:\/\/api.github.com\/repos\/([/\-\w]+)" + return re.findall(exp, in_url)[0] + + @staticmethod + def _summarize_prs(prs: typing.List[dict]) -> typing.Dict: + """ + Generate statistics from an input list of PR dictionaries, as output by get_october_prs + + Return a dictionary containing: + { + "n_prs": int + "top5": [(repo_shortname, ncontributions), ...] + } + """ + contributed_repos = [pr["repo_shortname"] for pr in prs] + return {"n_prs": len(prs), "top5": Counter(contributed_repos).most_common(5)} + + @staticmethod + def _build_top5str(stats: typing.List[tuple]) -> str: + """ + Build a string from the Top 5 contributions that is compatible with a discord.Embed field + + Top 5 contributions should be a list of tuples, as output in the stats dictionary by + _summarize_prs + + String is of the form: + n contribution(s) to [shortname](url) + ... + """ + baseURL = "https://www.github.com/" + contributionstrs = [] + for repo in stats['top5']: + n = repo[1] + contributionstrs.append(f"{n} {Stats._contributionator(n)} to [{repo[0]}]({baseURL}{repo[0]})") + + return "\n".join(contributionstrs) + + @staticmethod + def _contributionator(n: int) -> str: + """ + Return "contribution" or "contributions" based on the value of n + """ + if n == 1: + return "contribution" + else: + return "contributions" + + +def setup(bot): + bot.add_cog(Stats(bot)) -- cgit v1.2.3 From 2bbc0af09c2542781eaa87988920c22498fa4825 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 8 Oct 2018 12:22:16 +0200 Subject: Update feature_request.md slight improvements to the template. --- .github/ISSUE_TEMPLATE/feature_request.md | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index f0d1d9c0..bff367e9 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -4,21 +4,16 @@ about: A simple feature request template. --- -## Description +Provide a simple description of how the feature would work. This should not include technical details, but should instead be a high level description of how the user experience would be when you used this feature. -[provide a simple description of the feature request] +## Implementation details -## Reason +List the changes required for the implementation here. -- [replace with a reason] -- [replace with a reason] -- [replace with a reason] - -## Required Changes - -- [replace with a change] -- [replace with a change] -- [replace with a change] +- [ ] Write the bot command +- [ ] Add the command to our README.md +- [ ] etc. ## Other -[provide any additional details that can be helpful] + +Provide any additional information or clarifications here. -- cgit v1.2.3 From 8556c4498cfb74501c813bd7f583b6218f6f87e2 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Mon, 8 Oct 2018 12:27:58 +0200 Subject: Update feature_request.md --- .github/ISSUE_TEMPLATE/feature_request.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md index bff367e9..04d31a5d 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -14,6 +14,6 @@ List the changes required for the implementation here. - [ ] Add the command to our README.md - [ ] etc. -## Other +## Additional information Provide any additional information or clarifications here. -- cgit v1.2.3