1

I'm pretty new to GraphQL and Github Actions, here I'm trying to update an item in my GitHub Project (V2) using the Github CLI and the GraphQL API within the Github Action workflow file, I'm having trouble passing the $bugOptionId to the query properly.

I've check all the variables and they're all correct (based on the previous steps that is not shown here). Here's the snippet of the code:

      - name: Update item status to Bugs
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
          ITEM_ID: ${{ env.ITEM_ID }}
          STATUS_FIELD_ID: ${{ env.STATUS_FIELD_ID }}
          BUGS_OPTION_ID: ${{ env.BUGS_OPTION_ID }}
          PROJECT_ID: ${{ env.PROJECT_ID }}
        run: |
          gh api graphql -f query='
            mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $bugsOptionId: String!) {
              updateProjectV2ItemFieldValue(
                input: {
                  projectId: $projectId,
                  itemId: $itemId,
                  fieldId: $fieldId,
                  value: {singleSelectOptionId: $bugsOptionId}
                }
              ) {
                clientMutationId
              }
            }' -f itemId=$ITEM_ID -f projectId=$PROJECT_ID -f fieldId=$STATUS_FIELD_ID -f bugsOptionId=$BUGS_OPTION_ID

The code above produces this error:

gh: The single select option Id does not belong to the field
{"data":{"updateProjectV2ItemFieldValue":null},"errors":[{"type":"VALIDATION","path":["updateProjectV2ItemFieldValue"],"locations":[{"line":3,"column":5}],"message":"The single select option Id does not belong to the field"}]}
Error: Process completed with exit code 1.

I've tried to put a double quote around the variables:

-f bugsOptionId="$BUGS_OPTION_ID"
BUGS_OPTION_ID: "${{ env.BUGS_OPTION_ID }}"

But nothing works, so Finally I tried to put the $bugOptionId manually like this:

- name: Update item status to Bugs
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
          ITEM_ID: ${{ env.ITEM_ID }}
          STATUS_FIELD_ID: ${{ env.STATUS_FIELD_ID }}
          BUGS_OPTION_ID: "${{ env.BUGS_OPTION_ID }}"
          PROJECT_ID: ${{ env.PROJECT_ID }}
        run: |
          gh api graphql -f query='
            mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!) {
              updateProjectV2ItemFieldValue(
                input: {
                  projectId: $projectId,
                  itemId: $itemId,
                  fieldId: $fieldId,
                  value: {singleSelectOptionId: "acbd1234"}
                }
              ) {
                clientMutationId
              }
            }' -f itemId=$ITEM_ID -f projectId=$PROJECT_ID -f fieldId=$STATUS_FIELD_ID

Of course the real value is not "abcd1234" but it works and the api gives a response of something like this:

{"data":{"updateProjectV2ItemFieldValue":{"clientMutationId":null}}}

Did I miss something? I've been trying to solve this for 3 days reading the docs but couldn't find any solution other than manually put the bugOptionId.

PecorinoDev
  • 136
  • 1
  • 7

2 Answers2

1
  - name: Update item status to Bugs
    env:
      GITHUB_TOKEN: ${{ secrets.PROJECT_BOARD_PAT }}
      ITEM_ID: ${{ env.ITEM_ID }}
      STATUS_FIELD_ID: ${{ env.STATUS_FIELD_ID }}
      BUGS_OPTION_ID: ${{ env.BUGS_OPTION_ID }}
      PROJECT_ID: ${{ env.PROJECT_ID }}
    run: |
      BUGS_OPTION_ID_STR="${{ env.BUGS_OPTION_ID }}"
      gh api graphql -f query='
        mutation($projectId: ID!, $itemId: ID!, $fieldId: ID!, $bugsOptionId: String!) {
          updateProjectV2ItemFieldValue(
            input: {
              projectId: $projectId,
              itemId: $itemId,
              fieldId: $fieldId,
              value: {singleSelectOptionId: $bugsOptionId}
            }
          ) {
            clientMutationId
          }
        }' -f itemId=$ITEM_ID -f projectId=$PROJECT_ID -f fieldId=$STATUS_FIELD_ID -f bugsOptionId=$BUGS_OPTION_ID_STR

This solves my problem by adding BUGS_OPTION_ID_STR="${{ env.BUGS_OPTION_ID }}", it "converts" the BUGS_OPTION_ID to String before the api is executed.

PecorinoDev
  • 136
  • 1
  • 7
1

While you have already found your own solution, I found another one I wish to share. One that does not involve github actions.

I had found that my value for singleSelectOptionId was quoted. As in the value was "651c4cac". This was due to how I used jq to grab the ID from a previous query. After changing the jq invocation for my column ID to produce raw output (-r), the query succeeded.