Skip to content

Commit

Permalink
feat: get most used language for repo (#133)
Browse files Browse the repository at this point in the history
* feature: add helper methods for the Set
* add better lint support
  • Loading branch information
Mark deVilliers committed Jun 6, 2023
1 parent 5a786e6 commit 8222a81
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Linting
GOLANGCI_LINT_VERSION=1.51.2
GOLANGCI_LINT_VERSION=1.52.2

# Build a binary
.PHONY: build
Expand All @@ -15,11 +15,12 @@ test:

# The linting gods must be obeyed
.PHONY: lint
lint: ./bin/golangci-lint
./bin/golangci-lint run
lint: ./bin/$(GOLANGCI_LINT_VERSION)/golangci-lint
./bin/$(GOLANGCI_LINT_VERSION)/golangci-lint run

./bin/golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v$(GOLANGCI_LINT_VERSION)
./bin/$(GOLANGCI_LINT_VERSION)/golangci-lint:
mkdir -p ./bin/$(GOLANGCI_LINT_VERSION)/
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin/$(GOLANGCI_LINT_VERSION) v$(GOLANGCI_LINT_VERSION)

# Generate the mocks (embedded via go generate)
.PHONY: mocks
Expand Down
35 changes: 26 additions & 9 deletions pkg/gh/get_repo_details.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@ import (
"context"
"fmt"

"github.com/mdevilliers/org-scrounger/pkg/util"
"github.com/shurcooL/githubv4"
)

type Repository struct {
Name githubv4.String `json:"name"`
URL githubv4.String `json:"url"`
IsArchived githubv4.Boolean `json:"is_archived"`
Languages struct {
Edges []struct {
Size githubv4.Int `json:"size"`
} `graphql:"edges" json:"edges"`
Nodes []struct {
Name githubv4.String `json:"name"`
} `json:"nodes"`
} `json:"languages" graphql:"languages(first:10)"`
Ref struct {
Languages Languages `json:"languages" graphql:"languages(first:10)"`
Ref struct {
Target struct {
Commit struct {
Message githubv4.String `json:"message"`
Expand All @@ -46,6 +40,29 @@ type Repository struct {
VulnerabilityAlerts `graphql:"vulnerabilityAlerts(first:100, states:[OPEN])" json:"vulnerability_alerts"`
}

type Languages struct {
Edges []struct {
Size githubv4.Int `json:"size"`
} `graphql:"edges" json:"edges"`
Nodes []struct {
Name githubv4.String `json:"name"`
} `json:"nodes"`
}

func (l Languages) Top() string {

if len(l.Nodes) == 0 {
return ""
}

s := util.NewSet[string]()
for i, n := range l.Nodes {
s.AddWithValue(string(n.Name), int(l.Edges[i].Size))
}
ret, _ := s.TopValue()
return ret
}

type CheckRun struct {
Name githubv4.String `json:"name,omitempty"`
Summary githubv4.String `json:"summary,omitempty"`
Expand Down
24 changes: 24 additions & 0 deletions pkg/util/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ func (s Set[T]) Add(c T) {
}
}

// Add an item, incrementing the count
func (s Set[T]) AddWithValue(c T, n int) {
_, found := s[c]
if found {
s[c] += n
} else {
s[c] = n
}
}

// TopValue returns the key with the highest value and its value
func (s Set[T]) TopValue() (T, int) {

n := 0
var ret T
for k, v := range s {
if v > n {
n = v
ret = k
}
}
return ret, n
}

// Keys returns a slice of keys
func (s Set[T]) Keys() []T {
r := []T{}
Expand Down
21 changes: 21 additions & 0 deletions pkg/util/set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package util

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_Set(t *testing.T) {

s := NewSet[string]()
s.AddWithValue("foo", 123)
s.AddWithValue("bar", 456)
s.AddWithValue("bar", 1)

v, n := s.TopValue()

require.Equal(t, 457, n)
require.Equal(t, "bar", v)

}

0 comments on commit 8222a81

Please sign in to comment.