Skip to content

Commit

Permalink
Return DefaultBranch as part of ListBranches
Browse files Browse the repository at this point in the history
  • Loading branch information
azeemsgoogle committed Sep 3, 2021
1 parent 830c4f5 commit b2059ce
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions clients/githubrepo/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package githubrepo

import (
"context"
"encoding/json"
"fmt"
"log"
"sync"

"github.com/google/go-github/v38/github"
Expand Down Expand Up @@ -94,9 +96,13 @@ func (handler *branchesHandler) setup() error {
if err := handler.graphClient.Query(handler.ctx, handler.data, vars); err != nil {
handler.errSetup = sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("githubv4.Query: %v", err))
}
jsonData, err := json.MarshalIndent(*handler.data, "", " ")
if err != nil {
panic(err)
}
log.Println(string(jsonData))
handler.defaultBranchRef = getBranchRefFrom(handler.data.Repository.DefaultBranchRef)
handler.branches = getBranchRefsFrom(handler.data.Repository.Refs.Nodes)
// Maybe add defaultBranchRef to branches.
handler.branches = getBranchRefsFrom(handler.data.Repository.Refs.Nodes, handler.defaultBranchRef)
})
return handler.errSetup
}
Expand Down Expand Up @@ -194,10 +200,17 @@ func getBranchRefFrom(data branch) *clients.BranchRef {
return branchRef
}

func getBranchRefsFrom(data []branch) []*clients.BranchRef {
func getBranchRefsFrom(data []branch, defaultBranch *clients.BranchRef) []*clients.BranchRef {
branchRefs := make([]*clients.BranchRef, len(data))
var defaultFound bool
for i, b := range data {
branchRefs[i] = getBranchRefFrom(b)
if defaultBranch != nil && branchRefs[i].Name == defaultBranch.Name {
defaultFound = true
}
}
if !defaultFound {
branchRefs = append(branchRefs, defaultBranch)
}
return branchRefs
}

0 comments on commit b2059ce

Please sign in to comment.