Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support labels copier #207

Merged
merged 18 commits into from
Jan 9, 2025
Merged

feat: support labels copier #207

merged 18 commits into from
Jan 9, 2025

Conversation

v1v
Copy link
Member

@v1v v1v commented Jan 8, 2025

@v1v v1v requested a review from a team January 8, 2025 15:28
@v1v v1v self-assigned this Jan 8, 2025
@v1v v1v added the changelog:feature When you add a new feature label Jan 8, 2025
Comment on lines 22 to 31
add_label() {
local label=$1
local pr_url=$2

if [ "$DRY_RUN" == "true" ]; then
echo ">> [dry-run]: $label will be added"
else
gh pr edit --add-label "$label" "$pr_url"
fi
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make it on Action Script in the workflow directly

      uses: actions/github-script@v7
      env:
        labels: ${{ inputs.labels }}
        issue: ${{ inputs.issue }}
      with:
        script: |
          const labels = process.env.labels.split(',')
          if (labels.length > 0) {
            github.rest.issues.addLabels({
                issue_number: process.env.issue,
                owner: context.repo.owner,
                repo: context.repo.repo,
                labels: labels,
              })
          }

Copy link
Member Author

@v1v v1v Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the shell script as I was able to run it locally and test it quickly. In your snippet, I need to add also support for the exclude labels .

What's the reason you want to use the type-script approach?

}


pr_number=$(gh pr view --json body -q ".body" "$PR_URL" | sed -n -e '/automatic backport of pull request/,/done/p' | cut -d"#" -f2 | cut -d" " -f1)
Copy link
Contributor

@kuisathaverat kuisathaverat Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other way to avoid parsing the body?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, unfortunately, the PR reference in Mergify is within the PR description.

See elastic/apm-server#15186

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code get the label of a PR

          const { data } = await github.rest.issues.listLabelsOnIssue({
            issue_number: process.env.issue,
            owner: context.repo.owner,
            repo: context.repo.repo,
          })

Comment on lines 35 to 46
gh pr view --json labels -q '.labels[]|.name' ${REPOSITORY_URL}/pull/$pr_number | while read label ; do
if [[ -z "$labels" ]] || [[ ",$labels," =~ ",$label," ]]; then
if [[ -n "$EXCLUDED_LABEL" ]] && [[ $label =~ $EXCLUDED_LABEL ]]; then
echo ">> $label is excluded and will not be added since matches '$EXCLUDED_LABEL'"
else
add_label "$label" "$PR_URL"
fi
fi
for additional_label in $(echo $ADDITIONAL_LABELS | sed "s/,/ /g") ; do
add_label "$additional_label" "$PR_URL"
done
done
Copy link
Contributor

@kuisathaverat kuisathaverat Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is complex. You can use Action script to add the labels directly, duplicated labels will not apply

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not that complex IMO. I can simplify it a bit.

And still, I see a much powerful feature to use a shell script, so I can test it out in isolation locally than using the github-script action step, it's hard to test it locally

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v1v v1v added this pull request to the merge queue Jan 9, 2025
Merged via the queue into main with commit a2467bf Jan 9, 2025
5 checks passed
@v1v v1v deleted the feature/labels-mergify branch January 9, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:feature When you add a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants