Skip to content

Instantly share code, notes, and snippets.

@brasic
Created November 1, 2022 21:27
Show Gist options
  • Save brasic/0c3f587e1f43a57eb1fd158ceb707422 to your computer and use it in GitHub Desktop.
Save brasic/0c3f587e1f43a57eb1fd158ceb707422 to your computer and use it in GitHub Desktop.
create-branch-and-commit.sh
#!/usr/bin/env sh
set -euo pipefail
remote=`git remote |head -1`
repoNwo=`git remote get-url $remote | sed 's/.*://;s/\.git//'`
repoNodeId=`gh api repos/$repoNwo --jq '.node_id'`
branch=`git rev-parse --abbrev-ref HEAD`
newBranch="new-branch-$RANDOM"
contents=`echo -e 'Hello, GraphQL!\n' | base64`
expectedHeadOid=`git rev-parse HEAD`
curl https://api.github.com/graphql --silent \
-H "Authorization: bearer $TOKEN" \
-H "Accept: application/vnd.github.update-refs-preview+json" \
--data @- <<GRAPHQL | jq
{
"query": "mutation (\$refsInput: UpdateRefsInput!, \$commitInput: CreateCommitOnBranchInput!) {
updateRefs(input: \$refsInput) {
clientMutationId
}
createCommitOnBranch(input: \$commitInput) {
commit {
url
}
}
}",
"variables": {
"refsInput": {
"clientMutationId": "create-$newBranch-at-$expectedHeadOid",
"repositoryId": "$repoNodeId",
"refUpdates": [
{
"name": "refs/heads/$newBranch",
"beforeOid": "0000000000000000000000000000000000000000",
"afterOid": "$expectedHeadOid"
}
]
},
"commitInput": {
"branch": {
"repositoryNameWithOwner": "$repoNwo",
"branchName": "$newBranch"
},
"message": { "headline": "Hello from GraphQL!" },
"fileChanges": {
"additions": [
{
"path": "GraphQL-$RANDOM.md",
"contents": "$contents"
}
]
},
"expectedHeadOid": "$expectedHeadOid"
}
}
}
GRAPHQL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment