Last active
October 26, 2023 06:58
-
-
Save brasic/964dfc371d524a09d602745ae3b238ff to your computer and use it in GitHub Desktop.
createCommitOnBranch error example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
set -euo pipefail | |
# This is an example of intentionally generating an error message from the | |
# `createCommitOnBranch` mutation by passing an out of date `expectedHeadOid` | |
# value. Run this inside a git repository configured with a github remote. | |
# Ensure TOKEN is set to a Personal Access Token with write access to the given | |
# github repo. | |
# | |
# (This script assumes jq is installed for pretty-printing the response JSON) | |
# | |
# For more info on using the mutation see the documentation at: | |
# - https://docs.github.com/en/graphql/reference/mutations#createcommitonbranch | |
# - https://docs.github.com/en/graphql/reference/input-objects#createcommitonbranchinput | |
remote=`git remote |head -1` | |
repoNwo=`git remote get-url $remote | sed 's/.*://;s/\.git//'` | |
branch=`git rev-parse --abbrev-ref HEAD` | |
contents=`echo -e 'Hello, GraphQL!\n' | base64` | |
# note, we are *intentionally* telling the API to append a commit to the branch | |
# only if the tip is a value that we know it is not. Assuming this clone is up to | |
# date with the remote this will always fail with a descriptive error. | |
expectedHeadOid=`git rev-parse HEAD~` | |
curl https://api.github.com/graphql --silent \ | |
\ # echo HTTP status to demonstrate how GraphQL | |
\ # uses HTTP a mere RPC transport, response is 200 OK | |
\ # no matter what errors happen. | |
--write-out '%{stderr}HTTP status: %{response_code}\n\n' | |
-H "Authorization: bearer $TOKEN" \ | |
--data @- <<GRAPHQL | jq | |
{ | |
"query": "mutation (\$input: CreateCommitOnBranchInput!) { | |
createCommitOnBranch(input: \$input) { | |
commit { | |
url | |
} | |
} | |
}", | |
"variables": { | |
"input": { | |
"branch": { | |
"repositoryNameWithOwner": "$repoNwo", | |
"branchName": "$branch" | |
}, | |
"message": { "headline": "Hello from GraphQL! 😍" }, | |
"fileChanges": { | |
"additions": [ | |
{ | |
"path": "GraphQL.md", | |
"contents": "$contents" | |
} | |
] | |
}, | |
"expectedHeadOid": "$expectedHeadOid" | |
} | |
} | |
} | |
GRAPHQL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $ ./demo.sh | |
# HTTP status: 200 | |
{ | |
"data": { | |
"createCommitOnBranch": null | |
}, | |
"errors": [ | |
{ | |
"type": "STALE_DATA", | |
"path": [ | |
"createCommitOnBranch" | |
], | |
"locations": [ | |
{ | |
"line": 1, | |
"column": 52 | |
} | |
], | |
"message": "Expected branch to point to \"f786b7e2e0ec290972a2ada6858217ba16305933\" but it did not. Pull and try again." | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment