From 0887f12f333d61ff00a070bdf132aa2f5077f519 Mon Sep 17 00:00:00 2001 From: Ben Jarmak Date: Mon, 28 Oct 2024 15:09:58 -0400 Subject: [PATCH 1/2] Update the option selection to be optional --- .../project-get-set-single-select-field.yaml | 53 +++++++++++++------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/.github/workflows/project-get-set-single-select-field.yaml b/.github/workflows/project-get-set-single-select-field.yaml index 8ff09bbb..54883dee 100644 --- a/.github/workflows/project-get-set-single-select-field.yaml +++ b/.github/workflows/project-get-set-single-select-field.yaml @@ -15,11 +15,6 @@ on: type: string required: true - SINGLE_SELECT_OPTION_VALUE: - description: "The value of the option we'd like to get/set" - type: string - required: true - SINGLE_SELECT_FIELD_ID: description: "The graphQL node ID of the single-select field" type: string @@ -46,6 +41,12 @@ on: default: false type: boolean + # Optional, if not specified, we default to the first option in the selector + SINGLE_SELECT_OPTION_VALUE: + description: "The value of the option we'd like to get/set" + type: string + default: null + secrets: ADD_TO_PROJECT_GITHUB_TOKEN: description: "Project Access Token" @@ -71,20 +72,40 @@ jobs: GH_TOKEN: ${{ secrets.ADD_TO_PROJECT_GITHUB_TOKEN }} run: | # Get single_select option id - gh api graphql -f query=' - query { - node(id: "${{ inputs.PROJECT_ID }}") { - ... on ProjectV2 { - id - field(name: "${{ inputs.SINGLE_SELECT_FIELD_NAME }}") { - ... on ProjectV2SingleSelectField { + if [ -z "${{ inputs.SINGLE_SELECT_OPTION_VALUE }}" ]; then + # No option specified, we will get the first option in the list + # graphQL doesn't appear to provide a wildcard option, so separate queries are necessary + gh api graphql -f query=' + query { + node(id: "${{ inputs.PROJECT_ID }}") { + ... on ProjectV2 { id - options(names: "${{ inputs.SINGLE_SELECT_OPTION_VALUE }}") {id} + field(name: "${{ inputs.SINGLE_SELECT_FIELD_NAME }}") { + ... on ProjectV2SingleSelectField { + id + options {id} + } + } } } - } - } - }' > single_select_option_data.json + }' > single_select_option_data.json + else + # Get the specific value only + gh api graphql -f query=' + query { + node(id: "${{ inputs.PROJECT_ID }}") { + ... on ProjectV2 { + id + field(name: "${{ inputs.SINGLE_SELECT_FIELD_NAME }}") { + ... on ProjectV2SingleSelectField { + id + options(names: "${{ inputs.SINGLE_SELECT_OPTION_VALUE }}") {id} + } + } + } + } + }' > single_select_option_data.json + fi current_single_select_option_id=$(jq -r '.data.node.field.options[0].id' single_select_option_data.json) echo "SINGLE_SELECT_OPTION_ID=$current_single_select_option_id" >> "$GITHUB_OUTPUT" From eeacbbc5f130749130f84d8520b43fb479e40493 Mon Sep 17 00:00:00 2001 From: Ben Jarmak Date: Tue, 7 Jan 2025 11:42:33 -0500 Subject: [PATCH 2/2] Use intermediate env variables --- .github/workflows/project-get-item-id.yaml | 6 ++- .../project-get-set-iteration-field.yaml | 15 ++++--- .../project-get-set-single-select-field.yaml | 24 +++++++---- .../project-set-text-date-numeric-field.yaml | 27 +++++++----- .../project-update-linked-issues.yaml | 43 +++++++++++-------- 5 files changed, 69 insertions(+), 46 deletions(-) diff --git a/.github/workflows/project-get-item-id.yaml b/.github/workflows/project-get-item-id.yaml index 30554c1d..d5a08e89 100644 --- a/.github/workflows/project-get-item-id.yaml +++ b/.github/workflows/project-get-item-id.yaml @@ -41,13 +41,15 @@ jobs: id: get_item_id env: GH_TOKEN: ${{ secrets.ADD_TO_PROJECT_GITHUB_TOKEN }} + ENV_ITEM_NODE_ID: ${{ inputs.ITEM_NODE_ID }} + ENV_PROJECT_ID: ${{ inputs.PROJECT_ID }} run: | # Query up to 10 projects for the PR # There's no graphQL filter configured to query by a specific project # So we need to query all projects and filter the result ourselves gh api graphql -f query=' query { - node(id: "${{ inputs.ITEM_NODE_ID }}") { + node(id: "${{ env.ENV_ITEM_NODE_ID }}") { ... on PullRequest { projectItems(first: 10) { nodes { @@ -73,7 +75,7 @@ jobs: # Use jq to do the actual filtering item_project_id=$(jq -r '.data.node.projectItems.nodes[] | - select(.project.id == "${{ inputs.PROJECT_ID }}") | + select(.project.id == "${{ env.ENV_PROJECT_ID }}") | .id' project_data.json) echo "ITEM_PROJECT_ID=$item_project_id" >> $GITHUB_OUTPUT continue-on-error: true diff --git a/.github/workflows/project-get-set-iteration-field.yaml b/.github/workflows/project-get-set-iteration-field.yaml index 9ce50060..12ee0544 100644 --- a/.github/workflows/project-get-set-iteration-field.yaml +++ b/.github/workflows/project-get-set-iteration-field.yaml @@ -64,15 +64,17 @@ jobs: id: get_iteration_option_id env: GH_TOKEN: ${{ secrets.ADD_TO_PROJECT_GITHUB_TOKEN }} + ENV_PROJECT_ID: ${{ inputs.PROJECT_ID }} + ENV_ITERATION_FIELD_NAME: ${{ inputs.ITERATION_FIELD_NAME }} run: | # Get current iteration iteration id # The current iteration is always the first element in the returned list gh api graphql -f query=' query { - node(id: "${{ inputs.PROJECT_ID }}") { + node(id: "${{ env.ENV_PROJECT_ID }}") { ... on ProjectV2 { id - field(name: "${{ inputs.ITERATION_FIELD_NAME }}") { + field(name: "${{ env.ENV_ITERATION_FIELD_NAME }}") { ... on ProjectV2IterationField { id name @@ -94,6 +96,9 @@ jobs: if: ${{ inputs.UPDATE_ITEM == true }} env: GH_TOKEN: ${{ secrets.ADD_TO_PROJECT_GITHUB_TOKEN }} + ENV_PROJECT_ID: ${{ inputs.PROJECT_ID }} + ENV_ITEM_PROJECT_ID: ${{ inputs.ITEM_PROJECT_ID }} + ENV_ITERATION_FIELD_ID: ${{ inputs.ITERATION_FIELD_ID }} run: | # Set the iteration based on the query above # This overwrites whatever was in it before, we may want to make an "OVERWRITE" option @@ -101,9 +106,9 @@ jobs: mutation { updateProjectV2ItemFieldValue( input: { - projectId: "${{ inputs.PROJECT_ID }}" - itemId: "${{ inputs.ITEM_PROJECT_ID }}" - fieldId: "${{ inputs.ITERATION_FIELD_ID }}" + projectId: "${{ env.ENV_PROJECT_ID }}" + itemId: "${{ env.ENV_ITEM_PROJECT_ID }}" + fieldId: "${{ env.ENV_ITERATION_FIELD_ID }}" value: { iterationId: "${{ steps.get_iteration_option_id.outputs.ITERATION_OPTION_ID }}" } diff --git a/.github/workflows/project-get-set-single-select-field.yaml b/.github/workflows/project-get-set-single-select-field.yaml index 54883dee..6656f4f8 100644 --- a/.github/workflows/project-get-set-single-select-field.yaml +++ b/.github/workflows/project-get-set-single-select-field.yaml @@ -70,17 +70,20 @@ jobs: id: get_single_select_option_id env: GH_TOKEN: ${{ secrets.ADD_TO_PROJECT_GITHUB_TOKEN }} + ENV_SINGLE_SELECT_OPTION_VALUE: ${{ inputs.SINGLE_SELECT_OPTION_VALUE }} + ENV_PROJECT_ID: ${{ inputs.PROJECT_ID }} + ENV_SINGLE_SELECT_FIELD_NAME: ${{ inputs.SINGLE_SELECT_FIELD_NAME }} run: | # Get single_select option id - if [ -z "${{ inputs.SINGLE_SELECT_OPTION_VALUE }}" ]; then + if [ -z "${{ env.ENV_SINGLE_SELECT_OPTION_VALUE }}" ]; then # No option specified, we will get the first option in the list # graphQL doesn't appear to provide a wildcard option, so separate queries are necessary gh api graphql -f query=' query { - node(id: "${{ inputs.PROJECT_ID }}") { + node(id: "${{ env.ENV_PROJECT_ID }}") { ... on ProjectV2 { id - field(name: "${{ inputs.SINGLE_SELECT_FIELD_NAME }}") { + field(name: "${{ env.ENV_SINGLE_SELECT_FIELD_NAME }}") { ... on ProjectV2SingleSelectField { id options {id} @@ -93,13 +96,13 @@ jobs: # Get the specific value only gh api graphql -f query=' query { - node(id: "${{ inputs.PROJECT_ID }}") { + node(id: "${{ env.ENV_PROJECT_ID }}") { ... on ProjectV2 { id - field(name: "${{ inputs.SINGLE_SELECT_FIELD_NAME }}") { + field(name: "${{ env.ENV_SINGLE_SELECT_FIELD_NAME }}") { ... on ProjectV2SingleSelectField { id - options(names: "${{ inputs.SINGLE_SELECT_OPTION_VALUE }}") {id} + options(names: "${{ env.ENV_SINGLE_SELECT_OPTION_VALUE }}") {id} } } } @@ -114,6 +117,9 @@ jobs: if: ${{ inputs.UPDATE_ITEM == true }} env: GH_TOKEN: ${{ secrets.ADD_TO_PROJECT_GITHUB_TOKEN }} + ENV_PROJECT_ID: ${{ inputs.PROJECT_ID }} + ENV_ITEM_PROJECT_ID: ${{ inputs.ITEM_PROJECT_ID }} + ENV_SINGLE_SELECT_FIELD_ID: ${{ inputs.SINGLE_SELECT_FIELD_ID }} run: | # Set the single_select based on the query above # This overwrites whatever was in it before, we may want to make an "OVERWRITE" option @@ -121,9 +127,9 @@ jobs: mutation { updateProjectV2ItemFieldValue( input: { - projectId: "${{ inputs.PROJECT_ID }}" - itemId: "${{ inputs.ITEM_PROJECT_ID }}" - fieldId: "${{ inputs.SINGLE_SELECT_FIELD_ID }}" + projectId: "${{ env.ENV_PROJECT_ID }}" + itemId: "${{ env.ENV_ITEM_PROJECT_ID }}" + fieldId: "${{ env.ENV_SINGLE_SELECT_FIELD_ID }}" value: { singleSelectOptionId: "${{ steps.get_single_select_option_id.outputs.SINGLE_SELECT_OPTION_ID }}" } diff --git a/.github/workflows/project-set-text-date-numeric-field.yaml b/.github/workflows/project-set-text-date-numeric-field.yaml index 8ea45f41..42493160 100644 --- a/.github/workflows/project-set-text-date-numeric-field.yaml +++ b/.github/workflows/project-set-text-date-numeric-field.yaml @@ -58,19 +58,24 @@ jobs: id: update_item_text_date_numeric_field env: GH_TOKEN: ${{ secrets.ADD_TO_PROJECT_GITHUB_TOKEN }} + ENV_FIELD_TYPE: ${{ inputs.FIELD_TYPE }} + ENV_PROJECT_ID: ${{ inputs.PROJECT_ID }} + ENV_ITEM_PROJECT_ID: ${{ inputs.ITEM_PROJECT_ID }} + ENV_FIELD_ID: ${{ inputs.FIELD_ID }} + ENV_SET_VALUE: ${{ inputs.SET_VALUE }} run: | # Set the field based on the inputted desired value # This overwrites whatever was in it before, we may want to make an "OVERWRITE" option - if [ "${{ inputs.FIELD_TYPE }}" == "date" ] || \ - [ "${{ inputs.FIELD_TYPE }}" == "text" ]; then + if [ "${{ env.ENV_FIELD_TYPE }}" == "date" ] || \ + [ "${{ env.ENV_FIELD_TYPE }}" == "text" ]; then gh api graphql -f query=" mutation { updateProjectV2ItemFieldValue( input: { - projectId: \"${{ inputs.PROJECT_ID }}\" - itemId: \"${{ inputs.ITEM_PROJECT_ID }}\" - fieldId: \"${{ inputs.FIELD_ID }}\" - value: { ${{ inputs.FIELD_TYPE }}: \"${{ inputs.SET_VALUE }}\" } + projectId: \"${{ env.ENV_PROJECT_ID }}\" + itemId: \"${{ env.ENV_ITEM_PROJECT_ID }}\" + fieldId: \"${{ env.ENV_FIELD_ID }}\" + value: { ${{ env.ENV_FIELD_TYPE }}: \"${{ env.ENV_SET_VALUE }}\" } } ) { projectV2Item { @@ -79,15 +84,15 @@ jobs: } }" - elif [ "${{ inputs.FIELD_TYPE }}" == "number" ]; then + elif [ "${{ env.ENV_FIELD_TYPE }}" == "number" ]; then gh api graphql -f query=" mutation { updateProjectV2ItemFieldValue( input: { - projectId: \"${{ inputs.PROJECT_ID }}\" - itemId: \"${{ inputs.ITEM_PROJECT_ID }}\" - fieldId: \"${{ inputs.FIELD_ID }}\" - value: { ${{ inputs.FIELD_TYPE }}: ${{ inputs.SET_VALUE }} } + projectId: \"${{ env.ENV_PROJECT_ID }}\" + itemId: \"${{ env.ENV_ITEM_PROJECT_ID }}\" + fieldId: \"${{ env.ENV_FIELD_ID }}\" + value: { ${{ env.ENV_FIELD_TYPE }}: ${{ env.ENV_SET_VALUE }} } } ) { projectV2Item { diff --git a/.github/workflows/project-update-linked-issues.yaml b/.github/workflows/project-update-linked-issues.yaml index e382c88c..73be2c4f 100644 --- a/.github/workflows/project-update-linked-issues.yaml +++ b/.github/workflows/project-update-linked-issues.yaml @@ -54,13 +54,18 @@ jobs: id: sync_linked_issues env: GITHUB_TOKEN: ${{ secrets.ADD_TO_PROJECT_GITHUB_TOKEN }} + ENV_PR_NODE_ID: ${{ inputs.PR_NODE_ID }} + ENV_PROJECT_ID: ${{ inputs.PROJECT_ID }} + ENV_UPDATE_FIELD_TYPE: ${{ inputs.UPDATE_FIELD_TYPE }} + ENV_UPDATE_FIELD_ID: ${{ inputs.UPDATE_FIELD_ID }} + ENV_UPDATE_FIELD_VALUE: ${{ inputs.UPDATE_FIELD_VALUE }} run: | # Find the linked issues to the PR # If an issue is passed in, the json will return null and the for loop won't trigger # Potential future improvement could be some nicer error messaging on incorrect input gh api graphql -f query=' query { - node(id: "${{ inputs.PR_NODE_ID }}") { + node(id: "${{ env.ENV_PR_NODE_ID }}") { ... on PullRequest { closingIssuesReferences(first: 10) { nodes { @@ -73,43 +78,43 @@ jobs: } }' > linked_issues.json issue_ids=$(jq -r '.data.node.closingIssuesReferences.nodes[].projectItems.nodes[] | - select(.project.id == "${{ inputs.PROJECT_ID }}") | .id' linked_issues.json) + select(.project.id == "${{ env.ENV_PROJECT_ID }}") | .id' linked_issues.json) for issue_id in $issue_ids; do # Each field type has a different `value` that is needed by the mutation. - if [ "${{ inputs.UPDATE_FIELD_TYPE }}" == "iteration" ]; then + if [ "${{ env.ENV_UPDATE_FIELD_TYPE }}" == "iteration" ]; then gh api graphql -f query=' mutation { updateProjectV2ItemFieldValue( input: { - projectId: "${{ inputs.PROJECT_ID }}" + projectId: "${{ env.ENV_PROJECT_ID }}" itemId: "'"$issue_id"'" - fieldId: "${{ inputs.UPDATE_FIELD_ID }}" - value: {iterationId: "${{ inputs.UPDATE_FIELD_VALUE }}"}}) + fieldId: "${{ env.ENV_UPDATE_FIELD_ID }}" + value: {iterationId: "${{ env.ENV_UPDATE_FIELD_VALUE }}"}}) {projectV2Item {id}}}' - elif [ "${{ inputs.UPDATE_FIELD_TYPE }}" == "single_select" ]; then + elif [ "${{ env.ENV_UPDATE_FIELD_TYPE }}" == "single_select" ]; then gh api graphql -f query=' mutation { updateProjectV2ItemFieldValue( input: { - projectId: "${{ inputs.PROJECT_ID }}" + projectId: "${{ env.ENV_PROJECT_ID }}" itemId: "'"$issue_id"'" - fieldId: "${{ inputs.UPDATE_FIELD_ID }}" - value: {singleSelectOptionId: "${{ inputs.UPDATE_FIELD_VALUE }}"}}) + fieldId: "${{ env.ENV_UPDATE_FIELD_ID }}" + value: {singleSelectOptionId: "${{ env.ENV_UPDATE_FIELD_VALUE }}"}}) {projectV2Item {id}}}' - elif [ "${{ inputs.UPDATE_FIELD_TYPE }}" == "date" ] || \ - [ "${{ inputs.UPDATE_FIELD_TYPE }}" == "text" ]; then + elif [ "${{ env.ENV_UPDATE_FIELD_TYPE }}" == "date" ] || \ + [ "${{ env.ENV_UPDATE_FIELD_TYPE }}" == "text" ]; then gh api graphql -f query=" mutation { updateProjectV2ItemFieldValue( input: { - projectId: \"${{ inputs.PROJECT_ID }}\" + projectId: \"${{ env.ENV_PROJECT_ID }}\" itemId: \"$issue_id\" - fieldId: \"${{ inputs.UPDATE_FIELD_ID }}\" - value: {${{ inputs.UPDATE_FIELD_TYPE }}: \"${{ inputs.UPDATE_FIELD_VALUE }}\"} + fieldId: \"${{ env.ENV_UPDATE_FIELD_ID }}\" + value: {${{ env.ENV_UPDATE_FIELD_TYPE }}: \"${{ env.ENV_UPDATE_FIELD_VALUE }}\"} } ) { projectV2Item { @@ -118,15 +123,15 @@ jobs: } }" - elif [ "${{ inputs.UPDATE_FIELD_TYPE }}" == "number" ]; then + elif [ "${{ env.ENV_UPDATE_FIELD_TYPE }}" == "number" ]; then gh api graphql -f query=" mutation { updateProjectV2ItemFieldValue( input: { - projectId: \"${{ inputs.PROJECT_ID }}\" + projectId: \"${{ env.ENV_PROJECT_ID }}\" itemId: \"$issue_id\" - fieldId: \"${{ inputs.UPDATE_FIELD_ID }}\" - value: {${{ inputs.UPDATE_FIELD_TYPE }}: ${{ inputs.UPDATE_FIELD_VALUE }}}} + fieldId: \"${{ env.ENV_UPDATE_FIELD_ID }}\" + value: {${{ env.ENV_UPDATE_FIELD_TYPE }}: ${{ env.ENV_UPDATE_FIELD_VALUE }}}} ) {projectV2Item {id}}}" else