From b86bbce92496c9d8eaabb2dae55ca19a152b4cb3 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Mon, 21 Oct 2019 20:36:01 +0900 Subject: [PATCH 1/4] Update README --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9ec8de7..037bd79 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Create Pull Request action will: Linux ```yml - name: Create Pull Request - uses: peter-evans/create-pull-request@v1.5.2 + uses: peter-evans/create-pull-request@v1.5.3 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` @@ -26,7 +26,7 @@ Linux Multi platform - Linux, MacOS, Windows (beta) ```yml - name: Create Pull Request - uses: peter-evans/create-pull-request@v1.5.2-multi + uses: peter-evans/create-pull-request@v1.5.3-multi env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` @@ -105,7 +105,7 @@ jobs: - name: Create report file run: date +%s > report.txt - name: Create Pull Request - uses: peter-evans/create-pull-request@v1.5.2 + uses: peter-evans/create-pull-request@v1.5.3 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} COMMIT_MESSAGE: Add report file @@ -157,7 +157,7 @@ jobs: ncu -u npm install - name: Create Pull Request - uses: peter-evans/create-pull-request@v1.5.2 + uses: peter-evans/create-pull-request@v1.5.3 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} COMMIT_MESSAGE: update dependencies @@ -201,7 +201,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.5.2 + uses: peter-evans/create-pull-request@v1.5.3 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} COMMIT_MESSAGE: autopep8 action fixes @@ -232,7 +232,7 @@ The recommended method is to use `set-output`. Note that the step where output v 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.5.2 + uses: peter-evans/create-pull-request@v1.5.3 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PULL_REQUEST_TITLE: ${{ steps.vars.outputs.pr_title }} @@ -248,7 +248,7 @@ Since the action reads environment variables from the system, it's technically n 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.5.2 + uses: peter-evans/create-pull-request@v1.5.3 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` From 0be4e396cf399d8e16fc1290e3ec6e7becb576ac Mon Sep 17 00:00:00 2001 From: Renovate Bot Date: Mon, 21 Oct 2019 17:57:06 +0000 Subject: [PATCH 2/4] Update alpine Docker tag to v3.10.3 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 420b0e8..111acb9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM alpine:3.10.2 +FROM alpine:3.10.3 LABEL maintainer="Peter Evans " LABEL repository="https://github.com/peter-evans/create-pull-request" From 6e7e889e01fb6d87b831d9e69ba8257e1818ef33 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Tue, 22 Oct 2019 10:55:33 +0900 Subject: [PATCH 3/4] Minor fixes and refactor --- create-pull-request.py | 122 ++++++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/create-pull-request.py b/create-pull-request.py index 8d68c27..688d2ec 100755 --- a/create-pull-request.py +++ b/create-pull-request.py @@ -21,15 +21,6 @@ def get_github_event(github_event_path): def ignore_event(event_name, event_data): if event_name == "push": - # Ignore push events on deleted branches - # The event we want to ignore occurs when a PR is created but the repository owner decides - # not to commit the changes. They close the PR and delete the branch. This creates a - # "push" event that we want to ignore, otherwise it will create another branch and PR on - # the same commit. - deleted = "{deleted}".format(**event_data) - if deleted == "True": - print("Ignoring delete branch event.") - return True ref = "{ref}".format(**event_data) if not ref.startswith('refs/heads/'): print("Ignoring events for tags and remotes.") @@ -128,7 +119,8 @@ def process_event(event_name, event_data, repo, branch, base, remote_exists): push_result = push_changes(repo.git, branch, commit_message) print(push_result) - # If the remote existed then a PR likely exists and we can finish here + # If the remote existed then we are using fixed branch strategy. + # A PR should already exist and we can finish here. if remote_exists: print("Updated pull request branch %s." % branch) sys.exit() @@ -177,52 +169,6 @@ skip_ignore_event = bool(os.environ.get('SKIP_IGNORE')) if skip_ignore_event or not ignore_event(event_name, event_data): # Set the repo to the working directory repo = Repo(os.getcwd()) - - # Fetch/Set the branch name - branch = os.getenv('PULL_REQUEST_BRANCH', 'create-pull-request/patch') - - # Set the base branch - github_ref = os.environ['GITHUB_REF'] - if github_ref.startswith('refs/pull/'): - # Switch to the merging branch instead of the merge commit - base = os.environ['GITHUB_HEAD_REF'] - else: - base = github_ref[11:] - - # Optional base override - base = os.getenv('PULL_REQUEST_BASE', base) - - # Checkout the base branch - repo.git.checkout(base) - - # Skip if the current branch is a PR branch created by this action - if base.startswith(branch): - print("Branch '%s' was created by this action. Skipping." % base) - sys.exit() - - # Fetch an optional environment variable to determine the branch suffix - branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash') - if branch_suffix == "short-commit-hash": - # Suffix with the short SHA1 hash - branch = "%s-%s" % (branch, get_head_short_sha1(repo)) - elif branch_suffix == "timestamp": - # Suffix with the current timestamp - branch = "%s-%s" % (branch, int(time.time())) - elif branch_suffix == "random": - # Suffix with the current timestamp - branch = "%s-%s" % (branch, get_random_suffix()) - - # Check if the remote branch exists - remote_exists = remote_branch_exists(repo, branch) - - # If using short commit hash prefixes, check if a remote - # branch already exists for this HEAD commit - if branch_suffix == 'short-commit-hash' and remote_exists: - print( - "Pull request branch '%s' already exists for this commit. Skipping." % - branch) - sys.exit() - # Get the default for author email and name author_email, author_name = get_author_default(event_name, event_data) # Set commit author overrides @@ -230,6 +176,70 @@ if skip_ignore_event or not ignore_event(event_name, event_data): author_name = os.getenv('COMMIT_AUTHOR_NAME', author_name) # Set git configuration set_git_config(repo.git, author_email, author_name) + + # Fetch/Set the branch name + branch_prefix = os.getenv( + 'PULL_REQUEST_BRANCH', + 'create-pull-request/patch') + # Fetch the git ref + github_ref = os.environ['GITHUB_REF'] + # Fetch an optional base branch override + base_override = os.environ.get('PULL_REQUEST_BASE') + + # Set the base branch + if base_override is not None: + base = base_override + checkout_branch(repo.git, True, base) + elif github_ref.startswith('refs/pull/'): + # Switch to the merging branch instead of the merge commit + base = os.environ['GITHUB_HEAD_REF'] + repo.git.checkout(base) + else: + base = github_ref[11:] + + # Skip if the current branch is a PR branch created by this action. + # This may occur when using a PAT instead of GITHUB_TOKEN. + if base.startswith(branch_prefix): + print("Branch '%s' was created by this action. Skipping." % base) + sys.exit() + + # Fetch an optional environment variable to determine the branch suffix + branch_suffix = os.getenv('BRANCH_SUFFIX', 'short-commit-hash') + if branch_suffix == "short-commit-hash": + # Suffix with the short SHA1 hash + branch = "%s-%s" % (branch_prefix, get_head_short_sha1(repo)) + elif branch_suffix == "timestamp": + # Suffix with the current timestamp + branch = "%s-%s" % (branch_prefix, int(time.time())) + elif branch_suffix == "random": + # Suffix with the current timestamp + branch = "%s-%s" % (branch_prefix, get_random_suffix()) + elif branch_suffix == "none": + # Fixed branch name + branch = branch_prefix + else: + print( + "Branch suffix '%s' is not a valid value." % + branch_suffix) + sys.exit(1) + + # Check if the remote branch exists + remote_exists = remote_branch_exists(repo, branch) + + if remote_exists: + if branch_suffix == 'short-commit-hash': + # A remote branch already exists for the HEAD commit + print( + "Pull request branch '%s' already exists for this commit. Skipping." % + branch) + sys.exit() + elif branch_suffix in ['timestamp', 'random']: + # Generated branch name clash with an existing branch + print( + "Pull request branch '%s' already exists. Please re-run." % + branch) + sys.exit(1) + # Checkout branch checkout_branch(repo.git, remote_exists, branch) From a2ed0052e5f92edab1ddab8824220b8e6a7b11c2 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Tue, 22 Oct 2019 11:01:54 +0900 Subject: [PATCH 4/4] Update image version --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index b5c5574..bc2ddab 100644 --- a/action.yml +++ b/action.yml @@ -3,7 +3,7 @@ author: 'Peter Evans' description: 'Creates a pull request for changes to your repository in the actions workspace' runs: using: 'docker' - image: 'docker://peterevans/create-pull-request:1.5.3' + image: 'docker://peterevans/create-pull-request:1.5.4' branding: icon: 'git-pull-request' color: 'gray-dark'