Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
a9ee7487c9 | |||
305373e8f3 | |||
04bf3d439a | |||
ec6352b0a0 | |||
ce4df54075 | |||
c855a500d1 | |||
baf52378ae | |||
e6ebd7ca5c | |||
83fef4e8a1 | |||
d3dc225920 | |||
6342438c4e | |||
324fe6cdde | |||
4faa5d5a97 | |||
381bc30cef | |||
7531167f24 | |||
66a1436815 | |||
1d1fedd99c | |||
823751817d | |||
1edcf07a19 | |||
4c95214a9b | |||
b37c0a038c | |||
d9841567d1 | |||
2cce94bfb0 | |||
d968e8b11b | |||
6c73093f9b | |||
d17c882ef3 | |||
d5f4e48a66 |
@ -36,6 +36,8 @@ jobs:
|
|||||||
assignees: peter-evans
|
assignees: peter-evans
|
||||||
reviewers: peter-evans
|
reviewers: peter-evans
|
||||||
milestone: 1
|
milestone: 1
|
||||||
|
project: Example Project
|
||||||
|
project-column: To do
|
||||||
branch: example-patches
|
branch: example-patches
|
||||||
branch-suffix: random
|
branch-suffix: random
|
||||||
- name: Check outputs
|
- name: Check outputs
|
||||||
|
2
.github/workflows/create-pull-request.yml
vendored
2
.github/workflows/create-pull-request.yml
vendored
@ -28,6 +28,8 @@ jobs:
|
|||||||
assignees: peter-evans
|
assignees: peter-evans
|
||||||
reviewers: peter-evans
|
reviewers: peter-evans
|
||||||
milestone: 1
|
milestone: 1
|
||||||
|
project: Example Project
|
||||||
|
project-column: To do
|
||||||
branch: example-patches
|
branch: example-patches
|
||||||
branch-suffix: short-commit-hash
|
branch-suffix: short-commit-hash
|
||||||
- name: Check outputs
|
- name: Check outputs
|
||||||
|
16
.github/workflows/slash-command-dispatch.yml
vendored
Normal file
16
.github/workflows/slash-command-dispatch.yml
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
name: Slash Command Dispatch
|
||||||
|
on:
|
||||||
|
issue_comment:
|
||||||
|
types: [created]
|
||||||
|
jobs:
|
||||||
|
slashCommandDispatch:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Slash Command Dispatch
|
||||||
|
uses: peter-evans/slash-command-dispatch@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.REPO_ACCESS_TOKEN }}
|
||||||
|
reaction-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
commands: test, clean
|
||||||
|
permission: admin
|
||||||
|
repository: peter-evans/create-pull-request-tests
|
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,2 +1,6 @@
|
|||||||
|
__pycache__
|
||||||
|
.python-version
|
||||||
|
|
||||||
node_modules
|
node_modules
|
||||||
|
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
37
README.md
37
README.md
@ -36,17 +36,21 @@ These inputs are *all optional*. If not set, sensible default values will be use
|
|||||||
| Name | Description | Default |
|
| Name | Description | Default |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| `commit-message` | The message to use when committing changes. | `Auto-committed changes by create-pull-request action` |
|
| `commit-message` | The message to use when committing changes. | `Auto-committed changes by create-pull-request action` |
|
||||||
| `author-email` | The email address of the commit author. | For `push` events, the HEAD commit author. Otherwise, <GITHUB_ACTOR>@users.noreply.github.com, where `GITHUB_ACTOR` is the GitHub user that initiated the event. |
|
|
||||||
| `author-name` | The name of the commit author. | For `push` events, the HEAD commit author. Otherwise, <GITHUB_ACTOR>, the GitHub user that initiated the event. |
|
| `author-name` | The name of the commit author. | For `push` events, the HEAD commit author. Otherwise, <GITHUB_ACTOR>, the GitHub user that initiated the event. |
|
||||||
|
| `author-email` | The email address of the commit author. | For `push` events, the HEAD commit author. Otherwise, <GITHUB_ACTOR>@users.noreply.github.com, where `GITHUB_ACTOR` is the GitHub user that initiated the event. |
|
||||||
|
| `committer-name` | The name of the committer. | Defaults to match `author-name` |
|
||||||
|
| `committer-email` | The email address of the committer. | Defaults to match `author-email` |
|
||||||
| `title` | The title of the pull request. | `Auto-generated by create-pull-request action` |
|
| `title` | The title of the pull request. | `Auto-generated by create-pull-request action` |
|
||||||
| `body` | The body of the pull request. | `Auto-generated pull request by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action` |
|
| `body` | The body of the pull request. | `Auto-generated pull request by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action` |
|
||||||
| `labels` | A comma separated list of labels. | none |
|
| `labels` | A comma separated list of labels. | |
|
||||||
| `assignees` | A comma separated list of assignees (GitHub usernames). | none |
|
| `assignees` | A comma separated list of assignees (GitHub usernames). | |
|
||||||
| `reviewers` | A comma separated list of reviewers (GitHub usernames) to request a review from. | none |
|
| `reviewers` | A comma separated list of reviewers (GitHub usernames) to request a review from. | |
|
||||||
| `team-reviewers` | A comma separated list of GitHub teams to request a review from. | none |
|
| `team-reviewers` | A comma separated list of GitHub teams to request a review from. | |
|
||||||
| `milestone` | The number of the milestone to associate this pull request with. | none |
|
| `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`. | |
|
||||||
| `branch` | The branch name. See **Branch naming** below for details. | `create-pull-request/patch` |
|
| `branch` | The branch name. See **Branch naming** below for details. | `create-pull-request/patch` |
|
||||||
| `base` | Overrides the base branch. **Use with caution!** | Defaults to the currently checked out branch. |
|
| `base` | Sets the pull request base branch. | Defaults to the currently checked out branch, `GITHUB_REF`. For `pull_request` events, `GITHUB_HEAD_REF` |
|
||||||
| `branch-suffix` | The branch suffix type. Valid values are `short-commit-hash`, `timestamp`, `random` and `none`. See **Branch naming** below for details. | `short-commit-hash` |
|
| `branch-suffix` | The branch suffix type. Valid values are `short-commit-hash`, `timestamp`, `random` and `none`. See **Branch naming** below for details. | `short-commit-hash` |
|
||||||
|
|
||||||
**Outputs**
|
**Outputs**
|
||||||
@ -83,12 +87,27 @@ The branch name is defined by the input `branch` and defaults to `create-pull-re
|
|||||||
|
|
||||||
#### Strategy B - Create and update a pull request branch
|
#### Strategy B - Create and update a pull request branch
|
||||||
|
|
||||||
To use this strategy, set `branch-suffix` to the value `none`. The input `branch` defaults to `create-pull-request/patch`. Commits will be made to this branch and a pull request created. Any subsequent changes will be committed to the *same* branch and reflected in the existing pull request.
|
To use this strategy, set `branch-suffix` to the value `none`. The input `branch` defaults to `create-pull-request/patch`. Commits will be made to this branch and a pull request created. Any subsequent changes will be committed to the *same* branch and reflected in the open pull request. If the pull request is merged or closed a new one will be created.
|
||||||
|
|
||||||
### Ignoring files
|
### Ignoring files
|
||||||
|
|
||||||
If there are files or directories you want to ignore you can simply add them to a `.gitignore` file at the root of your repository. The action will respect this file.
|
If there are files or directories you want to ignore you can simply add them to a `.gitignore` file at the root of your repository. The action will respect this file.
|
||||||
|
|
||||||
|
### Commit as github-actions[bot]
|
||||||
|
|
||||||
|
You can make commits that appear to be made by the GitHub Actions bot as follows.
|
||||||
|
|
||||||
|
```yml
|
||||||
|
- name: Create Pull Request
|
||||||
|
uses: peter-evans/create-pull-request@v1
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
author-name: github-actions[bot]
|
||||||
|
author-email: 41898282+github-actions[bot]@users.noreply.github.com
|
||||||
|
committer-name: GitHub
|
||||||
|
committer-email: noreply@github.com
|
||||||
|
```
|
||||||
|
|
||||||
## Reference Example
|
## Reference Example
|
||||||
|
|
||||||
The following workflow is a reference example that sets all the main inputs.
|
The following workflow is a reference example that sets all the main inputs.
|
||||||
@ -124,6 +143,8 @@ jobs:
|
|||||||
assignees: peter-evans
|
assignees: peter-evans
|
||||||
reviewers: peter-evans
|
reviewers: peter-evans
|
||||||
milestone: 1
|
milestone: 1
|
||||||
|
project: Example Project
|
||||||
|
project-column: To do
|
||||||
branch: example-patches
|
branch: example-patches
|
||||||
branch-suffix: short-commit-hash
|
branch-suffix: short-commit-hash
|
||||||
- name: Check outputs
|
- name: Check outputs
|
||||||
|
14
action.yml
14
action.yml
@ -6,10 +6,14 @@ inputs:
|
|||||||
required: true
|
required: true
|
||||||
commit-message:
|
commit-message:
|
||||||
description: 'The message to use when committing changes.'
|
description: 'The message to use when committing changes.'
|
||||||
author-email:
|
|
||||||
description: 'The email address of the commit author.'
|
|
||||||
author-name:
|
author-name:
|
||||||
description: 'The name of the commit author.'
|
description: 'The name of the commit author.'
|
||||||
|
author-email:
|
||||||
|
description: 'The email address of the commit author.'
|
||||||
|
committer-name:
|
||||||
|
description: 'The name of the committer.'
|
||||||
|
committer-email:
|
||||||
|
description: 'The email address of the committer.'
|
||||||
title:
|
title:
|
||||||
description: 'The title of the pull request.'
|
description: 'The title of the pull request.'
|
||||||
body:
|
body:
|
||||||
@ -24,10 +28,14 @@ inputs:
|
|||||||
description: 'A comma separated list of GitHub teams to request a review from.'
|
description: 'A comma separated list of GitHub teams to request a review from.'
|
||||||
milestone:
|
milestone:
|
||||||
description: 'The number of the milestone to associate this pull request with.'
|
description: 'The number of the milestone to associate this pull request with.'
|
||||||
|
project:
|
||||||
|
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.'
|
||||||
branch:
|
branch:
|
||||||
description: 'The pull request branch name.'
|
description: 'The pull request branch name.'
|
||||||
base:
|
base:
|
||||||
description: 'Overrides the base branch.'
|
description: 'Sets the pull request base branch.'
|
||||||
branch-suffix:
|
branch-suffix:
|
||||||
description: 'The branch suffix type.'
|
description: 'The branch suffix type.'
|
||||||
outputs:
|
outputs:
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 441 KiB After Width: | Height: | Size: 417 KiB |
14
dist/index.js
vendored
14
dist/index.js
vendored
@ -965,7 +965,7 @@ async function run() {
|
|||||||
core.debug(`src: ${src}`);
|
core.debug(`src: ${src}`);
|
||||||
|
|
||||||
// Setup Python from the tool cache
|
// Setup Python from the tool cache
|
||||||
setupPython("3.8.0", "x64");
|
setupPython("3.8.x", "x64");
|
||||||
|
|
||||||
// Install requirements
|
// Install requirements
|
||||||
await exec.exec("pip", [
|
await exec.exec("pip", [
|
||||||
@ -978,8 +978,10 @@ async function run() {
|
|||||||
const inputs = {
|
const inputs = {
|
||||||
token: core.getInput("token"),
|
token: core.getInput("token"),
|
||||||
commitMessage: core.getInput("commit-message"),
|
commitMessage: core.getInput("commit-message"),
|
||||||
commitAuthorEmail: core.getInput("author-email"),
|
|
||||||
commitAuthorName: core.getInput("author-name"),
|
commitAuthorName: core.getInput("author-name"),
|
||||||
|
commitAuthorEmail: core.getInput("author-email"),
|
||||||
|
committerName: core.getInput("committer-name"),
|
||||||
|
committerEmail: core.getInput("committer-email"),
|
||||||
title: core.getInput("title"),
|
title: core.getInput("title"),
|
||||||
body: core.getInput("body"),
|
body: core.getInput("body"),
|
||||||
labels: core.getInput("labels"),
|
labels: core.getInput("labels"),
|
||||||
@ -987,6 +989,8 @@ async function run() {
|
|||||||
reviewers: core.getInput("reviewers"),
|
reviewers: core.getInput("reviewers"),
|
||||||
teamReviewers: core.getInput("team-reviewers"),
|
teamReviewers: core.getInput("team-reviewers"),
|
||||||
milestone: core.getInput("milestone"),
|
milestone: core.getInput("milestone"),
|
||||||
|
project: core.getInput("project"),
|
||||||
|
projectColumn: core.getInput("project-column"),
|
||||||
branch: core.getInput("branch"),
|
branch: core.getInput("branch"),
|
||||||
base: core.getInput("base"),
|
base: core.getInput("base"),
|
||||||
branchSuffix: core.getInput("branch-suffix"),
|
branchSuffix: core.getInput("branch-suffix"),
|
||||||
@ -997,8 +1001,10 @@ async function run() {
|
|||||||
// Set environment variables from inputs.
|
// Set environment variables from inputs.
|
||||||
if (inputs.token) process.env.GITHUB_TOKEN = inputs.token;
|
if (inputs.token) process.env.GITHUB_TOKEN = inputs.token;
|
||||||
if (inputs.commitMessage) process.env.COMMIT_MESSAGE = inputs.commitMessage;
|
if (inputs.commitMessage) process.env.COMMIT_MESSAGE = inputs.commitMessage;
|
||||||
if (inputs.commitAuthorEmail) process.env.COMMIT_AUTHOR_EMAIL = inputs.commitAuthorEmail;
|
|
||||||
if (inputs.commitAuthorName) process.env.COMMIT_AUTHOR_NAME = inputs.commitAuthorName;
|
if (inputs.commitAuthorName) process.env.COMMIT_AUTHOR_NAME = inputs.commitAuthorName;
|
||||||
|
if (inputs.commitAuthorEmail) process.env.COMMIT_AUTHOR_EMAIL = inputs.commitAuthorEmail;
|
||||||
|
if (inputs.committerName) process.env.COMMITTER_NAME = inputs.committerName;
|
||||||
|
if (inputs.committerEmail) process.env.COMMITTER_EMAIL = inputs.committerEmail;
|
||||||
if (inputs.title) process.env.PULL_REQUEST_TITLE = inputs.title;
|
if (inputs.title) process.env.PULL_REQUEST_TITLE = inputs.title;
|
||||||
if (inputs.body) process.env.PULL_REQUEST_BODY = inputs.body;
|
if (inputs.body) process.env.PULL_REQUEST_BODY = inputs.body;
|
||||||
if (inputs.labels) process.env.PULL_REQUEST_LABELS = inputs.labels;
|
if (inputs.labels) process.env.PULL_REQUEST_LABELS = inputs.labels;
|
||||||
@ -1006,6 +1012,8 @@ async function run() {
|
|||||||
if (inputs.reviewers) process.env.PULL_REQUEST_REVIEWERS = inputs.reviewers;
|
if (inputs.reviewers) process.env.PULL_REQUEST_REVIEWERS = inputs.reviewers;
|
||||||
if (inputs.teamReviewers) process.env.PULL_REQUEST_TEAM_REVIEWERS = inputs.teamReviewers;
|
if (inputs.teamReviewers) process.env.PULL_REQUEST_TEAM_REVIEWERS = inputs.teamReviewers;
|
||||||
if (inputs.milestone) process.env.PULL_REQUEST_MILESTONE = inputs.milestone;
|
if (inputs.milestone) process.env.PULL_REQUEST_MILESTONE = inputs.milestone;
|
||||||
|
if (inputs.project) process.env.PROJECT_NAME = inputs.project;
|
||||||
|
if (inputs.projectColumn) process.env.PROJECT_COLUMN_NAME = inputs.projectColumn;
|
||||||
if (inputs.branch) process.env.PULL_REQUEST_BRANCH = inputs.branch;
|
if (inputs.branch) process.env.PULL_REQUEST_BRANCH = inputs.branch;
|
||||||
if (inputs.base) process.env.PULL_REQUEST_BASE = inputs.base;
|
if (inputs.base) process.env.PULL_REQUEST_BASE = inputs.base;
|
||||||
if (inputs.branchSuffix) process.env.BRANCH_SUFFIX = inputs.branchSuffix;
|
if (inputs.branchSuffix) process.env.BRANCH_SUFFIX = inputs.branchSuffix;
|
||||||
|
269
dist/src/create-pull-request.py
vendored
269
dist/src/create-pull-request.py
vendored
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
''' Create Pull Request '''
|
""" Create Pull Request """
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
@ -13,18 +13,18 @@ from github import Github, GithubException
|
|||||||
def get_github_event(github_event_path):
|
def get_github_event(github_event_path):
|
||||||
with open(github_event_path) as f:
|
with open(github_event_path) as f:
|
||||||
github_event = json.load(f)
|
github_event = json.load(f)
|
||||||
if bool(os.environ.get('DEBUG_EVENT')):
|
if bool(os.environ.get("DEBUG_EVENT")):
|
||||||
print(os.environ['GITHUB_EVENT_NAME'])
|
print(os.environ["GITHUB_EVENT_NAME"])
|
||||||
print(json.dumps(github_event, sort_keys=True, indent=2))
|
print(json.dumps(github_event, sort_keys=True, indent=2))
|
||||||
return github_event
|
return github_event
|
||||||
|
|
||||||
|
|
||||||
def get_head_short_sha1(repo):
|
def get_head_short_sha1(repo):
|
||||||
return repo.git.rev_parse('--short', 'HEAD')
|
return repo.git.rev_parse("--short", "HEAD")
|
||||||
|
|
||||||
|
|
||||||
def get_random_suffix(size=7, chars=string.ascii_lowercase + string.digits):
|
def get_random_suffix(size=7, chars=string.ascii_lowercase + string.digits):
|
||||||
return ''.join(random.choice(chars) for _ in range(size))
|
return "".join(random.choice(chars) for _ in range(size))
|
||||||
|
|
||||||
|
|
||||||
def remote_branch_exists(repo, branch):
|
def remote_branch_exists(repo, branch):
|
||||||
@ -39,106 +39,132 @@ def get_author_default(event_name, event_data):
|
|||||||
email = "{head_commit[author][email]}".format(**event_data)
|
email = "{head_commit[author][email]}".format(**event_data)
|
||||||
name = "{head_commit[author][name]}".format(**event_data)
|
name = "{head_commit[author][name]}".format(**event_data)
|
||||||
else:
|
else:
|
||||||
email = os.environ['GITHUB_ACTOR'] + '@users.noreply.github.com'
|
email = os.environ["GITHUB_ACTOR"] + "@users.noreply.github.com"
|
||||||
name = os.environ['GITHUB_ACTOR']
|
name = os.environ["GITHUB_ACTOR"]
|
||||||
return email, name
|
return email, name
|
||||||
|
|
||||||
|
|
||||||
def set_git_config(git, email, name):
|
def get_repo_url(token, github_repository):
|
||||||
print("Configuring git user as '%s <%s>'" % (name, email))
|
return "https://x-access-token:%s@github.com/%s" % (token, github_repository)
|
||||||
git.config('--global', 'user.email', '"%s"' % email)
|
|
||||||
git.config('--global', 'user.name', '"%s"' % name)
|
|
||||||
|
|
||||||
|
|
||||||
def set_git_remote_url(git, token, github_repository):
|
|
||||||
git.remote(
|
|
||||||
'set-url', 'origin', "https://x-access-token:%s@github.com/%s" %
|
|
||||||
(token, github_repository))
|
|
||||||
|
|
||||||
|
|
||||||
def checkout_branch(git, remote_exists, branch):
|
def checkout_branch(git, remote_exists, branch):
|
||||||
if remote_exists:
|
if remote_exists:
|
||||||
print("Checking out branch '%s'" % branch)
|
print("Checking out branch '%s'" % branch)
|
||||||
git.stash('--include-untracked')
|
git.stash("--include-untracked")
|
||||||
git.checkout(branch)
|
git.checkout(branch)
|
||||||
try:
|
try:
|
||||||
git.stash('pop')
|
git.stash("pop")
|
||||||
except BaseException:
|
except BaseException:
|
||||||
git.checkout('--theirs', '.')
|
git.checkout("--theirs", ".")
|
||||||
git.reset()
|
git.reset()
|
||||||
else:
|
else:
|
||||||
print("Creating new branch '%s'" % branch)
|
print("Creating new branch '%s'" % branch)
|
||||||
git.checkout('HEAD', b=branch)
|
git.checkout("HEAD", b=branch)
|
||||||
|
|
||||||
|
|
||||||
def push_changes(git, branch, commit_message):
|
def push_changes(git, token, github_repository, branch, commit_message):
|
||||||
git.add('-A')
|
git.add("-A")
|
||||||
git.commit(m=commit_message)
|
git.commit(m=commit_message)
|
||||||
return git.push('-f', '--set-upstream', 'origin', branch)
|
repo_url = get_repo_url(token, github_repository)
|
||||||
|
return git.push("-f", repo_url, f"HEAD:refs/heads/{branch}")
|
||||||
|
|
||||||
|
|
||||||
def cs_string_to_list(str):
|
def cs_string_to_list(str):
|
||||||
# Split the comma separated string into a list
|
# Split the comma separated string into a list
|
||||||
l = [i.strip() for i in str.split(',')]
|
l = [i.strip() for i in str.split(",")]
|
||||||
# Remove empty strings
|
# Remove empty strings
|
||||||
return list(filter(None, l))
|
return list(filter(None, l))
|
||||||
|
|
||||||
|
|
||||||
|
def create_project_card(github_repo, project_name, project_column_name, pull_request):
|
||||||
|
# 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("::warning::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("::warning::Project column not found. Unable to create project card.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Create a project card for the pull request
|
||||||
|
column.create_card(content_id=pull_request.id, content_type="PullRequest")
|
||||||
|
print(
|
||||||
|
"Added pull request #%d to project '%s' under column '%s'"
|
||||||
|
% (pull_request.number, project.name, column.name)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def process_event(github_token, github_repository, repo, branch, base):
|
def process_event(github_token, github_repository, repo, branch, base):
|
||||||
# Fetch optional environment variables with default values
|
# Fetch optional environment variables with default values
|
||||||
commit_message = os.getenv(
|
commit_message = os.getenv(
|
||||||
'COMMIT_MESSAGE',
|
"COMMIT_MESSAGE", "Auto-committed changes by create-pull-request action"
|
||||||
"Auto-committed changes by create-pull-request action")
|
)
|
||||||
title = os.getenv(
|
title = os.getenv(
|
||||||
'PULL_REQUEST_TITLE',
|
"PULL_REQUEST_TITLE", "Auto-generated by create-pull-request action"
|
||||||
"Auto-generated by create-pull-request action")
|
)
|
||||||
body = os.getenv(
|
body = os.getenv(
|
||||||
'PULL_REQUEST_BODY', "Auto-generated pull request by "
|
"PULL_REQUEST_BODY",
|
||||||
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action")
|
"Auto-generated pull request by "
|
||||||
|
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action",
|
||||||
|
)
|
||||||
# Fetch optional environment variables with no default values
|
# Fetch optional environment variables with no default values
|
||||||
pull_request_labels = os.environ.get('PULL_REQUEST_LABELS')
|
pull_request_labels = os.environ.get("PULL_REQUEST_LABELS")
|
||||||
pull_request_assignees = os.environ.get('PULL_REQUEST_ASSIGNEES')
|
pull_request_assignees = os.environ.get("PULL_REQUEST_ASSIGNEES")
|
||||||
pull_request_milestone = os.environ.get('PULL_REQUEST_MILESTONE')
|
pull_request_milestone = os.environ.get("PULL_REQUEST_MILESTONE")
|
||||||
pull_request_reviewers = os.environ.get('PULL_REQUEST_REVIEWERS')
|
pull_request_reviewers = os.environ.get("PULL_REQUEST_REVIEWERS")
|
||||||
pull_request_team_reviewers = os.environ.get('PULL_REQUEST_TEAM_REVIEWERS')
|
pull_request_team_reviewers = os.environ.get("PULL_REQUEST_TEAM_REVIEWERS")
|
||||||
|
project_name = os.environ.get("PROJECT_NAME")
|
||||||
|
project_column_name = os.environ.get("PROJECT_COLUMN_NAME")
|
||||||
|
|
||||||
# Push the local changes to the remote branch
|
# Push the local changes to the remote branch
|
||||||
print("Pushing changes to 'origin/%s'" % branch)
|
print("Pushing changes to 'origin/%s'" % branch)
|
||||||
push_result = push_changes(repo.git, branch, commit_message)
|
push_result = push_changes(
|
||||||
|
repo.git, github_token, github_repository, branch, commit_message
|
||||||
|
)
|
||||||
print(push_result)
|
print(push_result)
|
||||||
|
|
||||||
# Create the pull request
|
# Create the pull request
|
||||||
github_repo = Github(github_token).get_repo(github_repository)
|
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,
|
title=title, body=body, base=base, head=branch
|
||||||
body=body,
|
)
|
||||||
base=base,
|
print(
|
||||||
head=branch)
|
"Created pull request #%d (%s => %s)" % (pull_request.number, branch, base)
|
||||||
print("Created pull request #%d (%s => %s)" %
|
)
|
||||||
(pull_request.number, branch, base))
|
|
||||||
except GithubException as e:
|
except GithubException as e:
|
||||||
if e.status == 422:
|
if e.status == 422:
|
||||||
# Format the branch name
|
# Format the branch name
|
||||||
head_branch = "%s:%s" % (github_repository.split("/")[0], branch)
|
head_branch = "%s:%s" % (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',
|
state="open", base=base, head=head_branch
|
||||||
base=base,
|
)[0]
|
||||||
head=head_branch)[0]
|
print(
|
||||||
print("Updated pull request #%d (%s => %s)" %
|
"Updated pull request #%d (%s => %s)"
|
||||||
(pull_request.number, branch, base))
|
% (pull_request.number, branch, base)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Set the output variables
|
# Set the output variables
|
||||||
os.system(
|
os.system("echo ::set-env name=PULL_REQUEST_NUMBER::%d" % pull_request.number)
|
||||||
'echo ::set-env name=PULL_REQUEST_NUMBER::%d' %
|
os.system("echo ::set-output name=pr_number::%d" % pull_request.number)
|
||||||
pull_request.number)
|
|
||||||
os.system(
|
|
||||||
'echo ::set-output name=pr_number::%d' %
|
|
||||||
pull_request.number)
|
|
||||||
|
|
||||||
# Set labels, assignees and milestone
|
# Set labels, assignees and milestone
|
||||||
if pull_request_labels is not None:
|
if pull_request_labels is not None:
|
||||||
@ -146,7 +172,9 @@ def process_event(github_token, github_repository, repo, branch, base):
|
|||||||
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
||||||
if pull_request_assignees is not None:
|
if pull_request_assignees is not None:
|
||||||
print("Applying assignees '%s'" % pull_request_assignees)
|
print("Applying assignees '%s'" % pull_request_assignees)
|
||||||
pull_request.as_issue().edit(assignees=cs_string_to_list(pull_request_assignees))
|
pull_request.as_issue().edit(
|
||||||
|
assignees=cs_string_to_list(pull_request_assignees)
|
||||||
|
)
|
||||||
if pull_request_milestone is not None:
|
if pull_request_milestone is not None:
|
||||||
print("Applying milestone '%s'" % pull_request_milestone)
|
print("Applying milestone '%s'" % pull_request_milestone)
|
||||||
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
||||||
@ -157,7 +185,8 @@ def process_event(github_token, github_repository, repo, branch, base):
|
|||||||
print("Requesting reviewers '%s'" % pull_request_reviewers)
|
print("Requesting reviewers '%s'" % pull_request_reviewers)
|
||||||
try:
|
try:
|
||||||
pull_request.create_review_request(
|
pull_request.create_review_request(
|
||||||
reviewers=cs_string_to_list(pull_request_reviewers))
|
reviewers=cs_string_to_list(pull_request_reviewers)
|
||||||
|
)
|
||||||
except GithubException as e:
|
except GithubException as e:
|
||||||
# Likely caused by "Review cannot be requested from pull request
|
# Likely caused by "Review cannot be requested from pull request
|
||||||
# author."
|
# author."
|
||||||
@ -168,58 +197,88 @@ def process_event(github_token, github_repository, repo, branch, base):
|
|||||||
if pull_request_team_reviewers is not None:
|
if pull_request_team_reviewers is not None:
|
||||||
print("Requesting team reviewers '%s'" % pull_request_team_reviewers)
|
print("Requesting team reviewers '%s'" % pull_request_team_reviewers)
|
||||||
pull_request.create_review_request(
|
pull_request.create_review_request(
|
||||||
team_reviewers=cs_string_to_list(pull_request_team_reviewers))
|
team_reviewers=cs_string_to_list(pull_request_team_reviewers)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a project card for the pull request
|
||||||
|
if project_name is not None and project_column_name is not None:
|
||||||
|
try:
|
||||||
|
create_project_card(
|
||||||
|
github_repo, project_name, project_column_name, pull_request
|
||||||
|
)
|
||||||
|
except GithubException as e:
|
||||||
|
# Likely caused by "Project already has the associated issue."
|
||||||
|
if e.status == 422:
|
||||||
|
print(
|
||||||
|
"Create project card failed - %s" % e.data["errors"][0]["message"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Fetch environment variables
|
# Fetch environment variables
|
||||||
github_token = os.environ['GITHUB_TOKEN']
|
github_token = os.environ["GITHUB_TOKEN"]
|
||||||
github_repository = os.environ['GITHUB_REPOSITORY']
|
github_repository = os.environ["GITHUB_REPOSITORY"]
|
||||||
github_ref = os.environ['GITHUB_REF']
|
github_ref = os.environ["GITHUB_REF"]
|
||||||
event_name = os.environ['GITHUB_EVENT_NAME']
|
event_name = os.environ["GITHUB_EVENT_NAME"]
|
||||||
# Get the JSON event data
|
# Get the JSON event data
|
||||||
event_data = get_github_event(os.environ['GITHUB_EVENT_PATH'])
|
event_data = get_github_event(os.environ["GITHUB_EVENT_PATH"])
|
||||||
|
|
||||||
|
# Get the default for author email and name
|
||||||
|
author_email, author_name = get_author_default(event_name, event_data)
|
||||||
|
# Set author name and email overrides
|
||||||
|
author_name = os.getenv("COMMIT_AUTHOR_NAME", author_name)
|
||||||
|
author_email = os.getenv("COMMIT_AUTHOR_EMAIL", author_email)
|
||||||
|
# Set committer name and email overrides
|
||||||
|
committer_name = os.getenv("COMMITTER_NAME", author_name)
|
||||||
|
committer_email = os.getenv("COMMITTER_EMAIL", author_email)
|
||||||
|
|
||||||
# Set the repo to the working directory
|
# Set the repo to the working directory
|
||||||
repo = Repo(os.getcwd())
|
repo = Repo(os.getcwd())
|
||||||
# Get the default for author email and name
|
# Set git environment. This will not persist after the action completes.
|
||||||
author_email, author_name = get_author_default(event_name, event_data)
|
print("Configuring git author as '%s <%s>'" % (author_name, author_email))
|
||||||
# Set commit author overrides
|
print("Configuring git committer as '%s <%s>'" % (committer_name, committer_email))
|
||||||
author_email = os.getenv('COMMIT_AUTHOR_EMAIL', author_email)
|
repo.git.update_environment(
|
||||||
author_name = os.getenv('COMMIT_AUTHOR_NAME', author_name)
|
GIT_AUTHOR_NAME=author_name,
|
||||||
# Set git configuration
|
GIT_AUTHOR_EMAIL=author_email,
|
||||||
set_git_config(repo.git, author_email, author_name)
|
GIT_COMMITTER_NAME=committer_name,
|
||||||
# Update URL for the 'origin' remote
|
GIT_COMMITTER_EMAIL=committer_email,
|
||||||
set_git_remote_url(repo.git, github_token, github_repository)
|
)
|
||||||
|
|
||||||
# Fetch/Set the branch name
|
# Fetch/Set the branch name
|
||||||
branch_prefix = os.getenv(
|
branch_prefix = os.getenv("PULL_REQUEST_BRANCH", "create-pull-request/patch")
|
||||||
'PULL_REQUEST_BRANCH',
|
|
||||||
'create-pull-request/patch')
|
|
||||||
# Fetch an optional base branch override
|
# Fetch an optional base branch override
|
||||||
base_override = os.environ.get('PULL_REQUEST_BASE')
|
base_override = os.environ.get("PULL_REQUEST_BASE")
|
||||||
|
|
||||||
# Set the base branch
|
# Set the base branch
|
||||||
if base_override is not None:
|
if base_override is not None:
|
||||||
base = base_override
|
base = base_override
|
||||||
print("Overriding the base with branch '%s'" % base)
|
print("Overriding the base with branch '%s'" % base)
|
||||||
checkout_branch(repo.git, True, base)
|
checkout_branch(repo.git, True, base)
|
||||||
elif github_ref.startswith('refs/pull/'):
|
elif github_ref.startswith("refs/pull/"):
|
||||||
# Check the PR is not raised from a fork of the repository
|
# Check the PR is not raised from a fork of the repository
|
||||||
head_repo = "{pull_request[head][repo][full_name]}".format(**event_data)
|
head_repo = "{pull_request[head][repo][full_name]}".format(**event_data)
|
||||||
if head_repo != github_repository:
|
if head_repo != github_repository:
|
||||||
print("::warning::Pull request was raised from a fork of the repository. " +
|
print(
|
||||||
"Limitations on forked repositories have been imposed by GitHub Actions. " +
|
"::warning::Pull request was raised from a fork of the repository. "
|
||||||
"Unable to continue. Exiting.")
|
+ "Limitations on forked repositories have been imposed by GitHub Actions. "
|
||||||
|
+ "Unable to continue. Exiting."
|
||||||
|
)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
# Switch to the merging branch instead of the merge commit
|
# Switch to the merging branch instead of the merge commit
|
||||||
base = os.environ['GITHUB_HEAD_REF']
|
base = os.environ["GITHUB_HEAD_REF"]
|
||||||
print(
|
print(
|
||||||
"Removing the merge commit by switching to the pull request head branch '%s'" %
|
"Removing the merge commit by switching to the pull request head branch '%s'"
|
||||||
base)
|
% base
|
||||||
|
)
|
||||||
checkout_branch(repo.git, True, base)
|
checkout_branch(repo.git, True, base)
|
||||||
else:
|
elif github_ref.startswith("refs/heads/"):
|
||||||
base = github_ref[11:]
|
base = github_ref[11:]
|
||||||
print("Currently checked out base assumed to be branch '%s'" % base)
|
print("Currently checked out base assumed to be branch '%s'" % base)
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
f"::warning::Currently checked out ref '{github_ref}' is not a valid base for a pull request. "
|
||||||
|
+ "Unable to continue. Exiting."
|
||||||
|
)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
# Skip if the current branch is a PR branch created by this action.
|
# Skip if the current branch is a PR branch created by this action.
|
||||||
# This may occur when using a PAT instead of GITHUB_TOKEN because
|
# This may occur when using a PAT instead of GITHUB_TOKEN because
|
||||||
@ -229,7 +288,7 @@ if base.startswith(branch_prefix):
|
|||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
# Fetch an optional environment variable to determine the branch suffix
|
# Fetch an optional environment variable to determine the branch suffix
|
||||||
branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash')
|
branch_suffix = os.getenv("BRANCH_SUFFIX", "short-commit-hash")
|
||||||
if branch_suffix == "short-commit-hash":
|
if branch_suffix == "short-commit-hash":
|
||||||
# Suffix with the short SHA1 hash
|
# Suffix with the short SHA1 hash
|
||||||
branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo))
|
branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo))
|
||||||
@ -243,9 +302,7 @@ elif branch_suffix == "none":
|
|||||||
# Fixed branch name
|
# Fixed branch name
|
||||||
branch = branch_prefix
|
branch = branch_prefix
|
||||||
else:
|
else:
|
||||||
print(
|
print("Branch suffix '%s' is not a valid value." % branch_suffix)
|
||||||
"Branch suffix '%s' is not a valid value." %
|
|
||||||
branch_suffix)
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Output head branch
|
# Output head branch
|
||||||
@ -255,19 +312,22 @@ print("Pull request branch to create/update set to '%s'" % branch)
|
|||||||
remote_exists = remote_branch_exists(repo, branch)
|
remote_exists = remote_branch_exists(repo, branch)
|
||||||
if remote_exists:
|
if remote_exists:
|
||||||
print(
|
print(
|
||||||
"Pull request branch '%s' already exists as remote branch 'origin/%s'" %
|
"Pull request branch '%s' already exists as remote branch 'origin/%s'"
|
||||||
(branch, branch))
|
% (branch, branch)
|
||||||
if branch_suffix == 'short-commit-hash':
|
)
|
||||||
|
if branch_suffix == "short-commit-hash":
|
||||||
# A remote branch already exists for the HEAD commit
|
# A remote branch already exists for the HEAD commit
|
||||||
print(
|
print(
|
||||||
"Pull request branch '%s' already exists for this commit. Skipping." %
|
"Pull request branch '%s' already exists for this commit. Skipping."
|
||||||
branch)
|
% branch
|
||||||
|
)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif branch_suffix in ['timestamp', 'random']:
|
elif branch_suffix in ["timestamp", "random"]:
|
||||||
# Generated branch name collision with an existing branch
|
# Generated branch name collision with an existing branch
|
||||||
print(
|
print(
|
||||||
"Pull request branch '%s' collided with a branch of the same name. Please re-run." %
|
"Pull request branch '%s' collided with a branch of the same name. Please re-run."
|
||||||
branch)
|
% branch
|
||||||
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Checkout branch
|
# Checkout branch
|
||||||
@ -275,19 +335,18 @@ checkout_branch(repo.git, remote_exists, branch)
|
|||||||
|
|
||||||
# Check if there are changes to pull request
|
# Check if there are changes to pull request
|
||||||
if remote_exists:
|
if remote_exists:
|
||||||
print("Checking for local working copy changes indicating a " +
|
print(
|
||||||
"diff with existing pull request branch 'origin/%s'" % branch)
|
"Checking for local working copy changes indicating a "
|
||||||
|
+ "diff with existing pull request branch 'origin/%s'" % branch
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print("Checking for local working copy changes indicating a " +
|
print(
|
||||||
"diff with base 'origin/%s'" % base)
|
"Checking for local working copy changes indicating a "
|
||||||
|
+ "diff with base 'origin/%s'" % base
|
||||||
|
)
|
||||||
|
|
||||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||||
print("Modified or untracked files detected.")
|
print("Modified or untracked files detected.")
|
||||||
process_event(
|
process_event(github_token, github_repository, repo, branch, base)
|
||||||
github_token,
|
|
||||||
github_repository,
|
|
||||||
repo,
|
|
||||||
branch,
|
|
||||||
base)
|
|
||||||
else:
|
else:
|
||||||
print("No modified or untracked files detected. Skipping.")
|
print("No modified or untracked files detected. Skipping.")
|
||||||
|
4
dist/src/requirements.txt
vendored
4
dist/src/requirements.txt
vendored
@ -1,2 +1,2 @@
|
|||||||
GitPython==3.0.4
|
GitPython==3.0.8
|
||||||
PyGithub==1.44.1
|
PyGithub==1.46
|
||||||
|
80
examples.md
80
examples.md
@ -282,11 +282,18 @@ jobs:
|
|||||||
|
|
||||||
### Filtering push events
|
### Filtering push events
|
||||||
|
|
||||||
For workflows using `on: push` you may want to ignore push events for tags and remotes.
|
For workflows using `on: push` you may want to ignore push events for tags and only execute for branches. Specifying `branches` causes only events on branches to trigger the workflow. The `'**'` wildcard will match any branch name.
|
||||||
These can be filtered out with the following `if` condition.
|
|
||||||
|
```yml
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- '**'
|
||||||
|
```
|
||||||
|
|
||||||
|
If you have a workflow that contains jobs to handle push events on branches as well as tags, you can make sure that the job where you use `create-pull-request` action only executes when `github.ref` is a branch by using an `if` condition as follows.
|
||||||
|
|
||||||
```yml
|
```yml
|
||||||
name: Create Pull Request
|
|
||||||
on: push
|
on: push
|
||||||
jobs:
|
jobs:
|
||||||
createPullRequest:
|
createPullRequest:
|
||||||
@ -295,6 +302,12 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
...
|
...
|
||||||
|
|
||||||
|
someOtherJob:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
### Dynamic configuration using variables
|
### Dynamic configuration using variables
|
||||||
@ -336,39 +349,42 @@ Alternatively, [`set-env`](https://help.github.com/en/github/automating-your-wor
|
|||||||
|
|
||||||
### Debugging GitHub Actions
|
### Debugging GitHub Actions
|
||||||
|
|
||||||
|
#### Runner Diagnostic Logging
|
||||||
|
|
||||||
|
[Runner diagnostic logging](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/managing-a-workflow-run#enabling-runner-diagnostic-logging) provides additional log files that contain information about how a runner is executing an action.
|
||||||
|
To enable runner diagnostic logging, set the secret `ACTIONS_RUNNER_DEBUG` to `true` in the repository that contains the workflow.
|
||||||
|
|
||||||
#### Step Debug Logging
|
#### Step Debug Logging
|
||||||
|
|
||||||
To enable [step debug logging](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/managing-a-workflow-run#enabling-step-debug-logging) set the secret `ACTIONS_STEP_DEBUG` to `true` in the repository that contains the workflow.
|
[Step debug logging](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/managing-a-workflow-run#enabling-step-debug-logging) increases the verbosity of a job's logs during and after a job's execution.
|
||||||
|
To enable step debug logging set the secret `ACTIONS_STEP_DEBUG` to `true` in the repository that contains the workflow.
|
||||||
|
|
||||||
#### Output Various Contexts
|
#### Output Various Contexts
|
||||||
|
|
||||||
```yml
|
```yml
|
||||||
- name: Dump event JSON
|
steps:
|
||||||
env:
|
- name: Dump GitHub context
|
||||||
EVENT_JSON_FILENAME: ${{ github.event_path }}
|
env:
|
||||||
run: cat "$EVENT_JSON_FILENAME"
|
GITHUB_CONTEXT: ${{ toJson(github) }}
|
||||||
- name: Dump GitHub context
|
run: echo "$GITHUB_CONTEXT"
|
||||||
env:
|
- name: Dump job context
|
||||||
GITHUB_CONTEXT: ${{ toJson(github) }}
|
env:
|
||||||
run: echo "$GITHUB_CONTEXT"
|
JOB_CONTEXT: ${{ toJson(job) }}
|
||||||
- name: Dump job context
|
run: echo "$JOB_CONTEXT"
|
||||||
env:
|
- name: Dump steps context
|
||||||
JOB_CONTEXT: ${{ toJson(job) }}
|
env:
|
||||||
run: echo "$JOB_CONTEXT"
|
STEPS_CONTEXT: ${{ toJson(steps) }}
|
||||||
- name: Dump steps context
|
run: echo "$STEPS_CONTEXT"
|
||||||
env:
|
- name: Dump runner context
|
||||||
STEPS_CONTEXT: ${{ toJson(steps) }}
|
env:
|
||||||
run: echo "$STEPS_CONTEXT"
|
RUNNER_CONTEXT: ${{ toJson(runner) }}
|
||||||
- name: Dump runner context
|
run: echo "$RUNNER_CONTEXT"
|
||||||
env:
|
- name: Dump strategy context
|
||||||
RUNNER_CONTEXT: ${{ toJson(runner) }}
|
env:
|
||||||
run: echo "$RUNNER_CONTEXT"
|
STRATEGY_CONTEXT: ${{ toJson(strategy) }}
|
||||||
- name: Dump strategy context
|
run: echo "$STRATEGY_CONTEXT"
|
||||||
env:
|
- name: Dump matrix context
|
||||||
STRATEGY_CONTEXT: ${{ toJson(strategy) }}
|
env:
|
||||||
run: echo "$STRATEGY_CONTEXT"
|
MATRIX_CONTEXT: ${{ toJson(matrix) }}
|
||||||
- name: Dump matrix context
|
run: echo "$MATRIX_CONTEXT"
|
||||||
env:
|
|
||||||
MATRIX_CONTEXT: ${{ toJson(matrix) }}
|
|
||||||
run: echo "$MATRIX_CONTEXT"
|
|
||||||
```
|
```
|
||||||
|
14
index.js
14
index.js
@ -10,7 +10,7 @@ async function run() {
|
|||||||
core.debug(`src: ${src}`);
|
core.debug(`src: ${src}`);
|
||||||
|
|
||||||
// Setup Python from the tool cache
|
// Setup Python from the tool cache
|
||||||
setupPython("3.8.0", "x64");
|
setupPython("3.8.x", "x64");
|
||||||
|
|
||||||
// Install requirements
|
// Install requirements
|
||||||
await exec.exec("pip", [
|
await exec.exec("pip", [
|
||||||
@ -23,8 +23,10 @@ async function run() {
|
|||||||
const inputs = {
|
const inputs = {
|
||||||
token: core.getInput("token"),
|
token: core.getInput("token"),
|
||||||
commitMessage: core.getInput("commit-message"),
|
commitMessage: core.getInput("commit-message"),
|
||||||
commitAuthorEmail: core.getInput("author-email"),
|
|
||||||
commitAuthorName: core.getInput("author-name"),
|
commitAuthorName: core.getInput("author-name"),
|
||||||
|
commitAuthorEmail: core.getInput("author-email"),
|
||||||
|
committerName: core.getInput("committer-name"),
|
||||||
|
committerEmail: core.getInput("committer-email"),
|
||||||
title: core.getInput("title"),
|
title: core.getInput("title"),
|
||||||
body: core.getInput("body"),
|
body: core.getInput("body"),
|
||||||
labels: core.getInput("labels"),
|
labels: core.getInput("labels"),
|
||||||
@ -32,6 +34,8 @@ async function run() {
|
|||||||
reviewers: core.getInput("reviewers"),
|
reviewers: core.getInput("reviewers"),
|
||||||
teamReviewers: core.getInput("team-reviewers"),
|
teamReviewers: core.getInput("team-reviewers"),
|
||||||
milestone: core.getInput("milestone"),
|
milestone: core.getInput("milestone"),
|
||||||
|
project: core.getInput("project"),
|
||||||
|
projectColumn: core.getInput("project-column"),
|
||||||
branch: core.getInput("branch"),
|
branch: core.getInput("branch"),
|
||||||
base: core.getInput("base"),
|
base: core.getInput("base"),
|
||||||
branchSuffix: core.getInput("branch-suffix"),
|
branchSuffix: core.getInput("branch-suffix"),
|
||||||
@ -42,8 +46,10 @@ async function run() {
|
|||||||
// Set environment variables from inputs.
|
// Set environment variables from inputs.
|
||||||
if (inputs.token) process.env.GITHUB_TOKEN = inputs.token;
|
if (inputs.token) process.env.GITHUB_TOKEN = inputs.token;
|
||||||
if (inputs.commitMessage) process.env.COMMIT_MESSAGE = inputs.commitMessage;
|
if (inputs.commitMessage) process.env.COMMIT_MESSAGE = inputs.commitMessage;
|
||||||
if (inputs.commitAuthorEmail) process.env.COMMIT_AUTHOR_EMAIL = inputs.commitAuthorEmail;
|
|
||||||
if (inputs.commitAuthorName) process.env.COMMIT_AUTHOR_NAME = inputs.commitAuthorName;
|
if (inputs.commitAuthorName) process.env.COMMIT_AUTHOR_NAME = inputs.commitAuthorName;
|
||||||
|
if (inputs.commitAuthorEmail) process.env.COMMIT_AUTHOR_EMAIL = inputs.commitAuthorEmail;
|
||||||
|
if (inputs.committerName) process.env.COMMITTER_NAME = inputs.committerName;
|
||||||
|
if (inputs.committerEmail) process.env.COMMITTER_EMAIL = inputs.committerEmail;
|
||||||
if (inputs.title) process.env.PULL_REQUEST_TITLE = inputs.title;
|
if (inputs.title) process.env.PULL_REQUEST_TITLE = inputs.title;
|
||||||
if (inputs.body) process.env.PULL_REQUEST_BODY = inputs.body;
|
if (inputs.body) process.env.PULL_REQUEST_BODY = inputs.body;
|
||||||
if (inputs.labels) process.env.PULL_REQUEST_LABELS = inputs.labels;
|
if (inputs.labels) process.env.PULL_REQUEST_LABELS = inputs.labels;
|
||||||
@ -51,6 +57,8 @@ async function run() {
|
|||||||
if (inputs.reviewers) process.env.PULL_REQUEST_REVIEWERS = inputs.reviewers;
|
if (inputs.reviewers) process.env.PULL_REQUEST_REVIEWERS = inputs.reviewers;
|
||||||
if (inputs.teamReviewers) process.env.PULL_REQUEST_TEAM_REVIEWERS = inputs.teamReviewers;
|
if (inputs.teamReviewers) process.env.PULL_REQUEST_TEAM_REVIEWERS = inputs.teamReviewers;
|
||||||
if (inputs.milestone) process.env.PULL_REQUEST_MILESTONE = inputs.milestone;
|
if (inputs.milestone) process.env.PULL_REQUEST_MILESTONE = inputs.milestone;
|
||||||
|
if (inputs.project) process.env.PROJECT_NAME = inputs.project;
|
||||||
|
if (inputs.projectColumn) process.env.PROJECT_COLUMN_NAME = inputs.projectColumn;
|
||||||
if (inputs.branch) process.env.PULL_REQUEST_BRANCH = inputs.branch;
|
if (inputs.branch) process.env.PULL_REQUEST_BRANCH = inputs.branch;
|
||||||
if (inputs.base) process.env.PULL_REQUEST_BASE = inputs.base;
|
if (inputs.base) process.env.PULL_REQUEST_BASE = inputs.base;
|
||||||
if (inputs.branchSuffix) process.env.BRANCH_SUFFIX = inputs.branchSuffix;
|
if (inputs.branchSuffix) process.env.BRANCH_SUFFIX = inputs.branchSuffix;
|
||||||
|
2
package-lock.json
generated
2
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "create-pull-request",
|
"name": "create-pull-request",
|
||||||
"version": "1.7.0",
|
"version": "1.8.0",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "create-pull-request",
|
"name": "create-pull-request",
|
||||||
"version": "1.7.3",
|
"version": "1.9.1",
|
||||||
"description": "Creates a pull request for changes to your repository in the actions workspace",
|
"description": "Creates a pull request for changes to your repository in the actions workspace",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "ncc build index.js -o dist"
|
"package": "ncc build index.js -o dist"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
''' Create Pull Request '''
|
""" Create Pull Request """
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
@ -13,18 +13,18 @@ from github import Github, GithubException
|
|||||||
def get_github_event(github_event_path):
|
def get_github_event(github_event_path):
|
||||||
with open(github_event_path) as f:
|
with open(github_event_path) as f:
|
||||||
github_event = json.load(f)
|
github_event = json.load(f)
|
||||||
if bool(os.environ.get('DEBUG_EVENT')):
|
if bool(os.environ.get("DEBUG_EVENT")):
|
||||||
print(os.environ['GITHUB_EVENT_NAME'])
|
print(os.environ["GITHUB_EVENT_NAME"])
|
||||||
print(json.dumps(github_event, sort_keys=True, indent=2))
|
print(json.dumps(github_event, sort_keys=True, indent=2))
|
||||||
return github_event
|
return github_event
|
||||||
|
|
||||||
|
|
||||||
def get_head_short_sha1(repo):
|
def get_head_short_sha1(repo):
|
||||||
return repo.git.rev_parse('--short', 'HEAD')
|
return repo.git.rev_parse("--short", "HEAD")
|
||||||
|
|
||||||
|
|
||||||
def get_random_suffix(size=7, chars=string.ascii_lowercase + string.digits):
|
def get_random_suffix(size=7, chars=string.ascii_lowercase + string.digits):
|
||||||
return ''.join(random.choice(chars) for _ in range(size))
|
return "".join(random.choice(chars) for _ in range(size))
|
||||||
|
|
||||||
|
|
||||||
def remote_branch_exists(repo, branch):
|
def remote_branch_exists(repo, branch):
|
||||||
@ -39,106 +39,132 @@ def get_author_default(event_name, event_data):
|
|||||||
email = "{head_commit[author][email]}".format(**event_data)
|
email = "{head_commit[author][email]}".format(**event_data)
|
||||||
name = "{head_commit[author][name]}".format(**event_data)
|
name = "{head_commit[author][name]}".format(**event_data)
|
||||||
else:
|
else:
|
||||||
email = os.environ['GITHUB_ACTOR'] + '@users.noreply.github.com'
|
email = os.environ["GITHUB_ACTOR"] + "@users.noreply.github.com"
|
||||||
name = os.environ['GITHUB_ACTOR']
|
name = os.environ["GITHUB_ACTOR"]
|
||||||
return email, name
|
return email, name
|
||||||
|
|
||||||
|
|
||||||
def set_git_config(git, email, name):
|
def get_repo_url(token, github_repository):
|
||||||
print("Configuring git user as '%s <%s>'" % (name, email))
|
return "https://x-access-token:%s@github.com/%s" % (token, github_repository)
|
||||||
git.config('--global', 'user.email', '"%s"' % email)
|
|
||||||
git.config('--global', 'user.name', '"%s"' % name)
|
|
||||||
|
|
||||||
|
|
||||||
def set_git_remote_url(git, token, github_repository):
|
|
||||||
git.remote(
|
|
||||||
'set-url', 'origin', "https://x-access-token:%s@github.com/%s" %
|
|
||||||
(token, github_repository))
|
|
||||||
|
|
||||||
|
|
||||||
def checkout_branch(git, remote_exists, branch):
|
def checkout_branch(git, remote_exists, branch):
|
||||||
if remote_exists:
|
if remote_exists:
|
||||||
print("Checking out branch '%s'" % branch)
|
print("Checking out branch '%s'" % branch)
|
||||||
git.stash('--include-untracked')
|
git.stash("--include-untracked")
|
||||||
git.checkout(branch)
|
git.checkout(branch)
|
||||||
try:
|
try:
|
||||||
git.stash('pop')
|
git.stash("pop")
|
||||||
except BaseException:
|
except BaseException:
|
||||||
git.checkout('--theirs', '.')
|
git.checkout("--theirs", ".")
|
||||||
git.reset()
|
git.reset()
|
||||||
else:
|
else:
|
||||||
print("Creating new branch '%s'" % branch)
|
print("Creating new branch '%s'" % branch)
|
||||||
git.checkout('HEAD', b=branch)
|
git.checkout("HEAD", b=branch)
|
||||||
|
|
||||||
|
|
||||||
def push_changes(git, branch, commit_message):
|
def push_changes(git, token, github_repository, branch, commit_message):
|
||||||
git.add('-A')
|
git.add("-A")
|
||||||
git.commit(m=commit_message)
|
git.commit(m=commit_message)
|
||||||
return git.push('-f', '--set-upstream', 'origin', branch)
|
repo_url = get_repo_url(token, github_repository)
|
||||||
|
return git.push("-f", repo_url, f"HEAD:refs/heads/{branch}")
|
||||||
|
|
||||||
|
|
||||||
def cs_string_to_list(str):
|
def cs_string_to_list(str):
|
||||||
# Split the comma separated string into a list
|
# Split the comma separated string into a list
|
||||||
l = [i.strip() for i in str.split(',')]
|
l = [i.strip() for i in str.split(",")]
|
||||||
# Remove empty strings
|
# Remove empty strings
|
||||||
return list(filter(None, l))
|
return list(filter(None, l))
|
||||||
|
|
||||||
|
|
||||||
|
def create_project_card(github_repo, project_name, project_column_name, pull_request):
|
||||||
|
# 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("::warning::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("::warning::Project column not found. Unable to create project card.")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Create a project card for the pull request
|
||||||
|
column.create_card(content_id=pull_request.id, content_type="PullRequest")
|
||||||
|
print(
|
||||||
|
"Added pull request #%d to project '%s' under column '%s'"
|
||||||
|
% (pull_request.number, project.name, column.name)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def process_event(github_token, github_repository, repo, branch, base):
|
def process_event(github_token, github_repository, repo, branch, base):
|
||||||
# Fetch optional environment variables with default values
|
# Fetch optional environment variables with default values
|
||||||
commit_message = os.getenv(
|
commit_message = os.getenv(
|
||||||
'COMMIT_MESSAGE',
|
"COMMIT_MESSAGE", "Auto-committed changes by create-pull-request action"
|
||||||
"Auto-committed changes by create-pull-request action")
|
)
|
||||||
title = os.getenv(
|
title = os.getenv(
|
||||||
'PULL_REQUEST_TITLE',
|
"PULL_REQUEST_TITLE", "Auto-generated by create-pull-request action"
|
||||||
"Auto-generated by create-pull-request action")
|
)
|
||||||
body = os.getenv(
|
body = os.getenv(
|
||||||
'PULL_REQUEST_BODY', "Auto-generated pull request by "
|
"PULL_REQUEST_BODY",
|
||||||
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action")
|
"Auto-generated pull request by "
|
||||||
|
"[create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub Action",
|
||||||
|
)
|
||||||
# Fetch optional environment variables with no default values
|
# Fetch optional environment variables with no default values
|
||||||
pull_request_labels = os.environ.get('PULL_REQUEST_LABELS')
|
pull_request_labels = os.environ.get("PULL_REQUEST_LABELS")
|
||||||
pull_request_assignees = os.environ.get('PULL_REQUEST_ASSIGNEES')
|
pull_request_assignees = os.environ.get("PULL_REQUEST_ASSIGNEES")
|
||||||
pull_request_milestone = os.environ.get('PULL_REQUEST_MILESTONE')
|
pull_request_milestone = os.environ.get("PULL_REQUEST_MILESTONE")
|
||||||
pull_request_reviewers = os.environ.get('PULL_REQUEST_REVIEWERS')
|
pull_request_reviewers = os.environ.get("PULL_REQUEST_REVIEWERS")
|
||||||
pull_request_team_reviewers = os.environ.get('PULL_REQUEST_TEAM_REVIEWERS')
|
pull_request_team_reviewers = os.environ.get("PULL_REQUEST_TEAM_REVIEWERS")
|
||||||
|
project_name = os.environ.get("PROJECT_NAME")
|
||||||
|
project_column_name = os.environ.get("PROJECT_COLUMN_NAME")
|
||||||
|
|
||||||
# Push the local changes to the remote branch
|
# Push the local changes to the remote branch
|
||||||
print("Pushing changes to 'origin/%s'" % branch)
|
print("Pushing changes to 'origin/%s'" % branch)
|
||||||
push_result = push_changes(repo.git, branch, commit_message)
|
push_result = push_changes(
|
||||||
|
repo.git, github_token, github_repository, branch, commit_message
|
||||||
|
)
|
||||||
print(push_result)
|
print(push_result)
|
||||||
|
|
||||||
# Create the pull request
|
# Create the pull request
|
||||||
github_repo = Github(github_token).get_repo(github_repository)
|
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,
|
title=title, body=body, base=base, head=branch
|
||||||
body=body,
|
)
|
||||||
base=base,
|
print(
|
||||||
head=branch)
|
"Created pull request #%d (%s => %s)" % (pull_request.number, branch, base)
|
||||||
print("Created pull request #%d (%s => %s)" %
|
)
|
||||||
(pull_request.number, branch, base))
|
|
||||||
except GithubException as e:
|
except GithubException as e:
|
||||||
if e.status == 422:
|
if e.status == 422:
|
||||||
# Format the branch name
|
# Format the branch name
|
||||||
head_branch = "%s:%s" % (github_repository.split("/")[0], branch)
|
head_branch = "%s:%s" % (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',
|
state="open", base=base, head=head_branch
|
||||||
base=base,
|
)[0]
|
||||||
head=head_branch)[0]
|
print(
|
||||||
print("Updated pull request #%d (%s => %s)" %
|
"Updated pull request #%d (%s => %s)"
|
||||||
(pull_request.number, branch, base))
|
% (pull_request.number, branch, base)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Set the output variables
|
# Set the output variables
|
||||||
os.system(
|
os.system("echo ::set-env name=PULL_REQUEST_NUMBER::%d" % pull_request.number)
|
||||||
'echo ::set-env name=PULL_REQUEST_NUMBER::%d' %
|
os.system("echo ::set-output name=pr_number::%d" % pull_request.number)
|
||||||
pull_request.number)
|
|
||||||
os.system(
|
|
||||||
'echo ::set-output name=pr_number::%d' %
|
|
||||||
pull_request.number)
|
|
||||||
|
|
||||||
# Set labels, assignees and milestone
|
# Set labels, assignees and milestone
|
||||||
if pull_request_labels is not None:
|
if pull_request_labels is not None:
|
||||||
@ -146,7 +172,9 @@ def process_event(github_token, github_repository, repo, branch, base):
|
|||||||
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
||||||
if pull_request_assignees is not None:
|
if pull_request_assignees is not None:
|
||||||
print("Applying assignees '%s'" % pull_request_assignees)
|
print("Applying assignees '%s'" % pull_request_assignees)
|
||||||
pull_request.as_issue().edit(assignees=cs_string_to_list(pull_request_assignees))
|
pull_request.as_issue().edit(
|
||||||
|
assignees=cs_string_to_list(pull_request_assignees)
|
||||||
|
)
|
||||||
if pull_request_milestone is not None:
|
if pull_request_milestone is not None:
|
||||||
print("Applying milestone '%s'" % pull_request_milestone)
|
print("Applying milestone '%s'" % pull_request_milestone)
|
||||||
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
||||||
@ -157,7 +185,8 @@ def process_event(github_token, github_repository, repo, branch, base):
|
|||||||
print("Requesting reviewers '%s'" % pull_request_reviewers)
|
print("Requesting reviewers '%s'" % pull_request_reviewers)
|
||||||
try:
|
try:
|
||||||
pull_request.create_review_request(
|
pull_request.create_review_request(
|
||||||
reviewers=cs_string_to_list(pull_request_reviewers))
|
reviewers=cs_string_to_list(pull_request_reviewers)
|
||||||
|
)
|
||||||
except GithubException as e:
|
except GithubException as e:
|
||||||
# Likely caused by "Review cannot be requested from pull request
|
# Likely caused by "Review cannot be requested from pull request
|
||||||
# author."
|
# author."
|
||||||
@ -168,58 +197,88 @@ def process_event(github_token, github_repository, repo, branch, base):
|
|||||||
if pull_request_team_reviewers is not None:
|
if pull_request_team_reviewers is not None:
|
||||||
print("Requesting team reviewers '%s'" % pull_request_team_reviewers)
|
print("Requesting team reviewers '%s'" % pull_request_team_reviewers)
|
||||||
pull_request.create_review_request(
|
pull_request.create_review_request(
|
||||||
team_reviewers=cs_string_to_list(pull_request_team_reviewers))
|
team_reviewers=cs_string_to_list(pull_request_team_reviewers)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create a project card for the pull request
|
||||||
|
if project_name is not None and project_column_name is not None:
|
||||||
|
try:
|
||||||
|
create_project_card(
|
||||||
|
github_repo, project_name, project_column_name, pull_request
|
||||||
|
)
|
||||||
|
except GithubException as e:
|
||||||
|
# Likely caused by "Project already has the associated issue."
|
||||||
|
if e.status == 422:
|
||||||
|
print(
|
||||||
|
"Create project card failed - %s" % e.data["errors"][0]["message"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
# Fetch environment variables
|
# Fetch environment variables
|
||||||
github_token = os.environ['GITHUB_TOKEN']
|
github_token = os.environ["GITHUB_TOKEN"]
|
||||||
github_repository = os.environ['GITHUB_REPOSITORY']
|
github_repository = os.environ["GITHUB_REPOSITORY"]
|
||||||
github_ref = os.environ['GITHUB_REF']
|
github_ref = os.environ["GITHUB_REF"]
|
||||||
event_name = os.environ['GITHUB_EVENT_NAME']
|
event_name = os.environ["GITHUB_EVENT_NAME"]
|
||||||
# Get the JSON event data
|
# Get the JSON event data
|
||||||
event_data = get_github_event(os.environ['GITHUB_EVENT_PATH'])
|
event_data = get_github_event(os.environ["GITHUB_EVENT_PATH"])
|
||||||
|
|
||||||
|
# Get the default for author email and name
|
||||||
|
author_email, author_name = get_author_default(event_name, event_data)
|
||||||
|
# Set author name and email overrides
|
||||||
|
author_name = os.getenv("COMMIT_AUTHOR_NAME", author_name)
|
||||||
|
author_email = os.getenv("COMMIT_AUTHOR_EMAIL", author_email)
|
||||||
|
# Set committer name and email overrides
|
||||||
|
committer_name = os.getenv("COMMITTER_NAME", author_name)
|
||||||
|
committer_email = os.getenv("COMMITTER_EMAIL", author_email)
|
||||||
|
|
||||||
# Set the repo to the working directory
|
# Set the repo to the working directory
|
||||||
repo = Repo(os.getcwd())
|
repo = Repo(os.getcwd())
|
||||||
# Get the default for author email and name
|
# Set git environment. This will not persist after the action completes.
|
||||||
author_email, author_name = get_author_default(event_name, event_data)
|
print("Configuring git author as '%s <%s>'" % (author_name, author_email))
|
||||||
# Set commit author overrides
|
print("Configuring git committer as '%s <%s>'" % (committer_name, committer_email))
|
||||||
author_email = os.getenv('COMMIT_AUTHOR_EMAIL', author_email)
|
repo.git.update_environment(
|
||||||
author_name = os.getenv('COMMIT_AUTHOR_NAME', author_name)
|
GIT_AUTHOR_NAME=author_name,
|
||||||
# Set git configuration
|
GIT_AUTHOR_EMAIL=author_email,
|
||||||
set_git_config(repo.git, author_email, author_name)
|
GIT_COMMITTER_NAME=committer_name,
|
||||||
# Update URL for the 'origin' remote
|
GIT_COMMITTER_EMAIL=committer_email,
|
||||||
set_git_remote_url(repo.git, github_token, github_repository)
|
)
|
||||||
|
|
||||||
# Fetch/Set the branch name
|
# Fetch/Set the branch name
|
||||||
branch_prefix = os.getenv(
|
branch_prefix = os.getenv("PULL_REQUEST_BRANCH", "create-pull-request/patch")
|
||||||
'PULL_REQUEST_BRANCH',
|
|
||||||
'create-pull-request/patch')
|
|
||||||
# Fetch an optional base branch override
|
# Fetch an optional base branch override
|
||||||
base_override = os.environ.get('PULL_REQUEST_BASE')
|
base_override = os.environ.get("PULL_REQUEST_BASE")
|
||||||
|
|
||||||
# Set the base branch
|
# Set the base branch
|
||||||
if base_override is not None:
|
if base_override is not None:
|
||||||
base = base_override
|
base = base_override
|
||||||
print("Overriding the base with branch '%s'" % base)
|
print("Overriding the base with branch '%s'" % base)
|
||||||
checkout_branch(repo.git, True, base)
|
checkout_branch(repo.git, True, base)
|
||||||
elif github_ref.startswith('refs/pull/'):
|
elif github_ref.startswith("refs/pull/"):
|
||||||
# Check the PR is not raised from a fork of the repository
|
# Check the PR is not raised from a fork of the repository
|
||||||
head_repo = "{pull_request[head][repo][full_name]}".format(**event_data)
|
head_repo = "{pull_request[head][repo][full_name]}".format(**event_data)
|
||||||
if head_repo != github_repository:
|
if head_repo != github_repository:
|
||||||
print("::warning::Pull request was raised from a fork of the repository. " +
|
print(
|
||||||
"Limitations on forked repositories have been imposed by GitHub Actions. " +
|
"::warning::Pull request was raised from a fork of the repository. "
|
||||||
"Unable to continue. Exiting.")
|
+ "Limitations on forked repositories have been imposed by GitHub Actions. "
|
||||||
|
+ "Unable to continue. Exiting."
|
||||||
|
)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
# Switch to the merging branch instead of the merge commit
|
# Switch to the merging branch instead of the merge commit
|
||||||
base = os.environ['GITHUB_HEAD_REF']
|
base = os.environ["GITHUB_HEAD_REF"]
|
||||||
print(
|
print(
|
||||||
"Removing the merge commit by switching to the pull request head branch '%s'" %
|
"Removing the merge commit by switching to the pull request head branch '%s'"
|
||||||
base)
|
% base
|
||||||
|
)
|
||||||
checkout_branch(repo.git, True, base)
|
checkout_branch(repo.git, True, base)
|
||||||
else:
|
elif github_ref.startswith("refs/heads/"):
|
||||||
base = github_ref[11:]
|
base = github_ref[11:]
|
||||||
print("Currently checked out base assumed to be branch '%s'" % base)
|
print("Currently checked out base assumed to be branch '%s'" % base)
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
f"::warning::Currently checked out ref '{github_ref}' is not a valid base for a pull request. "
|
||||||
|
+ "Unable to continue. Exiting."
|
||||||
|
)
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
# Skip if the current branch is a PR branch created by this action.
|
# Skip if the current branch is a PR branch created by this action.
|
||||||
# This may occur when using a PAT instead of GITHUB_TOKEN because
|
# This may occur when using a PAT instead of GITHUB_TOKEN because
|
||||||
@ -229,7 +288,7 @@ if base.startswith(branch_prefix):
|
|||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
# Fetch an optional environment variable to determine the branch suffix
|
# Fetch an optional environment variable to determine the branch suffix
|
||||||
branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash')
|
branch_suffix = os.getenv("BRANCH_SUFFIX", "short-commit-hash")
|
||||||
if branch_suffix == "short-commit-hash":
|
if branch_suffix == "short-commit-hash":
|
||||||
# Suffix with the short SHA1 hash
|
# Suffix with the short SHA1 hash
|
||||||
branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo))
|
branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo))
|
||||||
@ -243,9 +302,7 @@ elif branch_suffix == "none":
|
|||||||
# Fixed branch name
|
# Fixed branch name
|
||||||
branch = branch_prefix
|
branch = branch_prefix
|
||||||
else:
|
else:
|
||||||
print(
|
print("Branch suffix '%s' is not a valid value." % branch_suffix)
|
||||||
"Branch suffix '%s' is not a valid value." %
|
|
||||||
branch_suffix)
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Output head branch
|
# Output head branch
|
||||||
@ -255,19 +312,22 @@ print("Pull request branch to create/update set to '%s'" % branch)
|
|||||||
remote_exists = remote_branch_exists(repo, branch)
|
remote_exists = remote_branch_exists(repo, branch)
|
||||||
if remote_exists:
|
if remote_exists:
|
||||||
print(
|
print(
|
||||||
"Pull request branch '%s' already exists as remote branch 'origin/%s'" %
|
"Pull request branch '%s' already exists as remote branch 'origin/%s'"
|
||||||
(branch, branch))
|
% (branch, branch)
|
||||||
if branch_suffix == 'short-commit-hash':
|
)
|
||||||
|
if branch_suffix == "short-commit-hash":
|
||||||
# A remote branch already exists for the HEAD commit
|
# A remote branch already exists for the HEAD commit
|
||||||
print(
|
print(
|
||||||
"Pull request branch '%s' already exists for this commit. Skipping." %
|
"Pull request branch '%s' already exists for this commit. Skipping."
|
||||||
branch)
|
% branch
|
||||||
|
)
|
||||||
sys.exit()
|
sys.exit()
|
||||||
elif branch_suffix in ['timestamp', 'random']:
|
elif branch_suffix in ["timestamp", "random"]:
|
||||||
# Generated branch name collision with an existing branch
|
# Generated branch name collision with an existing branch
|
||||||
print(
|
print(
|
||||||
"Pull request branch '%s' collided with a branch of the same name. Please re-run." %
|
"Pull request branch '%s' collided with a branch of the same name. Please re-run."
|
||||||
branch)
|
% branch
|
||||||
|
)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Checkout branch
|
# Checkout branch
|
||||||
@ -275,19 +335,18 @@ checkout_branch(repo.git, remote_exists, branch)
|
|||||||
|
|
||||||
# Check if there are changes to pull request
|
# Check if there are changes to pull request
|
||||||
if remote_exists:
|
if remote_exists:
|
||||||
print("Checking for local working copy changes indicating a " +
|
print(
|
||||||
"diff with existing pull request branch 'origin/%s'" % branch)
|
"Checking for local working copy changes indicating a "
|
||||||
|
+ "diff with existing pull request branch 'origin/%s'" % branch
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print("Checking for local working copy changes indicating a " +
|
print(
|
||||||
"diff with base 'origin/%s'" % base)
|
"Checking for local working copy changes indicating a "
|
||||||
|
+ "diff with base 'origin/%s'" % base
|
||||||
|
)
|
||||||
|
|
||||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||||
print("Modified or untracked files detected.")
|
print("Modified or untracked files detected.")
|
||||||
process_event(
|
process_event(github_token, github_repository, repo, branch, base)
|
||||||
github_token,
|
|
||||||
github_repository,
|
|
||||||
repo,
|
|
||||||
branch,
|
|
||||||
base)
|
|
||||||
else:
|
else:
|
||||||
print("No modified or untracked files detected. Skipping.")
|
print("No modified or untracked files detected. Skipping.")
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
GitPython==3.0.4
|
GitPython==3.0.8
|
||||||
PyGithub==1.44.1
|
PyGithub==1.46
|
||||||
|
Reference in New Issue
Block a user