Skip to content

Commit

Permalink
Merge pull request #49 from Jmainguy/output
Browse files Browse the repository at this point in the history
feat: add output options as flags
  • Loading branch information
Jmainguy authored Mar 24, 2024
2 parents 97b3ea9 + 29687a0 commit 43d0741
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 16 deletions.
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ token: e0e9eac4e84446df6f3db180d07bfb222e91234
Running the progam
```
ghreport
ghreport [-output=<format>]
```

Sample output
### Sample output with default format.

```
jmainguy@fedora:~/Github/ghreport$ ./ghreport
Expand All @@ -60,6 +60,57 @@ https://github.com/Standouthost/Multicraft/pull/28
mergeable ✅
```

### Sample output with single-line format

```
jmainguy@fedora:~/Github/ghreport$ ./ghreport -output singleline
https://github.com/Jmainguy/coastie-operator/pull/2 author: dependabot Age: 409 days reviewDecision: 🔍 mergeable: ✅
https://github.com/Jmainguy/coastie-operator/pull/3 author: dependabot Age: 384 days reviewDecision: 🔍 mergeable: ✅
https://github.com/Jmainguy/coastie-operator/pull/4 author: renovate Age: 354 days reviewDecision: 🔍 mergeable: ✅
https://github.com/Standouthost/Multicraft/pull/9 author: TheWebGamer Age: 3321 days reviewDecision: ✅ mergeable: ❌
```


### Sample output with JSON format

```
jmainguy@fedora:~/Github/ghreport$ ./ghreport -output json
{"url":"https://github.com/Jmainguy/k8sCapcity/pull/50","author":"github-actions","age":"24 days","review_decision":"😅","mergeable":"✅"}
{"url":"https://github.com/Jmainguy/k8sDrainReport/pull/27","author":"github-actions","age":"8 days","review_decision":"😅","mergeable":"✅"}
{"url":"https://github.com/Standouthost/Multicraft/pull/9","author":"TheWebGamer","age":"3321 days","review_decision":"✅","mergeable":"❌"}
```

### Fun with jq and JSON
#### Select on repo name
```
jmainguy@fedora:~/Github/ghreport$ ./ghreport -output=json | jq '. | select(.url | contains("k8sCapcity"))'
{
"url": "https://github.com/Jmainguy/k8sCapcity/pull/50",
"author": "github-actions",
"age": "24 days",
"review_decision": "😅",
"mergeable": "✅"
}
```
#### Select on author
```
jmainguy@fedora:~/Github/ghreport$ ./ghreport -output=json | jq '. | select(.author == "github-actions")'
{
"url": "https://github.com/Jmainguy/k8sDrainReport/pull/27",
"author": "github-actions",
"age": "8 days",
"review_decision": "😅",
"mergeable": "✅"
}
{
"url": "https://github.com/Jmainguy/k8sCapcity/pull/50",
"author": "github-actions",
"age": "24 days",
"review_decision": "😅",
"mergeable": "✅"
}
```

## Releases
We currently build releases for RPM, DEB, macOS, and Windows.

Expand Down
71 changes: 66 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"text/tabwriter"
"time"
)

// Flags : Define a struct to hold command-line flags
type Flags struct {
OutputFormat string
}

func main() {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
// Parse command-line flags
flags := parseFlags()

config, err := getConfig()
if err != nil {
Expand Down Expand Up @@ -60,6 +68,8 @@ func main() {
repoMap[strings.ToLower(repoString)] = true
}

w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)

// List of repos to watch

for ownerRepo := range repoMap {
Expand All @@ -83,11 +93,62 @@ func main() {

mergeableEmoji := getMergeableEmoji(string(pr.Mergeable))

fmt.Fprintf(w, "%s\n\tauthor: %s\n\tAge: %s \n\treviewDecision: %s\n\tmergeable %s\n", pr.URL, pr.Owner, timeLabel, reviewDecisionEmoji, mergeableEmoji)

//fmt.Fprintf(w, "%s:\tauthor: %s\tcreatedAt %s\treviewDecison %s\tmergeable %s\n", pr.URL, pr.Owner, pr.CreatedAt, pr.ReviewDecision, pr.Mergeable)
// Choose output format based on the flag
switch flags.OutputFormat {
case "json":
outputJSON(pr, timeLabel, reviewDecisionEmoji, mergeableEmoji)
case "singleline":
outputSingleLine(pr, timeLabel, reviewDecisionEmoji, mergeableEmoji)
default:
outputDefault(w, pr, timeLabel, reviewDecisionEmoji, mergeableEmoji)
}
}
}

w.Flush()
if flags.OutputFormat == "" {
w.Flush()
}
}

