GeekFactory

int128.hatenablog.com

GitHub GraphQLで新しいPull Requestを作成する

GitHub GraphQLで新しいPull Requestを作成するにはcreatePullRequest mutationを利用します。RESTの場合と同様に、以下のようにheadとbaseを指定します。

  • head ref: 適用したい変更が含まれるブランチ。cross repositoryの場合はforkされたリポジトリ
  • base ref: 変更を適用するブランチ。cross repositoryの場合はfork元のリポジトリ。一般にはデフォルトブランチ。

本稿の内容はすべてGraphQL API Explorerで再現可能です。

Pull Requestの作成

ここでは以下のPull Requestを作成します。

まず、head repositoryのIDを取得します。

query {
  repository(owner: "octocat", name: "Spoon-Knife") {
    id
  }
}
{
  "data": {
    "repository": {
      "id": "MDEwOlJlcG9zaXRvcnkxMzAwMTky"
    }
  }
}

createPullRequest mutationを実行します。以下の引数が必須です。

repositoryId (ID!) The Node ID of the repository.

baseRefName (String!) The name of the branch you want your changes pulled into. This should be an existing branch on the current repository. You cannot update the base branch on a pull request to point to another repository.

headRefName (String!) The name of the branch where your changes are implemented. For cross-repository pull requests in the same network, namespace head_ref_name with a user like this: username:branch.

title (String!) The title of the pull request.

https://developer.github.com/v4/input_object/createpullrequestinput/

repositoryIdにはhead repositoryのIDを指定します。また、cross repositoryの場合はheadRefNameにユーザ名のプレフィックスを付けます。以下の例を見てください。

mutation {
  createPullRequest(input: {
    repositoryId: "MDEwOlJlcG9zaXRvcnkxMzAwMTky",
    baseRefName: "master",
    headRefName: "int128:patch-1",
    title: "Example",
    body: "Using the GraphQL mutation."
  }) {
    pullRequest {
      id
    }
  }
}
{
  "data": {
    "createPullRequest": {
      "pullRequest": {
        "id": "MDExOlB1bGxSZXF1ZXN0MzY1MDUyNTMy"
      }
    }
  }
}

これで下記のPull Requestが作成されました。

github.com

Pull Requestの検索

前項で作成したPull Requestを検索します。

  1. head refに紐づくPull Requestを検索する。
  2. base repositoryに存在するPull Requestを検索する。

まず、head refに紐づくPull Requestを検索してみましょう。以下のようにassociatedPullRequests connectionを利用します。

{
  repository(owner: "int128", name: "Spoon-Knife") {
    ref(qualifiedName: "refs/heads/patch-1") {
      associatedPullRequests(baseRefName: "master", first: 1) {
        nodes {
          id
          title
        }
      }
    }
  }
}
{
  "data": {
    "repository": {
      "ref": {
        "associatedPullRequests": {
          "nodes": [
            {
              "id": "MDExOlB1bGxSZXF1ZXN0MzY1MDUyNTMy",
              "title": "Example"
            }
          ]
        }
      }
    }
  }
}

head refから検索する方法はhead refが存在する場合にのみ使えます。Pull Requestをマージ(or クローズ)した後にブランチを削除した場合は、head refが存在しないので検索できなくなります。

次は、base repositoryに存在するPull Requestを検索してみましょう。

{
  repository(owner: "octocat", name: "Spoon-Knife") {
    pullRequests(baseRefName: "master", headRefName: "patch-1", first: 1) {
      totalCount
      nodes {
        id
        title
        url
      }
    }
  }
}
{
  "data": {
    "repository": {
      "pullRequests": {
        "totalCount": 620,
        "nodes": [
          {
            "id": "MDExOlB1bGxSZXF1ZXN0MTU5NzIz",
            "title": "Edited index.html via GitHub",
            "url": "https://github.com/octocat/Spoon-Knife/pull/21"
          }
        ]
      }
    }
  }
}

まったく関係のないPull Requestが出てきてしまいました。この方法では patch-1 というhead refのPull Requestがすべて表示されてしまいます。pullRequests connectionでは検索条件にhead repositoryを入れられないため、フォークされたリポジトリがすべて検索対象になる問題があります。

今のところ、head refから検索する方が確実にPull Requestを絞り込めるのでよさそうです。