Skip to content

Commit

Permalink
adding support for json output for orgs and repo
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Nov 21, 2021
1 parent 8894c8c commit 7f7e1d9
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all:
gofmt -s -w .
go build -o org-stats
go build -o codestats
run:
go run main.go
168 changes: 161 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Org-Stats
# Code Stats

This is a small project that will allow for easily calculating stats for all
repos across an organization.
repos across an organization, or a single repository of interest. Other metrics
could be added for other entities beyond repository health too!

🚧️ *under development* 🚧️

Expand All @@ -14,16 +15,74 @@ To build the library:
$ make
```

This will compile an executable, `org-stats` that you can interact with.
This will compile an executable, `codestats` that you can interact with.

## Commands

### Stats
### Repository Stats

```bash
$ ./codestats repo buildsi/build-abi-containers
```

You can also pretty print json:

```bash
$ go run main.go repo buildsi/build-abi-containers --pretty
build-abi-containers

{
"buildsi/build-abi-containers": {
"Stats": [
{
"Name": "Has-Codeowners",
"Pass": false
},
{
"Name": "Has-Maintainers",
"Pass": false
},
{
"Name": "Has-GitHub-Actions",
"Pass": true
},
{
"Name": "Has-CircleCI",
"Pass": false
},
{
"Name": "Has-Travis",
"Pass": false
},
{
"Name": "Has-PullApprove",
"Pass": false
},
{
"Name": "Has-Glide",
"Pass": false
}
],
"Name": "build-abi-containers",
"Branch": "main",
"Url": "",
"Stars": 2,
"Forks": 1,
"Issues": 3,
"Language": "Python",
"Archived": false,
"CreatedAt": "2021-05-23T20:02:51Z",
"UpdatedAt": "2021-10-16T12:37:19Z"
}
}
```

### Organization Stats

If you want to get stats for an org:

```bash
$ ./org-stats stats buildsi
$ ./codestats org buildsi

build-notes
build-si-modeling
Expand All @@ -34,10 +93,105 @@ build-sandbox
...
```

Currently we print the repository names, and then print the result object.
This will eventually generate json output that we can save and pipe into a web interface (to be developed).
You can also add an optional pattern to only parse a subset of repos, or add `--pretty` to pretty print the json.

```bash
$ go run main.go stats buildsi --pattern build-abi-containers --pretty
build-abi-containers
build-abi-containers-results
{
"buildsi": [
{
"Stats": [
{
"Name": "Has-Codeowners",
"Pass": false
},
{
"Name": "Has-Maintainers",
"Pass": false
},
{
"Name": "Has-GitHub-Actions",
"Pass": true
},
{
"Name": "Has-CircleCI",
"Pass": false
},
{
"Name": "Has-Travis",
"Pass": false
},
{
"Name": "Has-PullApprove",
"Pass": false
},
{
"Name": "Has-Glide",
"Pass": false
}
],
"Name": "build-abi-containers",
"Branch": "main",
"Url": "",
"Stars": 2,
"Forks": 1,
"Issues": 3,
"Language": "Python",
"Archived": false,
"CreatedAt": "2021-05-23T20:02:51Z",
"UpdatedAt": "2021-10-16T12:37:19Z"
},
{
"Stats": [
{
"Name": "Has-Codeowners",
"Pass": false
},
{
"Name": "Has-Maintainers",
"Pass": false
},
{
"Name": "Has-GitHub-Actions",
"Pass": true
},
{
"Name": "Has-CircleCI",
"Pass": false
},
{
"Name": "Has-Travis",
"Pass": false
},
{
"Name": "Has-PullApprove",
"Pass": false
},
{
"Name": "Has-Glide",
"Pass": false
}
],
"Name": "build-abi-containers-results",
"Branch": "main",
"Url": "",
"Stars": 1,
"Forks": 0,
"Issues": 0,
"Language": "Python",
"Archived": false,
"CreatedAt": "2021-06-08T23:44:24Z",
"UpdatedAt": "2021-08-29T14:25:50Z"
}
]
}
```


This will eventually generate json output that we can save and pipe into a web interface (to be developed).

### TODO

- save output to json
Expand Down
51 changes: 51 additions & 0 deletions cli/org.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cli

import (
"encoding/json"
"fmt"
"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/vsoch/codestats/github"
)

// Args and flags
type OrgArgs struct {
Orgs []string `desc:"One or more GitHub organization names to parse."`
}
type OrgFlags struct {
Pretty bool `long:"pretty" desc:"If printing to the terminal, print it pretty."`
Pattern string `long:"pattern" desc:"Only include repos that match this regular expression."`
}

var Org = cmd.Sub{
Name: "org",
Alias: "o",
Short: "Generate stats (json) for org projects.",
Flags: &OrgFlags{},
Args: &OrgArgs{},
Run: RunOrg,
}

func init() {
cmd.Register(&Org)
}

func RunOrg(r *cmd.Root, c *cmd.Sub) {
args := c.Args.(*OrgArgs)
flags := c.Flags.(*OrgFlags)

// a lookup of repo results by org
results := map[string][]github.RepoResult{}
for _, org := range args.Orgs {
results[org] = github.GetOrgStats(org, flags.Pattern)
}

// Parse into json
var outJson []byte
if flags.Pretty {
outJson, _ = json.MarshalIndent(results, "", " ")
} else {
outJson, _ = json.Marshal(results)
}
output := string(outJson)
fmt.Printf(output)
}
50 changes: 50 additions & 0 deletions cli/repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cli

import (
"encoding/json"
"fmt"
"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/vsoch/codestats/github"
)

// Args and flags
type RepoArgs struct {
Repos []string `desc:"One or more GitHub repository names to parse."`
}
type RepoFlags struct {
Pretty bool `long:"pretty" desc:"If printing to the terminal, print it pretty."`
}

var Repo = cmd.Sub{
Name: "repo",
Alias: "r",
Short: "Generate stats (json) for repositories.",
Flags: &RepoFlags{},
Args: &RepoArgs{},
Run: RunRepo,
}

func init() {
cmd.Register(&Repo)
}

func RunRepo(r *cmd.Root, c *cmd.Sub) {
args := c.Args.(*RepoArgs)
flags := c.Flags.(*RepoFlags)

// a lookup of repo results by org
results := map[string]github.RepoResult{}
for _, repo := range args.Repos {
results[repo] = github.GetRepoStats(repo)
}

// Parse into json
var outJson []byte
if flags.Pretty {
outJson, _ = json.MarshalIndent(results, "", " ")
} else {
outJson, _ = json.Marshal(results)
}
output := string(outJson)
fmt.Printf(output)
}
37 changes: 0 additions & 37 deletions cli/stats.go

This file was deleted.

19 changes: 18 additions & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package github

import (
"encoding/json"
"github.com/vsoch/org-stats/utils"
"github.com/vsoch/codestats/utils"
"log"
)

Expand Down Expand Up @@ -40,6 +40,23 @@ func GetOrgRepos(orgName string) Repos {
return repos
}

func GetRepo(repoName string) Repository {

url := "https://api.github.com/repos/" + repoName

headers := make(map[string]string)
headers["Accept"] = "application/vnd.github.v3+json"
response := utils.GetRequest(url, headers)

// The response gets parsed into a spack package
repo := Repository{}
err := json.Unmarshal([]byte(response), &repo)
if err != nil {
log.Fatalf("Issue unmarshalling repository data structure\n")
}
return repo
}

func GetCommits(name string, branch string) Commits {
url := "https://api.github.com/repos/" + name + "/commits"

Expand Down

0 comments on commit 7f7e1d9

Please sign in to comment.