diff --git a/github/create-pull-request/README.md b/github/create-pull-request/README.md index 3b2aa86..311ceec 100644 --- a/github/create-pull-request/README.md +++ b/github/create-pull-request/README.md @@ -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`. @@ -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 ``` diff --git a/github/create-pull-request/rwx-ci-cd.template.yml b/github/create-pull-request/rwx-ci-cd.template.yml index 668a0e8..01c627c 100644 --- a/github/create-pull-request/rwx-ci-cd.template.yml +++ b/github/create-pull-request/rwx-ci-cd.template.yml @@ -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: @@ -32,6 +45,7 @@ touch "$file" - key: create-pr + after: cleanup-previous-run call: $LEAF_DIGEST use: code-change with: @@ -39,6 +53,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 - key: update-pr after: assert--create-pr @@ -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 @@ -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') @@ -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 }} @@ -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') @@ -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 }} diff --git a/github/create-pull-request/rwx-package.yml b/github/create-pull-request/rwx-package.yml index 6462ca4..1eef5ed 100644 --- a/github/create-pull-request/rwx-package.yml +++ b/github/create-pull-request/rwx-package.yml @@ -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 @@ -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] @@ -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) @@ -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 }}