-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
203 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package insights | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
"strconv" | ||
|
||
"github.com/charmbracelet/bubbles/table" | ||
"github.com/charmbracelet/lipgloss" | ||
"github.com/open-sauced/go-api/client" | ||
"github.com/open-sauced/pizza-cli/pkg/api" | ||
"github.com/open-sauced/pizza-cli/pkg/constants" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type repoOptions struct { | ||
// APIClient is the http client for making calls to the open-sauced api | ||
APIClient *client.APIClient | ||
|
||
// Repo is a git repository url | ||
Repo string | ||
|
||
// Period is the number of days, used for query filtering | ||
// Constrained to either 30 or 60 | ||
Period int32 | ||
} | ||
|
||
// NewRepoCommand returns a new cobra command for 'pizza insights repo' | ||
func NewRepoCommand() *cobra.Command { | ||
opts := &repoOptions{} | ||
cmd := &cobra.Command{ | ||
Use: "repo url [flags]", | ||
Short: "Gather insights about an indexed git repository", | ||
Long: "Gather insights about an indexed git repository. This command will show info about contributors, pull requests, etc.", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) != 1 { | ||
return fmt.Errorf("must provide exactly one git repository url") | ||
} | ||
opts.Repo = args[0] | ||
period, _ := cmd.Flags().GetInt32(constants.FlagNamePeriod) | ||
allowedPeriods := []int32{30, 60} | ||
if !slices.Contains(allowedPeriods, period) { | ||
return fmt.Errorf("%s flag must be one of %v", constants.FlagNamePeriod, allowedPeriods) | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
endpointURL, _ := cmd.Flags().GetString(constants.FlagNameEndpoint) | ||
opts.APIClient = api.NewGoClient(endpointURL) | ||
return opts.run(context.TODO()) | ||
}, | ||
} | ||
cmd.Flags().Int32VarP(&opts.Period, constants.FlagNamePeriod, "p", 30, "Number of days, used for query filtering") | ||
return cmd | ||
} | ||
|
||
type repositoryInsights struct { | ||
RepoID int `json:"repo_id"` | ||
RepoURL string `json:"repo_url"` | ||
AllPullRequests int `json:"all_pull_requests"` | ||
AcceptedPullRequests int `json:"accepted_pull_requests"` | ||
SpamPullRequests int `json:"spam_pull_requests"` | ||
} | ||
|
||
func (opts *repoOptions) run(ctx context.Context) error { | ||
repo, err := findRepositoryByOwnerAndRepoName(ctx, opts.APIClient, opts.Repo) | ||
if err != nil { | ||
return err | ||
} | ||
if repo == nil { | ||
return nil | ||
} | ||
repoInsights := &repositoryInsights{ | ||
RepoID: int(repo.Id), | ||
RepoURL: repo.SvnUrl, | ||
} | ||
pullRequestInsights, err := getPullRequestInsights(ctx, opts.APIClient, repo.Id, opts.Period) | ||
if err != nil { | ||
return err | ||
} | ||
repoInsights.AllPullRequests = int(pullRequestInsights.AllPrs) | ||
repoInsights.AcceptedPullRequests = int(pullRequestInsights.AcceptedPrs) | ||
repoInsights.SpamPullRequests = int(pullRequestInsights.SpamPrs) | ||
repoInsights.RenderTable() | ||
return nil | ||
} | ||
|
||
func (ri *repositoryInsights) RenderTable() { | ||
if ri == nil { | ||
return | ||
} | ||
rows := []table.Row{ | ||
{ | ||
"Repository ID", | ||
strconv.Itoa(ri.RepoID), | ||
}, | ||
{ | ||
"All pull requests", | ||
strconv.Itoa(ri.AllPullRequests), | ||
}, | ||
{ | ||
"Accepted pull requests", | ||
strconv.Itoa(ri.AcceptedPullRequests), | ||
}, | ||
{ | ||
"Spam pull requests", | ||
strconv.Itoa(ri.SpamPullRequests), | ||
}, | ||
} | ||
var maxRowWidth int | ||
for i := range rows { | ||
rowWidth := len(rows[i][0]) | ||
if rowWidth > maxRowWidth { | ||
maxRowWidth = rowWidth | ||
} | ||
} | ||
columns := []table.Column{ | ||
{ | ||
Title: "Repository URL", | ||
Width: getMaxTableRowWidth(rows), | ||
}, | ||
{ | ||
Title: ri.RepoURL, | ||
Width: len(ri.RepoURL), | ||
}, | ||
} | ||
styles := table.DefaultStyles() | ||
styles.Header.MarginTop(1) | ||
styles.Selected = lipgloss.NewStyle() | ||
repoTable := table.New( | ||
table.WithColumns(columns), | ||
table.WithRows(rows), | ||
table.WithHeight(len(rows)), | ||
table.WithStyles(styles), | ||
) | ||
fmt.Println(repoTable.View()) | ||
} | ||
|
||
func getPullRequestInsights(ctx context.Context, apiClient *client.APIClient, repoID, period int32) (*client.DbPRInsight, error) { | ||
data, _, err := apiClient.PullRequestsServiceAPI. | ||
GetPullRequestInsights(ctx). | ||
RepoIds(fmt.Sprintf("%d", repoID)). | ||
Execute() | ||
if err != nil { | ||
return nil, fmt.Errorf("error while calling 'PullRequestsServiceAPI.GetPullRequestInsights' with repository %d': %w", repoID, err) | ||
} | ||
index := slices.IndexFunc(data, func(insight client.DbPRInsight) bool { | ||
return insight.Interval == period | ||
}) | ||
if index == -1 { | ||
return nil, fmt.Errorf("could not find pull request insights for repository %d with interval %d", repoID, period) | ||
} | ||
return &data[index], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters