project-automation-pr.yaml 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. name: Project Automation (PR)
  2. on:
  3. pull_request:
  4. types:
  5. - opened
  6. - ready_for_review
  7. - reopened
  8. - review_requested
  9. - closed
  10. - labeled
  11. - unlabeled
  12. jobs:
  13. track_pr:
  14. runs-on: ubuntu-latest
  15. steps:
  16. - name: Get project data
  17. env:
  18. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  19. ORGANIZATION: hugo-toha
  20. PROJECT_NUMBER: 4
  21. run: |
  22. gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
  23. query($org: String!, $number: Int!) {
  24. organization(login: $org){
  25. projectNext(number: $number) {
  26. id
  27. fields(first:20) {
  28. nodes {
  29. id
  30. name
  31. settings
  32. }
  33. }
  34. }
  35. }
  36. }' -f org="$ORGANIZATION" -F number=$PROJECT_NUMBER > project_data.json
  37. echo 'PROJECT_ID='$(jq -r '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
  38. # Read the ID of the "Type" field options
  39. echo 'TYPE_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.id' project_data.json) >> $GITHUB_ENV
  40. echo 'PROJECT_ID='$(jq -r '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
  41. echo 'TYPE_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.id' project_data.json) >> $GITHUB_ENV
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. # Read the id of the "Status" field options
  48. echo 'STATUS_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.id' project_data.json) >> $GITHUB_ENV
  49. 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
  50. 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
  51. 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
  52. 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
  53. - name: Add PR to project
  54. env:
  55. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  56. PR_ID: ${{ github.event.pull_request.node_id }}
  57. run: |
  58. item_id="$( gh api graphql -f query='
  59. mutation($project:ID!, $pr:ID!) {
  60. addProjectNextItem(input: {projectId: $project, contentId: $pr}) {
  61. projectNextItem {
  62. id
  63. }
  64. }
  65. }' -f project="$PROJECT_ID" -f pr="$PR_ID" --jq '.data.addProjectNextItem.projectNextItem.id')"
  66. echo 'ITEM_ID='$item_id >> $GITHUB_ENV
  67. - name: Export Labels
  68. env:
  69. PR_DATA: ${{ toJson(github.event.pull_request) }}
  70. run: |
  71. echo 'LABELS=($(echo "$PR_DATA" | jq '.labels[]' | jq -r '.name'))<<EOF' >> $GITHUB_ENV
  72. - name: Set "Type" field
  73. env:
  74. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  75. run: |
  76. # Only execute this step if the PR contains at least one label
  77. if [ "${#LABELS[@]}" -gt 0 ]; then
  78. # Let by default the type is "Bug"
  79. OPTION_ID=$TYPE_BUG
  80. # If it has "feature" label then set the type to "Feature"
  81. if [[ "${LABELS[*]}" =~ "feature" ]]; then
  82. OPTION_ID=$TYPE_FEATURE
  83. fi
  84. # If it has "enhancement" label then set the type to "Enhancement"
  85. if [[ "${LABELS[*]}" =~ "enhancement" ]]; then
  86. OPTION_ID=$TYPE_ENHANCEMENT
  87. fi
  88. # If it has "documentation" label then set the type to "Documentation"
  89. if [[ "${LABELS[*]}" =~ "documentation" ]]; then
  90. OPTION_ID=$TYPE_DOCUMENTATION
  91. fi
  92. # If it has "translation" label then set the type to "Translation"
  93. if [[ "${LABELS[*]}" =~ "translation" ]]; then
  94. OPTION_ID=$TYPE_TRANSLATION
  95. fi
  96. # Set the "Type" field to appropriate option
  97. gh api graphql -f query='
  98. mutation ($project: ID!, $item: ID!, $field: ID!, $opt_id: ID!) {
  99. updateProjectNextItemField(input: {
  100. projectId: $project
  101. itemId: $item
  102. fieldId: $field
  103. value: $opt_id
  104. }) {
  105. projectNextItem {
  106. id
  107. }
  108. }
  109. }' -f project="$PROJECT_ID" -f item="$ITEM_ID" -f field="$TYPE_ID" -f opt_id="$OPTION_ID" --silent
  110. fi
  111. - name: Set "Status" field
  112. env:
  113. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  114. run: |
  115. MERGED=${{github.event.pull_request.merged}}
  116. STATE=${{github.event.pull_request.state}}
  117. REVIEWERS=${{github.event.pull_request.requested_reviewers}}
  118. DRAFT=${{github.event.pull_request.draft}}
  119. echo "Merged: $MERGED"
  120. echo "State: $STATE"
  121. echo "Draft: $DRAFT"
  122. echo "Reviewer: $REVIEWER"
  123. OPTION_ID=$STATUS_TODO
  124. if [[ ("${MERGED}" == "true") || ("${STATUS}" == "closed") ]]
  125. then
  126. OPTION_ID=$STATUS_DONE
  127. elif [[ ${#REVIEWERS[@]} -gt 0 ]]
  128. then
  129. OPTION_ID=$STATUS_READY_FOR_REVIEW
  130. else
  131. OPTION_ID=$STATUS_IN_PROGRESS
  132. fi
  133. gh api graphql -f query='
  134. mutation ($project: ID!, $item: ID!, $field: ID!, $status_id: ID!) {
  135. updateProjectNextItemField(input: {
  136. projectId: $project
  137. itemId: $item
  138. fieldId: $field
  139. value: $status_id
  140. }) {
  141. projectNextItem {
  142. id
  143. }
  144. }
  145. }' -f project="$PROJECT_ID" -f item="$ITEM_ID" -f field="$STATUS_ID" -f status_id="$OPTION_ID" --silent