Skip to content

Commit

Permalink
Move error function return value to the standard position
Browse files Browse the repository at this point in the history
Handle cases where GetContents returns nil HTTP respons (like in Context
cancellation)
  • Loading branch information
Oded-B committed Jun 21, 2024
1 parent 4d55de3 commit 16ea028
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd/telefonistka/bump-version-overwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func bumpVersionOverwrite(targetRepo string, targetFile string, file string, git
ghPrClientDetails.PrLogger = log.WithFields(log.Fields{}) // TODO what fields should be here?

defaultBranch, _ := ghPrClientDetails.GetDefaultBranch()
initialFileContent, err, statusCode := githubapi.GetFileContent(ghPrClientDetails, defaultBranch, targetFile)
initialFileContent, statusCode, err := githubapi.GetFileContent(ghPrClientDetails, defaultBranch, targetFile)
if statusCode == 404 {
ghPrClientDetails.PrLogger.Infof("File %s was not found\n", targetFile)
} else if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/telefonistka/bump-version-regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func bumpVersionRegex(targetRepo string, targetFile string, regex string, replac
r := regexp.MustCompile(regex)
defaultBranch, _ := ghPrClientDetails.GetDefaultBranch()

initialFileContent, err, _ := githubapi.GetFileContent(ghPrClientDetails, defaultBranch, targetFile)
initialFileContent, _, err := githubapi.GetFileContent(ghPrClientDetails, defaultBranch, targetFile)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Fail to fetch file content:%s\n", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/telefonistka/bump-version-yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func bumpVersionYaml(targetRepo string, targetFile string, address string, value

defaultBranch, _ := ghPrClientDetails.GetDefaultBranch()

initialFileContent, err, _ := githubapi.GetFileContent(ghPrClientDetails, defaultBranch, targetFile)
initialFileContent, _, err := githubapi.GetFileContent(ghPrClientDetails, defaultBranch, targetFile)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Fail to fetch file content:%s\n", err)
os.Exit(1)
Expand Down
17 changes: 11 additions & 6 deletions internal/pkg/githubapi/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ func ApprovePr(approverClient *github.Client, ghPrClientDetails GhPrClientDetail
}

func GetInRepoConfig(ghPrClientDetails GhPrClientDetails, defaultBranch string) (*cfg.Config, error) {
inRepoConfigFileContentString, err, _ := GetFileContent(ghPrClientDetails, defaultBranch, "telefonistka.yaml")
inRepoConfigFileContentString, _, err := GetFileContent(ghPrClientDetails, defaultBranch, "telefonistka.yaml")
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Could not get in-repo configuration: err=%s\n", err)
return nil, err
Expand All @@ -1020,18 +1020,23 @@ func GetInRepoConfig(ghPrClientDetails GhPrClientDetails, defaultBranch string)
return c, err
}

func GetFileContent(ghPrClientDetails GhPrClientDetails, branch string, filePath string) (string, error, int) {
func GetFileContent(ghPrClientDetails GhPrClientDetails, branch string, filePath string) (string, int, error) {
rGetContentOps := github.RepositoryContentGetOptions{Ref: branch}
fileContent, _, resp, err := ghPrClientDetails.GhClientPair.v3Client.Repositories.GetContents(ghPrClientDetails.Ctx, ghPrClientDetails.Owner, ghPrClientDetails.Repo, filePath, &rGetContentOps)
prom.InstrumentGhCall(resp)
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Fail to get file:%s\n%v\n", err, resp)
return "", err, resp.StatusCode
if resp == nil {
return "", 0, err
}
prom.InstrumentGhCall(resp)
return "", resp.StatusCode, err
} else {
prom.InstrumentGhCall(resp)
}
fileContentString, err := fileContent.GetContent()
if err != nil {
ghPrClientDetails.PrLogger.Errorf("Fail to serlize file:%s\n", err)
return "", err, resp.StatusCode
return "", resp.StatusCode, err
}
return fileContentString, nil, resp.StatusCode
return fileContentString, resp.StatusCode, nil
}

0 comments on commit 16ea028

Please sign in to comment.