|
| 1 | +package drift |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + orchestrator "github.com/diggerhq/digger/libs/ci" |
| 6 | + "github.com/samber/lo" |
| 7 | + "log" |
| 8 | +) |
| 9 | + |
| 10 | +type GithubIssueNotification struct { |
| 11 | + GithubService *orchestrator.PullRequestService |
| 12 | + RelatedPrNumber *int64 |
| 13 | +} |
| 14 | + |
| 15 | +func (ghi *GithubIssueNotification) SendNotificationForProject(projectName string, repoFullName string, plan string) error { |
| 16 | + log.Printf("Info: Sending drift notification regarding project: %v", projectName) |
| 17 | + title := fmt.Sprintf("Drift detected in project: %v", projectName) |
| 18 | + message := fmt.Sprintf(":bangbang: Drift detected in digger project %v details below: \n\n```\n%v\n```", projectName, plan) |
| 19 | + existingIssues, err := (*ghi.GithubService).ListIssues() |
| 20 | + if err != nil { |
| 21 | + log.Printf("failed to retrieve issues: %v", err) |
| 22 | + return fmt.Errorf("failed to retrieve issues: %v", err) |
| 23 | + } |
| 24 | + |
| 25 | + theIssue, exists := lo.Find(existingIssues, func(item *orchestrator.Issue) bool { |
| 26 | + return item.Title == title |
| 27 | + }) |
| 28 | + if exists { |
| 29 | + _, err := (*ghi.GithubService).UpdateIssue(theIssue.ID, theIssue.Title, message) |
| 30 | + if err != nil { |
| 31 | + log.Printf("error while updating issue: %v", err) |
| 32 | + } |
| 33 | + return err |
| 34 | + } else { |
| 35 | + labels := []string{"digger"} |
| 36 | + _, err := (*ghi.GithubService).PublishIssue(title, message, &labels) |
| 37 | + if err != nil { |
| 38 | + log.Printf("error while publishing issue: %v", err) |
| 39 | + } |
| 40 | + return err |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (ghi *GithubIssueNotification) SendErrorNotificationForProject(projectName string, repoFullName string, err error) error { |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func (ghi *GithubIssueNotification) Flush() error { |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
0 commit comments