Skip to content

Commit

Permalink
Merge pull request #37 from peter-evans/dev
Browse files Browse the repository at this point in the history
Add issue-number input and output
  • Loading branch information
peter-evans authored Apr 17, 2020
2 parents 03a52c3 + 287e494 commit 0030306
Show file tree
Hide file tree
Showing 20 changed files with 312 additions and 209 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ If the file does not exist the action exits silently.
#### Inputs
- `token` - `GITHUB_TOKEN` or a `repo` scoped [PAT](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). Defaults to `GITHUB_TOKEN`.
- `issue-number` - The issue number of an existing issue to update
- `title` (**required**) - The title of the issue
- `content-filepath` (**required**) - The file path to the issue content
- `labels` - A comma separated list of labels
- `assignees` - A comma separated list of assignees (GitHub usernames)
- `project` - The name of the project for which a card should be created (Requires `project-column-name`)
- `project-column` - The name of the project column under which a card should be created

#### Outputs

- `issue-number` - The number of the created issue

## Actions that pair with this action

- [Link Checker](https://github.com/peter-evans/link-checker) - An action for link checking repository Markdown and HTML files
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ inputs:
token:
description: 'The GitHub authentication token'
default: ${{ github.token }}
issue-number:
description: 'The issue number of an existing issue to update'
title:
description: 'The title of the issue.'
required: true
Expand All @@ -17,6 +19,9 @@ inputs:
description: 'The name of the project for which a card should be created.'
project-column:
description: 'The name of the project column under which a card should be created.'
outputs:
issue-number:
description: 'The number of the created issue.'
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
''' Create Issue From File '''
import os
from pathlib import Path
from github import Github
from github import Github, GithubException

# Fetch required environment variables
github_token = os.environ['GITHUB_TOKEN']
Expand All @@ -11,11 +11,44 @@
issue_content_path = os.environ['CIFF_CONTENT_FILEPATH']

# Fetch optional environment variables
issue_number = os.environ.get('CIFF_ISSUE_NUMBER')
issue_labels = os.environ.get('CIFF_LABELS')
issue_assignees = os.environ.get('CIFF_ASSIGNEES')
project_name = os.environ.get('CIFF_PROJECT_NAME')
project_column_name = os.environ.get('CIFF_PROJECT_COLUMN_NAME')


def create_project_card(github_repo, project_name, project_column_name, issue):
# Locate the project by name
project = None
for project_item in github_repo.get_projects("all"):
if project_item.name == project_name:
project = project_item
break

if not project:
print("::error::Project not found. Unable to create project card.")
return

# Locate the column by name
column = None
for column_item in project.get_columns():
if column_item.name == project_column_name:
column = column_item
break

if not column:
print("::error::Project column not found. Unable to create project card.")
return

# Create a project card for the pull request
column.create_card(content_id=issue.id, content_type="Issue")
print(
"Added issue #%d to project '%s' under column '%s'"
% (issue.number, project.name, column.name)
)


# If the file does not exist there is no issue to create
if not Path(issue_content_path).is_file():
print("File not found")
Expand All @@ -28,9 +61,19 @@
# Fetch the repository object
g = Github(github_token)
repo = g.get_repo(github_repository)
# Create the issue
issue = repo.create_issue(issue_title, issue_content)
print("Created issue %d" % (issue.number))

if issue_number is not None:
# Update an existing issue
issue = repo.get_issue(int(issue_number))
issue.edit(title=issue_title, body=issue_content)
print("Updated issue %d" % (issue.number))
else:
# Create an issue
issue = repo.create_issue(title=issue_title, body=issue_content)
print("Created issue %d" % (issue.number))

# Set the step output
os.system(f"echo ::set-output name=issue-number::{issue.number}")

if issue_labels is not None:
# Split the labels input into a list
Expand All @@ -50,30 +93,17 @@
print("Assigning issue to assignees")
issue.edit(assignees=assignees_list)

# Create a project card for the pull request
if project_name is not None and project_column_name is not None:
# Locate the project by name
project = None
for project_item in repo.get_projects("all"):
if project_item.name == project_name:
project = project_item
break

if not project:
print("Project not found")
exit(0)

# Locate the column by name
column = None
for column_item in project.get_columns():
if column_item.name == project_column_name:
column = column_item
break

if not column:
print("Project column not found")
exit(0)

# Add the issue to the project
card = column.create_card(content_id=issue.id, content_type="Issue")
print("Added issue %d to project \"%s\" under column \"%s\"" \
% (issue.number, project.name, column.name))
try:
create_project_card(
repo, project_name, project_column_name, issue
)
except GithubException as e:
# Likely caused by "Project already has the associated issue."
if e.status == 422:
print(
"Create project card failed - {}".format(
e.data["errors"][0]["message"]
)
)
3 changes: 3 additions & 0 deletions dist/ciff/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setuptools==46.1.3
wheel==0.34.2
PyGithub==1.47
Loading

0 comments on commit 0030306

Please sign in to comment.