Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@busches 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

This comment has been minimized.

Copy link
Owner Author

@EverlastingBugstopper EverlastingBugstopper commented Sep 23, 2021

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

@busches

This comment has been minimized.

Copy link

@busches 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

This comment has been minimized.

Copy link
Owner Author

@EverlastingBugstopper EverlastingBugstopper commented Sep 23, 2021

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

This comment has been minimized.

Copy link

@Collin3 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

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