| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- name: Sync core-team issues to Jira
- on:
- issues:
- types: [opened, reopened, edited, labeled]
- permissions:
- issues: write
- contents: read
- jobs:
- sync:
- if: contains(github.event.issue.labels.*.name, 'core-team')
- runs-on: ubuntu-latest
- steps:
- - name: Create or update Jira ticket
- id: jira
- env:
- JIRA_EMAIL: ${{ secrets.JIRA_EMAIL }}
- JIRA_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
- JIRA_BASE: ${{ secrets.JIRA_BASE }}
- PROJECT_KEY: VEN
- run: |
- set -e # Exit on any error
- set -x # Print each command before execution
-
- # Verify environment variables
- if [ -z "$JIRA_EMAIL" ] || [ -z "$JIRA_TOKEN" ] || [ -z "$JIRA_BASE" ]; then
- echo "Error: Required Jira environment variables are not set"
- exit 1
- fi
-
- GH_URL="https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }}"
- echo "GitHub URL: $GH_URL"
-
- # Test jq installation
- if ! command -v jq &> /dev/null; then
- echo "Error: jq is not installed"
- exit 1
- fi
-
- # Prepare the data with error checking
- SUMMARY=$(jq -Rs . <<<"${{ github.event.issue.title }}" || { echo "Error processing summary"; exit 1; })
- DESC=$(jq -Rs . <<<"Imported from GitHub: $GH_URL" || { echo "Error processing description"; exit 1; })
- JQL=$(jq -Rs . <<<"description ~ \"$GH_URL\"" || { echo "Error processing JQL"; exit 1; })
-
- echo "Summary: $SUMMARY"
- echo "Description: $DESC"
- echo "JQL: $JQL"
-
- # URL encode the JQL query
- ENCODED_JQL=$(echo "$JQL" | jq -sRr @uri)
- SEARCH="$JIRA_BASE/rest/api/3/search?jql=$ENCODED_JQL"
-
- echo "Searching Jira with URL: $SEARCH"
-
- # Search with error checking
- SEARCH_RESULT=$(curl -f -s -u "$JIRA_EMAIL:$JIRA_TOKEN" "$SEARCH" || {
- echo "Error: Jira search failed with status $?"
- exit 1
- })
-
- echo "Search result: $SEARCH_RESULT"
-
- # Parse the key with error checking
- KEY=$(echo "$SEARCH_RESULT" | jq -r '.issues[0].key // empty' || {
- echo "Error parsing search result"
- exit 1
- })
-
- echo "Found existing key: $KEY"
-
- if [ -z "$KEY" ]; then
- echo "No existing ticket found, creating new one"
- BODY=$(jq -n --arg summary "$SUMMARY" --arg desc "$DESC" --arg proj "$PROJECT_KEY" '
- {fields:{project:{key:$proj},summary:$summary,description:$desc,
- issuetype:{name:"Task"},labels:["core-team"]}}' || {
- echo "Error creating request body"
- exit 1
- })
-
- echo "Creating ticket with body: $BODY"
-
- # Create with error checking
- CREATE_RESULT=$(curl -f -s -u "$JIRA_EMAIL:$JIRA_TOKEN" \
- -H "Content-Type: application/json" \
- --data "$BODY" "$JIRA_BASE/rest/api/3/issue" || {
- echo "Error: Jira create failed with status $?"
- exit 1
- })
-
- echo "Create result: $CREATE_RESULT"
-
- KEY=$(echo "$CREATE_RESULT" | jq -r .key || {
- echo "Error parsing create result"
- exit 1
- })
-
- echo "Created new key: $KEY"
- fi
-
- if [ -z "$KEY" ]; then
- echo "Error: Failed to get a valid Jira key"
- exit 1
- fi
-
- echo "Final key value: $KEY"
- echo "key=$KEY" >> $GITHUB_OUTPUT
- - name: Record the mapping on the GH issue
- uses: actions/github-script@v7
- env:
- JIRA_KEY: ${{ steps.jira.outputs.key }}
- with:
- script: |
- const issue_number = context.issue.number;
- const owner = context.repo.owner;
- const repo = context.repo.repo;
- const jira = process.env.JIRA_KEY;
- const body = `🔗 Linked to Core Team Jira ticket **${jira}**`;
- const {data:comments} = await github.rest.issues.listComments({owner,repo,issue_number});
- const existing = comments.find(c=>/Linked to Core Team Jira ticket/i.test(c.body));
- if (existing) {
- await github.rest.issues.updateComment({owner,repo,comment_id:existing.id,body});
- } else {
- await github.rest.issues.createComment({owner,repo,issue_number,body});
- }
|