Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
4c95214a9b | |||
b37c0a038c | |||
d9841567d1 | |||
2cce94bfb0 | |||
d968e8b11b | |||
6c73093f9b | |||
d17c882ef3 | |||
d5f4e48a66 | |||
484d8bd85d | |||
74b6a9337d | |||
2dd62d446e | |||
b6a98c049d | |||
67e8822279 | |||
956a240181 | |||
179aca65d7 | |||
e4c8b68e33 |
5
.github/workflows/dockerhub-description.yml
vendored
5
.github/workflows/dockerhub-description.yml
vendored
@ -2,7 +2,10 @@ name: Update Docker Hub Description
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- master
|
||||
paths:
|
||||
- README.md
|
||||
- .github/workflows/dockerhub-description.yml
|
||||
jobs:
|
||||
dockerHubDescription:
|
||||
runs-on: ubuntu-latest
|
||||
|
10
README.md
10
README.md
@ -19,11 +19,13 @@ See [examples](examples.md) for detailed use cases.
|
||||
|
||||
```yml
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
```
|
||||
|
||||
You can also pin to a [specific release](https://github.com/peter-evans/create-pull-request/releases) version in the format `@v1.x.x`
|
||||
|
||||
**Note**: If you want pull requests created by this action to trigger an `on: pull_request` workflow then you must use a [Personal Access Token](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) instead of the default `GITHUB_TOKEN`.
|
||||
See [this issue](https://github.com/peter-evans/create-pull-request/issues/48) for further details.
|
||||
|
||||
@ -44,7 +46,7 @@ These inputs are *all optional*. If not set, sensible default values will be use
|
||||
| `team-reviewers` | A comma separated list of GitHub teams to request a review from. | none |
|
||||
| `milestone` | The number of the milestone to associate this pull request with. | none |
|
||||
| `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` |
|
||||
|
||||
**Outputs**
|
||||
@ -55,7 +57,7 @@ Note that in order to read the step output the action step must have an id.
|
||||
```yml
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
- name: Check outputs
|
||||
@ -105,7 +107,7 @@ jobs:
|
||||
run: date +%s > report.txt
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Add report file
|
||||
|
@ -27,7 +27,7 @@ inputs:
|
||||
branch:
|
||||
description: 'The pull request branch name.'
|
||||
base:
|
||||
description: 'Overrides the base branch.'
|
||||
description: 'Sets the pull request base branch.'
|
||||
branch-suffix:
|
||||
description: 'The branch suffix type.'
|
||||
outputs:
|
||||
|
74
dist/src/create-pull-request.py
vendored
74
dist/src/create-pull-request.py
vendored
@ -7,8 +7,7 @@ import string
|
||||
import sys
|
||||
import time
|
||||
from git import Repo
|
||||
from github import Github
|
||||
from github import GithubException
|
||||
from github import Github, GithubException
|
||||
|
||||
|
||||
def get_github_event(github_event_path):
|
||||
@ -46,6 +45,7 @@ def get_author_default(event_name, event_data):
|
||||
|
||||
|
||||
def set_git_config(git, email, name):
|
||||
print("Configuring git user as '%s <%s>'" % (name, email))
|
||||
git.config('--global', 'user.email', '"%s"' % email)
|
||||
git.config('--global', 'user.name', '"%s"' % name)
|
||||
|
||||
@ -58,6 +58,7 @@ def set_git_remote_url(git, token, github_repository):
|
||||
|
||||
def checkout_branch(git, remote_exists, branch):
|
||||
if remote_exists:
|
||||
print("Checking out branch '%s'" % branch)
|
||||
git.stash('--include-untracked')
|
||||
git.checkout(branch)
|
||||
try:
|
||||
@ -66,6 +67,7 @@ def checkout_branch(git, remote_exists, branch):
|
||||
git.checkout('--theirs', '.')
|
||||
git.reset()
|
||||
else:
|
||||
print("Creating new branch '%s'" % branch)
|
||||
git.checkout('HEAD', b=branch)
|
||||
|
||||
|
||||
@ -101,7 +103,7 @@ def process_event(github_token, github_repository, repo, branch, base):
|
||||
pull_request_team_reviewers = os.environ.get('PULL_REQUEST_TEAM_REVIEWERS')
|
||||
|
||||
# Push the local changes to the remote branch
|
||||
print("Pushing changes.")
|
||||
print("Pushing changes to 'origin/%s'" % branch)
|
||||
push_result = push_changes(repo.git, branch, commit_message)
|
||||
print(push_result)
|
||||
|
||||
@ -114,18 +116,18 @@ def process_event(github_token, github_repository, repo, branch, base):
|
||||
base=base,
|
||||
head=branch)
|
||||
print("Created pull request #%d (%s => %s)" %
|
||||
(pull_request.number, branch, base))
|
||||
(pull_request.number, branch, base))
|
||||
except GithubException as e:
|
||||
if e.status == 422:
|
||||
# Format the branch name
|
||||
head_branch = "%s:%s" % (github_repository.split("/")[0], branch)
|
||||
# Get the pull request
|
||||
pull_request = github_repo.get_pulls(
|
||||
state='open',
|
||||
state='open',
|
||||
base=base,
|
||||
head=head_branch)[0]
|
||||
print("Updated pull request #%d (%s => %s)" %
|
||||
(pull_request.number, branch, base))
|
||||
(pull_request.number, branch, base))
|
||||
else:
|
||||
print(str(e))
|
||||
sys.exit(1)
|
||||
@ -140,30 +142,31 @@ def process_event(github_token, github_repository, repo, branch, base):
|
||||
|
||||
# Set labels, assignees and milestone
|
||||
if pull_request_labels is not None:
|
||||
print("Applying labels")
|
||||
print("Applying labels '%s'" % pull_request_labels)
|
||||
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
||||
if pull_request_assignees is not None:
|
||||
print("Applying assignees")
|
||||
print("Applying assignees '%s'" % pull_request_assignees)
|
||||
pull_request.as_issue().edit(assignees=cs_string_to_list(pull_request_assignees))
|
||||
if pull_request_milestone is not None:
|
||||
print("Applying milestone")
|
||||
print("Applying milestone '%s'" % pull_request_milestone)
|
||||
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
||||
pull_request.as_issue().edit(milestone=milestone)
|
||||
|
||||
# Set pull request reviewers
|
||||
if pull_request_reviewers is not None:
|
||||
print("Requesting reviewers")
|
||||
print("Requesting reviewers '%s'" % pull_request_reviewers)
|
||||
try:
|
||||
pull_request.create_review_request(
|
||||
reviewers=cs_string_to_list(pull_request_reviewers))
|
||||
except GithubException as e:
|
||||
# Likely caused by "Review cannot be requested from pull request author."
|
||||
# Likely caused by "Review cannot be requested from pull request
|
||||
# author."
|
||||
if e.status == 422:
|
||||
print("Requesting reviewers failed - %s" % e.data["message"])
|
||||
|
||||
# Set pull request team reviewers
|
||||
if pull_request_team_reviewers is not None:
|
||||
print("Requesting team reviewers")
|
||||
print("Requesting team reviewers '%s'" % pull_request_team_reviewers)
|
||||
pull_request.create_review_request(
|
||||
team_reviewers=cs_string_to_list(pull_request_team_reviewers))
|
||||
|
||||
@ -198,16 +201,33 @@ base_override = os.environ.get('PULL_REQUEST_BASE')
|
||||
# Set the base branch
|
||||
if base_override is not None:
|
||||
base = base_override
|
||||
print("Overriding the base with branch '%s'" % base)
|
||||
checkout_branch(repo.git, True, base)
|
||||
elif github_ref.startswith('refs/pull/'):
|
||||
# Check the PR is not raised from a fork of the repository
|
||||
head_repo = "{pull_request[head][repo][full_name]}".format(**event_data)
|
||||
if head_repo != github_repository:
|
||||
print("::warning::Pull request was raised from a fork of the repository. " +
|
||||
"Limitations on forked repositories have been imposed by GitHub Actions. " +
|
||||
"Unable to continue. Exiting.")
|
||||
sys.exit()
|
||||
# Switch to the merging branch instead of the merge commit
|
||||
base = os.environ['GITHUB_HEAD_REF']
|
||||
repo.git.checkout(base)
|
||||
else:
|
||||
print(
|
||||
"Removing the merge commit by switching to the pull request head branch '%s'" %
|
||||
base)
|
||||
checkout_branch(repo.git, True, base)
|
||||
elif github_ref.startswith('refs/heads/'):
|
||||
base = github_ref[11:]
|
||||
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.
|
||||
# This may occur when using a PAT instead of GITHUB_TOKEN.
|
||||
# This may occur when using a PAT instead of GITHUB_TOKEN because
|
||||
# a PAT allows workflow actions to trigger further events.
|
||||
if base.startswith(branch_prefix):
|
||||
print("Branch '%s' was created by this action. Skipping." % base)
|
||||
sys.exit()
|
||||
@ -232,10 +252,15 @@ else:
|
||||
branch_suffix)
|
||||
sys.exit(1)
|
||||
|
||||
# Check if the remote branch exists
|
||||
remote_exists = remote_branch_exists(repo, branch)
|
||||
# Output head branch
|
||||
print("Pull request branch to create/update set to '%s'" % branch)
|
||||
|
||||
# Check if the determined head branch exists as a remote
|
||||
remote_exists = remote_branch_exists(repo, branch)
|
||||
if remote_exists:
|
||||
print(
|
||||
"Pull request branch '%s' already exists as remote branch 'origin/%s'" %
|
||||
(branch, branch))
|
||||
if branch_suffix == 'short-commit-hash':
|
||||
# A remote branch already exists for the HEAD commit
|
||||
print(
|
||||
@ -243,9 +268,9 @@ if remote_exists:
|
||||
branch)
|
||||
sys.exit()
|
||||
elif branch_suffix in ['timestamp', 'random']:
|
||||
# Generated branch name clash with an existing branch
|
||||
# Generated branch name collision with an existing branch
|
||||
print(
|
||||
"Pull request branch '%s' already exists. Please re-run." %
|
||||
"Pull request branch '%s' collided with a branch of the same name. Please re-run." %
|
||||
branch)
|
||||
sys.exit(1)
|
||||
|
||||
@ -253,8 +278,15 @@ if remote_exists:
|
||||
checkout_branch(repo.git, remote_exists, branch)
|
||||
|
||||
# Check if there are changes to pull request
|
||||
if remote_exists:
|
||||
print("Checking for local working copy changes indicating a " +
|
||||
"diff with existing pull request branch 'origin/%s'" % branch)
|
||||
else:
|
||||
print("Checking for local working copy changes indicating a " +
|
||||
"diff with base 'origin/%s'" % base)
|
||||
|
||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||
print("Repository has modified or untracked files.")
|
||||
print("Modified or untracked files detected.")
|
||||
process_event(
|
||||
github_token,
|
||||
github_repository,
|
||||
@ -262,4 +294,4 @@ if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||
branch,
|
||||
base)
|
||||
else:
|
||||
print("Repository has no modified or untracked files. Skipping.")
|
||||
print("No modified or untracked files detected. Skipping.")
|
||||
|
2
dist/src/requirements.txt
vendored
2
dist/src/requirements.txt
vendored
@ -1,2 +1,2 @@
|
||||
GitPython==3.0.4
|
||||
GitPython==3.0.5
|
||||
PyGithub==1.44.1
|
||||
|
93
examples.md
93
examples.md
@ -3,6 +3,7 @@
|
||||
- [Use case: Create a pull request to update X periodically](#use-case-create-a-pull-request-to-update-x-periodically)
|
||||
- [Update NPM dependencies](#update-npm-dependencies)
|
||||
- [Keep Go up to date](#keep-go-up-to-date)
|
||||
- [Update SwaggerUI for GitHub Pages](#update-swaggerui-for-github-pages)
|
||||
- [Spider and download a website](#spider-and-download-a-website)
|
||||
- [Use case: Create a pull request to update X by calling the GitHub API](#use-case-create-a-pull-request-to-update-x-by-calling-the-github-api)
|
||||
- [Call the GitHub API from an external service](#call-the-github-api-from-an-external-service)
|
||||
@ -41,7 +42,7 @@ jobs:
|
||||
ncu -u
|
||||
npm install
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: update dependencies
|
||||
@ -75,7 +76,7 @@ jobs:
|
||||
- run: echo "##[set-output name=pr_title;]update to latest Go release ${{ steps.ensure_go.outputs.go_version}}"
|
||||
id: pr_title_maker
|
||||
- name: Create pull request
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
title: ${{ steps.pr_title_maker.outputs.pr_title }}
|
||||
@ -85,8 +86,69 @@ jobs:
|
||||
branch: ensure-latest-go/patch-${{ steps.ensure_go.outputs.go_version }}
|
||||
```
|
||||
|
||||
### Update SwaggerUI for GitHub Pages
|
||||
|
||||
When using [GitHub Pages to host Swagger documentation](https://github.com/peter-evans/swagger-github-pages), this workflow updates the repository with the latest distribution of [SwaggerUI](https://github.com/swagger-api/swagger-ui).
|
||||
|
||||
You must create a file called `swagger-ui.version` at the root of your repository before running.
|
||||
```yml
|
||||
name: Update Swagger UI
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 10 * * *'
|
||||
jobs:
|
||||
updateSwagger:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v1
|
||||
- name: Get Latest Swagger UI Release
|
||||
id: swagger-ui
|
||||
run: |
|
||||
echo ::set-output name=release_tag::$(curl -sL https://api.github.com/repos/swagger-api/swagger-ui/releases/latest | jq -r ".tag_name")
|
||||
echo ::set-output name=current_tag::$(<swagger-ui.version)
|
||||
- name: Update Swagger UI
|
||||
if: steps.swagger-ui.outputs.current_tag != steps.swagger-ui.outputs.release_tag
|
||||
env:
|
||||
RELEASE_TAG: ${{ steps.swagger-ui.outputs.release_tag }}
|
||||
SWAGGER_YAML: "swagger.yaml"
|
||||
run: |
|
||||
# Delete the dist directory and index.html
|
||||
rm -fr dist index.html
|
||||
# Download the release
|
||||
curl -sL -o $RELEASE_TAG https://api.github.com/repos/swagger-api/swagger-ui/tarball/$RELEASE_TAG
|
||||
# Extract the dist directory
|
||||
tar -xzf $RELEASE_TAG --strip-components=1 $(tar -tzf $RELEASE_TAG | head -1 | cut -f1 -d"/")/dist
|
||||
rm $RELEASE_TAG
|
||||
# Move index.html to the root
|
||||
mv dist/index.html .
|
||||
# Fix references in index.html
|
||||
sed -i "s|https://petstore.swagger.io/v2/swagger.json|$SWAGGER_YAML|g" index.html
|
||||
sed -i "s|href=\"./|href=\"dist/|g" index.html
|
||||
sed -i "s|src=\"./|src=\"dist/|g" index.html
|
||||
# Update current release
|
||||
echo ${{ steps.swagger-ui.outputs.release_tag }} > swagger-ui.version
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: Update swagger-ui to ${{ steps.swagger-ui.outputs.release_tag }}
|
||||
title: Update SwaggerUI to ${{ steps.swagger-ui.outputs.release_tag }}
|
||||
body: |
|
||||
Updates [swagger-ui][1] to ${{ steps.swagger-ui.outputs.release_tag }}
|
||||
|
||||
Auto-generated by [create-pull-request][2]
|
||||
|
||||
[1]: https://github.com/swagger-api/swagger-ui
|
||||
[2]: https://github.com/peter-evans/create-pull-request
|
||||
labels: dependencies, automated pr
|
||||
branch: swagger-ui-updates
|
||||
branch-suffix: none
|
||||
```
|
||||
|
||||
### Spider and download a website
|
||||
|
||||
This workflow spiders a website and downloads the content. Any changes to the website will be raised in a pull request.
|
||||
|
||||
```yml
|
||||
name: Download Website
|
||||
on:
|
||||
@ -110,7 +172,7 @@ jobs:
|
||||
--domains quotes.toscrape.com \
|
||||
http://quotes.toscrape.com/
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: update local website copy
|
||||
@ -169,7 +231,7 @@ An `on: repository_dispatch` workflow can be triggered from another workflow wit
|
||||
|
||||
This is a pattern that works well for any automated code linting and fixing. A pull request can be created to fix or modify something during an `on: pull_request` workflow. The pull request containing the fix will be raised with the original pull request as the base. This can be then be merged to update the original pull request and pass any required tests.
|
||||
|
||||
Note that due to [limitations on forked repositories](https://help.github.com/en/github/automating-your-workflow-with-github-actions/virtual-environments-for-github-actions#token-permissions) workflows for this use case do not work for pull requests raised from forks.
|
||||
Note that due to [limitations on forked repositories](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#permissions-for-the-github_token) workflows for this use case do not work for pull requests raised from forks.
|
||||
|
||||
### autopep8
|
||||
|
||||
@ -202,7 +264,7 @@ jobs:
|
||||
run: echo ::set-output name=branch-name::"autopep8-patches/$GITHUB_HEAD_REF"
|
||||
- name: Create Pull Request
|
||||
if: steps.autopep8.outputs.exit-code == 2
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
commit-message: autopep8 action fixes
|
||||
@ -249,7 +311,7 @@ The recommended method is to use [`set-output`](https://help.github.com/en/githu
|
||||
echo ::set-output name=pr_body::"This PR was auto-generated on $(date +%d-%m-%Y) \
|
||||
by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
title: ${{ steps.vars.outputs.pr_title }}
|
||||
@ -265,7 +327,7 @@ Alternatively, [`set-env`](https://help.github.com/en/github/automating-your-wor
|
||||
echo ::set-env name=PULL_REQUEST_BODY::"This PR was auto-generated on $(date +%d-%m-%Y) \
|
||||
by [create-pull-request](https://github.com/peter-evans/create-pull-request)."
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@v1.7.2
|
||||
uses: peter-evans/create-pull-request@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
title: ${{ env.PULL_REQUEST_TITLE }}
|
||||
@ -274,15 +336,20 @@ Alternatively, [`set-env`](https://help.github.com/en/github/automating-your-wor
|
||||
|
||||
### Debugging GitHub Actions
|
||||
|
||||
**Step Debug Logging** : To enable step debug logging set the secret `ACTIONS_STEP_DEBUG` to `true` in the repository that contains the workflow.
|
||||
#### Runner Diagnostic Logging
|
||||
|
||||
**Output Various Contexts**
|
||||
[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](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
|
||||
|
||||
```yml
|
||||
- name: Dump event JSON
|
||||
env:
|
||||
EVENT_JSON_FILENAME: ${{ github.event_path }}
|
||||
run: cat "$EVENT_JSON_FILENAME"
|
||||
steps:
|
||||
- name: Dump GitHub context
|
||||
env:
|
||||
GITHUB_CONTEXT: ${{ toJson(github) }}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "create-pull-request",
|
||||
"version": "1.7.2",
|
||||
"version": "1.7.4",
|
||||
"description": "Creates a pull request for changes to your repository in the actions workspace",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
|
@ -7,8 +7,7 @@ import string
|
||||
import sys
|
||||
import time
|
||||
from git import Repo
|
||||
from github import Github
|
||||
from github import GithubException
|
||||
from github import Github, GithubException
|
||||
|
||||
|
||||
def get_github_event(github_event_path):
|
||||
@ -46,6 +45,7 @@ def get_author_default(event_name, event_data):
|
||||
|
||||
|
||||
def set_git_config(git, email, name):
|
||||
print("Configuring git user as '%s <%s>'" % (name, email))
|
||||
git.config('--global', 'user.email', '"%s"' % email)
|
||||
git.config('--global', 'user.name', '"%s"' % name)
|
||||
|
||||
@ -58,6 +58,7 @@ def set_git_remote_url(git, token, github_repository):
|
||||
|
||||
def checkout_branch(git, remote_exists, branch):
|
||||
if remote_exists:
|
||||
print("Checking out branch '%s'" % branch)
|
||||
git.stash('--include-untracked')
|
||||
git.checkout(branch)
|
||||
try:
|
||||
@ -66,6 +67,7 @@ def checkout_branch(git, remote_exists, branch):
|
||||
git.checkout('--theirs', '.')
|
||||
git.reset()
|
||||
else:
|
||||
print("Creating new branch '%s'" % branch)
|
||||
git.checkout('HEAD', b=branch)
|
||||
|
||||
|
||||
@ -101,7 +103,7 @@ def process_event(github_token, github_repository, repo, branch, base):
|
||||
pull_request_team_reviewers = os.environ.get('PULL_REQUEST_TEAM_REVIEWERS')
|
||||
|
||||
# Push the local changes to the remote branch
|
||||
print("Pushing changes.")
|
||||
print("Pushing changes to 'origin/%s'" % branch)
|
||||
push_result = push_changes(repo.git, branch, commit_message)
|
||||
print(push_result)
|
||||
|
||||
@ -114,18 +116,18 @@ def process_event(github_token, github_repository, repo, branch, base):
|
||||
base=base,
|
||||
head=branch)
|
||||
print("Created pull request #%d (%s => %s)" %
|
||||
(pull_request.number, branch, base))
|
||||
(pull_request.number, branch, base))
|
||||
except GithubException as e:
|
||||
if e.status == 422:
|
||||
# Format the branch name
|
||||
head_branch = "%s:%s" % (github_repository.split("/")[0], branch)
|
||||
# Get the pull request
|
||||
pull_request = github_repo.get_pulls(
|
||||
state='open',
|
||||
state='open',
|
||||
base=base,
|
||||
head=head_branch)[0]
|
||||
print("Updated pull request #%d (%s => %s)" %
|
||||
(pull_request.number, branch, base))
|
||||
(pull_request.number, branch, base))
|
||||
else:
|
||||
print(str(e))
|
||||
sys.exit(1)
|
||||
@ -140,30 +142,31 @@ def process_event(github_token, github_repository, repo, branch, base):
|
||||
|
||||
# Set labels, assignees and milestone
|
||||
if pull_request_labels is not None:
|
||||
print("Applying labels")
|
||||
print("Applying labels '%s'" % pull_request_labels)
|
||||
pull_request.as_issue().edit(labels=cs_string_to_list(pull_request_labels))
|
||||
if pull_request_assignees is not None:
|
||||
print("Applying assignees")
|
||||
print("Applying assignees '%s'" % pull_request_assignees)
|
||||
pull_request.as_issue().edit(assignees=cs_string_to_list(pull_request_assignees))
|
||||
if pull_request_milestone is not None:
|
||||
print("Applying milestone")
|
||||
print("Applying milestone '%s'" % pull_request_milestone)
|
||||
milestone = github_repo.get_milestone(int(pull_request_milestone))
|
||||
pull_request.as_issue().edit(milestone=milestone)
|
||||
|
||||
# Set pull request reviewers
|
||||
if pull_request_reviewers is not None:
|
||||
print("Requesting reviewers")
|
||||
print("Requesting reviewers '%s'" % pull_request_reviewers)
|
||||
try:
|
||||
pull_request.create_review_request(
|
||||
reviewers=cs_string_to_list(pull_request_reviewers))
|
||||
except GithubException as e:
|
||||
# Likely caused by "Review cannot be requested from pull request author."
|
||||
# Likely caused by "Review cannot be requested from pull request
|
||||
# author."
|
||||
if e.status == 422:
|
||||
print("Requesting reviewers failed - %s" % e.data["message"])
|
||||
|
||||
# Set pull request team reviewers
|
||||
if pull_request_team_reviewers is not None:
|
||||
print("Requesting team reviewers")
|
||||
print("Requesting team reviewers '%s'" % pull_request_team_reviewers)
|
||||
pull_request.create_review_request(
|
||||
team_reviewers=cs_string_to_list(pull_request_team_reviewers))
|
||||
|
||||
@ -198,16 +201,33 @@ base_override = os.environ.get('PULL_REQUEST_BASE')
|
||||
# Set the base branch
|
||||
if base_override is not None:
|
||||
base = base_override
|
||||
print("Overriding the base with branch '%s'" % base)
|
||||
checkout_branch(repo.git, True, base)
|
||||
elif github_ref.startswith('refs/pull/'):
|
||||
# Check the PR is not raised from a fork of the repository
|
||||
head_repo = "{pull_request[head][repo][full_name]}".format(**event_data)
|
||||
if head_repo != github_repository:
|
||||
print("::warning::Pull request was raised from a fork of the repository. " +
|
||||
"Limitations on forked repositories have been imposed by GitHub Actions. " +
|
||||
"Unable to continue. Exiting.")
|
||||
sys.exit()
|
||||
# Switch to the merging branch instead of the merge commit
|
||||
base = os.environ['GITHUB_HEAD_REF']
|
||||
repo.git.checkout(base)
|
||||
else:
|
||||
print(
|
||||
"Removing the merge commit by switching to the pull request head branch '%s'" %
|
||||
base)
|
||||
checkout_branch(repo.git, True, base)
|
||||
elif github_ref.startswith('refs/heads/'):
|
||||
base = github_ref[11:]
|
||||
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.
|
||||
# This may occur when using a PAT instead of GITHUB_TOKEN.
|
||||
# This may occur when using a PAT instead of GITHUB_TOKEN because
|
||||
# a PAT allows workflow actions to trigger further events.
|
||||
if base.startswith(branch_prefix):
|
||||
print("Branch '%s' was created by this action. Skipping." % base)
|
||||
sys.exit()
|
||||
@ -232,10 +252,15 @@ else:
|
||||
branch_suffix)
|
||||
sys.exit(1)
|
||||
|
||||
# Check if the remote branch exists
|
||||
remote_exists = remote_branch_exists(repo, branch)
|
||||
# Output head branch
|
||||
print("Pull request branch to create/update set to '%s'" % branch)
|
||||
|
||||
# Check if the determined head branch exists as a remote
|
||||
remote_exists = remote_branch_exists(repo, branch)
|
||||
if remote_exists:
|
||||
print(
|
||||
"Pull request branch '%s' already exists as remote branch 'origin/%s'" %
|
||||
(branch, branch))
|
||||
if branch_suffix == 'short-commit-hash':
|
||||
# A remote branch already exists for the HEAD commit
|
||||
print(
|
||||
@ -243,9 +268,9 @@ if remote_exists:
|
||||
branch)
|
||||
sys.exit()
|
||||
elif branch_suffix in ['timestamp', 'random']:
|
||||
# Generated branch name clash with an existing branch
|
||||
# Generated branch name collision with an existing branch
|
||||
print(
|
||||
"Pull request branch '%s' already exists. Please re-run." %
|
||||
"Pull request branch '%s' collided with a branch of the same name. Please re-run." %
|
||||
branch)
|
||||
sys.exit(1)
|
||||
|
||||
@ -253,8 +278,15 @@ if remote_exists:
|
||||
checkout_branch(repo.git, remote_exists, branch)
|
||||
|
||||
# Check if there are changes to pull request
|
||||
if remote_exists:
|
||||
print("Checking for local working copy changes indicating a " +
|
||||
"diff with existing pull request branch 'origin/%s'" % branch)
|
||||
else:
|
||||
print("Checking for local working copy changes indicating a " +
|
||||
"diff with base 'origin/%s'" % base)
|
||||
|
||||
if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||
print("Repository has modified or untracked files.")
|
||||
print("Modified or untracked files detected.")
|
||||
process_event(
|
||||
github_token,
|
||||
github_repository,
|
||||
@ -262,4 +294,4 @@ if repo.is_dirty() or len(repo.untracked_files) > 0:
|
||||
branch,
|
||||
base)
|
||||
else:
|
||||
print("Repository has no modified or untracked files. Skipping.")
|
||||
print("No modified or untracked files detected. Skipping.")
|
||||
|
@ -1,2 +1,2 @@
|
||||
GitPython==3.0.4
|
||||
GitPython==3.0.5
|
||||
PyGithub==1.44.1
|
||||
|
Reference in New Issue
Block a user