Skip to content

Instantly share code, notes, and snippets.

@EverlastingBugstopper
Last active August 24, 2022 10:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save EverlastingBugstopper/d6aa0d9a49bcf39f2df53e1cfb9bb88a to your computer and use it in GitHub Desktop.
Save EverlastingBugstopper/d6aa0d9a49bcf39f2df53e1cfb9bb88a to your computer and use it in GitHub Desktop.
A bash script to parse the JSON output of `rover subgraph check` or `rover graph check`
#! /bin/bash
# This script performs a subgraph check and generates a markdown file from Rover's JSON output
# Change the following line to refer to your graph ref, schema, and subgraph name
CHECK_OUTPUT=$(rover subgraph check my-graph --schema ./products.graphql --name products --output json)
CHECK_SUCCESS=$(echo $CHECK_OUTPUT | jq '.data.success')
if [ $CHECK_SUCCESS = "true" ]; then
echo "### Check Success"
echo ""
echo "#### Change Details"
echo ""
fi
CHANGES=$(echo "${CHECK_OUTPUT}" | jq '.data.changes')
if [ "$CHANGES" != "null" ]; then
CHANGE_SIZE=$(echo "${CHECK_OUTPUT}" | jq -r '.data.changes | length')
if [ "$CHANGE_SIZE" = "0" ]; then
echo "No changes"
fi
if [ "$CHANGE_SIZE" != "0" ]; then
echo "| Severity | Code | Description |"
echo "| -------- | ---- | ----------- |"
for CHANGE in $(echo "${CHECK_OUTPUT}" | jq -r '.data.changes[] | @base64'); do
_jq() {
echo ${CHANGE} | base64 -d | jq -r ${1}
}
CHANGE_SEVERITY=$(_jq '.severity')
CHANGE_CODE=$(_jq '.code')
CHANGE_DESCRIPTION=$(_jq '.description')
echo "| $CHANGE_SEVERITY | $CHANGE_CODE | $CHANGE_DESCRIPTION |"
done
fi
echo ""
fi
if [ $CHECK_SUCCESS = "false" ]; then
echo "### Check Failure"
echo ""
echo $CHECK_OUTPUT | jq -r '.error.message'
echo ""
echo "#### Error Details"
echo ""
for BUILD_ERROR in $(echo "${CHECK_OUTPUT}" | jq -r '.error.details.build_errors[] | @base64'); do
_jq() {
echo ${BUILD_ERROR} | base64 -d | jq -r ${1}
}
BUILD_ERROR_CODE=$(_jq '.code')
if [ $BUILD_ERROR_CODE != "null" ]; then
echo -n "$BUILD_ERROR_CODE: "
else
echo -n "UNKNOWN: "
fi
echo $(_jq '.message')
done
fi
CHECK_URL=$(echo $CHECK_OUTPUT | jq '.data.target_url')
if [ $CHECK_URL != null ]; then
echo ""
echo "See more details in [Apollo Studio]($CHECK_URL)"
fi
echo ""
@busches
Copy link

busches commented Sep 23, 2021

Note I had to make the following change to get this to work on OSX:

CHANGES=$(echo "${CHECK_OUTPUT}" | jq '.data.changes')

if [ "$CHANGES" != "null" ]; then

Likely could have used [[ since this is bash, but I will ultimately have to send it to sh on CI.

@EverlastingBugstopper
Copy link
Author

Thanks @busches - I've updated the gist w/your suggested changes :)

@busches
Copy link

busches commented Sep 23, 2021

I tweaked a few more times to get display something when no changes are present, instead of an empty table. And apparently alpine doesn't support base64 --decode only base64 -d so I made that change too. https://gist.github.com/busches/811012cd1df8243efcbef71ae5c90410
Thanks for the script, much better than sending the plain output to GH :)

@EverlastingBugstopper
Copy link
Author

Glad it helped! We'll definitely want to get proper markdown support at some point but there's a bunch of other really neat stuff cooking as well - glad this stopgap will work for you

@Collin3
Copy link

Collin3 commented Oct 13, 2021

Hey! I also wanted to thank you for the inspiration/jumping off point 🙏. I made a few minor tweaks as well. If you like them, feel free to add here else ignore them. You can view our script here and the changes are more or less the following things:

  1. Wrapped the Change Details Table into a <details> tag so it was collapsable and prevented the comment from being a wall of text
  2. Moved the Error if block up above the change details because we found it was possible to have change details AND be an error status
  3. Strip out the " from the URL so the github link displays as a link instead of text
  4. Used github emojis instead of text for the success/failure status ✅ / ❌

Ends up looking like this an then if you open up the Change Details dropdown you see the table
Screen Shot 2021-10-13 at 2 03 39 PM

@adiii717
Copy link

Seems like the response body is now change, and most of the object is not available and the script seems broken

{
 "json_version": "1",
 "data": {
   "target_url": "url?variant=env",
   "operation_check_count": 259,
   "changes": [
     {
       "code": "TYPE_REMOVED",
       "description": "type `DeletedAlluser`: removed",
       "severity": "FAIL"
     }
     {
       "code": "FIELD_REMOVED",
       "description": "type `ABC`: field `esMessagesDeleted` removed",
       "severity": "FAIL"
     },
     {
       "code": "FIELD_REMOVED",
       "description": "type `Mutation`: field `XYZ` removed",
       "severity": "FAIL"
     }
   ],
   "failure_count": 3,
   "success": false
 },
 "error": {
   "message": "This operation check has encountered 4 schema changes that would break operations from existing client traffic.",
   "code": "E030"
 }
}

@adiii717
Copy link

come up with this
https://gist.github.com/adiii717/a5428c0f91cb56521d7d5f2f834c7309

### | Severity | Code | Description |
| -------- | ---- | ----------- |
| FAIL | TYPE_REMOVED | type `demo`: removed |
| FAIL | FIELD_REMOVED | type `demo`: field `demo` removed |
| FAIL | FIELD_REMOVED | type `demo`: field `demo` removed |
| FAIL | FIELD_REMOVED | type `demo`: field `demo` removed |
### Check Failure
This operation check has encountered 4 schema changes that would break operations from existing client traffic.

### Status: ❌
StatusCode: E030
Error Message: This operation check has encountered 4 schema changes that would break operations from existing client traffic.

See more details in [Apollo Studio]

and I was on gitlab so this work for me

  - export RESULT=$(curl -s https://gist.githubusercontent.com/adiii717/a5428c0f91cb56521d7d5f2f834c7309/raw/77ab4077617f252ba1a675e77fb67cd284655d93/rover_check_markdown.sh | bash -s -- "rover subgraph check demo@develop -s demo.gql --name demo --output json")
  - echo "${RESULT}" > result.md && export MD_RESULT=$(awk 'ORS="\n\n"' result.md)
  - curl --request POST --header "Content-Type:multipart/form-data" --header "PRIVATE-TOKEN:${GITLAB_TOKEN}" "https://gitlab.com/api/v4/projects/${CI_MERGE_REQUEST_PROJECT_ID}/merge_requests/${CI_MERGE_REQUEST_IID}/notes?body"  -d  "body=$MD_RESULT"
  

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment