|
| 1 | +#!/usr/bin/env python3 |
| 2 | +''' Create Issue From File ''' |
| 3 | +import os |
| 4 | +from pathlib import Path |
| 5 | +from github import Github, GithubException |
| 6 | + |
| 7 | +# Fetch required environment variables |
| 8 | +github_token = os.environ['GITHUB_TOKEN'] |
| 9 | +github_repository = os.environ['GITHUB_REPOSITORY'] |
| 10 | +issue_title = os.environ['CIFF_TITLE'] |
| 11 | +issue_content_path = os.environ['CIFF_CONTENT_FILEPATH'] |
| 12 | + |
| 13 | +# Fetch optional environment variables |
| 14 | +issue_number = os.environ.get('CIFF_ISSUE_NUMBER') |
| 15 | +issue_labels = os.environ.get('CIFF_LABELS') |
| 16 | +issue_assignees = os.environ.get('CIFF_ASSIGNEES') |
| 17 | +project_name = os.environ.get('CIFF_PROJECT_NAME') |
| 18 | +project_column_name = os.environ.get('CIFF_PROJECT_COLUMN_NAME') |
| 19 | + |
| 20 | + |
| 21 | +def create_project_card(github_repo, project_name, project_column_name, issue): |
| 22 | + # Locate the project by name |
| 23 | + project = None |
| 24 | + for project_item in github_repo.get_projects("all"): |
| 25 | + if project_item.name == project_name: |
| 26 | + project = project_item |
| 27 | + break |
| 28 | + |
| 29 | + if not project: |
| 30 | + print("::error::Project not found. Unable to create project card.") |
| 31 | + return |
| 32 | + |
| 33 | + # Locate the column by name |
| 34 | + column = None |
| 35 | + for column_item in project.get_columns(): |
| 36 | + if column_item.name == project_column_name: |
| 37 | + column = column_item |
| 38 | + break |
| 39 | + |
| 40 | + if not column: |
| 41 | + print("::error::Project column not found. Unable to create project card.") |
| 42 | + return |
| 43 | + |
| 44 | + # Create a project card for the pull request |
| 45 | + column.create_card(content_id=issue.id, content_type="Issue") |
| 46 | + print( |
| 47 | + "Added issue #%d to project '%s' under column '%s'" |
| 48 | + % (issue.number, project.name, column.name) |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +# If the file does not exist there is no issue to create |
| 53 | +if not Path(issue_content_path).is_file(): |
| 54 | + print("File not found") |
| 55 | + exit(0) |
| 56 | + |
| 57 | +# Fetch the file content |
| 58 | +with open(issue_content_path, 'r') as f: |
| 59 | + issue_content = f.read() |
| 60 | + |
| 61 | +# Fetch the repository object |
| 62 | +g = Github(github_token) |
| 63 | +repo = g.get_repo(github_repository) |
| 64 | + |
| 65 | +if issue_number is not None: |
| 66 | + # Update an existing issue |
| 67 | + issue = repo.get_issue(int(issue_number)) |
| 68 | + issue.edit(title=issue_title, body=issue_content) |
| 69 | + print("Updated issue %d" % (issue.number)) |
| 70 | +else: |
| 71 | + # Create an issue |
| 72 | + issue = repo.create_issue(title=issue_title, body=issue_content) |
| 73 | + print("Created issue %d" % (issue.number)) |
| 74 | + |
| 75 | +# Set the step output |
| 76 | +os.system(f"echo ::set-output name=issue-number::{issue.number}") |
| 77 | + |
| 78 | +if issue_labels is not None: |
| 79 | + # Split the labels input into a list |
| 80 | + labels_list = [l.strip() for l in issue_labels.split(',')] |
| 81 | + # Remove empty strings |
| 82 | + labels_list = list(filter(None, labels_list)) |
| 83 | + # Apply labels to issue |
| 84 | + print("Applying labels") |
| 85 | + issue.edit(labels=labels_list) |
| 86 | + |
| 87 | +if issue_assignees is not None: |
| 88 | + # Split the assignees input into a list |
| 89 | + assignees_list = [l.strip() for l in issue_assignees.split(',')] |
| 90 | + # Remove empty strings |
| 91 | + assignees_list = list(filter(None, assignees_list)) |
| 92 | + # Assign issue |
| 93 | + print("Assigning issue to assignees") |
| 94 | + issue.edit(assignees=assignees_list) |
| 95 | + |
| 96 | +# Create a project card for the pull request |
| 97 | +if project_name is not None and project_column_name is not None: |
| 98 | + try: |
| 99 | + create_project_card( |
| 100 | + repo, project_name, project_column_name, issue |
| 101 | + ) |
| 102 | + except GithubException as e: |
| 103 | + # Likely caused by "Project already has the associated issue." |
| 104 | + if e.status == 422: |
| 105 | + print( |
| 106 | + "Create project card failed - {}".format( |
| 107 | + e.data["errors"][0]["message"] |
| 108 | + ) |
| 109 | + ) |
0 commit comments