Add PR creation from Fork

This commit is contained in:
Jérémy Derussé 2020-03-27 18:56:55 +01:00
parent ac9f92d6e7
commit e48dab0c1c
No known key found for this signature in database
GPG key ID: 2083FA5758C473D2
9 changed files with 48 additions and 12 deletions

View file

@ -30,6 +30,7 @@ jobs:
project: Example Project project: Example Project
project-column: To do project-column: To do
branch: example-patches branch: example-patches
request-to-parent: false
- name: Check outputs - name: Check outputs
run: | run: |
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}" echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"

View file

@ -56,6 +56,7 @@ With the exception of `token`, all inputs are **optional**. If not set, sensible
| `project` | The name of the project for which a card should be created. Requires `project-column`. | | | `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`. | | | `project-column` | The name of the project column under which a card should be created. Requires `project`. | |
| `branch` | The branch name. See [Branch naming](#branch-naming) for details. | `create-pull-request/patch` | | `branch` | The branch name. See [Branch naming](#branch-naming) for details. | `create-pull-request/patch` |
| `request-to-parent` | Whether or not the pull request should be create on the parent repository. | `false` |
| `base` | Sets the pull request base branch. | Defaults to the branch checked out in the workflow. | | `base` | Sets the pull request base branch. | Defaults to the branch checked out in the workflow. |
| `branch-suffix` | The branch suffix type. Valid values are `random`, `timestamp` and `short-commit-hash`. See [Branch naming](#branch-naming) for details. | | | `branch-suffix` | The branch suffix type. Valid values are `random`, `timestamp` and `short-commit-hash`. See [Branch naming](#branch-naming) for details. | |
@ -186,6 +187,7 @@ jobs:
project: Example Project project: Example Project
project-column: To do project-column: To do
branch: example-patches branch: example-patches
request-to-parent: false
- name: Check outputs - name: Check outputs
run: | run: |
echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}" echo "Pull Request Number - ${{ env.PULL_REQUEST_NUMBER }}"

View file

@ -32,6 +32,9 @@ inputs:
description: 'The name of the project column under which a card should be created.' description: 'The name of the project column under which a card should be created.'
branch: branch:
description: 'The pull request branch name.' description: 'The pull request branch name.'
request-to-parent:
description: 'Whether or not the pull request should be create on the parent repository.'
default: false
base: base:
description: 'The pull request base branch.' description: 'The pull request base branch.'
branch-suffix: branch-suffix:
@ -43,5 +46,5 @@ runs:
using: 'node12' using: 'node12'
main: 'dist/index.js' main: 'dist/index.js'
branding: branding:
icon: 'git-pull-request' icon: 'git-pull-request'
color: 'gray-dark' color: 'gray-dark'

View file

@ -56,25 +56,37 @@ def create_or_update_pull_request(
team_reviewers, team_reviewers,
project_name, project_name,
project_column_name, project_column_name,
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:
github_repo = github_repo.parent
if github_repo is None:
raise ValueError("The repository is not a fork. The parameter request-to-parent should be set to false.")
head_branch = f"{head_repo.owner.login}:{branch}"
# Create the pull request # Create the pull request
github_repo = Github(github_token).get_repo(github_repository)
try: try:
pull_request = github_repo.create_pull( pull_request = github_repo.create_pull(
title=title, body=body, base=base, head=branch title=title, body=body, base=base, head=head_branch
) )
print(f"Created pull request #{pull_request.number} ({branch} => {base})") print(f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
except GithubException as e: except GithubException as e:
if e.status == 422: if e.status == 422:
# A pull request exists for this branch and base # A pull request exists for this branch and base
head_branch = "{}:{}".format(github_repository.split("/")[0], branch)
# Get the pull request # Get the pull request
pull_request = github_repo.get_pulls( pull_request = github_repo.get_pulls(
state="open", base=base, head=head_branch state="open", base=base, head=head_branch
)[0] )[0]
# Update title and body # Update title and body
pull_request.as_issue().edit(title=title, body=body) pull_request.as_issue().edit(title=title, body=body)
print(f"Updated pull request #{pull_request.number} ({branch} => {base})") print(f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
else: else:
print(str(e)) print(str(e))
raise raise

View file

@ -224,4 +224,5 @@ if result["action"] in ["created", "updated"]:
os.environ.get("CPR_TEAM_REVIEWERS"), os.environ.get("CPR_TEAM_REVIEWERS"),
os.environ.get("CPR_PROJECT_NAME"), os.environ.get("CPR_PROJECT_NAME"),
os.environ.get("CPR_PROJECT_COLUMN_NAME"), os.environ.get("CPR_PROJECT_COLUMN_NAME"),
os.environ.get("CPR_REQUEST_TO_PARENT"),
) )

2
dist/index.js vendored
View file

@ -4275,6 +4275,7 @@ async function run() {
project: core.getInput("project"), project: core.getInput("project"),
projectColumn: core.getInput("project-column"), projectColumn: core.getInput("project-column"),
branch: core.getInput("branch"), branch: core.getInput("branch"),
request_to_parent: core.getInput("request-to-parent"),
base: core.getInput("base"), base: core.getInput("base"),
branchSuffix: core.getInput("branch-suffix") branchSuffix: core.getInput("branch-suffix")
}; };
@ -4296,6 +4297,7 @@ async function run() {
if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project; if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project;
if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn; if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn;
if (inputs.branch) process.env.CPR_BRANCH = inputs.branch; 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; if (inputs.base) process.env.CPR_BASE = inputs.base;
if (inputs.branchSuffix) process.env.CPR_BRANCH_SUFFIX = inputs.branchSuffix; if (inputs.branchSuffix) process.env.CPR_BRANCH_SUFFIX = inputs.branchSuffix;

View file

@ -56,25 +56,37 @@ def create_or_update_pull_request(
team_reviewers, team_reviewers,
project_name, project_name,
project_column_name, project_column_name,
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:
github_repo = github_repo.parent
if github_repo is None:
raise ValueError("The repository is not a fork. The parameter request-to-parent should be set to false.")
head_branch = f"{head_repo.owner.login}:{branch}"
# Create the pull request # Create the pull request
github_repo = Github(github_token).get_repo(github_repository)
try: try:
pull_request = github_repo.create_pull( pull_request = github_repo.create_pull(
title=title, body=body, base=base, head=branch title=title, body=body, base=base, head=head_branch
) )
print(f"Created pull request #{pull_request.number} ({branch} => {base})") print(f"Created pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
except GithubException as e: except GithubException as e:
if e.status == 422: if e.status == 422:
# A pull request exists for this branch and base # A pull request exists for this branch and base
head_branch = "{}:{}".format(github_repository.split("/")[0], branch)
# Get the pull request # Get the pull request
pull_request = github_repo.get_pulls( pull_request = github_repo.get_pulls(
state="open", base=base, head=head_branch state="open", base=base, head=head_branch
)[0] )[0]
# Update title and body # Update title and body
pull_request.as_issue().edit(title=title, body=body) pull_request.as_issue().edit(title=title, body=body)
print(f"Updated pull request #{pull_request.number} ({branch} => {base})") print(f"Updated pull request #{pull_request.number} ({head_branch} => {github_repo.owner.login}:{base})")
else: else:
print(str(e)) print(str(e))
raise raise

View file

@ -192,7 +192,7 @@ result = coub.create_or_update_branch(repo, repo_url, commit_message, base, bran
if result["action"] in ["created", "updated"]: if result["action"] in ["created", "updated"]:
# The branch was created or updated # The branch was created or updated
print(f"Pushing pull request branch to 'origin/{branch}'") print(f"Pushing pull request branch to '{repo.full_name}/{branch}'")
repo.git.push("--force", repo_url, f"HEAD:refs/heads/{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 # Set the base. It would have been 'None' if not specified as an input
@ -224,4 +224,5 @@ if result["action"] in ["created", "updated"]:
os.environ.get("CPR_TEAM_REVIEWERS"), os.environ.get("CPR_TEAM_REVIEWERS"),
os.environ.get("CPR_PROJECT_NAME"), os.environ.get("CPR_PROJECT_NAME"),
os.environ.get("CPR_PROJECT_COLUMN_NAME"), os.environ.get("CPR_PROJECT_COLUMN_NAME"),
os.environ.get("CPR_REQUEST_TO_PARENT"),
) )

View file

@ -63,6 +63,7 @@ async function run() {
project: core.getInput("project"), project: core.getInput("project"),
projectColumn: core.getInput("project-column"), projectColumn: core.getInput("project-column"),
branch: core.getInput("branch"), branch: core.getInput("branch"),
request_to_parent: core.getInput("request-to-parent"),
base: core.getInput("base"), base: core.getInput("base"),
branchSuffix: core.getInput("branch-suffix") branchSuffix: core.getInput("branch-suffix")
}; };
@ -84,6 +85,7 @@ async function run() {
if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project; if (inputs.project) process.env.CPR_PROJECT_NAME = inputs.project;
if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn; if (inputs.projectColumn) process.env.CPR_PROJECT_COLUMN_NAME = inputs.projectColumn;
if (inputs.branch) process.env.CPR_BRANCH = inputs.branch; 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; if (inputs.base) process.env.CPR_BASE = inputs.base;
if (inputs.branchSuffix) process.env.CPR_BRANCH_SUFFIX = inputs.branchSuffix; if (inputs.branchSuffix) process.env.CPR_BRANCH_SUFFIX = inputs.branchSuffix;