|
| 1 | +# ___________________________________________________________________________ |
| 2 | +# |
| 3 | +# Pyomo: Python Optimization Modeling Objects |
| 4 | +# Copyright (c) 2008-2024 |
| 5 | +# National Technology and Engineering Solutions of Sandia, LLC |
| 6 | +# Under the terms of Contract DE-NA0003525 with National Technology and |
| 7 | +# Engineering Solutions of Sandia, LLC, the U.S. Government retains certain |
| 8 | +# rights in this software. |
| 9 | +# This software is distributed under the 3-clause BSD License. |
| 10 | +# ___________________________________________________________________________ |
| 11 | + |
| 12 | + |
| 13 | +""" |
| 14 | +This script is intended to query the GitHub REST API and get a list of open |
| 15 | +issues for a given repository, returning a random selection of n issues. |
| 16 | +
|
| 17 | +We colloquially call this the "Big Wheel of Misfortune" (BWOM) |
| 18 | +""" |
| 19 | + |
| 20 | +import sys |
| 21 | +import random |
| 22 | +import os |
| 23 | + |
| 24 | +from github import Github, Auth |
| 25 | + |
| 26 | + |
| 27 | +def get_random_open_issues(repository, number_to_return): |
| 28 | + """ |
| 29 | + Return a random selection of open issues from a repository. |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + repository : String |
| 34 | + The org/repo combination for target repository (GitHub). E.g., |
| 35 | + IDAES/idaes-pse. |
| 36 | + number_to_return : int |
| 37 | + The number of random open issues to return. |
| 38 | +
|
| 39 | + Returns |
| 40 | + ------- |
| 41 | + random_issues : List |
| 42 | + A list of dictionaries containing information about randomly selected open issues. |
| 43 | + """ |
| 44 | + # Collect the authorization token from the user's environment |
| 45 | + token = os.environ.get('GH_TOKEN') |
| 46 | + auth_token = Auth.Token(token) |
| 47 | + # Create a connection to GitHub |
| 48 | + gh = Github(auth=auth_token) |
| 49 | + # Create a repository object for the requested repository |
| 50 | + repo = gh.get_repo(repository) |
| 51 | + # Get all open issues |
| 52 | + open_issues = repo.get_issues(state='open') |
| 53 | + open_issues_list = [issue for issue in open_issues] |
| 54 | + |
| 55 | + # Randomly select the specified number of issues |
| 56 | + random_issues = random.sample( |
| 57 | + open_issues_list, min(number_to_return, len(open_issues_list)) |
| 58 | + ) |
| 59 | + |
| 60 | + return random_issues |
| 61 | + |
| 62 | + |
| 63 | +def print_big_wheel(): |
| 64 | + """Prints a specified ASCII art representation of a big wheel.""" |
| 65 | + wheel = [ |
| 66 | + " . __", |
| 67 | + " / \\ . ' || ' .", |
| 68 | + " )J( .` || `.", |
| 69 | + " (8)7) . \\ || / .", |
| 70 | + " (') .'/ _ \\ .-''-. / _ \\", |
| 71 | + " (=) .' J `- .' .--. '. -` L", |
| 72 | + " (') .' F======' ((<>)) '======J", |
| 73 | + " )J(' L '. `||' .' F", |
| 74 | + " (7(8) \\ _.- `-||-' -._ /", |
| 75 | + " \\' . / || \\ .", |
| 76 | + " / | . / || \\ .", |
| 77 | + " / | ` . _||_ . `", |
| 78 | + " / |___________ _.-||_________", |
| 79 | + " (()\\.'| ___.....'''' ||._ .'", |
| 80 | + " \\.`- .'. /__\\/ .'|", |
| 81 | + ".'_______________________________.' ||", |
| 82 | + " |'---------------------------'|==.||", |
| 83 | + " ||.' || ||.' ||", |
| 84 | + " ||===========================|| (__)", |
| 85 | + " || ||", |
| 86 | + " (__) LGB (__)", |
| 87 | + " Credit: ascii.co.uk/art/spinningwheel", |
| 88 | + ] |
| 89 | + for line in wheel: |
| 90 | + print(line) |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + if len(sys.argv) < 2 or len(sys.argv) > 3: |
| 95 | + print(f"Usage: {sys.argv[0]} <Org/Repo> [num_issues]") |
| 96 | + print( |
| 97 | + " <Org/Repo> : the GitHub organization/repository combo (e.g., Pyomo/pyomo)" |
| 98 | + ) |
| 99 | + print( |
| 100 | + " [Number of issues] : optional number of random open issues to return (default is 5)" |
| 101 | + ) |
| 102 | + print("") |
| 103 | + print( |
| 104 | + "ALSO REQUIRED: Please generate a GitHub token (with repo permissions) " |
| 105 | + "and export to the environment variable GH_TOKEN." |
| 106 | + ) |
| 107 | + print(" Visit GitHub's official documentation for more details.") |
| 108 | + sys.exit(1) |
| 109 | + |
| 110 | + repository = sys.argv[1] |
| 111 | + if len(sys.argv) == 3: |
| 112 | + try: |
| 113 | + num_issues = int(sys.argv[2]) |
| 114 | + if num_issues <= 0: |
| 115 | + raise (ValueError("Need a positive number; why did you try <= 0?")) |
| 116 | + except ValueError as e: |
| 117 | + print( |
| 118 | + "*** ERROR: You did something weird when declaring the number of issues. Defaulting to 5.\n" |
| 119 | + f"(For posterity, this is the error that was returned: {e})\n" |
| 120 | + ) |
| 121 | + num_issues = 5 |
| 122 | + else: |
| 123 | + num_issues = 5 |
| 124 | + |
| 125 | + print("Spinning the Big Wheel of Misfortune...\n") |
| 126 | + print_big_wheel() |
| 127 | + |
| 128 | + random_issues = get_random_open_issues(repository, num_issues) |
| 129 | + |
| 130 | + print(f"\nRandomly selected open issues from {repository}:") |
| 131 | + for issue in random_issues: |
| 132 | + print(f"- Issue #{issue.number}: {issue.title} (URL: {issue.html_url})") |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == '__main__': |
| 136 | + main() |
0 commit comments