10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Apps ScriptでGithub GraphQL API v4を叩く

Last updated at Posted at 2018-06-27

やりたいこと

  • gasからGithub GraphQL API v4を叩きたい
  • GraphQLをどうやってリクエストにのせればよいのかわからない
  • わかった!

どうやったか

実際に動かしているのはこれです(typescriptで書いてます)
https://github.com/abeyuya/gas-slack-bot-aoba/blob/master/src/lib/github.ts

以下に関係する箇所を抜粋しておきます


const accessToken = process.env.GITHUB_ACCESS_TOKEN || "";
const endpoint = "https://api.github.com/graphql";

const graphql = `
{
  user(login: "abeyuya") {
    organizations(last: 5) {
      nodes {
        name
        url
        repositories(last:100) {
          nodes {
            name
            url
            pullRequests(last:30 states:[OPEN] ) {
              nodes {
                url
                title
                reviewRequests(last:10) {
                  nodes {
                    requestedReviewer {
                      ... on User {
                        url
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
`;

const buildRequestOption = (graphql: string) => {
  return {
    method: "post",
    contentType: "application/json",
    headers: {
      Authorization: `bearer ${accessToken}`,
    },
    payload: JSON.stringify({ query: graphql }),
  };
};


export const getAssignedPullRequests = () => {
  const option = buildRequestOption(graphql);
  const res = UrlFetchApp.fetch(endpoint, option);
  const json = JSON.parse(res.getContentText());
  return json;
};

graphql自体は こちら のexplorerで試行錯誤して書いて、その結果をコピペしてきました

10
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?