Skip to content

Commit

Permalink
feat: now a PR is used to merge pizza CLI changes (#3)
Browse files Browse the repository at this point in the history
* feat: now a PR is used to merge pizza CLI changes

* docs: updated docs to reflect PRs being used to merge changes now

* now the action uses the OpenSauced bot account

* docs: typo fix

* updated PR script with PR feedback

* docs: fixed a typo in a filename
  • Loading branch information
nickytonline authored Sep 3, 2024
1 parent 84329a9 commit d2a1dcf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
uses: open-sauced/pizza-action@v1.0.0
with:
# optional and false by default
commit-and-push: "true"
commit-and-pr: "true"
```
We suggest you add this to a workflow file in the `.github/workflows` directory of your repository and call it something like `pizza-action.yml`.
Expand All @@ -42,6 +42,12 @@ The pizza CLI's "generate codeowners ./" command requires a full repository hist

Arguments to pass to the pizza CLI. Default is `generate codeowners ./`.

### `commit-and-push`
### `commit-and-pr`

Whether to commit and push the changes made by the pizza-cli. Default is `false`.
Whether to commit the changes made by the pizza-cli and to create a pull request for the changes. Default is `false`.

## Troubleshooting

One common isssue is the following error, `pull request create failed: GraphQL: GitHub Actions is not permitted to create or approve pull requests (createPullRequest)`. To fix this, check _Allow GitHub Actions to create and approve pull requests_ in the repository settings under the actions section.

![GitHub Actions section of a repository's settings](repository-settings.png)
24 changes: 16 additions & 8 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ inputs:
description: "OpenSauced Pizza CLI command to run"
default: "generate codeowners ./"
required: true
commit-and-push:
commit-and-pr:
description: "Commit and push changes"
default: "false"
required: false
Expand Down Expand Up @@ -43,13 +43,21 @@ runs:
shell: bash
working-directory: ${{ github.workspace }}

- name: Commit and push changes
if: ${{ inputs.commit-and-push == 'true' }}
- name: Make script executable
run: chmod +x ${{ github.action_path }}/scripts/create-pr.sh
shell: bash

- name: Setup git config
run: |
git config user.name github-actions
git config user.email github-actions@github.com
git add .
git commit -m "Auto-commit from GitHub Actions"
git push
git config user.name 'open-sauced[bot]'
git config user.email '63161813+open-sauced[bot]@users.noreply.github.com'
shell: bash
working-directory: ${{ github.workspace }}

- name: Create a PR with pizza CLI changes
env:
GH_TOKEN: ${{ github.token }}
if: ${{ inputs.commit-and-pr == 'true' }}
run: ${{ github.action_path }}/scripts/create-pr.sh
shell: bash
working-directory: ${{ github.workspace }}
Binary file added repository-settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions scripts/create-pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

PR_TITLE="chore (automated): OpenSauced updates"
BRANCH_NAME="chore_automated_open-sauced-updates_$(date +%s)"

git checkout -b "$BRANCH_NAME"

# There are potentially multiple files to add from the pizza CLI output
git add .

# Check if there are any changes to commit
if [[ -n "$(git status --porcelain)" ]]; then
echo "Creating PR \"$PR_TITLE\" for branch "$BRANCH_NAME""
git commit -m "$PR_TITLE"
git push origin "$BRANCH_NAME"

# Attempt to create the PR (dry run)
if gh pr create --title "$PR_TITLE" --body "This is an automated PR generated by the OpenSauced pizza-action." --dry-run; then
# If dry run is successful, create the actual PR
PR_URL=$(gh pr create --title "$PR_TITLE" --body "This is an automated PR generated by the OpenSauced pizza-action.")
echo "PR created successfully: $PR_URL"
else
echo "Failed to create PR. There might be an issue with branch permissions or other repository settings."
fi
else
# Shouldn't end up here, but log that there was nothing to sync
echo "Looks like there was nothing to update."
fi

0 comments on commit d2a1dcf

Please sign in to comment.