Skip to content

Commit 340e629

Browse files
authored
Merge pull request #152 from peter-evans/dev
Add input for draft pull requests
2 parents 3474dda + abc19ca commit 340e629

File tree

8 files changed

+70
-21
lines changed

8 files changed

+70
-21
lines changed

.github/workflows/cpr-example-command.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
milestone: 1
2929
project: Example Project
3030
project-column: To do
31+
draft: false
3132
branch: example-patches
3233
request-to-parent: false
3334
- name: Check outputs

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ All inputs are **optional**. If not set, sensible default values will be used.
5353
| `milestone` | The number of the milestone to associate this pull request with. | |
5454
| `project` | The name of the project for which a card should be created. Requires `project-column`. | |
5555
| `project-column` | The name of the project column under which a card should be created. Requires `project`. | |
56+
| `draft` | Create a [draft pull request](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests#draft-pull-requests). | `false` |
5657
| `branch` | The branch name. See [Branch naming](#branch-naming) for details. | `create-pull-request/patch` |
5758
| `request-to-parent` | Create the pull request in the parent repository of the checked out fork. See [push pull request branches to a fork](https://github.com/peter-evans/create-pull-request/blob/master/docs/concepts-guidelines.md#push-pull-request-branches-to-a-fork) for details. | `false` |
5859
| `base` | Sets the pull request base branch. | Defaults to the branch checked out in the workflow. |
@@ -179,6 +180,7 @@ jobs:
179180
milestone: 1
180181
project: Example Project
181182
project-column: To do
183+
draft: false
182184
branch: example-patches
183185
request-to-parent: false
184186
- name: Check outputs

dist/cpr/create_or_update_pull_request.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
import os
55

66

7+
def string_to_bool(str):
8+
if str is None:
9+
return False
10+
else:
11+
return str.lower() in [
12+
"true",
13+
"1",
14+
"t",
15+
"y",
16+
"yes",
17+
"on",
18+
]
19+
20+
721
def cs_string_to_list(str):
822
# Split the comma separated string into a list
923
l = [i.strip() for i in str.split(",")]
@@ -56,27 +70,31 @@ def create_or_update_pull_request(
5670
team_reviewers,
5771
project_name,
5872
project_column_name,
73+
draft,
5974
request_to_parent,
6075
):
61-
if request_to_parent is None:
62-
request_to_parent = False
63-
else:
64-
request_to_parent = request_to_parent.lower() in ['true', '1', 't', 'y', 'yes', 'on']
65-
6676
github_repo = head_repo = Github(github_token).get_repo(github_repository)
67-
if request_to_parent:
77+
if string_to_bool(request_to_parent):
6878
github_repo = github_repo.parent
6979
if github_repo is None:
70-
raise ValueError("The checked out repository is not a fork. Input 'request-to-parent' should be set to false.")
80+
raise ValueError(
81+
"The checked out repository is not a fork. Input 'request-to-parent' should be set to false."
82+
)
7183

7284
head_branch = f"{head_repo.owner.login}:{branch}"
7385

7486
# Create the pull request
7587
try:
7688
pull_request = github_repo.create_pull(
77-
title=title, body=body, base=base, head=head_branch
89+
title=title,
90+
body=body,
91+
base=base,
92+
head=head_branch,
93+
draft=string_to_bool(draft),
94+
)
95+
print(
96+
f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})"
7897
)
79-
print(f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
8098
except GithubException as e:
8199
if e.status == 422:
82100
# A pull request exists for this branch and base
@@ -86,7 +104,9 @@ def create_or_update_pull_request(
86104
)[0]
87105
# Update title and body
88106
pull_request.as_issue().edit(title=title, body=body)
89-
print(f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
107+
print(
108+
f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})"
109+
)
90110
else:
91111
print(str(e))
92112
raise

dist/cpr/create_pull_request.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,6 @@ def set_committer_author(repo, committer, author):
224224
os.environ.get("CPR_TEAM_REVIEWERS"),
225225
os.environ.get("CPR_PROJECT_NAME"),
226226
os.environ.get("CPR_PROJECT_COLUMN_NAME"),
227+
os.environ.get("CPR_DRAFT"),
227228
os.environ.get("CPR_REQUEST_TO_PARENT"),
228229
)

