Skip to content

Commit 0030306

Browse files
authored
Merge pull request #37 from peter-evans/dev
Add issue-number input and output
2 parents 03a52c3 + 287e494 commit 0030306

20 files changed

+312
-209
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ If the file does not exist the action exits silently.
2626
#### Inputs
2727
2828
- `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`.
29+
- `issue-number` - The issue number of an existing issue to update
2930
- `title` (**required**) - The title of the issue
3031
- `content-filepath` (**required**) - The file path to the issue content
3132
- `labels` - A comma separated list of labels
3233
- `assignees` - A comma separated list of assignees (GitHub usernames)
3334
- `project` - The name of the project for which a card should be created (Requires `project-column-name`)
3435
- `project-column` - The name of the project column under which a card should be created
3536

37+
#### Outputs
38+
39+
- `issue-number` - The number of the created issue
40+
3641
## Actions that pair with this action
3742

3843
- [Link Checker](https://github.com/peter-evans/link-checker) - An action for link checking repository Markdown and HTML files

action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ inputs:
44
token:
55
description: 'The GitHub authentication token'
66
default: ${{ github.token }}
7+
issue-number:
8+
description: 'The issue number of an existing issue to update'
79
title:
810
description: 'The title of the issue.'
911
required: true
@@ -17,6 +19,9 @@ inputs:
1719
description: 'The name of the project for which a card should be created.'
1820
project-column:
1921
description: 'The name of the project column under which a card should be created.'
22+
outputs:
23+
issue-number:
24+
description: 'The number of the created issue.'
2025
runs:
2126
using: 'node12'
2227
main: 'dist/index.js'

dist/src/create_issue_from_file.py dist/ciff/create_issue_from_file.py

+60-30
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
''' Create Issue From File '''
33
import os
44
from pathlib import Path
5-
from github import Github
5+
from github import Github, GithubException
66

77
# Fetch required environment variables
88
github_token = os.environ['GITHUB_TOKEN']
@@ -11,11 +11,44 @@
1111
issue_content_path = os.environ['CIFF_CONTENT_FILEPATH']
1212

1313
# Fetch optional environment variables
14+
issue_number = os.environ.get('CIFF_ISSUE_NUMBER')
1415
issue_labels = os.environ.get('CIFF_LABELS')
1516
issue_assignees = os.environ.get('CIFF_ASSIGNEES')
1617
project_name = os.environ.get('CIFF_PROJECT_NAME')
1718
project_column_name = os.environ.get('CIFF_PROJECT_COLUMN_NAME')
1819

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+
1952
# If the file does not exist there is no issue to create
2053
if not Path(issue_content_path).is_file():
2154
print("File not found")
@@ -28,9 +61,19 @@
2861
# Fetch the repository object
2962
g = Github(github_token)
3063
repo = g.get_repo(github_repository)
31-
# Create the issue
32-
issue = repo.create_issue(issue_title, issue_content)
33-
print("Created issue %d" % (issue.number))
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}")
3477

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

96+
# Create a project card for the pull request
5397
if project_name is not None and project_column_name is not None:
54-
# Locate the project by name
55-
project = None
56-
for project_item in repo.get_projects("all"):
57-
if project_item.name == project_name:
58-
project = project_item
59-
break
60-
61-
if not project:
62-
print("Project not found")
63-
exit(0)
64-
65-
# Locate the column by name
66-
column = None
67-
for column_item in project.get_columns():
68-
if column_item.name == project_column_name:
69-
column = column_item
70-
break
71-
72-
if not column:
73-
print("Project column not found")
74-
exit(0)
75-
76-
# Add the issue to the project
77-
card = column.create_card(content_id=issue.id, content_type="Issue")
78-
print("Added issue %d to project \"%s\" under column \"%s\"" \
79-
% (issue.number, project.name, column.name))
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+
)

dist/ciff/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
setuptools==46.1.3
2+
wheel==0.34.2
3+
PyGithub==1.47

0 commit comments

Comments
 (0)