Querying Github using GraphQL and Golang

in #github7 years ago (edited)

Summary

I recently had to compile a list of changes(pull requests that have been merged) to make a changelog for an open source project. This is can be done manually if there are only a few of them but in my case I had around 40 changes that needed to be compiled.

Luckily for me, Github has an api which can be used to automate that and more.

My scripting language for this will be Golang for no particular reason,

  1. Github token

  2. Github explorer to make query

  3. Golang script https://gist.github.com/Lougarou/9a69c31f1e0e505ac2f032f31df4d571

Note: Does not cover how to make a GraphQL query but has an example

Github Token
From: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/

Settings

Settings icon in the user bar

Developer Settings

Personal Access Tokens

Finally generate new token

Generate token button

Github Explorer
Next go to https://developer.github.com/v4/explorer/ and try your GraphQl query

query {
  repository(owner:"flutter", name:"flutter") {
    pullRequests(last: 50, states:MERGED) {
      edges {
        node {
          title
          url
          createdAt
          bodyHTML
          number
        }
      }
    }
  }
}

Notice that we have queried 50 entries with the state required to be MERGED

Golang Script

package main

import (
    "context"
    "fmt"

    "github.com/shurcooL/githubql"
    "golang.org/x/oauth2"
)

var GITHUBTOKEN = "YOUR_TOKEN_HERE"

func main() {
    src := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: GITHUBTOKEN},
    )
    httpClient := oauth2.NewClient(context.Background(), src)
    client := githubql.NewClient(httpClient)

    type pullrequest struct {
        Title     githubql.String
        Url       githubql.String
        CreatedAt githubql.String
        BodyText  githubql.String
        Number    githubql.Int
    }
    var query struct {
        Repository struct {
            PullRequests struct {
                Nodes []pullrequest
            } `graphql:"pullRequests(last: 50, states: MERGED)"`
        } `graphql:"repository(owner: \"flutter\", name: \"flutter\")"`
    }
    err := client.Query(context.Background(), &query, nil)
    if err != nil {
        // Handle error.
        panic(err)
    }
    for i := len(query.Repository.PullRequests.Nodes) - 1; i >= 0; i-- {
        fmt.Printf("* [#%d](%s) - **(All Platforms)** %s\n",
            query.Repository.PullRequests.Nodes[i].Number,
            query.Repository.PullRequests.Nodes[i].Url,
            query.Repository.PullRequests.Nodes[i].Title)

    }
}

For Golang, you will need the following libraries https://github.com/shurcooL/githubv4 and https://godoc.org/golang.org/x/oauth2

The library does not support the format of the query we made in GraphQL. Instead we have to prepare structs for the result and the queries formatted as shown in the gist below.

Finally, we loop through our result and output the changes in a markdown format.

[as shown on https://therandomtechadventure.blogspot.com/ ]

Sort:  

@yogisawesome, I gave you an upvote on your first post! Please give me a follow and I will give you a follow in return!

Please also take a moment to read this post regarding bad behavior on Steemit.

Coin Marketplace

STEEM 0.22
TRX 0.27
JST 0.041
BTC 104893.66
ETH 3859.77
SBD 3.29