GitHub GraphQL APIで新しいブランチを作成できるようになっていたので試してみました。本記事の内容はGraphQL API Explorerで実行できます。
リポジトリに新しいブランチやタグを作成するにはcreateRef mutationを利用します。createRefを実行するには以下の引数が必要です。
name
(String!) The fully qualified name of the new Ref (ie: refs/heads/my_new_branch).
oid
(GitObjectID!) The GitObjectID that the new Ref shall target. Must point to a commit.
repositoryId
(ID!) The Node ID of the Repository to create the Ref in.
リポジトリID(repositoryId
)とコミットID(oid
)を取得するには以下のクエリを実行します。リポジトリ名やブランチ名は必要なものに置き換えてください。
query GetCommitID { repository(owner: "int128", name: "sandbox") { id ref(qualifiedName: "refs/heads/master") { target { oid } } } }
{ "data": { "repository": { "id": "MDEwOlJlcG9zaXRvcnk2OTgzMjM2Ng==", "ref": { "target": { "oid": "7a59fd9a6334706b942f1707531a0cf8a8523ce7" } } } } }
クエリの実行結果からリポジトリIDは MDEwOlJlcG9zaXRvcnk2OTgzMjM2Ng==
、コミットIDは 7a59fd9a6334706b942f1707531a0cf8a8523ce7
であることが分かります。これらのIDをcreateRefに渡します。
以下のmutationを実行するとexample1ブランチが作成されます。
mutation CreateBranch { createRef(input: {repositoryId: "MDEwOlJlcG9zaXRvcnk2OTgzMjM2Ng==", name: "refs/heads/example1", oid: "7a59fd9a6334706b942f1707531a0cf8a8523ce7"}) { clientMutationId } }
REST APIの場合は1回のリクエストでブランチ作成を実行できますが、GraphQLの場合はqueryとmutationで2回のリクエストが必要です。