Skip to content

Commit

Permalink
adding support for GitHub token from env
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 7f7e1d9 commit dbc091b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ This will compile an executable, `codestats` that you can interact with.
$ ./codestats repo buildsi/build-abi-containers
```

You can also save to file:

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

You can also pretty print json:

```bash
Expand Down Expand Up @@ -194,9 +200,7 @@ This will eventually generate json output that we can save and pipe into a web i

### TODO

- save output to json
- allow for custom config to specify metrics/stats desired
- allow to provide GitHub token for use
- need way to also customize rendering of stats - should go handle the UI generation?
- should there be a common format for a stat, beyond hard coding? E.g., most seem like checking if something exists - this could be YAML

Expand Down
12 changes: 9 additions & 3 deletions cli/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/vsoch/codestats/github"
"io/ioutil"
)

// Args and flags
Expand All @@ -14,6 +15,7 @@ type OrgArgs struct {
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."`
Outfile string `long:"outfile" desc:"Save output to file."`
}

var Org = cmd.Sub{
Expand Down Expand Up @@ -41,11 +43,15 @@ func RunOrg(r *cmd.Root, c *cmd.Sub) {

// Parse into json
var outJson []byte
if flags.Pretty {
if flags.Pretty || flags.Outfile != "" {
outJson, _ = json.MarshalIndent(results, "", " ")
} else {
outJson, _ = json.Marshal(results)
}
output := string(outJson)
fmt.Printf(output)

if flags.Outfile != "" {
_ = ioutil.WriteFile(flags.Outfile, outJson, 0644)
} else {
fmt.Printf(string(outJson))
}
}
13 changes: 9 additions & 4 deletions cli/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"fmt"
"github.com/DataDrake/cli-ng/v2/cmd"
"github.com/vsoch/codestats/github"
"io/ioutil"
)

// 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."`
Pretty bool `long:"pretty" desc:"If printing to the terminal, print it pretty."`
Outfile string `long:"outfile" desc:"Save output to file."`
}

var Repo = cmd.Sub{
Expand Down Expand Up @@ -40,11 +42,14 @@ func RunRepo(r *cmd.Root, c *cmd.Sub) {

// Parse into json
var outJson []byte
if flags.Pretty {
if flags.Pretty || flags.Outfile != "" {
outJson, _ = json.MarshalIndent(results, "", " ")
} else {
outJson, _ = json.Marshal(results)
}
output := string(outJson)
fmt.Printf(output)
if flags.Outfile != "" {
_ = ioutil.WriteFile(flags.Outfile, outJson, 0644)
} else {
fmt.Printf(string(outJson))
}
}
37 changes: 21 additions & 16 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ package github

import (
"encoding/json"
"github.com/vsoch/codestats/utils"
"fmt"
"log"
)

func GetReleases(name string) Releases {
"os"

url := "https://api.github.com/repos/" + name + "/releases"
"github.com/vsoch/codestats/utils"
)

func githubGetRequest(url string) string {
headers := make(map[string]string)
token := os.Getenv("GITHUB_TOKEN")
headers["Accept"] = "application/vnd.github.v3+json"
response := utils.GetRequest(url, headers)
if token != "" {
headers["Authorization"] = fmt.Sprintf("token %s", token)
}
return utils.GetRequest(url, headers)
}

func GetReleases(name string) Releases {

response := githubGetRequest("https://api.github.com/repos/" + name + "/releases")

// The response gets parsed into a spack package
releases := Releases{}
Expand All @@ -25,11 +34,7 @@ func GetReleases(name string) Releases {

func GetOrgRepos(orgName string) Repos {

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

headers := make(map[string]string)
headers["Accept"] = "application/vnd.github.v3+json"
response := utils.GetRequest(url, headers)
response := githubGetRequest("https://api.github.com/orgs/" + orgName + "/repos")

// The response gets parsed into a spack package
repos := Repos{}
Expand All @@ -42,11 +47,7 @@ func GetOrgRepos(orgName string) 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)
response := githubGetRequest("https://api.github.com/repos/" + repoName)

// The response gets parsed into a spack package
repo := Repository{}
Expand All @@ -63,6 +64,10 @@ func GetCommits(name string, branch string) Commits {
headers := make(map[string]string)
headers["Accept"] = "application/vnd.github.v3+json"
headers["Sha"] = branch
token := os.Getenv("GITHUB_TOKEN")
if token != "" {
headers["Authorization"] = fmt.Sprintf("token %s", token)
}
response := utils.GetRequest(url, headers)

commits := Commits{}
Expand Down

0 comments on commit dbc091b

Please sign in to comment.