Retrieve the latest comment from a github issue - go

I am wondering what would be the most efficient way to retrieve the latest comment from a github issue using Go.
I actually know how to do this already but I am not satisfied with the performance so I would love to get some suggestions
package main
import (
"context"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"net/url"
"os"
)
func main() {
owner, repo := "owner", "repo"
token := oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}
ts := oauth2.StaticTokenSource(&token)
ctx := context.Background()
tc := oauth2.NewClient(ctx, ts)
gc := github.NewClient(tc)
gc.BaseURL, _ = url.Parse("https://api.github.com/")
opts := github.IssueListByRepoOptions{}
issues, _, _ := gc.Issues.ListByRepo(ctx, owner, repo, &opts)
// Implement Here: get latest comment for issues[0]
return
}
Thanks in advance :)

You can use Rest API v3 or GraphQL v4. If you plan to loop through a lot of issues, graphQL definitly worth it
Using Rest API v3
Using go-github as you suggested, you can use :
ListComments(ctx context.Context, owner string, repo string, number int, opts *IssueListCommentsOptions)
For example from this test
For example to get the last comment for the last 20 opened issues (from your code).
package main
import (
"context"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"net/url"
"os"
"log"
)
func main() {
owner, repo := "google", "gson"
token := oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}
ts := oauth2.StaticTokenSource(&token)
ctx := context.Background()
tc := oauth2.NewClient(ctx, ts)
gc := github.NewClient(tc)
gc.BaseURL, _ = url.Parse("https://api.github.com/")
opts := github.IssueListByRepoOptions{}
issues, _, _ := gc.Issues.ListByRepo(ctx, owner, repo, &opts)
for i := 0; i < len(issues); i++ {
opt := &github.IssueListCommentsOptions{}
comments, _, err := gc.Issues.ListComments(ctx, owner, repo, *issues[i].Number, opt)
if err != nil {
log.Println(err)
} else if len(comments) > 0 {
log.Println(*comments[0].Body)
} else {
log.Println("no comment for this issue")
}
}
}
It will perform :
one request to get the last 20 opened issues
one request for each issue to get the last comments
So a total of 21 requests
Using GraphQL v4
You can use githubv4 library to use Github GraphQL v4.
The same as previous example in GraphQL would be :
package main
import (
"context"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
"os"
"encoding/json"
"log"
)
func main() {
owner, repo := "google", "gson"
token := oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}
ts := oauth2.StaticTokenSource(&token)
httpClient := oauth2.NewClient(context.Background(), ts)
client := githubv4.NewClient(httpClient)
{
var q struct {
Repository struct {
Issues struct {
Nodes []struct {
Number int
Comments struct {
Nodes []struct {
Body githubv4.String
}
} `graphql:"comments(last:$commentsLast)"`
}
PageInfo struct {
EndCursor githubv4.String
HasNextPage githubv4.Boolean
}
} `graphql:"issues(last:$issuesLast,states:OPEN)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}
variables := map[string]interface{}{
"repositoryOwner": githubv4.String(owner),
"repositoryName": githubv4.String(repo),
"issuesLast": githubv4.NewInt(20),
"commentsLast": githubv4.NewInt(1),
}
err := client.Query(context.Background(), &q, variables)
if err != nil {
log.Println(err)
return
}
printJSON(q)
}
}
func printJSON(v interface{}) {
w := json.NewEncoder(os.Stdout)
w.SetIndent("", "\t")
err := w.Encode(v)
if err != nil {
panic(err)
}
}
This is modification of the example from the github repo
The code above will perform exactly 1 request

Related

How to validate API key with function in Martini?

So I currently have a function that will take in a string APIKey to check it against my Mongo collection. If nothing is found (not authenticated), it returns false - if a user is found, it returns true. My problem, however, is I'm unsure how to integrate this with a Martini POST route. Here is my code:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/go-martini/martini"
_ "github.com/joho/godotenv/autoload"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type User struct {
Name string
APIKey string
}
func validateAPIKey(users *mongo.Collection, APIKey string) bool {
var user User
filter := bson.D{{"APIKey", APIKey}}
if err := users.FindOne(context.TODO(), filter).Decode(&user); err != nil {
fmt.Printf("Found 0 results for API Key: %s\n", APIKey)
return false
}
fmt.Printf("Found: %s\n", user.Name)
return true
}
func uploadHandler() {
}
func main() {
mongoURI := os.Getenv("MONGO_URI")
mongoOptions := options.Client().ApplyURI(mongoURI)
client, _ := mongo.Connect(context.TODO(), mongoOptions)
defer client.Disconnect(context.TODO())
if err := client.Ping(context.TODO(), nil); err != nil {
log.Fatal(err, "Unable to access MongoDB server, exiting...")
}
// users := client.Database("sharex_api").Collection("authorized_users") // commented out when testing to ignore unused warnings
m := martini.Classic()
m.Post("/api/v1/upload", uploadHandler)
m.RunOnAddr(":8085")
}
The validateAPIKey function works exactly as intended if tested alone, I am just unsure how I would run this function for a specific endpoint (in this case, /api/v1/upload).

Golang Gin Redirect and Render a Template with New Variables

I am trying to pass some variables after some processes on my Handler function. How can I redirect to a new page an pass some json variables to this new template?
// main package
func main() {
apiRoutes := gin.Default()
apiRoutes.POST("api/ipg/send", controllers.GatewayIpgSendHandler)
apiRoutes.GET("ipg/:token", controllers.GatewayIpgRequestHandler)
// ... rest of the codes
}
// controllers package
func GatewayIpgRequestHandler(context *gin.Context) {
// some processes that lead to these variables.
wage := 123
amount := 13123
redirectUrl := "www.test.com/callback"
// What should I do here to pass those
// three variables above to an existing `custom-view.tmpl` file
// in my `templates` folder.
}
Here is the php(laravel) equivalent of what I want to do.
You can use cookies or query parameters to pass variables. Use one of the solutions provided in GatewayIpgRequestHandler.
main.go
package main
import (
"github.com/gin-gonic/gin"
"temp/controllers"
)
func main() {
apiRoutes := gin.Default()
apiRoutes.GET("ipg/:token", controllers.GatewayIpgRequestHandler)
apiRoutes.GET("api/callback/cookies", controllers.APICallBackWithCookies)
apiRoutes.GET("api/callback/query_params", controllers.APICallBackWithQueryParams)
apiRoutes.Run()
}
controller.go
package controllers
import (
"github.com/gin-gonic/gin"
"net/http"
"net/url"
)
func APICallBackWithCookies(c *gin.Context) {
wage, err := c.Cookie("wage")
if err != nil {
return
}
amount, err := c.Cookie("amount")
if err != nil {
return
}
c.JSON(http.StatusOK, gin.H{"wage": wage, "amount": amount})
}
func APICallBackWithQueryParams(c *gin.Context) {
wage := c.Query("wage")
amount := c.Query("amount")
c.JSON(http.StatusOK, gin.H{"wage": wage, "amount": amount})
}
func GatewayIpgRequestHandler(c *gin.Context) {
// first solution
c.SetCookie("wage", "123", 10, "/", c.Request.URL.Hostname(), false, true)
c.SetCookie("amount", "13123", 10, "/", c.Request.URL.Hostname(), false, true)
location := url.URL{Path: "/api/callback/cookies",}
c.Redirect(http.StatusFound, location.RequestURI())
// second solution
q := url.Values{}
q.Set("wage", "123")
q.Set("amount", "13123")
location := url.URL{Path: "/api/callback/query_params", RawQuery: q.Encode()}
c.Redirect(http.StatusFound, location.RequestURI())
}

golang comments and docs fields when doing ast.Inspect — why are they blank?

I'm trying to get at the Docs and Comments of structs and struct fields, but I can't seem to be able to do so, they just turn up empty:
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)
func main() {
src := `package test
// Hello
type A struct {
// Where
B int // Are you
}
`
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
panic(err)
}
ast.Inspect(f, func(n ast.Node) bool {
switch t := n.(type) {
case *ast.TypeSpec:
fmt.Println(t.Doc.Text())
case *ast.StructType:
for _, field := range t.Fields.List {
fmt.Println(field.Doc.Text())
fmt.Println(field.Comment.Text())
}
}
return true
})
}
yields three blank lines: https://play.golang.org/p/4Eh9gS-PUg
Saw the similar question Go parser not detecting Doc comments on struct type but when trying to run the accepted example it turns up all empty — so I'm wondering if something has changed since that version.
In order to get comments, you have to pass the parser.ParseComments flag in argument to parser.ParseFile():
parser.ParseFile(fset, "", src, parser.ParseComments)
All possible mode flags are documented here:
https://golang.org/pkg/go/parser/#Mode

how to use redis geo radius command go lang

I'm developing with Go lang and very new to it. I would like to use the redis GEORADIUS command and get back my results including the distance.
I have now used two packages radix.v2 and redigo to try and get results but had no joy. I have no issues using simple commands such as SET and GET.
Any help will be greatly appreciated.
Edit: added code :-)
package main
import (
"fmt"
"github.com/mediocregopher/radix.v2/redis"
//"reflect"
"encoding/json"
)
var (
client *redis.Client
)
type Facility struct {
Id string
Dist string
}
func main() {
client, err := redis.Dial("tcp", "192.168.99.100:6379")
if err != nil {
// handle err
}
surname, err := client.Cmd("GET", "surname").Str()
if err != nil {
// handle err
}
fmt.Println(surname)
reply, _ := client.Cmd("GEORADIUS", "venues", 50, 0, 25, "mi", "WITHDIST", "ASC").Array()
if err != nil {
fmt.Println(err)
}
//fmt.Println(reflect.TypeOf(reply))
//fmt.Println(reply)
var facilities []Facility
for _, results := range reply {
facility, _ := results.Array()
id, _ := facility[0].Str()
dist, _ := facility[1].Str()
facilities = append(facilities, Facility{
Id: id,
Dist: dist,
})
}
//fmt.Println(facilities)
resp, _ := json.Marshal(facilities)
fmt.Fprintf(w, string(resp))
}
Cheers
Stephen
You can look at the code in the redis package.
the file commands.go has all the signatures
a few signatures are below
GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd
GeoPos(key string, members ...string) *GeoPosCmd
GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) GeoRadiusByMember(key, member string, query *GeoRadiusQuery)
GeoDist(key string, member1, member2, unit string) *FloatCmd

Parsing go src, trying to convert *ast.GenDecl to types.Interface

I am trying to parse go source files that contain interfaces and find the interfaces defined methods / signatures. I am using ast to parse the file. I am able to get some high level declarations, such as *ast.GenDecl, but I am not able to get to the next level of determining if this type is an interface and what its methods are.
This is for a scaffolding type problem I am trying to solve where a user defines the interface for a service and a tool will build out the skeleton of the service
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"reflect"
)
func main() {
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service
type ServiceInterface interface {
Create(NewServiceRequest) (JsonResponse, error)
Delete(DelServiceRequest) (JsonResponse, error)
}`, 0)
for _, v := range f.Decls {
switch t := v.(type) {
case *ast.FuncDecl:
fmt.Println("func ", t.Name.Name)
case *ast.GenDecl:
switch x := t.Specs[0].(type) {
default:
fmt.Println(x, reflect.TypeOf(x))
}
default:
fmt.Printf("skipping %t\n", t)
}
}
}
Results in but I cant seem to find anything about the internals of interface declaration at all.
&{<nil> ServiceInterface 0x8202d8260 <nil>} *ast.TypeSpec
When working with the AST, I find it helpful to dump an example using the spew package:
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service ....`)
spew.Dump(f)
I find it easy to write the required code from the spew output.
Here's some code to get you started. It prints interface and method names:
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
)
func main() {
fset := token.NewFileSet()
f, _ := parser.ParseFile(fset, "/tmp/tmp.go", `package service
type ServiceInterface interface {
Create(NewServiceRequest) (JsonResponse, error)
Delete(DelServiceRequest) (JsonResponse, error)
}`, 0)
for _, x := range f.Decls {
if x, ok := x.(*ast.GenDecl); ok {
if x.Tok != token.TYPE {
continue
}
for _, x := range x.Specs {
if x, ok := x.(*ast.TypeSpec); ok {
iname := x.Name
if x, ok := x.Type.(*ast.InterfaceType); ok {
for _, x := range x.Methods.List {
if len(x.Names) == 0 {
continue
}
mname := x.Names[0].Name
fmt.Println("interface:", iname, "method:", mname)
}
}
}
}
}
}
}
http://play.golang.org/p/eNyB7O6FIc

Resources