Skip to content

Commit

Permalink
Move types out from "modules/structs" to not pollute api
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinakkaya committed Jan 25, 2024
1 parent ae47624 commit 0f61e59
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 114 deletions.
17 changes: 0 additions & 17 deletions modules/structs/repo_collaborator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,3 @@ type RepoCollaboratorPermission struct {
RoleName string `json:"role_name"`
User *User `json:"user"`
}

type WeekData struct {
Week int64 `json:"week"` // Starting day of the week as Unix timestamp
Additions int `json:"additions"` // Number of additions in that week
Deletions int `json:"deletions"` // Number of deletions in that week
Commits int `json:"commits"` // Number of commits in that week
}

// ContributorData represents statistical git commit count data
type ContributorData struct {
Name string `json:"name"` // Display name of the contributor
Login string `json:"login"` // Login name of the contributor in case it exists
AvatarLink string `json:"avatar_link"`
HomeLink string `json:"home_link"`
TotalCommits int64 `json:"total_commits"`
Weeks []*WeekData `json:"weeks"`
}
6 changes: 0 additions & 6 deletions modules/structs/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ type Commit struct {
Stats *CommitStats `json:"stats"`
}

// ExtendedCommitStats contains information for commit stats with author data
type ExtendedCommitStats struct {
Author *CommitUser `json:"author"`
Stats *CommitStats `json:"stats"`
}

// CommitDateOptions store dates for GIT_AUTHOR_DATE and GIT_COMMITTER_DATE
type CommitDateOptions struct {
// swagger:strfmt date-time
Expand Down
7 changes: 0 additions & 7 deletions routers/api/v1/swagger/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,6 @@ type swaggerCommitList struct {
Body []api.Commit `json:"body"`
}

// ContributorDataMap
// swagger:response ContributorDataMap
type swaggerContributorDataMap struct {
// in: body
Body map[string]*api.ContributorData `json:"body"`
}

// ChangedFileList
// swagger:response ChangedFileList
type swaggerChangedFileList struct {
Expand Down
51 changes: 37 additions & 14 deletions services/repository/contributors_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,34 @@ var (
generateLock = sync.Map{}
)

type WeekData struct {
Week int64 `json:"week"` // Starting day of the week as Unix timestamp
Additions int `json:"additions"` // Number of additions in that week
Deletions int `json:"deletions"` // Number of deletions in that week
Commits int `json:"commits"` // Number of commits in that week
}

// ContributorData represents statistical git commit count data
type ContributorData struct {
Name string `json:"name"` // Display name of the contributor
Login string `json:"login"` // Login name of the contributor in case it exists
AvatarLink string `json:"avatar_link"`
HomeLink string `json:"home_link"`
TotalCommits int64 `json:"total_commits"`
Weeks []*WeekData `json:"weeks"`
}

// ExtendedCommitStats contains information for commit stats with author data
type ExtendedCommitStats struct {
Author *api.CommitUser `json:"author"`
Stats *api.CommitStats `json:"stats"`
}

// CreateWeeks converts list of sundays to list of *api.WeekData
func CreateWeeks(sundays []int64) []*api.WeekData {
var weeks []*api.WeekData
func CreateWeeks(sundays []int64) []*WeekData {
var weeks []*WeekData
for _, week := range sundays {
weeks = append(weeks, &api.WeekData{
weeks = append(weeks, &WeekData{
Week: week,
Additions: 0,
Deletions: 0,
Expand All @@ -53,7 +76,7 @@ func CreateWeeks(sundays []int64) []*api.WeekData {
}

// GetContributorStats returns contributors stats for git commits for given revision or default branch
func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_model.Repository, revision string) (map[string]*api.ContributorData, error) {
func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_model.Repository, revision string) (map[string]*ContributorData, error) {
// as GetContributorStats is resource intensive we cache the result
cacheKey := fmt.Sprintf(contributorStatsCacheKey, repo.FullName(), revision)
if !cache.IsExist(cacheKey) {
Expand Down Expand Up @@ -82,15 +105,15 @@ func GetContributorStats(ctx context.Context, cache cache.Cache, repo *repo_mode
switch v := cache.Get(cacheKey).(type) {
case error:
return nil, v
case map[string]*api.ContributorData:
case map[string]*ContributorData:
return v, nil
default:
return nil, fmt.Errorf("unexpected type in cache detected")
}
}

// ExtendedCommitStats return the list of *api.ExtendedCommitStats for the given revision
func ExtendedCommitStats(repo *git.Repository, revision string /*, limit int */) ([]*api.ExtendedCommitStats, error) {
// GetExtendedCommitStats return the list of *ExtendedCommitStats for the given revision
func GetExtendedCommitStats(repo *git.Repository, revision string /*, limit int */) ([]*ExtendedCommitStats, error) {
baseCommit, err := repo.GetCommit(revision)
if err != nil {
return nil, err
Expand All @@ -108,7 +131,7 @@ func ExtendedCommitStats(repo *git.Repository, revision string /*, limit int */)
// AddOptionFormat("--max-count=%d", limit)
gitCmd.AddDynamicArguments(baseCommit.ID.String())

var extendedCommitStats []*api.ExtendedCommitStats
var extendedCommitStats []*ExtendedCommitStats
stderr := new(strings.Builder)
err = gitCmd.Run(&git.RunOpts{
Dir: repo.Path,
Expand Down Expand Up @@ -157,7 +180,7 @@ func ExtendedCommitStats(repo *git.Repository, revision string /*, limit int */)
scanner.Scan()
scanner.Text() // empty line at the end

res := &api.ExtendedCommitStats{
res := &ExtendedCommitStats{
Author: &api.CommitUser{
Identity: api.Identity{
Name: authorName,
Expand Down Expand Up @@ -194,7 +217,7 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey
if len(revision) == 0 {
revision = repo.DefaultBranch
}
extendedCommitStats, err := ExtendedCommitStats(gitRepo, revision)
extendedCommitStats, err := GetExtendedCommitStats(gitRepo, revision)
if err != nil {
err := fmt.Errorf("ExtendedCommitStats: %w", err)
_ = cache.Put(cacheKey, err, contributorStatsCacheTimeout)
Expand All @@ -209,8 +232,8 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey
sundays, _ := util.ListSundaysBetween(startingSunday, endingSunday)

unknownUserAvatarLink := user_model.NewGhostUser().AvatarLink(ctx)
contributorsCommitStats := make(map[string]*api.ContributorData)
contributorsCommitStats["total"] = &api.ContributorData{
contributorsCommitStats := make(map[string]*ContributorData)
contributorsCommitStats["total"] = &ContributorData{
Name: "Total",
AvatarLink: unknownUserAvatarLink,
Weeks: CreateWeeks(sundays),
Expand All @@ -234,13 +257,13 @@ func generateContributorStats(genDone chan struct{}, cache cache.Cache, cacheKey
if avatarLink == "" {
avatarLink = unknownUserAvatarLink
}
contributorsCommitStats[userEmail] = &api.ContributorData{
contributorsCommitStats[userEmail] = &ContributorData{
Name: v.Author.Name,
AvatarLink: avatarLink,
Weeks: CreateWeeks(sundays),
}
} else {
contributorsCommitStats[userEmail] = &api.ContributorData{
contributorsCommitStats[userEmail] = &ContributorData{
Name: u.DisplayName(),
Login: u.LowerName,
AvatarLink: u.AvatarLink(ctx),
Expand Down
70 changes: 0 additions & 70 deletions templates/swagger/v1_json.tmpl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0f61e59

Please sign in to comment.