dist/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4274,6 +4274,7 @@ async function run() {
42744274
milestone: core.getInput("milestone"),
42754275
project: core.getInput("project"),
42764276
projectColumn: core.getInput("project-column"),
4277+
draft: core.getInput("draft"),
42774278
branch: core.getInput("branch"),
42784279
request_to_parent: core.getInput("request-to-parent"),
42794280
base: core.getInput("base"),
@@ -4296,6 +4297,7 @@ async function run() {
42964297
if (inputs.milestone) process.env.CPR_MILESTONE = inputs.milestone;
42974298
if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project;
42984299
if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn;
4300+
if (inputs.draft) process.env.CPR_DRAFT = inputs.draft;
42994301
if (inputs.branch) process.env.CPR_BRANCH = inputs.branch;
43004302
if (inputs.request_to_parent) process.env.CPR_REQUEST_TO_PARENT = inputs.request_to_parent;
43014303
if (inputs.base) process.env.CPR_BASE = inputs.base;

src/cpr/create_or_update_pull_request.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@
44
import os
55

66

7+
def string_to_bool(str):
8+
if str is None:
9+
return False
10+
else:
11+
return str.lower() in [
12+
"true",
13+
"1",
14+
"t",
15+
"y",
16+
"yes",
17+
"on",
18+
]
19+
20+
721
def cs_string_to_list(str):
822
# Split the comma separated string into a list
923
l = [i.strip() for i in str.split(",")]
@@ -56,27 +70,31 @@ def create_or_update_pull_request(
5670
team_reviewers,
5771
project_name,
5872
project_column_name,
73+
draft,
5974
request_to_parent,
6075
):
61-
if request_to_parent is None:
62-
request_to_parent = False
63-
else:
64-
request_to_parent = request_to_parent.lower() in ['true', '1', 't', 'y', 'yes', 'on']
65-
6676
github_repo = head_repo = Github(github_token).get_repo(github_repository)
67-
if request_to_parent:
77+
if string_to_bool(request_to_parent):
6878
github_repo = github_repo.parent
6979
if github_repo is None:
70-
raise ValueError("The checked out repository is not a fork. Input 'request-to-parent' should be set to false.")
80+
raise ValueError(
81+
"The checked out repository is not a fork. Input 'request-to-parent' should be set to false."
82+
)
7183

7284
head_branch = f"{head_repo.owner.login}:{branch}"
7385

7486
# Create the pull request
7587
try:
7688
pull_request = github_repo.create_pull(
77-
title=title, body=body, base=base, head=head_branch
89+
title=title,
90+
body=body,
91+
base=base,
92+
head=head_branch,
93+
draft=string_to_bool(draft),
94+
)
95+
print(
96+
f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})"
7897
)
79-
print(f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
8098
except GithubException as e:
8199
if e.status == 422:
82100
# A pull request exists for this branch and base
@@ -86,7 +104,9 @@ def create_or_update_pull_request(
86104
)[0]
87105
# Update title and body
88106
pull_request.as_issue().edit(title=title, body=body)
89-
print(f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
107+
print(
108+
f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})"
109+
)
90110
else:
91111
print(str(e))
92112
raise

src/cpr/create_pull_request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def set_committer_author(repo, committer, author):
192192

193193
if result["action"] in ["created", "updated"]:
194194
# The branch was created or updated
195-
print(f"Pushing pull request branch to '{repo.full_name}/{branch}'")
195+
print(f"Pushing pull request branch to 'origin/{branch}'")
196196
repo.git.push("--force", repo_url, f"HEAD:refs/heads/{branch}")
197197

198198
# Set the base. It would have been 'None' if not specified as an input
@@ -224,5 +224,6 @@ def set_committer_author(repo, committer, author):
224224
os.environ.get("CPR_TEAM_REVIEWERS"),
225225
os.environ.get("CPR_PROJECT_NAME"),
226226
os.environ.get("CPR_PROJECT_COLUMN_NAME"),
227+
os.environ.get("CPR_DRAFT"),
227228
os.environ.get("CPR_REQUEST_TO_PARENT"),
228229
)

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ async function run() {
6262
milestone: core.getInput("milestone"),
6363
project: core.getInput("project"),
6464
projectColumn: core.getInput("project-column"),
65+
draft: core.getInput("draft"),
6566
branch: core.getInput("branch"),
6667
request_to_parent: core.getInput("request-to-parent"),
6768
base: core.getInput("base"),
@@ -84,6 +85,7 @@ async function run() {
8485
if (inputs.milestone) process.env.CPR_MILESTONE = inputs.milestone;
8586
if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project;
8687
if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn;
88+
if (inputs.draft) process.env.CPR_DRAFT = inputs.draft;
8789
if (inputs.branch) process.env.CPR_BRANCH = inputs.branch;
8890
if (inputs.request_to_parent) process.env.CPR_REQUEST_TO_PARENT = inputs.request_to_parent;
8991
if (inputs.base) process.env.CPR_BASE = inputs.base;

0 commit comments

Comments
 (0)