// Function to parse command-line flags
func parseFlags() Flags {
var outputFormat string
flag.StringVar(&outputFormat, "output", "", "Output format (default, singleline, json)")
flag.Parse()
return Flags{OutputFormat: outputFormat}
}

// Function to output data in default format
func outputDefault(w *tabwriter.Writer, pr PullRequest, timeLabel, reviewDecisionEmoji, mergeableEmoji string) {
fmt.Fprintf(w, "%s\n\tauthor: %s\n\tAge: %s \n\treviewDecision: %s\n\tmergeable %s\n", pr.URL, pr.Owner, timeLabel, reviewDecisionEmoji, mergeableEmoji)
}

// Function to output data in single-line format
func outputSingleLine(pr PullRequest, timeLabel, reviewDecisionEmoji, mergeableEmoji string) {
fmt.Printf("%s author: %s Age: %s reviewDecision: %s mergeable: %s\n", pr.URL, pr.Owner, timeLabel, reviewDecisionEmoji, mergeableEmoji)
}

// Function to output data in JSON format
func outputJSON(pr PullRequest, timeLabel, reviewDecisionEmoji, mergeableEmoji string) {
data := struct {
URL string `json:"url"`
Author string `json:"author"`
Age string `json:"age"`
ReviewDecision string `json:"review_decision"`
Mergeable string `json:"mergeable"`
}{
URL: pr.URL,
Author: string(pr.Owner),
Age: timeLabel,
ReviewDecision: reviewDecisionEmoji,
Mergeable: mergeableEmoji,
}

jsonData, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(string(jsonData))
}
10 changes: 5 additions & 5 deletions prs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/shurcooL/githubv4"
)

func getPrFromRepo(client Client, org, repo string) ([]PR, error) {
func getPrFromRepo(client Client, org, repo string) ([]PullRequest, error) {
var repoQuery struct {
Repository struct {
PullRequests struct {
Expand All @@ -23,7 +23,7 @@ func getPrFromRepo(client Client, org, repo string) ([]PR, error) {
githubv4.PullRequestStateOpen,
}

var PRS []PR
var PRS []PullRequest

variables := map[string]interface{}{
"org": githubv4.String(org),
Expand Down Expand Up @@ -57,12 +57,12 @@ func getPrFromRepo(client Client, org, repo string) ([]PR, error) {
return PRS, nil
}

func extractPRDataFromEdges(edges []PullRequestEdge) []PR {
var PRS []PR
func extractPRDataFromEdges(edges []PullRequestEdge) []PullRequest {
var PRS []PullRequest

for _, edge := range edges {
if !bool(edge.Node.IsDraft) {
var pr PR
var pr PullRequest
pr.URL = edge.Node.URL.String()
pr.CreatedAt = edge.Node.CreatedAt
pr.Owner = edge.Node.Author.Login
Expand Down
4 changes: 2 additions & 2 deletions prs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestExtractPRDataFromEdges(t *testing.T) {
}

// Define expected output
expected := []PR{
expected := []PullRequest{
{
URL: "https://github.com/my-org/my-repo/pull/1",
CreatedAt: githubv4.DateTime{Time: time.Now().UTC().Truncate(time.Hour)},
Expand All @@ -92,7 +92,7 @@ func TestGetPrFromRepo(t *testing.T) {
// Mock data for testing
org := "testOrg"
repo := "testRepo"
prs := []PR{
prs := []PullRequest{
{CreatedAt: githubv4.DateTime{}, URL: "https://github.com/testOrg/testRepo/pull/1", Owner: "john_doe"},
}

Expand Down
4 changes: 2 additions & 2 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/shurcooL/githubv4"
)

// PR : A pullRequest
type PR struct {
// PullRequest : A pullRequest
type PullRequest struct {
CreatedAt githubv4.DateTime `json:"createdAt"`
URL string `json:"url"`
Owner githubv4.String `json:"owner"`
Expand Down

0 comments on commit 43d0741

Please sign in to comment.