1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
// githubv4dev is a test program currently being used for developing githubv4 package.
//
// Warning: It performs some queries and mutations against real GitHub API.
//
// It's not meant to be a clean or readable example. But it's functional.
// Better, actual examples will be created in the future.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
func main() {
flag.Parse()
err := run()
if err != nil {
log.Println(err)
}
}
func run() error {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_GRAPHQL_TEST_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
client := githubv4.NewClient(httpClient)
// Query some details about a repository, an issue in it, and its comments.
{
type githubV4Actor struct {
Login githubv4.String
AvatarURL githubv4.URI `graphql:"avatarUrl(size:72)"`
URL githubv4.URI
}
var q struct {
Repository struct {
DatabaseID githubv4.Int
URL githubv4.URI
Issue struct {
Author githubV4Actor
PublishedAt githubv4.DateTime
LastEditedAt *githubv4.DateTime
Editor *githubV4Actor
Body githubv4.String
ReactionGroups []struct {
Content githubv4.ReactionContent
Users struct {
Nodes []struct {
Login githubv4.String
}
TotalCount githubv4.Int
} `graphql:"users(first:10)"`
ViewerHasReacted githubv4.Boolean
}
ViewerCanUpdate githubv4.Boolean
Comments struct {
Nodes []struct {
Body githubv4.String
Author struct {
Login githubv4.String
}
Editor struct {
Login githubv4.String
}
}
PageInfo struct {
EndCursor githubv4.String
HasNextPage githubv4.Boolean
}
} `graphql:"comments(first:$commentsFirst,after:$commentsAfter)"`
} `graphql:"issue(number:$issueNumber)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
Viewer struct {
Login githubv4.String
CreatedAt githubv4.DateTime
ID githubv4.ID
DatabaseID githubv4.Int
}
RateLimit struct {
Cost githubv4.Int
Limit githubv4.Int
Remaining githubv4.Int
ResetAt githubv4.DateTime
}
}
variables := map[string]interface{}{
"repositoryOwner": githubv4.String("shurcooL-test"),
"repositoryName": githubv4.String("test-repo"),
"issueNumber": githubv4.Int(1),
"commentsFirst": githubv4.NewInt(1),
"commentsAfter": githubv4.NewString("Y3Vyc29yOjE5NTE4NDI1Ng=="),
}
err := client.Query(context.Background(), &q, variables)
if err != nil {
return err
}
printJSON(q)
//goon.Dump(out)
//fmt.Println(github.Stringify(out))
}
// Toggle a 👍 reaction on an issue.
//
// That involves first doing a query (and determining whether the reaction already exists),
// then either adding or removing it.
{
var q struct {
Repository struct {
Issue struct {
ID githubv4.ID
Reactions struct {
ViewerHasReacted githubv4.Boolean
} `graphql:"reactions(content:$reactionContent)"`
} `graphql:"issue(number:$issueNumber)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}
variables := map[string]interface{}{
"repositoryOwner": githubv4.String("shurcooL-test"),
"repositoryName": githubv4.String("test-repo"),
"issueNumber": githubv4.Int(2),
"reactionContent": githubv4.ReactionContentThumbsUp,
}
err := client.Query(context.Background(), &q, variables)
if err != nil {
return err
}
fmt.Println("already reacted:", q.Repository.Issue.Reactions.ViewerHasReacted)
if !q.Repository.Issue.Reactions.ViewerHasReacted {
// Add reaction.
var m struct {
AddReaction struct {
Subject struct {
ReactionGroups []struct {
Content githubv4.ReactionContent
Users struct {
TotalCount githubv4.Int
}
}
}
} `graphql:"addReaction(input:$input)"`
}
input := githubv4.AddReactionInput{
SubjectID: q.Repository.Issue.ID,
Content: githubv4.ReactionContentThumbsUp,
}
err := client.Mutate(context.Background(), &m, input, nil)
if err != nil {
return err
}
printJSON(m)
fmt.Println("Successfully added reaction.")
} else {
// Remove reaction.
var m struct {
RemoveReaction struct {
Subject struct {
ReactionGroups []struct {
Content githubv4.ReactionContent
Users struct {
TotalCount githubv4.Int
}
}
}
} `graphql:"removeReaction(input:$input)"`
}
input := githubv4.RemoveReactionInput{
SubjectID: q.Repository.Issue.ID,
Content: githubv4.ReactionContentThumbsUp,
}
err := client.Mutate(context.Background(), &m, input, nil)
if err != nil {
return err
}
printJSON(m)
fmt.Println("Successfully removed reaction.")
}
}
return nil
}
// printJSON prints v as JSON encoded with indent to stdout. It panics on any error.
func printJSON(v interface{}) {
w := json.NewEncoder(os.Stdout)
w.SetIndent("", "\t")
err := w.Encode(v)
if err != nil {
panic(err)
}
}
|