|
| 1 | +from datetime import date |
| 2 | + |
| 3 | +from bottle import run, template, request, post, get |
| 4 | +from github import Github, GithubException |
| 5 | + |
| 6 | + |
| 7 | +@get('/') |
| 8 | +def login(): |
| 9 | + return ''' |
| 10 | + <h1>HacktoberFest Checker</h1> |
| 11 | + <h2>Login</h2> |
| 12 | + <p>Please enter your Github username</p> |
| 13 | + <form action="/" method="post"> |
| 14 | + Github username: <input name="username" type="text" /> |
| 15 | + </form> |
| 16 | + ''' |
| 17 | + |
| 18 | + |
| 19 | +@post('/') |
| 20 | +def search_prs(): |
| 21 | + # Statements taken from https://github.com/pybites/hacktoberfest-checker/blob/master/controllers/index.js |
| 22 | + statements = [ |
| 23 | + "It's not too late to start!", |
| 24 | + "Off to a great start, keep going!", |
| 25 | + "Half way there, keep it up!", |
| 26 | + "So close!", |
| 27 | + "Way to go!", |
| 28 | + "Now you're just showing off!" |
| 29 | + ] |
| 30 | + # Extract the username entered in the web form |
| 31 | + username = request.forms.get('username') |
| 32 | + |
| 33 | + # check with GitHub that the entered username actually exists… if not show the unknown template |
| 34 | + gh = Github() |
| 35 | + try: |
| 36 | + user = gh.get_user(username).name |
| 37 | + except GithubException: |
| 38 | + return template('unknown') |
| 39 | + |
| 40 | + # Now we're going to grab a list of all the PRs put through by the user… there should be more checking though, ie: |
| 41 | + # for invalid and spam labels at the very least. |
| 42 | + try: |
| 43 | + # Query based upon: |
| 44 | + # const options = { |
| 45 | + # q: `-label:invalid |
| 46 | + # +created:2017-09-30T00:00:00-12:00..2017-10-31T23:59:59-12:00 |
| 47 | + # +type:pr |
| 48 | + # +is:public |
| 49 | + # +author:${username}` |
| 50 | + # }; |
| 51 | + # from https://github.com/pybites/hacktoberfest-checker/blob/master/controllers/index.js |
| 52 | + # The was written for 2017, so it needs to be updated for *this* year, |
| 53 | + # we can automagically pick the current year: |
| 54 | + year = str(date.today().year) |
| 55 | + # Request a list of PRs within the specific date range… |
| 56 | + query = f'{year}-09-30T00:00:00-12:00..{year}-10-31T23:59:59-12:00' |
| 57 | + pr_list = gh.search_issues('', author=username, type='pr', created=query) |
| 58 | + except GithubException: |
| 59 | + # any errors from GitHub at this stage we just assumes means no PRs found |
| 60 | + pr_list = [] |
| 61 | + pr_list.totalCount = 0 |
| 62 | + |
| 63 | + # We're going to pick out the details for each PR that we want to display to the user |
| 64 | + pr_detail = {"url": [], "title": [], "date": []} |
| 65 | + # Provided there are PRs, loop through them extracting the url, title and date into the pr_detail arrays |
| 66 | + if pr_list.totalCount > 0: |
| 67 | + for pr in pr_list: |
| 68 | + pr_detail["url"].append(pr.pull_request.html_url) |
| 69 | + pr_detail["title"].append(pr.title) |
| 70 | + pr_detail["date"].append(pr.created_at) |
| 71 | + |
| 72 | + pr_count = len(pr_detail["title"]) |
| 73 | + return template('pr_list', pr_data=pr_detail, name=pr_list[0].user.name, |
| 74 | + prs=pr_detail["title"], dates=pr_detail["date"], urls=pr_detail["url"], |
| 75 | + pr_count=pr_count, statement=statements[pr_count]) |
| 76 | + |
| 77 | + return template('pr_list', pr_count=0, name=user, statement=statements[0]) |
| 78 | + |
| 79 | + |
| 80 | +run(host='localhost', port=8080) |
0 commit comments