|
| 1 | +from github import Github |
| 2 | +from pathlib import Path |
| 3 | +import random |
| 4 | +import os |
| 5 | + |
| 6 | +# Fetch required environment variables |
| 7 | +github_token = os.environ['GITHUB_TOKEN'] |
| 8 | +github_repository = os.environ['GITHUB_REPOSITORY'] |
| 9 | +issue_title = os.environ['ISSUE_TITLE'] |
| 10 | +issue_content_path = os.environ['ISSUE_CONTENT_FILEPATH'] |
| 11 | + |
| 12 | +# Fetch optional environment variables |
| 13 | +issue_labels = os.environ.get('ISSUE_LABELS') |
| 14 | +issue_assignees = os.environ.get('ISSUE_ASSIGNEES') |
| 15 | + |
| 16 | +# If the file does not exist there is no issue to create |
| 17 | +if not Path(issue_content_path).is_file(): |
| 18 | + print("File not found") |
| 19 | + exit(0) |
| 20 | + |
| 21 | +# Fetch the file content |
| 22 | +with open(issue_content_path, 'r') as f: |
| 23 | + issue_content = f.read() |
| 24 | + |
| 25 | +# Fetch the repository object |
| 26 | +g = Github(github_token) |
| 27 | +# Split username/repository |
| 28 | +github_repository_parts = [l.strip() for l in github_repository.split('/')] |
| 29 | +repo = g.get_user().get_repo(github_repository_parts[1]) |
| 30 | +# Create the issue |
| 31 | +issue = repo.create_issue(issue_title, issue_content) |
| 32 | +print("Created issue %d" % (issue.number)) |
| 33 | + |
| 34 | +if issue_labels is not None: |
| 35 | + # Split the labels input into a list |
| 36 | + labels_list = [l.strip() for l in issue_labels.split(',')] |
| 37 | + # Remove empty strings |
| 38 | + labels_list = list(filter(None, labels_list)) |
| 39 | + # Apply labels to issue |
| 40 | + print("Applying labels") |
| 41 | + issue.edit(labels=labels_list) |
| 42 | + |
| 43 | +if issue_assignees is not None: |
| 44 | + # Split the assignees input into a list |
| 45 | + assignees_list = [l.strip() for l in issue_assignees.split(',')] |
| 46 | + # Remove empty strings |
| 47 | + assignees_list = list(filter(None, assignees_list)) |
| 48 | + # Assign issue |
| 49 | + print("Assigning issue to assignees") |
| 50 | + issue.edit(assignees=assignees_list) |
0 commit comments