Skip to content

Commit

Permalink
fix broken graphql connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jrnxf committed Sep 3, 2024
1 parent 55e16b0 commit ab0c5c5
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@ Check out these amazing projects that inspired `gh-eco`!

[![github](https://img.shields.io/github/followers/jrnxf?style=social)](https://github.com/jrnxf)
[![twitter](https://img.shields.io/twitter/follow/_jrnxf?color=white&style=social)](https://twitter.com/_jrnxf)
[![youtube](https://img.shields.io/youtube/channel/subscribers/UCEDfokz6igeN4bX7Whq49-g?style=social)](https://youtube.com/user/thatvegandev)
[![youtube](https://img.shields.io/youtube/channel/subscribers/UCEDfokz6igeN4bX7Whq49-g?style=social)](https://www.youtube.com/@jrnxf)
68 changes: 44 additions & 24 deletions api/github/github.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,71 @@
package github

import (
"context"
"fmt"
"log"
"strings"
"sync"

tea "github.com/charmbracelet/bubbletea"
"github.com/cli/go-gh"
gh "github.com/cli/go-gh/v2"
"github.com/jrnxf/gh-eco/api/github/mutations"
"github.com/jrnxf/gh-eco/api/github/queries"
"github.com/jrnxf/gh-eco/ui/commands"
"github.com/jrnxf/gh-eco/utils"
ghv4 "github.com/shurcooL/githubv4"
graphql "github.com/shurcooL/graphql"
"golang.org/x/oauth2"
)

const GH_ECO_REPO_ID string = "R_kgDOHVAImQ"

func GetUser(login string) tea.Cmd {
return func() tea.Msg {
client, err := gh.GQLClient(nil)
var (
clientInstance *ghv4.Client
once sync.Once
)

// GetClient initializes a GitHub GraphQL client instance with a token obtained from GitHub CLI.
func GetClient() *ghv4.Client {
once.Do(func() {
output, _, err := gh.Exec("auth", "token")
if err != nil {
return commands.GetUserResponse{Err: err}
fmt.Println("Unable to retrieve access token")
}

token := strings.TrimSpace(output.String())
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient := oauth2.NewClient(context.Background(), src)
clientInstance = ghv4.NewClient(httpClient)
})

return clientInstance
}

func GetUser(login string) tea.Cmd {
return func() tea.Msg {
client := GetClient()

var query queries.GetUserQuery

variables := map[string]interface{}{
"login": graphql.String(login),
"first": graphql.Int(6),
}
err = client.Query("GetUser", &query, variables)

err := client.Query(context.Background(), &query, variables)
if err != nil {
fmt.Println(err.Error())
return commands.GetUserResponse{Err: err}
}

return commands.GetUserResponse{User: utils.MapGetUserQueryToDisplayUser(query)}
}
}

func GetReadme(name string, owner string) tea.Cmd {
return func() tea.Msg {
client, err := gh.GQLClient(nil)
if err != nil {
log.Println(err)
return commands.GetReadmeResponse{Err: err}
}
client := GetClient()

var query queries.GetReadmeQuery

Expand All @@ -50,57 +74,53 @@ func GetReadme(name string, owner string) tea.Cmd {
"owner": graphql.String(owner),
"expression": graphql.String("HEAD:README.md"),
}
err = client.Query("GetReadme", &query, variables)

err := client.Query(context.Background(), &query, variables)
if err != nil {
log.Println(err)
return commands.GetReadmeResponse{Err: err}
}

return commands.GetReadmeResponse{Readme: query.Repository.Object.Blob}
}
}

func StarStarrable(starrableId string) tea.Cmd {
return func() tea.Msg {
client, err := gh.GQLClient(nil)
if err != nil {
log.Println(err)
return commands.StarStarrableResponse{Err: err}
}
client := GetClient()

var mutation mutations.AddStarMutation

variables := map[string]interface{}{
"starrableId": graphql.ID(starrableId),
}

err = client.Mutate("StarStarrable", &mutation, variables)
err := client.Mutate(context.Background(), &mutation, variables, nil)
if err != nil {
log.Println(err)
return commands.StarStarrableResponse{Err: err}
}

return commands.StarStarrableResponse{Starrable: mutation.AddStar.Starrable}
}
}

func RemoveStarStarrable(starrableId string) tea.Cmd {
return func() tea.Msg {
client, err := gh.GQLClient(nil)
if err != nil {
log.Println(err)
return commands.RemoveStarStarrableResponse{Err: err}
}
client := GetClient()

var mutation mutations.RemoveStarMutation

variables := map[string]interface{}{
"starrableId": graphql.ID(starrableId),
}

err = client.Mutate("RemoveStarStarrable", &mutation, variables)
err := client.Mutate(context.Background(), &mutation, variables, nil)
if err != nil {
log.Println(err)
return commands.RemoveStarStarrableResponse{Err: err}
}

return commands.RemoveStarStarrableResponse{Starrable: mutation.RemoveStar.Starrable}
}
}
58 changes: 38 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,39 +1,57 @@
module github.com/jrnxf/gh-eco

go 1.17
go 1.21

toolchain go1.22.2

require (
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
github.com/charmbracelet/glamour v0.5.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/cli/go-gh v0.0.3
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
github.com/charmbracelet/bubbles v0.19.0
github.com/charmbracelet/bubbletea v0.27.0
github.com/charmbracelet/glamour v0.7.0
github.com/charmbracelet/lipgloss v0.12.1
github.com/shurcooL/githubv4 v0.0.0-20240727222349-48295856cce7
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466
golang.org/x/oauth2 v0.22.0
golang.org/x/term v0.13.0
)

require (
github.com/alecthomas/chroma/v2 v2.8.0 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/charmbracelet/x/input v0.1.0 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.13.0 // indirect
github.com/alecthomas/chroma v0.10.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cli/go-gh v1.2.1 // indirect
github.com/cli/go-gh/v2 v2.9.0
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.1 // indirect
github.com/cli/shurcooL-graphql v0.0.4 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/microcosm-cc/bluemonday v1.0.21 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/microcosm-cc/bluemonday v1.0.26 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/yuin/goldmark v1.4.4 // indirect
github.com/yuin/goldmark-emoji v1.0.1 // indirect
golang.org/x/net v0.0.0-20221002022538-bcab6841153b // indirect
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
github.com/yuin/goldmark v1.5.4 // indirect
github.com/yuin/goldmark-emoji v1.0.2 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit ab0c5c5

Please sign in to comment.