From 7b912e89031ca1aad6095a69e138a1a21a0b3c62 Mon Sep 17 00:00:00 2001 From: Azeem Shaikh Date: Fri, 3 Sep 2021 07:40:32 -0700 Subject: [PATCH] Return DefaultBranch as part of ListBranches (#960) Co-authored-by: Azeem Shaikh --- clients/githubrepo/branches.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/clients/githubrepo/branches.go b/clients/githubrepo/branches.go index 2c5b28e9210..ecef83449cb 100644 --- a/clients/githubrepo/branches.go +++ b/clients/githubrepo/branches.go @@ -95,8 +95,7 @@ func (handler *branchesHandler) setup() error { handler.errSetup = sce.Create(sce.ErrScorecardInternal, fmt.Sprintf("githubv4.Query: %v", err)) } 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 } @@ -194,10 +193,18 @@ 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 { + // nolint: makezero + branchRefs = append(branchRefs, defaultBranch) } return branchRefs }