Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion github/create-pull-request/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ with repository permissions for:
- Contents: read and write
- Pull Requests: read and write

To use the `labels` parameter, the app also needs:

- Issues: read and write (required to add labels)

## Instructions

- Clone a git repository using the [git/clone](https://www.rwx.com/docs/mint/leaves/git/clone) package. Remember to pass `preserve-git-dir: true`.
Expand Down Expand Up @@ -55,11 +59,12 @@ tasks:
run: rwx packages update | tee $RWX_VALUES/update-output

- key: create-pull-request
call: github/create-pull-request 1.0.3
call: github/create-pull-request 1.1.0
use: [update-packages]
with:
github-token: ${{ github-apps.your-orgs-bot.token }}
branch-prefix: rwx-packages-update
pull-request-title: Update RWX packages
pull-request-body: "```\n${{ tasks.update-packages.values.update-output }}\n```"
labels: dependencies,automated
```
35 changes: 33 additions & 2 deletions github/create-pull-request/rwx-ci-cd.template.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
- key: gh-cli
call: github/install-cli 1.0.8

- key: cleanup-previous-run
use: gh-cli
run: |
branch="${RUN_ID}-${RUN_ID}"
existing_pr=$(gh --repo rwx-cloud/mint-update-leaves-testing pr list --head "$branch" --json number --jq '.[0].number')
if [ -n "$existing_pr" ]; then
echo "Closing leftover PR #$existing_pr from previous failed attempt"
gh --repo rwx-cloud/mint-update-leaves-testing pr close "$existing_pr" --delete-branch || true
fi
env:
GITHUB_TOKEN: ${{ github-apps.rwx-cloud-bot.token }}
RUN_ID: ${{ run.id }}

- key: code
call: git/clone 2.0.0
with:
Expand Down Expand Up @@ -32,13 +45,15 @@
touch "$file"

- key: create-pr
after: cleanup-previous-run
call: $LEAF_DIGEST
use: code-change
with:
github-token: ${{ github-apps.rwx-cloud-bot.token }}
branch-prefix: ${{ run.id }}
pull-request-title: "pull request title ${{ tasks.code-change.values.nonce }}"
pull-request-body: "pull request body ${{ tasks.code-change.values.nonce }}"
labels: test-label

- key: update-pr
after: assert--create-pr
Expand All @@ -49,6 +64,7 @@
branch-prefix: ${{ run.id }}
pull-request-title: "pull request title ${{ tasks.code-change.values.nonce }}"
pull-request-body: "pull request body ${{ tasks.code-change.values.nonce }}"
labels: test-label-2

- key: clone-branch-after-create
call: git/clone 2.0.0
Expand All @@ -73,7 +89,7 @@
echo "Checking for added file"
test -f ${{ tasks.code-change.values.file }}

pr_details=$(gh --repo rwx-cloud/mint-update-leaves-testing pr view "$PR_NUMBER" --json body,title,commits)
pr_details=$(gh --repo rwx-cloud/mint-update-leaves-testing pr view "$PR_NUMBER" --json body,title,commits,labels)
echo "$pr_details"

pr_title=$(echo "$pr_details" | jq -r '.title')
Expand All @@ -87,6 +103,13 @@
echo "PR #${PR_NUMBER} has $commit_count commits, expected 1"
exit 1
fi

echo "Checking for labels"
has_test_label=$(echo "$pr_details" | jq '[.labels[].name] | contains(["test-label"])')
if [ "$has_test_label" != "true" ]; then
echo "PR #${PR_NUMBER} does not have expected label 'test-label'"
exit 1
fi
env:
GITHUB_TOKEN: ${{ github-apps.rwx-cloud-bot.token }}
PR_NUMBER: ${{ tasks.create-pr.values.pull-request-number }}
Expand All @@ -101,7 +124,7 @@
echo "Checking for file added in update"
test -f ${{ tasks.code-change2.values.file }}

pr_details=$(gh --repo rwx-cloud/mint-update-leaves-testing pr view "$PR_NUMBER" --json body,title,commits)
pr_details=$(gh --repo rwx-cloud/mint-update-leaves-testing pr view "$PR_NUMBER" --json body,title,commits,labels)
echo "$pr_details"

pr_title=$(echo "$pr_details" | jq -r '.title')
Expand All @@ -118,6 +141,14 @@

echo "Checking for updated PR"
test "${{ tasks.create-pr.values.pull-request-number }}" = "${{ tasks.update-pr.values.pull-request-number }}"

echo "Checking for labels after update"
has_labels=$(echo "$pr_details" | jq '[.labels[].name] | contains(["test-label", "test-label-2"])')
if [ "$has_labels" != "true" ]; then
echo "PR #${PR_NUMBER} does not have expected labels 'test-label' and 'test-label-2'"
echo "Labels found: $(echo "$pr_details" | jq '[.labels[].name]')"
exit 1
fi
env:
GITHUB_TOKEN: ${{ github-apps.rwx-cloud-bot.token }}
PR_NUMBER: ${{ tasks.create-pr.values.pull-request-number }}
Expand Down
26 changes: 23 additions & 3 deletions github/create-pull-request/rwx-package.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: github/create-pull-request
version: 1.0.3
version: 1.1.0
description: Creates a pull request
source_code_url: https://github.com/rwx-cloud/packages/tree/main/github/create-pull-request
issue_tracker_url: https://github.com/rwx-cloud/packages/issues
Expand All @@ -17,6 +17,9 @@ parameters:
pull-request-body:
description: "The body to use for the pull request"
required: true
labels:
description: "Comma-separated list of labels to add to the pull request"
required: false

outputs:
values-from: [create-or-update-pull-request]
Expand Down Expand Up @@ -59,10 +62,26 @@ tasks:
git checkout -b "$branch"
git push -f origin "$branch"

# Create labels if they don't exist
if [ -n "$LABELS" ]; then
echo "$LABELS" | tr ',' '\n' | while read -r label; do
label=$(echo "$label" | xargs) # trim whitespace
gh label create "$label" || true
done
fi

if [ "$need_to_create_pr" = "true" ]; then
gh pr create --title "$PULL_REQUEST_TITLE" --body "$PULL_REQUEST_BODY"
label_args=""
if [ -n "$LABELS" ]; then
label_args="--label $LABELS"
fi
gh pr create --title "$PULL_REQUEST_TITLE" --body "$PULL_REQUEST_BODY" $label_args
else
gh pr edit --title "$PULL_REQUEST_TITLE" --body "$PULL_REQUEST_BODY"
label_args=""
if [ -n "$LABELS" ]; then
label_args="--add-label $LABELS"
fi
gh pr edit --title "$PULL_REQUEST_TITLE" --body "$PULL_REQUEST_BODY" $label_args
fi

pr_details=$(gh pr view "$branch" --json number,url)
Expand All @@ -71,5 +90,6 @@ tasks:
env:
BRANCH_PREFIX: ${{ params.branch-prefix }}
GITHUB_TOKEN: ${{ params.github-token }}
LABELS: ${{ params.labels }}
PULL_REQUEST_TITLE: ${{ params.pull-request-title }}
PULL_REQUEST_BODY: ${{ params.pull-request-body }}