project-automation-pr.yaml 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. - synchronize
  13. jobs:
  14. pr_automation:
  15. runs-on: ubuntu-latest
  16. steps:
  17. - name: Get project data
  18. env:
  19. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  20. ORGANIZATION: hugo-toha
  21. PROJECT_NUMBER: 4
  22. run: |
  23. gh api graphql --header 'GraphQL-Features: projects_next_graphql' -f query='
  24. query($org: String!, $number: Int!) {
  25. organization(login: $org){
  26. projectNext(number: $number) {
  27. id
  28. fields(first:20) {
  29. nodes {
  30. id
  31. name
  32. settings
  33. }
  34. }
  35. }
  36. }
  37. }' -f org="$ORGANIZATION" -F number=$PROJECT_NUMBER > project_data.json
  38. echo 'PROJECT_ID='$(jq -r '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
  39. # Read the ID of the "Type" field options
  40. echo 'TYPE_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.id' project_data.json) >> $GITHUB_ENV
  41. echo 'PROJECT_ID='$(jq -r '.data.organization.projectNext.id' project_data.json) >> $GITHUB_ENV
  42. echo 'TYPE_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Type") |.id' project_data.json) >> $GITHUB_ENV
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. # Read the id of the "Status" field options
  49. echo 'STATUS_ID='$(jq -r '.data.organization.projectNext.fields.nodes[] | select(.name== "Status") |.id' project_data.json) >> $GITHUB_ENV
  50. 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
  51. 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
  52. 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
  53. 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
  54. - name: Add PR to project
  55. env:
  56. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  57. PR_ID: ${{ github.event.pull_request.node_id }}
  58. run: |
  59. item_id="$( gh api graphql -f query='
  60. mutation($project:ID!, $pr:ID!) {
  61. addProjectNextItem(input: {projectId: $project, contentId: $pr}) {
  62. projectNextItem {
  63. id
  64. }
  65. }
  66. }' -f project="$PROJECT_ID" -f pr="$PR_ID" --jq '.data.addProjectNextItem.projectNextItem.id')"
  67. echo 'ITEM_ID='$item_id >> $GITHUB_ENV
  68. - name: Export Labels
  69. env:
  70. PR_DATA: ${{ toJson(github.event.pull_request) }}
  71. run: |
  72. echo 'LABELS='$(echo "$PR_DATA" | jq -r '[.labels[].name] | join(" ")') >> $GITHUB_ENV
  73. - name: Set "Type" field
  74. env:
  75. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  76. run: |
  77. # Only execute this step if the PR contains at least one label
  78. if [ "${#LABELS[@]}" -gt 0 ]; then
  79. # Let by default the type is "Bug"
  80. OPTION_ID=$TYPE_BUG
  81. # If it has "feature" label then set the type to "Feature"
  82. if [[ "${LABELS[*]}" =~ "feature" ]]; then
  83. OPTION_ID=$TYPE_FEATURE
  84. fi
  85. # If it has "enhancement" label then set the type to "Enhancement"
  86. if [[ "${LABELS[*]}" =~ "enhancement" ]]; then
  87. OPTION_ID=$TYPE_ENHANCEMENT
  88. fi
  89. # If it has "documentation" label then set the type to "Documentation"
  90. if [[ "${LABELS[*]}" =~ "documentation" ]]; then
  91. OPTION_ID=$TYPE_DOCUMENTATION
  92. fi
  93. # If it has "translation" label then set the type to "Translation"
  94. if [[ "${LABELS[*]}" =~ "translation" ]]; then
  95. OPTION_ID=$TYPE_TRANSLATION
  96. fi
  97. # Set the "Type" field to appropriate option
  98. gh api graphql -f query='
  99. mutation ($project: ID!, $item: ID!, $field: ID!, $opt_id: ID!) {
  100. updateProjectNextItemField(input: {
  101. projectId: $project
  102. itemId: $item
  103. fieldId: $field
  104. value: $opt_id
  105. }) {
  106. projectNextItem {
  107. id
  108. }
  109. }
  110. }' -f project="$PROJECT_ID" -f item="$ITEM_ID" -f field="$TYPE_ID" -f opt_id="$OPTION_ID" --silent
  111. fi
  112. - name: Set "Status" field
  113. env:
  114. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  115. run: |
  116. MERGED=${{github.event.pull_request.merged}}
  117. STATE=${{github.event.pull_request.state}}
  118. REVIEWERS=${{github.event.pull_request.requested_reviewers}}
  119. DRAFT=${{github.event.pull_request.draft}}
  120. echo "Merged: $MERGED"
  121. echo "State: $STATE"
  122. echo "Draft: $DRAFT"
  123. echo "Reviewer: $REVIEWER"
  124. OPTION_ID=$STATUS_TODO
  125. if [[ ("${MERGED}" == "true") || ("${STATE}" == "closed") ]]
  126. then
  127. OPTION_ID=$STATUS_DONE
  128. elif [[ ${#REVIEWERS[@]} -gt 0 ]]
  129. then
  130. OPTION_ID=$STATUS_READY_FOR_REVIEW
  131. else
  132. OPTION_ID=$STATUS_IN_PROGRESS
  133. fi
  134. # Expose the OPTION_ID so that it can be used in later steps
  135. echo 'PR_STATUS='$OPTION_ID >> $GITHUB_ENV
  136. gh api graphql -f query='
  137. mutation ($project: ID!, $item: ID!, $field: ID!, $status_id: ID!) {
  138. updateProjectNextItemField(input: {
  139. projectId: $project
  140. itemId: $item
  141. fieldId: $field
  142. value: $status_id
  143. }) {
  144. projectNextItem {
  145. id
  146. }
  147. }
  148. }' -f project="$PROJECT_ID" -f item="$ITEM_ID" -f field="$STATUS_ID" -f status_id="$OPTION_ID" --silent
  149. - name: Find Linked Issues
  150. id: linked_issues
  151. uses: hossainemruz/linked-issues@main
  152. with:
  153. pr_url: ${{github.event.pull_request.html_url}}
  154. format: IssueNumber
  155. - name: Update Linked Issues Status
  156. env:
  157. GITHUB_TOKEN: ${{secrets.PROJECT_AUTOMATION}}
  158. run: |
  159. declare -a issues=(${{ steps.linked_issues.outputs.issues }})
  160. # Loop through the every issues and update their Status to same as the PR Status
  161. for i in "${issues[@]}"
  162. do
  163. # Find the Issue ID
  164. ISSUE_ID="$(gh api graphql -f query='
  165. query($owner: String!, $name: String!, $issue_number: Int!) {
  166. repository(owner: $owner, name: $name) {
  167. issue(number: $issue_number) {
  168. id
  169. }
  170. }
  171. }' -f owner="${{github.event.pull_request.head.repo.owner.login}}" -f name="${{github.event.pull_request.head.repo.name}}" -F issue_number=$i --jq='.data.repository.issue.id')"
  172. # Find the id of the Issue at the project board
  173. item_id="$( gh api graphql -f query='
  174. mutation($project:ID!, $pr:ID!) {
  175. addProjectNextItem(input: {projectId: $project, contentId: $pr}) {
  176. projectNextItem {
  177. id
  178. }
  179. }
  180. }' -f project="$PROJECT_ID" -f pr="$ISSUE_ID" --jq '.data.addProjectNextItem.projectNextItem.id')"
  181. # Update the Issue Status
  182. gh api graphql -f query='
  183. mutation ($project: ID!, $item: ID!, $field: ID!, $status_id: ID!) {
  184. updateProjectNextItemField(input: {
  185. projectId: $project
  186. itemId: $item
  187. fieldId: $field
  188. value: $status_id
  189. }) {
  190. projectNextItem {
  191. id
  192. }
  193. }
  194. }' -f project="$PROJECT_ID" -f item="$item_id" -f field="$STATUS_ID" -f status_id="$PR_STATUS" --silent
  195. done