Github Create Branch with GraphQL

First gather some context:

{
  repository(owner: "gliptak", name: "githubapitest1") {
    id
    defaultBranchRef {
      name
      target {
        oid
      }
    }
  }
}

Substitute repositoryId and oid from above and use the name of the to be created branch:

{
  "input": {
    "clientMutationId": "1234",
    "name": "refs/heads/<BRANCH_NAME>",
    "oid": "<OID>",
    "repositoryId": "<REPOSITORY_ID"
  }
}
mutation AddBranch($input: CreateRefInput!) {
  createRef(input: $input) {
    ref {
      name
    }
    clientMutationId
  }
}

Success returns

{
  "data": {
    "createRef": {
      "ref": {
        "name": "my_branch"
      },
      "clientMutationId": "1234"
    }
  }
}

And in case of error, relevant details are returned

{
  "data": {
    "createRef": null
  },
  "errors": [
    {
      "type": "UNPROCESSABLE",
      "path": [
        "createRef"
      ],
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "message": "A ref named \"refs/heads/my_branch\" already exists in the repository."
    }
  ]
}
comments powered by Disqus

Related