-
Notifications
You must be signed in to change notification settings - Fork 175
chore(ci): Add workflow to sync JIRA ticket with GitHub issue #6391
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
Draft
Yaminyam
wants to merge
3
commits into
main
Choose a base branch
from
ci/ticket-issue-sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+124
−0
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| name: ticket-issue-number-sync | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize] | ||
|
|
||
| jobs: | ||
| ticket-issue-number-sync: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
|
|
||
| - name: Extract JIRA ticket number from PR title | ||
| id: extract_ticket | ||
| run: | | ||
| # Extract JIRA ticket number from PR title (e.g., "fix(BA-1234): aaaa" -> "BA-1234") | ||
| TITLE="${{ github.event.pull_request.title }}" | ||
| echo "PR Title: $TITLE" | ||
|
|
||
| # Extract ticket number using regex pattern BA-XXXX format | ||
| TICKET_NUMBER=$(echo "$TITLE" | grep -oE 'BA-[0-9]+' | head -1) | ||
|
|
||
| if [ -n "$TICKET_NUMBER" ]; then | ||
| echo "Extracted ticket: $TICKET_NUMBER" | ||
| echo "ticket_number=$TICKET_NUMBER" >> $GITHUB_OUTPUT | ||
| echo "has_ticket=true" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "No JIRA ticket number found in PR title" | ||
| echo "has_ticket=false" >> $GITHUB_OUTPUT | ||
| fi | ||
|
|
||
| - name: Get GitHub Issue URL from JIRA | ||
| if: steps.extract_ticket.outputs.has_ticket == 'true' | ||
| id: get_jira_issue | ||
| env: | ||
| JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }} | ||
| JIRA_USERNAME: ${{ secrets.JIRA_USERNAME }} | ||
| JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }} | ||
| run: | | ||
| TICKET_NUMBER="${{ steps.extract_ticket.outputs.ticket_number }}" | ||
| echo "Fetching GitHub Issue URL for JIRA ticket: $TICKET_NUMBER" | ||
|
|
||
| # Get ticket information through JIRA API | ||
| JIRA_RESPONSE=$(curl -s -u "$JIRA_USERNAME:$JIRA_API_TOKEN" \ | ||
| -H "Accept: application/json" \ | ||
| "$JIRA_BASE_URL/rest/api/3/issue/$TICKET_NUMBER") | ||
|
|
||
| echo "JIRA API Response: $JIRA_RESPONSE" | ||
|
|
||
| # Extract issue number from GitHub Issue URL field | ||
| # Search for fields containing GitHub issue URLs (customfield_* or regular fields) | ||
| GITHUB_ISSUE_URL=$(echo "$JIRA_RESPONSE" | jq -r ' | ||
| .fields | ||
| | to_entries[] | ||
| | select(.value != null and .value != "") | ||
| | select( | ||
| (.value | type == "string" and test("github.com/.*/issues/[0-9]+")) | ||
| ) | ||
| | .value | ||
| ' 2>/dev/null | head -1) | ||
|
|
||
| # Fail immediately if GitHub Issue URL is not found | ||
| if [ -z "$GITHUB_ISSUE_URL" ] || [ "$GITHUB_ISSUE_URL" = "null" ]; then | ||
| echo "❌ No GitHub Issue URL found in JIRA ticket $TICKET_NUMBER" | ||
| echo "Available fields (first 20):" | ||
| echo "$JIRA_RESPONSE" | jq -r '.fields | keys[]' 2>/dev/null | head -20 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "Found GitHub Issue URL: $GITHUB_ISSUE_URL" | ||
| # Extract GitHub issue number (e.g., https://github.com/owner/repo/issues/123 -> 123) | ||
| GITHUB_ISSUE_NUMBER=$(echo "$GITHUB_ISSUE_URL" | grep -oE '/issues/[0-9]+' | grep -oE '[0-9]+' | head -1) | ||
|
|
||
| if [ -n "$GITHUB_ISSUE_NUMBER" ]; then | ||
| echo "✅ Extracted GitHub Issue Number: $GITHUB_ISSUE_NUMBER" | ||
| echo "github_issue_number=$GITHUB_ISSUE_NUMBER" >> $GITHUB_OUTPUT | ||
| echo "has_github_issue=true" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "❌ Could not extract issue number from GitHub Issue URL: $GITHUB_ISSUE_URL" | ||
| echo "Please verify the GitHub Issue URL format is correct (expected: https://github.com/owner/repo/issues/NUMBER)" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - name: Update PR body with resolves clause | ||
| if: steps.extract_ticket.outputs.has_ticket == 'true' && steps.get_jira_issue.outputs.has_github_issue == 'true' | ||
| env: | ||
| TICKET_NUMBER: ${{ steps.extract_ticket.outputs.ticket_number }} | ||
| GITHUB_ISSUE_NUMBER: ${{ steps.get_jira_issue.outputs.github_issue_number }} | ||
| run: | | ||
| echo "Using GitHub issue number: $GITHUB_ISSUE_NUMBER from JIRA" | ||
|
|
||
| # Get current PR body | ||
| PR_BODY=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | ||
| -H "Accept: application/vnd.github.v3+json" \ | ||
| "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" | \ | ||
| jq -r '.body // ""') | ||
|
|
||
| echo "Current PR body: $PR_BODY" | ||
|
|
||
| # Generate resolves clause | ||
| RESOLVES_CLAUSE="resolves #$GITHUB_ISSUE_NUMBER ($TICKET_NUMBER)" | ||
| echo "Generated resolves clause: $RESOLVES_CLAUSE" | ||
|
|
||
| # Replace existing resolves clause or add new one | ||
| if echo "$PR_BODY" | grep -qiE "resolves #[0-9]+ ?\("; then | ||
| # Replace existing resolves clause (case-insensitive) | ||
| NEW_BODY=$(echo "$PR_BODY" | sed -E "s/[Rr]esolves #[0-9]+ ?\([^)]+\)/$RESOLVES_CLAUSE/g") | ||
| echo "Replaced existing resolves clause" | ||
| else | ||
| # Add new resolves clause at the beginning | ||
| NEW_BODY=$(printf "%s\n\n%s" "$RESOLVES_CLAUSE" "$PR_BODY") | ||
| echo "Added new resolves clause" | ||
| fi | ||
|
|
||
| # Update PR body | ||
| curl -s -X PATCH \ | ||
| -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | ||
| -H "Accept: application/vnd.github.v3+json" \ | ||
| -H "Content-Type: application/json" \ | ||
| "https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" \ | ||
| -d "{\"body\": $(echo "$NEW_BODY" | jq -R -s .)}" | ||
|
|
||
| echo "✅ Added resolves clause: $RESOLVES_CLAUSE" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Workflow does not contain permissions Medium
Copilot Autofix
AI 4 months ago
To fix this, add an explicit
permissionsblock so theGITHUB_TOKENused by this workflow follows the principle of least privilege. This workflow needs to read PR metadata and update the PR body via the GitHub API, which maps topull-requests: write. It does not need to push commits or modify repository contents, socontentscan be limited toread. The cleanest fix is to add a job-levelpermissionsblock under theticket-issue-number-syncjob, aboveruns-on. This keeps the scope tight and avoids affecting other workflows.Concretely, in
.github/workflows/ticket-issue-number-sync.yml, underjobs:, inside theticket-issue-number-sync:job, insert:indented to match other job keys. No imports or additional methods are required, since this is purely a workflow configuration change.