-
Notifications
You must be signed in to change notification settings - Fork 150
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
ci: automatically relate a backport PR to the backport issue #1278
Conversation
Signed-off-by: Yang Chiu <yang.chiu@suse.com>
WalkthroughThis pull request introduces a new GitHub Actions workflow named Changes
Assessment against linked issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (2)
.github/workflows/backport-pr.yml (2)
47-50
: Improve error handling in title encoding.The current implementation could benefit from better error handling and input validation.
Apply this diff to improve reliability:
- name: URL encode the original issue title if: env.BACKPORT == 'true' - run: echo "ORIGINAL_ISSUE_TITLE=$(node -e 'console.log(encodeURIComponent("${{ fromJson(steps.original-issue.outputs.data).title }}"))')" >> $GITHUB_ENV + run: | + ISSUE_TITLE='${{ fromJson(steps.original-issue.outputs.data).title }}' + if [ -z "${ISSUE_TITLE}" ]; then + echo "Error: Original issue title is empty" + exit 1 + fi + ENCODED_TITLE=$(node -e "try { console.log(encodeURIComponent('${ISSUE_TITLE}')); } catch (e) { console.error(e); process.exit(1); }") + echo "ORIGINAL_ISSUE_TITLE=${ENCODED_TITLE}" >> $GITHUB_ENV🧰 Tools
🪛 actionlint
49-49: shellcheck reported issue in this script: SC2086:info:1:138: Double quote to prevent globbing and word splitting
(shellcheck)
57-63
: Add validation before linking PR.The current implementation should validate the backport issue number before attempting to comment.
Apply this diff to improve reliability:
- name: Link the PR with the corresponding backport issue if: env.BACKPORT == 'true' run: | + if [ -z "${BACKPORT_ISSUE_NUMBER}" ] || [ "${BACKPORT_ISSUE_NUMBER}" = "null" ]; then + echo "Error: Invalid backport issue number" + exit 1 + fi # Relate the pull request to the backport issue gh issue comment --repo longhorn/longhorn ${{ env.BACKPORT_ISSUE_NUMBER }} --body "Related PR: ${{ github.event.pull_request.html_url }}" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
📒 Files selected for processing (1)
.github/workflows/backport-pr.yml
(1 hunks)
🧰 Additional context used
🪛 actionlint
.github/workflows/backport-pr.yml
18-18: shellcheck reported issue in this script: SC2086:info:2:27: Double quote to prevent globbing and word splitting
(shellcheck)
18-18: shellcheck reported issue in this script: SC2086:info:4:28: Double quote to prevent globbing and word splitting
(shellcheck)
18-18: "github.event.pull_request.title" is potentially untrusted. avoid using it directly in inline scripts. instead, pass it through an environment variable. see https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions for more details
(expression)
27-27: shellcheck reported issue in this script: SC2116:style:2:8: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'
(shellcheck)
27-27: shellcheck reported issue in this script: SC2001:style:4:10: See if you can use ${variable//search/replace} instead
(shellcheck)
27-27: shellcheck reported issue in this script: SC2086:info:5:26: Double quote to prevent globbing and word splitting
(shellcheck)
27-27: shellcheck reported issue in this script: SC2086:info:9:56: Double quote to prevent globbing and word splitting
(shellcheck)
27-27: "github.event.pull_request.body" is potentially untrusted. avoid using it directly in inline scripts. instead, pass it through an environment variable. see https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions for more details
(expression)
49-49: shellcheck reported issue in this script: SC2086:info:1:138: Double quote to prevent globbing and word splitting
(shellcheck)
53-53: shellcheck reported issue in this script: SC2086:info:2:56: Double quote to prevent globbing and word splitting
(shellcheck)
🔇 Additional comments (3)
.github/workflows/backport-pr.yml (3)
1-13
: LGTM! Workflow configuration is well-structured.
The workflow trigger configuration correctly targets PR open events on master and versioned branches, which aligns with the backport PR workflow requirements.
14-16
: LGTM! Repository checkout is properly configured.
Using the latest stable version (v4) of actions/checkout is appropriate for this workflow.
38-46
: LGTM! Issue fetching is properly implemented.
The use of octokit/request-action with proper authentication is secure and follows best practices.
- name: Check if PR is a backport | ||
run: | | ||
if [[ "${{ github.event.pull_request.title }}" =~ "backport #" ]]; then | ||
echo "BACKPORT=true" >> $GITHUB_ENV | ||
else | ||
echo "BACKPORT=false" >> $GITHUB_ENV | ||
fi | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Address security concerns in backport detection.
The current implementation has potential security risks and shell scripting issues:
- Direct use of untrusted input in shell script
- Missing quotes around variables
Apply this diff to improve security and reliability:
- name: Check if PR is a backport
run: |
- if [[ "${{ github.event.pull_request.title }}" =~ "backport #" ]]; then
+ PR_TITLE='${{ github.event.pull_request.title }}'
+ if [[ "${PR_TITLE}" =~ "backport #" ]]; then
echo "BACKPORT=true" >> $GITHUB_ENV
else
echo "BACKPORT=false" >> $GITHUB_ENV
fi
Committable suggestion skipped: line range outside the PR's diff.
🧰 Tools
🪛 actionlint
18-18: shellcheck reported issue in this script: SC2086:info:2:27: Double quote to prevent globbing and word splitting
(shellcheck)
18-18: shellcheck reported issue in this script: SC2086:info:4:28: Double quote to prevent globbing and word splitting
(shellcheck)
18-18: "github.event.pull_request.title" is potentially untrusted. avoid using it directly in inline scripts. instead, pass it through an environment variable. see https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions for more details
(expression)
- name: Extract backport branch and issue number | ||
if: env.BACKPORT == 'true' | ||
run: | | ||
# Extract branch from the target branch of the PR | ||
BRANCH=$(echo "${{ github.event.pull_request.base.ref }}") | ||
BRANCH=${BRANCH%.x} # Remove the '.x' suffix | ||
BRANCH=$(echo "${BRANCH}" | sed 's/\./\\./g') # Escape periods | ||
echo "BRANCH=$BRANCH" >> $GITHUB_ENV | ||
|
||
# Extract issue number from the PR description | ||
ORIGINAL_ISSUE_NUMBER=$(echo "${{ github.event.pull_request.body }}" | grep -oE 'issues/[0-9]+' | cut -d'/' -f2) | ||
echo "ORIGINAL_ISSUE_NUMBER=$ORIGINAL_ISSUE_NUMBER" >> $GITHUB_ENV | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve robustness of branch and issue extraction.
The current implementation needs improvements in error handling and shell scripting:
- Missing validation for required values
- Issue number extraction could fail silently
- Branch name processing can be simplified
Apply this diff to improve reliability:
- name: Extract backport branch and issue number
if: env.BACKPORT == 'true'
run: |
# Extract branch from the target branch of the PR
- BRANCH=$(echo "${{ github.event.pull_request.base.ref }}")
- BRANCH=${BRANCH%.x} # Remove the '.x' suffix
- BRANCH=$(echo "${BRANCH}" | sed 's/\./\\./g') # Escape periods
+ BRANCH="${{ github.event.pull_request.base.ref }}"
+ BRANCH="${BRANCH%.x}" # Remove the '.x' suffix
+ BRANCH="${BRANCH//./\\.}" # Escape periods
echo "BRANCH=$BRANCH" >> $GITHUB_ENV
# Extract issue number from the PR description
- ORIGINAL_ISSUE_NUMBER=$(echo "${{ github.event.pull_request.body }}" | grep -oE 'issues/[0-9]+' | cut -d'/' -f2)
+ PR_BODY='${{ github.event.pull_request.body }}'
+ if ! ORIGINAL_ISSUE_NUMBER=$(echo "${PR_BODY}" | grep -oE 'issues/[0-9]+' | cut -d'/' -f2); then
+ echo "Error: Could not extract original issue number from PR description"
+ exit 1
+ fi
echo "ORIGINAL_ISSUE_NUMBER=$ORIGINAL_ISSUE_NUMBER" >> $GITHUB_ENV
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
- name: Extract backport branch and issue number | |
if: env.BACKPORT == 'true' | |
run: | | |
# Extract branch from the target branch of the PR | |
BRANCH=$(echo "${{ github.event.pull_request.base.ref }}") | |
BRANCH=${BRANCH%.x} # Remove the '.x' suffix | |
BRANCH=$(echo "${BRANCH}" | sed 's/\./\\./g') # Escape periods | |
echo "BRANCH=$BRANCH" >> $GITHUB_ENV | |
# Extract issue number from the PR description | |
ORIGINAL_ISSUE_NUMBER=$(echo "${{ github.event.pull_request.body }}" | grep -oE 'issues/[0-9]+' | cut -d'/' -f2) | |
echo "ORIGINAL_ISSUE_NUMBER=$ORIGINAL_ISSUE_NUMBER" >> $GITHUB_ENV | |
- name: Extract backport branch and issue number | |
if: env.BACKPORT == 'true' | |
run: | | |
# Extract branch from the target branch of the PR | |
BRANCH="${{ github.event.pull_request.base.ref }}" | |
BRANCH="${BRANCH%.x}" # Remove the '.x' suffix | |
BRANCH="${BRANCH//./\\.}" # Escape periods | |
echo "BRANCH=$BRANCH" >> $GITHUB_ENV | |
# Extract issue number from the PR description | |
PR_BODY='${{ github.event.pull_request.body }}' | |
if ! ORIGINAL_ISSUE_NUMBER=$(echo "${PR_BODY}" | grep -oE 'issues/[0-9]+' | cut -d'/' -f2); then | |
echo "Error: Could not extract original issue number from PR description" | |
exit 1 | |
fi | |
echo "ORIGINAL_ISSUE_NUMBER=$ORIGINAL_ISSUE_NUMBER" >> $GITHUB_ENV |
🧰 Tools
🪛 actionlint
27-27: shellcheck reported issue in this script: SC2116:style:2:8: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'
(shellcheck)
27-27: shellcheck reported issue in this script: SC2001:style:4:10: See if you can use ${variable//search/replace} instead
(shellcheck)
27-27: shellcheck reported issue in this script: SC2086:info:5:26: Double quote to prevent globbing and word splitting
(shellcheck)
27-27: shellcheck reported issue in this script: SC2086:info:9:56: Double quote to prevent globbing and word splitting
(shellcheck)
27-27: "github.event.pull_request.body" is potentially untrusted. avoid using it directly in inline scripts. instead, pass it through an environment variable. see https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions for more details
(expression)
- name: Find corresponding backport issue number | ||
if: env.BACKPORT == 'true' | ||
run: | | ||
BACKPORT_ISSUE_NUMBER=$(curl -s "https://api.github.com/search/issues?q=repo:longhorn/longhorn+is:open+is:issue+in:title+${{ env.BRANCH }}+${{ env.ORIGINAL_ISSUE_TITLE }}" | jq .items[0].number) | ||
echo "BACKPORT_ISSUE_NUMBER=$BACKPORT_ISSUE_NUMBER" >> $GITHUB_ENV | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add error handling for backport issue search.
The current implementation doesn't handle cases where the search returns no results or multiple matches.
Apply this diff to improve error handling:
- name: Find corresponding backport issue number
if: env.BACKPORT == 'true'
run: |
- BACKPORT_ISSUE_NUMBER=$(curl -s "https://api.github.com/search/issues?q=repo:longhorn/longhorn+is:open+is:issue+in:title+${{ env.BRANCH }}+${{ env.ORIGINAL_ISSUE_TITLE }}" | jq .items[0].number)
+ SEARCH_RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
+ "https://api.github.com/search/issues?q=repo:longhorn/longhorn+is:open+is:issue+in:title+${{ env.BRANCH }}+${{ env.ORIGINAL_ISSUE_TITLE }}")
+ TOTAL_COUNT=$(echo "${SEARCH_RESPONSE}" | jq .total_count)
+
+ if [ "${TOTAL_COUNT}" = "0" ]; then
+ echo "Error: No matching backport issue found"
+ exit 1
+ elif [ "${TOTAL_COUNT}" -gt "1" ]; then
+ echo "Warning: Multiple matching backport issues found, using the first one"
+ fi
+
+ BACKPORT_ISSUE_NUMBER=$(echo "${SEARCH_RESPONSE}" | jq .items[0].number)
+ if [ "${BACKPORT_ISSUE_NUMBER}" = "null" ]; then
+ echo "Error: Failed to extract backport issue number"
+ exit 1
+ fi
echo "BACKPORT_ISSUE_NUMBER=$BACKPORT_ISSUE_NUMBER" >> $GITHUB_ENV
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
- name: Find corresponding backport issue number | |
if: env.BACKPORT == 'true' | |
run: | | |
BACKPORT_ISSUE_NUMBER=$(curl -s "https://api.github.com/search/issues?q=repo:longhorn/longhorn+is:open+is:issue+in:title+${{ env.BRANCH }}+${{ env.ORIGINAL_ISSUE_TITLE }}" | jq .items[0].number) | |
echo "BACKPORT_ISSUE_NUMBER=$BACKPORT_ISSUE_NUMBER" >> $GITHUB_ENV | |
- name: Find corresponding backport issue number | |
if: env.BACKPORT == 'true' | |
run: | | |
SEARCH_RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/search/issues?q=repo:longhorn/longhorn+is:open+is:issue+in:title+${{ env.BRANCH }}+${{ env.ORIGINAL_ISSUE_TITLE }}") | |
TOTAL_COUNT=$(echo "${SEARCH_RESPONSE}" | jq .total_count) | |
if [ "${TOTAL_COUNT}" = "0" ]; then | |
echo "Error: No matching backport issue found" | |
exit 1 | |
elif [ "${TOTAL_COUNT}" -gt "1" ]; then | |
echo "Warning: Multiple matching backport issues found, using the first one" | |
fi | |
BACKPORT_ISSUE_NUMBER=$(echo "${SEARCH_RESPONSE}" | jq .items[0].number) | |
if [ "${BACKPORT_ISSUE_NUMBER}" = "null" ]; then | |
echo "Error: Failed to extract backport issue number" | |
exit 1 | |
fi | |
echo "BACKPORT_ISSUE_NUMBER=$BACKPORT_ISSUE_NUMBER" >> $GITHUB_ENV |
🧰 Tools
🪛 actionlint
53-53: shellcheck reported issue in this script: SC2086:info:2:56: Double quote to prevent globbing and word splitting
(shellcheck)
Which issue(s) this PR fixes:
Issue longhorn/longhorn#8002
What this PR does / why we need it:
automatically relate a backport PR to the backport issue
Special notes for your reviewer:
Additional documentation or context