Skip to content

Commit

Permalink
Switch Graphql for organization/repositories query closes #10 (#12)
Browse files Browse the repository at this point in the history
* GraphQL

Co-authored-by: adam <hello@admcpr.com>
  • Loading branch information
adam7 and adam authored Dec 15, 2022
1 parent 83f7d95 commit cbbd3a4
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 20 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ require (
github.com/charmbracelet/bubbletea v0.22.1
github.com/charmbracelet/lipgloss v0.6.0
github.com/cli/go-gh v0.1.1
github.com/cli/shurcooL-graphql v0.0.2
)

require (
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.2 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/kr/text v0.2.0 // indirect
Expand Down
24 changes: 13 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func YesNo(b bool) string {
return "No"
}

func buildRepositoryTable(repositories []Repository) table.Model {
func buildRepositoryTable(organizationQuery OrganizationQuery) table.Model {
columns := []table.Column{
{Title: "Name", Width: 20},
{Title: "Issues", Width: 10},
Expand All @@ -61,24 +61,26 @@ func buildRepositoryTable(repositories []Repository) table.Model {
{Title: "Delete Branch On Merge", Width: 10},
}

rows := make([]table.Row, len(repositories))
for i, repo := range repositories {
edges := organizationQuery.Organization.Repositories.Edges

rows := make([]table.Row, len(edges))
for i, repo := range edges {
rows[i] = table.Row{
repo.Name,
YesNo(repo.HasIssues),
YesNo(repo.HasWiki),
YesNo(repo.HasProjects),
YesNo(repo.AllowRebaseMerge),
YesNo(repo.AllowAutoMerge),
YesNo(repo.DeleteBranchOnMerge),
repo.Node.Name,
YesNo(repo.Node.HasIssuesEnabled),
YesNo(repo.Node.HasWikiEnabled),
YesNo(repo.Node.HasProjectsEnabled),
YesNo(repo.Node.RebaseMergeAllowed),
YesNo(repo.Node.AutoMergeAllowed),
YesNo(repo.Node.DeleteBranchOnMerge),
}
}

t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(repositories)),
table.WithHeight(len(edges)),
)

s := table.DefaultStyles()
Expand Down
2 changes: 1 addition & 1 deletion messages.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

type RepositoryListMsg struct {
Repositories []Repository
OrganizationQuery OrganizationQuery
}

type AuthenticationMsg struct {
Expand Down
20 changes: 13 additions & 7 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package main

import (
"fmt"
"log"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/cli/go-gh"
graphql "github.com/cli/shurcooL-graphql"
)

/* Model management */
Expand Down Expand Up @@ -100,7 +102,8 @@ func (m OrganisationModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {

case RepositoryListMsg:
m.RepositoryTable = buildRepositoryTable(msg.Repositories)
// m.RepositoryTable = buildRepositoryTable(msg.Repositories)
m.RepositoryTable = buildRepositoryTable(msg.OrganizationQuery)
return m, nil

case tea.KeyMsg:
Expand All @@ -127,18 +130,21 @@ func (m OrganisationModel) View() string {
}

func (m OrganisationModel) GetRepositories() tea.Msg {
client, err := gh.RESTClient(nil)
client, err := gh.GQLClient(nil)
if err != nil {
return AuthenticationErrorMsg{Err: err}
}
response := []Repository{}

err = client.Get(fmt.Sprintf("orgs/%s/repos", m.Title), &response)
var organizationQuery = OrganizationQuery{}

variables := map[string]interface{}{
"login": graphql.String(m.Title),
"first": graphql.Int(30),
}
err = client.Query("OrganizationRepositories", &organizationQuery, variables)
if err != nil {
fmt.Println(err)
return ErrMsg{Err: err}
log.Fatal(err)
}

return RepositoryListMsg{Repositories: response}
return RepositoryListMsg{OrganizationQuery: organizationQuery}
}
55 changes: 55 additions & 0 deletions queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"time"
)

type OrganizationQuery struct {
Organization struct {
Id string
Repositories struct {
Edges []struct {
Node struct {
Name string
Id string
AutoMergeAllowed bool
DeleteBranchOnMerge bool
RebaseMergeAllowed bool
HasDiscussionsEnabled bool
HasIssuesEnabled bool
HasWikiEnabled bool
HasProjectsEnabled bool
IsArchived bool
IsDisabled bool
IsFork bool
IsLocked bool
IsMirror bool
IsPrivate bool
IsTemplate bool
StargazerCount int
SquashMergeAllowed bool
UpdatedAt time.Time
DefaultBranchRef struct {
Name string
BranchProtectionRule struct {
AllowsDeletions bool
AllowsForcePushes bool
DismissesStaleReviews bool
IsAdminEnforced bool
RequiredApprovingReviewCount int
RequiresApprovingReviews bool
RequiresCodeOwnerReviews bool
RequiresCommitSignatures bool
RequiresConversationResolution bool
RequiresLinearHistory bool
RequiresStatusChecks bool
} `graphql:"branchProtectionRule"`
} `graphql:"defaultBranchRef"`
VulnerabilityAlerts struct {
TotalCount int
} `graphql:"vulnerabilityAlerts"`
}
} `graphql:"edges"`
} `graphql:"repositories(first: $first)"`
} `graphql:"organization(login: $login)"`
}

0 comments on commit cbbd3a4

Please sign in to comment.