sync-jira.yml 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. name: Sync core-team issues to Jira
  2. on:
  3. issues:
  4. types: [opened, reopened, edited, labeled]
  5. permissions:
  6. issues: write
  7. contents: read
  8. jobs:
  9. sync:
  10. if: contains(github.event.issue.labels.*.name, 'core-team')
  11. runs-on: ubuntu-latest
  12. steps:
  13. - name: Create or update Jira ticket
  14. id: jira
  15. env:
  16. JIRA_EMAIL: ${{ secrets.JIRA_EMAIL }}
  17. JIRA_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
  18. JIRA_BASE: ${{ secrets.JIRA_BASE }}
  19. PROJECT_KEY: VEN
  20. run: |
  21. set -e # Exit on any error
  22. set -x # Print each command before execution
  23. # Verify environment variables
  24. if [ -z "$JIRA_EMAIL" ] || [ -z "$JIRA_TOKEN" ] || [ -z "$JIRA_BASE" ]; then
  25. echo "Error: Required Jira environment variables are not set"
  26. exit 1
  27. fi
  28. GH_URL="https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }}"
  29. echo "GitHub URL: $GH_URL"
  30. # Test jq installation
  31. if ! command -v jq &> /dev/null; then
  32. echo "Error: jq is not installed"
  33. exit 1
  34. fi
  35. # Prepare the data with error checking
  36. SUMMARY=$(jq -Rs . <<<"${{ github.event.issue.title }}" || { echo "Error processing summary"; exit 1; })
  37. DESC=$(jq -Rs . <<<"Imported from GitHub: $GH_URL" || { echo "Error processing description"; exit 1; })
  38. JQL=$(jq -Rs . <<<"description ~ \"$GH_URL\"" || { echo "Error processing JQL"; exit 1; })
  39. echo "Summary: $SUMMARY"
  40. echo "Description: $DESC"
  41. echo "JQL: $JQL"
  42. # URL encode the JQL query
  43. ENCODED_JQL=$(echo "$JQL" | jq -sRr @uri)
  44. SEARCH="$JIRA_BASE/rest/api/3/search?jql=$ENCODED_JQL"
  45. echo "Searching Jira with URL: $SEARCH"
  46. # Search with error checking
  47. SEARCH_RESULT=$(curl -f -s -u "$JIRA_EMAIL:$JIRA_TOKEN" "$SEARCH" || {
  48. echo "Error: Jira search failed with status $?"
  49. exit 1
  50. })
  51. echo "Search result: $SEARCH_RESULT"
  52. # Parse the key with error checking
  53. KEY=$(echo "$SEARCH_RESULT" | jq -r '.issues[0].key // empty' || {
  54. echo "Error parsing search result"
  55. exit 1
  56. })
  57. echo "Found existing key: $KEY"
  58. if [ -z "$KEY" ]; then
  59. echo "No existing ticket found, creating new one"
  60. BODY=$(jq -n --arg summary "$SUMMARY" --arg desc "$DESC" --arg proj "$PROJECT_KEY" '
  61. {fields:{project:{key:$proj},summary:$summary,description:$desc,
  62. issuetype:{name:"Task"},labels:["core-team"]}}' || {
  63. echo "Error creating request body"
  64. exit 1
  65. })
  66. echo "Creating ticket with body: $BODY"
  67. # Create with error checking
  68. CREATE_RESULT=$(curl -f -s -u "$JIRA_EMAIL:$JIRA_TOKEN" \
  69. -H "Content-Type: application/json" \
  70. --data "$BODY" "$JIRA_BASE/rest/api/3/issue" || {
  71. echo "Error: Jira create failed with status $?"
  72. exit 1
  73. })
  74. echo "Create result: $CREATE_RESULT"
  75. KEY=$(echo "$CREATE_RESULT" | jq -r .key || {
  76. echo "Error parsing create result"
  77. exit 1
  78. })
  79. echo "Created new key: $KEY"
  80. fi
  81. if [ -z "$KEY" ]; then
  82. echo "Error: Failed to get a valid Jira key"
  83. exit 1
  84. fi
  85. echo "Final key value: $KEY"
  86. echo "key=$KEY" >> $GITHUB_OUTPUT
  87. - name: Record the mapping on the GH issue
  88. uses: actions/github-script@v7
  89. env:
  90. JIRA_KEY: ${{ steps.jira.outputs.key }}
  91. with:
  92. script: |
  93. const issue_number = context.issue.number;
  94. const owner = context.repo.owner;
  95. const repo = context.repo.repo;
  96. const jira = process.env.JIRA_KEY;
  97. const body = `🔗 Linked to Core Team Jira ticket **${jira}**`;
  98. const {data:comments} = await github.rest.issues.listComments({owner,repo,issue_number});
  99. const existing = comments.find(c=>/Linked to Core Team Jira ticket/i.test(c.body));
  100. if (existing) {
  101. await github.rest.issues.updateComment({owner,repo,comment_id:existing.id,body});
  102. } else {
  103. await github.rest.issues.createComment({owner,repo,issue_number,body});
  104. }