Skip to content

Commit

Permalink
Add use Branch input in config.json file
Browse files Browse the repository at this point in the history
  • Loading branch information
colussim committed May 6, 2024
1 parent 22b3195 commit 8694e2a
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 125 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ If '**Projects**' and '**Repos**' are not specified, the analysis will be conduc
"Project": "",
"Repos": "",
```
For Bitbucket DC, you must provide the URL with your server address and change the '**Protocol**' entry if you are using an https connection , ending with '**/**'. The '**Branch**' entry is not used at the moment.
For Bitbucket DC, you must provide the URL with your server address and change the '**Protocol**' entry if you are using an https connection , ending with '**/**'. The '**branch**' input allows you to select a specific branch for all repositories within an organization or project, or for a single repository. For example, if you only want all branches to be "main", '**"Branch":"main"**' .
```json
"Url": "http://X.X.X.X/"
```
Expand Down
24 changes: 19 additions & 5 deletions golc.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,14 +859,22 @@ func main() {
fileexclusionEX := getFileNameIfExists(fileexclusion)

startTime = time.Now()
projects, err := getbibucketdc.GetProjectBitbucketList(platformConfig["Url"].(string), platformConfig["Baseapi"].(string), platformConfig["Apiver"].(string), platformConfig["AccessToken"].(string), fileexclusionEX, platformConfig["Project"].(string), platformConfig["Repos"].(string))
projects, err := getbibucketdc.GetProjectBitbucketList(platformConfig["Url"].(string), platformConfig["Baseapi"].(string), platformConfig["Apiver"].(string), platformConfig["AccessToken"].(string), fileexclusionEX, platformConfig["Project"].(string), platformConfig["Repos"].(string), platformConfig["Branch"].(string))
if err != nil {
fmt.Printf("❌ Error Get Info Projects in Bitbucket server '%s' : ", err)
os.Exit(1)
}

// Run scanning repositories
NumberRepos = AnalyseReposListBitSRV(DestinationResult, platformConfig["Users"].(string), platformConfig["AccessToken"].(string), platformConfig["Protocol"].(string), platformConfig["Url"].(string), platformConfig["DevOps"].(string), projects)
if len(projects) == 0 {
fmt.Printf("❌ No Analysis performed...")
os.Exit(1)

} else {

// Run scanning repositories
NumberRepos = AnalyseReposListBitSRV(DestinationResult, platformConfig["Users"].(string), platformConfig["AccessToken"].(string), platformConfig["Protocol"].(string), platformConfig["Url"].(string), platformConfig["DevOps"].(string), projects)

}

case "bitbucket":
var fileexclusion = platformConfig["FileExclusion"].(string)
Expand All @@ -879,9 +887,15 @@ func main() {
fmt.Printf("❌ Error Get Info Projects in Bitbucket cloud '%s' : ", err)
return
}
if len(projects1) == 0 {
fmt.Printf("❌ No Analysis performed...")
os.Exit(1)

// Run scanning repositories
NumberRepos = AnalyseReposListBitC(DestinationResult, platformConfig["AccessToken"].(string), platformConfig["Protocol"].(string), platformConfig["Baseapi"].(string), platformConfig["Workspace"].(string), platformConfig["DevOps"].(string), projects1)
} else {

// Run scanning repositories
NumberRepos = AnalyseReposListBitC(DestinationResult, platformConfig["AccessToken"].(string), platformConfig["Protocol"].(string), platformConfig["Baseapi"].(string), platformConfig["Workspace"].(string), platformConfig["DevOps"].(string), projects1)
}

case "file":

Expand Down
Binary file modified imgs/report.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified imgs/webui.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
130 changes: 97 additions & 33 deletions pkg/devops/getbitbucket/getbitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ type ParamsReposCloud struct {
Branch string
}

type SizeResponse struct {
Size int `json:"size"`
}

const PrefixMsg = "Get Projects..."

func loadExclusionList(filename string) (*ExclusionList, error) {
Expand Down Expand Up @@ -288,36 +292,49 @@ func GetReposProjectCloud(parms ParamsReposProjectCloud) ([]ProjectBranch, int,
fmt.Printf("\r\t✅ Repo: %s - Number of branches: %d\n", repo.Name, len(branches))

// Finding the branch with the largest size
if len(branches) > 1 {
for _, branch := range branches {
messageB := fmt.Sprintf("\t Analysis branch <%s> size...", branch.Name)
spin1.Prefix = messageB
spin1.Start()

for _, branch := range branches {
messageB := fmt.Sprintf("\t Analysis branch <%s> size...", branch.Name)
spin1.Prefix = messageB
spin1.Start()
size, err := fetchBranchSize(parms.Workspace, repo.Slug, branch.Name, parms.AccessToken, parms.URL, parms.APIVersion)
messageF = ""
spin1.FinalMSG = messageF

size, err := fetchBranchSize(parms.Workspace, repo.Slug, branch.Name, parms.AccessToken, parms.URL, parms.APIVersion)
messageF = ""
spin1.FinalMSG = messageF

spin1.Stop()
if err != nil {
fmt.Println("❌ Error retrieving branch size:", err)
spin1.Stop()
os.Exit(1)
}
if err != nil {
fmt.Println("❌ Error retrieving branch size:", err)
spin1.Stop()
os.Exit(1)
}

if size > largestRepoSize {
largestRepoSize = size
//largestRepoProject = project.Name
largestRepoBranch = branch.Name
}

if size > largestRepoSize {
largestRepoSize = size
//largestRepoProject = project.Name
largestRepoBranch = branch.Name
}
} else {
size1, err1 := fetchBranchSize1(parms.Workspace, repo.Slug, parms.AccessToken, parms.URL, parms.APIVersion)

if err1 != nil {
fmt.Println("\n❌ Error retrieving branch size:", err1)
spin1.Stop()
os.Exit(1)
}
largestRepoSize = size1
largestRepoBranch = branches[0].Name
}

importantBranches = append(importantBranches, ProjectBranch{
ProjectKey: project.Key,
RepoSlug: repo.Slug,
MainBranch: largestRepoBranch,
LargestSize: largestRepoSize,
})
Nobranch = 0
}
} else {
emptyRepo++
Expand Down Expand Up @@ -400,28 +417,40 @@ func GetRepos(parms ParamsReposCloud) ([]ProjectBranch, int, int) {
}
// Finding the branch with the largest size
if Nobranch == 0 {
for _, branch := range branches {
messageB := fmt.Sprintf("\t Analysis branch <%s> size...", branch.Name)
spin1.Prefix = messageB
spin1.Start()
if len(branches) > 1 {
for _, branch := range branches {
messageB := fmt.Sprintf("\t Analysis branch <%s> size...", branch.Name)
spin1.Prefix = messageB
spin1.Start()

size, err := fetchBranchSize(parms.Workspace, parms.Repos[0].Slug, branch.Name, parms.AccessToken, parms.URL, parms.APIVersion)
messageF := ""
spin1.FinalMSG = messageF
size, err := fetchBranchSize(parms.Workspace, parms.Repos[0].Slug, branch.Name, parms.AccessToken, parms.URL, parms.APIVersion)
messageF := ""
spin1.FinalMSG = messageF

spin1.Stop()
if err != nil {
fmt.Println("❌ Error retrieving branch size:", err)
spin1.Stop()
continue
}
if err != nil {
fmt.Println("❌ Error retrieving branch size:", err)
spin1.Stop()
continue
}

if size > largestRepoSize {
largestRepoSize = size
//largestRepoProject = project.Name
largestRepoBranch = branch.Name
}

if size > largestRepoSize {
largestRepoSize = size
//largestRepoProject = project.Name
largestRepoBranch = branch.Name
}
} else {
size1, err1 := fetchBranchSize1(parms.Workspace, parms.Repos[0].Slug, parms.AccessToken, parms.URL, parms.APIVersion)

if err1 != nil {
fmt.Println("\n❌ Error retrieving branch size:", err1)
spin1.Stop()
os.Exit(1)
}
largestRepoSize = size1
largestRepoBranch = branches[0].Name
}
}
Nobranch = 0
Expand Down Expand Up @@ -1010,6 +1039,41 @@ func fetchBranchSize(workspace, repoSlug, branchName, accessToken, url, apiver s
return totalSize, nil
}

func fetchBranchSize1(workspace, repoSlug, accessToken, url, apiver string) (int, error) {

url1 := fmt.Sprintf("%s%s/repositories/%s/%s/?fields=size", url, apiver, workspace, repoSlug)

req, err := http.NewRequest("GET", url1, nil)
if err != nil {
return 0, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return 0, err
}

var data SizeResponse

err = json.Unmarshal(body, &data)
if err != nil {
return 0, err
}

totalSize := data.Size

return totalSize, nil

}

func fetchDirectorySize(workspace string, repoSlug string, branchName string, components string, accessToken string, url string, apiver string) (int, error) {

url1 := fmt.Sprintf("%s%s/reposiories/%s/%s/src/%s/%s/?pagelen=100", url, apiver, workspace, repoSlug, branchName, components)
Expand Down
Loading

0 comments on commit 8694e2a

Please sign in to comment.