Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stash): added alternative branch download for stash #185

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 64 additions & 15 deletions pkg/git/stash/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,37 @@ func (f *FileService) downloadLines(url string, start int) (lines []string, next
// Download downloads a file from Stash.
// Stash's API returns the file's contents as a paginated list of lines
func (f *FileService) Download(org, repo, path, branch string) (string, error) {
url := f.EncodeURL(org, repo, path, branch)
body := f.cache.Get(url)
if body != "" {
return body, nil

var err error

if file, err := f.getFile(org, repo, path, branch); err == nil {
return file, nil
}
allLines := make([]string, 0)
start := -1
for start != 0 {
lines, nextStart, err := f.downloadLines(url, start)
if err != nil {
return "", err

// If we are unable to retrieve files from the designated branch, we should attempt to access an alternative branch.
// It is not always clear which branch is the true master branch due to different naming conventions.
// Therefore, we will need to try one of them [main, master] to ensure we can access the necessary files.
var branchAlternatives = map[string]string{
"master": "main",
"refs/heads/master": "refs/heads/main",
"main": "master",
"refs/heads/main": "refs/heads/master",
}

if alternativeBranchName, ok := branchAlternatives[branch]; ok {
f.Logger.Info(fmt.Sprintf("DownloadContents failed with %v branch, trying with %v branch", branch, alternativeBranchName))

if altFile, altErr := f.getFile(org, repo, path, alternativeBranchName); altErr == nil {
f.Logger.Infof("Download from secondary branch %v succeeded", alternativeBranchName)
return altFile, nil
}
allLines = append(allLines, lines...)
start = nextStart

f.Logger.Errorf("Download failed also for branch %v", alternativeBranchName)
// We deliberately want to return the original err, not altErr
return "", err
}
ret := strings.Join(allLines, "\n")
f.cache.Add(url, ret)
return ret, nil

return "", nil
}

// EncodeURL returns the git url for a given org, repo, path and branch
Expand All @@ -124,3 +137,39 @@ func (f *FileService) DecodeURL(url string) (org, repo, path, branch string) {
branch = match[4]
return
}

func (f *FileService) getFile(org, repo, path, branch string) (string, error) {
url := f.EncodeURL(org, repo, path, branch)
body := f.cache.Get(url)
if body != "" {
f.Logger.Infof(fmt.Sprintf("Found cached file for key: %v\n", url))
return body, nil
}

f.Logger.Infof(fmt.Sprintf("Didn't find any cached files for key: %v. Will proceed to download\n", url))

result, err := f.getAllLines(url)

if err != nil {
f.Logger.Errorf(fmt.Sprintf("Failed to download file from: %v\n", url))
return "", err
}

f.Logger.Debug(fmt.Sprintf("Successfully downloaded file from %v branch", branch))
f.cache.Add(url, result)
return result, nil
}

func (f *FileService) getAllLines(url string) (string, error) {
allLines := make([]string, 0)
start := -1
for start != 0 {
lines, nextStart, err := f.downloadLines(url, start)
if err != nil {
return "", err
}
allLines = append(allLines, lines...)
start = nextStart
}
return strings.Join(allLines, "\n"), nil
}
Loading