project-automation-issue.yaml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. name: Project Automation (Issue)
  2. on:
  3. issues:
  4. types:
  5. - opened
  6. - reopened
  7. - closed
  8. - labeled
  9. - unlabeled
  10. - connected
  11. jobs:
  12. issue_automation:
  13. runs-on: ubuntu-latest
  14. steps:
  15. - name: Get project data
  16. env:
  17. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  18. ORGANIZATION: hugo-toha
  19. PROJECT_NUMBER: 4
  20. run: |
  21. gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
  22. query($org: String!, $number: Int!) {
  23. organization(login: $org){
  24. projectNext(number: $number) {
  25. id
  26. fields(first:20) {
  27. nodes {
  28. id
  29. name
  30. settings
  31. }
  32. }
  33. }
  34. }
  35. }' -f org="$ORGANIZATION" -F number=$PROJECT_NUMBER > project_data.json
  36. echo 'PROJECT_ID='$(jq -r '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
  37. # Read the ID of the "Type" field options
  38. echo 'TYPE_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.id' project_data.json) >> $GITHUB_ENV
  39. echo 'PROJECT_ID='$(jq -r '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
  40. echo 'TYPE_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.id' project_data.json) >> $GITHUB_ENV
  41. echo 'TYPE_BUG='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.settings | fromjson.options[] | select(.name=="Bug") |.id' project_data.json) >> $GITHUB_ENV
  42. echo 'TYPE_FEATURE='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.settings | fromjson.options[] | select(.name=="Feature") |.id' project_data.json) >> $GITHUB_ENV
  43. echo 'TYPE_ENHANCEMENT='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.settings | fromjson.options[] | select(.name=="Enhancement") |.id' project_data.json) >> $GITHUB_ENV
  44. echo 'TYPE_DOCUMENTATION='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.settings | fromjson.options[] | select(.name=="Documentation") |.id' project_data.json) >> $GITHUB_ENV
  45. echo 'TYPE_TRANSLATION='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.settings | fromjson.options[] | select(.name=="Translation") |.id' project_data.json) >> $GITHUB_ENV
  46. # Read the id of the "Status" field options
  47. echo 'STATUS_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.id' project_data.json) >> $GITHUB_ENV
  48. echo 'STATUS_TODO='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.settings | fromjson.options[] | select(.name=="Todo") |.id' project_data.json) >> $GITHUB_ENV
  49. echo 'STATUS_IN_PROGRESS='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.settings | fromjson.options[] | select(.name=="In Progress") |.id' project_data.json) >> $GITHUB_ENV
  50. echo 'STATUS_READY_FOR_REVIEW='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.settings | fromjson.options[] | select(.name=="Ready for Review") |.id' project_data.json) >> $GITHUB_ENV
  51. echo 'STATUS_DONE='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.settings | fromjson.options[] | select(.name=="Done") |.id' project_data.json) >> $GITHUB_ENV
  52. - name: Add Issue to project
  53. env:
  54. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  55. ISSUE_ID: ${{ github.event.issue.node_id }}
  56. run: |
  57. item_id="$( gh api graphql -f query='
  58. mutation($project:ID!, $issue:ID!) {
  59. addProjectNextItem(input: {projectId: $project, contentId: $issue}) {
  60. projectNextItem {
  61. id
  62. }
  63. }
  64. }' -f project="$PROJECT_ID" -f issue="$ISSUE_ID" --jq '.data.addProjectNextItem.projectNextItem.id')"
  65. echo 'ITEM_ID='$item_id >> $GITHUB_ENV
  66. - name: Export Labels
  67. env:
  68. ISSUE_DATA: ${{ toJson(github.event.issue) }}
  69. run: |
  70. echo 'LABELS=($(echo "$ISSUE_DATA" | jq '.labels[]' | jq -r '.name'))<<EOF' >> $GITHUB_ENV
  71. - name: Set "Type" field
  72. env:
  73. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  74. run: |
  75. # Only execute this step if the Issue contains at least one label
  76. if [ "${#LABELS[@]}" -gt 0 ]; then
  77. # Let by default the type is "Bug"
  78. OPTION_ID=$TYPE_BUG
  79. # If it has "feature" label then set the type to "Feature"
  80. if [[ "${LABELS[*]}" =~ "feature" ]]; then
  81. OPTION_ID=$TYPE_FEATURE
  82. fi
  83. # If it has "enhancement" label then set the type to "Enhancement"
  84. if [[ "${LABELS[*]}" =~ "enhancement" ]]; then
  85. OPTION_ID=$TYPE_ENHANCEMENT
  86. fi
  87. # If it has "documentation" label then set the type to "Documentation"
  88. if [[ "${LABELS[*]}" =~ "documentation" ]]; then
  89. OPTION_ID=$TYPE_DOCUMENTATION
  90. fi
  91. # If it has "translation" label then set the type to "Translation"
  92. if [[ "${LABELS[*]}" =~ "translation" ]]; then
  93. OPTION_ID=$TYPE_TRANSLATION
  94. fi
  95. # Set the "Type" field to appropriate option
  96. gh api graphql -f query='
  97. mutation ($project: ID!, $item: ID!, $field: ID!, $opt_id: ID!) {
  98. updateProjectNextItemField(input: {
  99. projectId: $project
  100. itemId: $item
  101. fieldId: $field
  102. value: $opt_id
  103. }) {
  104. projectNextItem {
  105. id
  106. }
  107. }
  108. }' -f project="$PROJECT_ID" -f item="$ITEM_ID" -f field="$TYPE_ID" -f opt_id="$OPTION_ID" --silent
  109. fi