From abc19caa82b506bc29c5a0067ceca250aa262f09 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Sat, 4 Apr 2020 09:47:11 +0900 Subject: [PATCH] Add input for draft pull requests --- .github/workflows/cpr-example-command.yml | 1 + README.md | 2 ++ dist/cpr/create_or_update_pull_request.py | 40 +++++++++++++++++------ dist/cpr/create_pull_request.py | 1 + dist/index.js | 2 ++ src/cpr/create_or_update_pull_request.py | 40 +++++++++++++++++------ src/cpr/create_pull_request.py | 3 +- src/index.js | 2 ++ 8 files changed, 70 insertions(+), 21 deletions(-) diff --git a/.github/workflows/cpr-example-command.yml b/.github/workflows/cpr-example-command.yml index f050cb9..c08b6f4 100644 --- a/.github/workflows/cpr-example-command.yml +++ b/.github/workflows/cpr-example-command.yml @@ -28,6 +28,7 @@ jobs: milestone: 1 project: Example Project project-column: To do + draft: false branch: example-patches request-to-parent: false - name: Check outputs diff --git a/README.md b/README.md index 249c4ed..eac00f1 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ All inputs are **optional**. If not set, sensible default values will be used. | `milestone` | The number of the milestone to associate this pull request with. | | | `project` | The name of the project for which a card should be created. Requires `project-column`. | | | `project-column` | The name of the project column under which a card should be created. Requires `project`. | | +| `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` | | `branch` | The branch name. See [Branch naming](#branch-naming) for details. | `create-pull-request/patch` | | `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` | | `base` | Sets the pull request base branch. | Defaults to the branch checked out in the workflow. | @@ -179,6 +180,7 @@ jobs: milestone: 1 project: Example Project project-column: To do + draft: false branch: example-patches request-to-parent: false - name: Check outputs diff --git a/dist/cpr/create_or_update_pull_request.py b/dist/cpr/create_or_update_pull_request.py index ed08b24..de953a0 100644 --- a/dist/cpr/create_or_update_pull_request.py +++ b/dist/cpr/create_or_update_pull_request.py @@ -4,6 +4,20 @@ from github import Github, GithubException import os +def string_to_bool(str): + if str is None: + return False + else: + return str.lower() in [ + "true", + "1", + "t", + "y", + "yes", + "on", + ] + + def cs_string_to_list(str): # Split the comma separated string into a list l = [i.strip() for i in str.split(",")] @@ -56,27 +70,31 @@ def create_or_update_pull_request( team_reviewers, project_name, project_column_name, + draft, request_to_parent, ): - if request_to_parent is None: - request_to_parent = False - else: - request_to_parent = request_to_parent.lower() in ['true', '1', 't', 'y', 'yes', 'on'] - github_repo = head_repo = Github(github_token).get_repo(github_repository) - if request_to_parent: + if string_to_bool(request_to_parent): github_repo = github_repo.parent if github_repo is None: - raise ValueError("The checked out repository is not a fork. Input 'request-to-parent' should be set to false.") + raise ValueError( + "The checked out repository is not a fork. Input 'request-to-parent' should be set to false." + ) head_branch = f"{head_repo.owner.login}:{branch}" # Create the pull request try: pull_request = github_repo.create_pull( - title=title, body=body, base=base, head=head_branch + title=title, + body=body, + base=base, + head=head_branch, + draft=string_to_bool(draft), + ) + print( + f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})" ) - print(f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})") except GithubException as e: if e.status == 422: # A pull request exists for this branch and base @@ -86,7 +104,9 @@ def create_or_update_pull_request( )[0] # Update title and body pull_request.as_issue().edit(title=title, body=body) - print(f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})") + print( + f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})" + ) else: print(str(e)) raise diff --git a/dist/cpr/create_pull_request.py b/dist/cpr/create_pull_request.py index 098a69b..ead7f02 100755 --- a/dist/cpr/create_pull_request.py +++ b/dist/cpr/create_pull_request.py @@ -224,5 +224,6 @@ if result["action"] in ["created", "updated"]: os.environ.get("CPR_TEAM_REVIEWERS"), os.environ.get("CPR_PROJECT_NAME"), os.environ.get("CPR_PROJECT_COLUMN_NAME"), + os.environ.get("CPR_DRAFT"), os.environ.get("CPR_REQUEST_TO_PARENT"), ) diff --git a/dist/index.js b/dist/index.js index c506302..25afc05 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4274,6 +4274,7 @@ async function run() { milestone: core.getInput("milestone"), project: core.getInput("project"), projectColumn: core.getInput("project-column"), + draft: core.getInput("draft"), branch: core.getInput("branch"), request_to_parent: core.getInput("request-to-parent"), base: core.getInput("base"), @@ -4296,6 +4297,7 @@ async function run() { if (inputs.milestone) process.env.CPR_MILESTONE = inputs.milestone; if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project; if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn; + if (inputs.draft) process.env.CPR_DRAFT = inputs.draft; if (inputs.branch) process.env.CPR_BRANCH = inputs.branch; if (inputs.request_to_parent) process.env.CPR_REQUEST_TO_PARENT = inputs.request_to_parent; if (inputs.base) process.env.CPR_BASE = inputs.base; diff --git a/src/cpr/create_or_update_pull_request.py b/src/cpr/create_or_update_pull_request.py index ed08b24..de953a0 100644 --- a/src/cpr/create_or_update_pull_request.py +++ b/src/cpr/create_or_update_pull_request.py @@ -4,6 +4,20 @@ from github import Github, GithubException import os +def string_to_bool(str): + if str is None: + return False + else: + return str.lower() in [ + "true", + "1", + "t", + "y", + "yes", + "on", + ] + + def cs_string_to_list(str): # Split the comma separated string into a list l = [i.strip() for i in str.split(",")] @@ -56,27 +70,31 @@ def create_or_update_pull_request( team_reviewers, project_name, project_column_name, + draft, request_to_parent, ): - if request_to_parent is None: - request_to_parent = False - else: - request_to_parent = request_to_parent.lower() in ['true', '1', 't', 'y', 'yes', 'on'] - github_repo = head_repo = Github(github_token).get_repo(github_repository) - if request_to_parent: + if string_to_bool(request_to_parent): github_repo = github_repo.parent if github_repo is None: - raise ValueError("The checked out repository is not a fork. Input 'request-to-parent' should be set to false.") + raise ValueError( + "The checked out repository is not a fork. Input 'request-to-parent' should be set to false." + ) head_branch = f"{head_repo.owner.login}:{branch}" # Create the pull request try: pull_request = github_repo.create_pull( - title=title, body=body, base=base, head=head_branch + title=title, + body=body, + base=base, + head=head_branch, + draft=string_to_bool(draft), + ) + print( + f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})" ) - print(f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})") except GithubException as e: if e.status == 422: # A pull request exists for this branch and base @@ -86,7 +104,9 @@ def create_or_update_pull_request( )[0] # Update title and body pull_request.as_issue().edit(title=title, body=body) - print(f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})") + print( + f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})" + ) else: print(str(e)) raise diff --git a/src/cpr/create_pull_request.py b/src/cpr/create_pull_request.py index 37052ec..ead7f02 100755 --- a/src/cpr/create_pull_request.py +++ b/src/cpr/create_pull_request.py @@ -192,7 +192,7 @@ result = coub.create_or_update_branch(repo, repo_url, commit_message, base, bran if result["action"] in ["created", "updated"]: # The branch was created or updated - print(f"Pushing pull request branch to '{repo.full_name}/{branch}'") + print(f"Pushing pull request branch to 'origin/{branch}'") repo.git.push("--force", repo_url, f"HEAD:refs/heads/{branch}") # Set the base. It would have been 'None' if not specified as an input @@ -224,5 +224,6 @@ if result["action"] in ["created", "updated"]: os.environ.get("CPR_TEAM_REVIEWERS"), os.environ.get("CPR_PROJECT_NAME"), os.environ.get("CPR_PROJECT_COLUMN_NAME"), + os.environ.get("CPR_DRAFT"), os.environ.get("CPR_REQUEST_TO_PARENT"), ) diff --git a/src/index.js b/src/index.js index 82bc834..827ebad 100644 --- a/src/index.js +++ b/src/index.js @@ -62,6 +62,7 @@ async function run() { milestone: core.getInput("milestone"), project: core.getInput("project"), projectColumn: core.getInput("project-column"), + draft: core.getInput("draft"), branch: core.getInput("branch"), request_to_parent: core.getInput("request-to-parent"), base: core.getInput("base"), @@ -84,6 +85,7 @@ async function run() { if (inputs.milestone) process.env.CPR_MILESTONE = inputs.milestone; if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project; if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn; + if (inputs.draft) process.env.CPR_DRAFT = inputs.draft; if (inputs.branch) process.env.CPR_BRANCH = inputs.branch; if (inputs.request_to_parent) process.env.CPR_REQUEST_TO_PARENT = inputs.request_to_parent; if (inputs.base) process.env.CPR_BASE = inputs.base;