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