sync-jira.yml 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. GH_URL="https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }}"
  22. SUMMARY=$(jq -Rs . <<<"${{ github.event.issue.title }}")
  23. DESC=$(jq -Rs . <<<"Imported from GitHub: $GH_URL")
  24. # look for an existing ticket whose description contains the GH URL
  25. JQL=$(jq -Rs . <<<"description ~ \"$GH_URL\"")
  26. SEARCH="$JIRA_BASE/rest/api/3/search?jql=$JQL"
  27. KEY=$(curl -s -u "$JIRA_EMAIL:$JIRA_TOKEN" "$SEARCH" \
  28. | jq -r '.issues[0].key // empty')
  29. if [ -z "$KEY" ]; then
  30. BODY=$(jq -n --arg summary "$SUMMARY" --arg desc "$DESC" --arg proj "$PROJECT_KEY" '
  31. {fields:{project:{key:$proj},summary:$summary,description:$desc,
  32. issuetype:{name:"Task"},labels:["core-team"]}}')
  33. KEY=$(curl -s -u "$JIRA_EMAIL:$JIRA_TOKEN" \
  34. -H "Content-Type: application/json" \
  35. --data "$BODY" "$JIRA_BASE/rest/api/3/issue" | jq -r .key)
  36. fi
  37. echo "key=$KEY" >> $GITHUB_OUTPUT
  38. - name: Record the mapping on the GH issue
  39. uses: actions/github-script@v7
  40. with:
  41. script: |
  42. const issue_number = context.issue.number;
  43. const owner = context.repo.owner;
  44. const repo = context.repo.repo;
  45. const jira = "${{ steps.jira.outputs.key }}";
  46. const body = `🔗 Linked to Core Team Jira ticket **${jira}**`;
  47. const {data:comments} = await github.issues.listComments({owner,repo,issue_number});
  48. const existing = comments.find(c=>/Linked to Core Team Jira ticket/i.test(c.body));
  49. if (existing) {
  50. await github.issues.updateComment({owner,repo,comment_id:existing.id,body});
  51. } else {
  52. await github.issues.createComment({owner,repo,issue_number,body});
  53. }