|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +name: Set PR or Issue Roadmap Value on Close |
| 17 | + |
| 18 | +on: |
| 19 | + pull_request_target: |
| 20 | + # Run this action when a PR is closed |
| 21 | + types: [closed] |
| 22 | + issues: |
| 23 | + # Run this action when an issue is closed |
| 24 | + types: [closed] |
| 25 | + |
| 26 | +env: |
| 27 | + ORG: ${{ github.event.repository.owner.login }} |
| 28 | + PR_NUMBER: ${{ github.event.pull_request.number }} # evaluates to null for issues |
| 29 | + ISSUE_NUMBER: ${{ github.event.issue.number }} # evaluates to null for PRs |
| 30 | + REPO: ${{ github.event.repository.name }} |
| 31 | + |
| 32 | + # The environment vars below are hard-coded from external queries to save time + complexity here |
| 33 | + # Note: PVT means Project V2, not "Private" |
| 34 | + # PVT = Project V2, PVTSSF = Project V2 Single Select Field, PVTIF = Project V2 Iteration Field |
| 35 | + PROJECT_ID: "PVT_kwDOABpemM4AEhOI" |
| 36 | + ROADMAP_FIELD_ID: "PVTSSF_lADOABpemM4AEhOIzgC_MXI" |
| 37 | + |
| 38 | +jobs: |
| 39 | + set_roadmap_value: |
| 40 | + runs-on: ubuntu-latest |
| 41 | + |
| 42 | + steps: |
| 43 | + - name: Generate token |
| 44 | + id: generate_token |
| 45 | + |
| 46 | + with: |
| 47 | + app_id: ${{ secrets.CCCL_AUTH_APP_ID }} |
| 48 | + private_key: ${{ secrets.CCCL_AUTH_APP_PEM }} |
| 49 | + |
| 50 | + - name: Get PR Project ID |
| 51 | + if: github.event_name == 'pull_request_target' |
| 52 | + id: get_pr_id |
| 53 | + env: |
| 54 | + GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }} |
| 55 | + run: | |
| 56 | + # Query up to 10 projects for the PR |
| 57 | + gh api graphql -f query=' |
| 58 | + query { |
| 59 | + organization(login: "${{ env.ORG }}") { |
| 60 | + repository(name: "${{ env.REPO }}") { |
| 61 | + issueOrPullRequest(number: ${{ env.PR_NUMBER }}) { |
| 62 | + ... on PullRequest { |
| 63 | + id |
| 64 | + projectItems(first: 10) { |
| 65 | + edges { |
| 66 | + node { |
| 67 | + id |
| 68 | + project { |
| 69 | + id |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + }' > project_data.json |
| 79 | + |
| 80 | + # Filter the json result to only the project-specific ID for the PR |
| 81 | + # A PR can be in multiple projects so we need to filter by the project ID we want |
| 82 | + pr_id=$(jq -r '.data.organization.repository.issueOrPullRequest.projectItems.edges[] | |
| 83 | + select(.node.project.id == "${{ env.PROJECT_ID }}") | |
| 84 | + .node.id' project_data.json) |
| 85 | + echo "ITEM_ID=$pr_id" >> $GITHUB_ENV |
| 86 | + continue-on-error: true |
| 87 | + |
| 88 | + - name: Get Issue Project ID |
| 89 | + if: github.event_name == 'issues' |
| 90 | + id: get_issue_id |
| 91 | + env: |
| 92 | + GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }} |
| 93 | + run: | |
| 94 | + # Query up to 10 projects for the Issue |
| 95 | + gh api graphql -f query=' |
| 96 | + query { |
| 97 | + organization(login: "${{ env.ORG }}") { |
| 98 | + repository(name: "${{ env.REPO }}") { |
| 99 | + issueOrPullRequest(number: ${{ env.ISSUE_NUMBER }}) { |
| 100 | + ... on Issue { |
| 101 | + id |
| 102 | + projectItems(first: 10) { |
| 103 | + edges { |
| 104 | + node { |
| 105 | + id |
| 106 | + project { |
| 107 | + id |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + }' > project_data.json |
| 117 | + |
| 118 | + # Filter the json result to only the project-specific ID for the PR |
| 119 | + # A PR can be in multiple projects so we need to filter by the project ID we want |
| 120 | + issue_id=$(jq -r '.data.organization.repository.issueOrPullRequest.projectItems.edges[] | |
| 121 | + select(.node.project.id == "${{ env.PROJECT_ID }}") | |
| 122 | + .node.id' project_data.json) |
| 123 | + echo "ITEM_ID=$issue_id" >> $GITHUB_ENV |
| 124 | + continue-on-error: true |
| 125 | + |
| 126 | + - name: Get Current Release |
| 127 | + id: get_current_release |
| 128 | + env: |
| 129 | + GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }} |
| 130 | + run: | |
| 131 | + # Get current roadmap id |
| 132 | + # We maintain the roadmap as a single select field in the project, with the first value being the upcoming release |
| 133 | +
|
| 134 | + gh api graphql -f query=' |
| 135 | + query MyQuery { |
| 136 | + node(id: "${{ env.PROJECT_ID }}") { |
| 137 | + ... on ProjectV2 { |
| 138 | + id |
| 139 | + field(name: "Roadmap") { |
| 140 | + ... on ProjectV2SingleSelectField { |
| 141 | + id |
| 142 | + options { |
| 143 | + id |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + }' > roadmap_option_data.json |
| 150 | + current_roadmap_option_id=$(jq -r '.data.node.field.options[0].id' roadmap_option_data.json) |
| 151 | + echo "CURRENT_ROADMAP_OPTION_ID=$current_roadmap_option_id" >> $GITHUB_ENV |
| 152 | + continue-on-error: true |
| 153 | + |
| 154 | + - name: Set Item Roadmap |
| 155 | + id: set_item_roadmap |
| 156 | + env: |
| 157 | + GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }} |
| 158 | + run: | |
| 159 | + # Update the PR status to In Review |
| 160 | + gh api graphql -f query=' |
| 161 | + mutation { |
| 162 | + updateProjectV2ItemFieldValue( |
| 163 | + input: { |
| 164 | + projectId: "${{ env.PROJECT_ID }}" |
| 165 | + itemId: "${{ env.ITEM_ID }}" |
| 166 | + fieldId: "${{ env.ROADMAP_FIELD_ID }}" |
| 167 | + value: { |
| 168 | + singleSelectOptionId: "${{ env.CURRENT_ROADMAP_OPTION_ID }}" |
| 169 | + } |
| 170 | + } |
| 171 | + ) { |
| 172 | + projectV2Item { |
| 173 | + id |
| 174 | + } |
| 175 | + } |
| 176 | + }' |
| 177 | + continue-on-error: true |
0 commit comments