Skip to content

Commit 9f5bdbd

Browse files
Merge pull request #6651 from devtron-labs/fix-panic-cd-trigger
fix: panic handling on cd trigger
1 parent 4efb10c commit 9f5bdbd

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

pkg/deployment/gitOps/git/GitServiceBitbucket.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,43 @@ func (impl GitBitbucketClient) CommitValues(ctx context.Context, config *ChartCo
347347
}
348348

349349
//extracting the latest commit hash from the paginated api response of above method, reference of api & response - https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/commits
350-
commitHash = commits.(map[string]interface{})["values"].([]interface{})[0].(map[string]interface{})["hash"].(string)
351-
commitTimeString := commits.(map[string]interface{})["values"].([]interface{})[0].(map[string]interface{})["date"].(string)
352-
commitTime, err = time.Parse(time.RFC3339, commitTimeString)
350+
commitsMap, ok := commits.(map[string]interface{})
351+
if !ok {
352+
impl.logger.Errorw("unexpected response format from bitbucket", "commits", commits)
353+
return "", time.Time{}, fmt.Errorf("unexpected response format from bitbucket")
354+
}
355+
356+
values, ok := commitsMap["values"]
357+
if !ok || values == nil {
358+
impl.logger.Errorw("no values found in bitbucket response", "commits", commits, "commitsMap", commitsMap)
359+
return "", time.Time{}, fmt.Errorf("no commits found in bitbucket response")
360+
}
361+
362+
valuesArray, ok := values.([]interface{})
363+
if !ok || len(valuesArray) == 0 {
364+
impl.logger.Errorw("empty values array in bitbucket response", "commits", commits, "values", values)
365+
return "", time.Time{}, fmt.Errorf("empty commits array in bitbucket response")
366+
}
367+
368+
firstCommit, ok := valuesArray[0].(map[string]interface{})
369+
if !ok {
370+
impl.logger.Errorw("invalid commit format in bitbucket response", "commits", commits, "firstCommit", valuesArray[0])
371+
return "", time.Time{}, fmt.Errorf("invalid commit format in bitbucket response")
372+
}
373+
374+
commitHash, ok = firstCommit["hash"].(string)
375+
if !ok || commitHash == "" {
376+
impl.logger.Errorw("no hash found in commit", "commits", commits, "firstCommit", firstCommit)
377+
return "", time.Time{}, fmt.Errorf("no hash found in commit")
378+
}
379+
380+
dateStr, ok := firstCommit["date"].(string)
381+
if !ok || dateStr == "" {
382+
impl.logger.Errorw("no date found in commit", "firstCommit", firstCommit)
383+
return "", time.Time{}, fmt.Errorf("no date found in commit response")
384+
}
385+
386+
commitTime, err = time.Parse(time.RFC3339, dateStr)
353387
if err != nil {
354388
util.TriggerGitOpsMetrics("CommitValues", "GitBitbucketClient", start, err)
355389
impl.logger.Errorw("error in getting commitTime", "err", err)

0 commit comments

Comments
 (0)