Skip to content

Commit a891004

Browse files
authored
Merge pull request #1664 from diggerhq/feat/github-issues-dedup
deduplicatoin of github issues drift
2 parents 571990e + 0bc797d commit a891004

File tree

11 files changed

+74
-3
lines changed

11 files changed

+74
-3
lines changed

cli/pkg/bitbucket/bitbucket.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ func (svc BitbucketAPI) PublishIssue(title string, body string) (int64, error) {
133133
return 0, fmt.Errorf("implement me")
134134
}
135135

136+
func (svc BitbucketAPI) UpdateIssue(ID int64, title string, body string) (int64, error) {
137+
return 0, fmt.Errorf("implement me")
138+
}
139+
136140
func (b BitbucketAPI) EditComment(prNumber int, id string, comment string) error {
137141
url := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d/comments/%s", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber, id)
138142

cli/pkg/digger/digger_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package digger
22

33
import (
4+
"fmt"
45
"github.com/diggerhq/digger/libs/ci"
56
"github.com/diggerhq/digger/libs/execution"
67
orchestrator "github.com/diggerhq/digger/libs/scheduler"
@@ -97,6 +98,10 @@ func (m *MockPRManager) PublishIssue(title string, body string) (int64, error) {
9798
return 0, nil
9899
}
99100

101+
func (m *MockPRManager) UpdateIssue(ID int64, title string, body string) (int64, error) {
102+
return 0, fmt.Errorf("implement me")
103+
}
104+
100105
func (m *MockPRManager) SetStatus(prNumber int, status string, statusContext string) error {
101106
m.Commands = append(m.Commands, RunInfo{"SetStatus", strconv.Itoa(prNumber) + " " + status + " " + statusContext, time.Now()})
102107
return nil

ee/cli/pkg/drift/github_issue.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package drift
33
import (
44
"fmt"
55
orchestrator "github.com/diggerhq/digger/libs/ci"
6+
"github.com/samber/lo"
7+
"log"
68
)
79

810
type GithubIssueNotification struct {
@@ -11,8 +13,30 @@ type GithubIssueNotification struct {
1113
}
1214

1315
func (ghi GithubIssueNotification) Send(projectName string, plan string) error {
16+
log.Printf("Info: Sending drift notification regarding project: %v", projectName)
1417
title := fmt.Sprintf("Drift detected in project: %v", projectName)
1518
message := fmt.Sprintf(":bangbang: Drift detected in digger project %v details below: \n\n```\n%v\n```", projectName, plan)
16-
_, err := (*ghi.GithubService).PublishIssue(title, message)
17-
return err
19+
existingIssues, err := (*ghi.GithubService).ListIssues()
20+
if err != nil {
21+
log.Printf("failed to retrive issues: %v", err)
22+
return fmt.Errorf("failed to retrive 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+
_, err := (*ghi.GithubService).PublishIssue(title, message)
36+
if err != nil {
37+
log.Printf("error while publishing issue: %v", err)
38+
}
39+
return err
40+
}
41+
return nil
1842
}

libs/ci/azure/azure.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func (svc *AzureReposService) PublishIssue(title string, body string) (int64, er
196196
return 0, fmt.Errorf("implement me")
197197
}
198198

199+
func (svc *AzureReposService) UpdateIssue(ID int64, title string, body string) (int64, error) {
200+
return 0, fmt.Errorf("implement me")
201+
}
202+
199203
func (a *AzureReposService) SetStatus(prNumber int, status string, statusContext string) error {
200204
var gitStatusState git.GitStatusState
201205
if status == "success" {

libs/ci/ci.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type PullRequestService interface {
77
PublishComment(prNumber int, comment string) (*Comment, error)
88
ListIssues() ([]*Issue, error)
99
PublishIssue(title string, body string) (int64, error)
10+
UpdateIssue(ID int64, title string, body string) (int64, error)
1011
EditComment(prNumber int, id string, comment string) error
1112
CreateCommentReaction(id string, reaction string) error
1213
GetComments(prNumber int) ([]Comment, error)

libs/ci/github/github.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ func (svc GithubService) PublishIssue(title string, body string) (int64, error)
147147
return *githubissue.ID, err
148148
}
149149

150+
func (svc GithubService) UpdateIssue(ID int64, title string, body string) (int64, error) {
151+
githubissue, _, err := svc.Client.Issues.Edit(context.Background(), svc.Owner, svc.RepoName, int(ID), &github.IssueRequest{Title: &title, Body: &body})
152+
if err != nil {
153+
return 0, fmt.Errorf("could not edit issue: %v", err)
154+
}
155+
return *githubissue.ID, err
156+
}
157+
150158
func (svc GithubService) PublishComment(prNumber int, comment string) (*ci.Comment, error) {
151159
githubComment, _, err := svc.Client.Issues.CreateComment(context.Background(), svc.Owner, svc.RepoName, prNumber, &github.IssueComment{Body: &comment})
152160
if err != nil {

libs/ci/github/mocks.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (t MockCiService) PublishIssue(title string, body string) (int64, error) {
4747
return 0, fmt.Errorf("implement me")
4848
}
4949

50+
func (svc MockCiService) UpdateIssue(ID int64, title string, body string) (int64, error) {
51+
return 0, fmt.Errorf("implement me")
52+
}
53+
5054
func (t MockCiService) SetStatus(prNumber int, status string, statusContext string) error {
5155
return nil
5256
}

libs/ci/gitlab/gitlab.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func (svc GitLabService) PublishIssue(title string, body string) (int64, error)
196196
return 0, fmt.Errorf("implement me")
197197
}
198198

199+
func (svc GitLabService) UpdateIssue(ID int64, title string, body string) (int64, error) {
200+
return 0, fmt.Errorf("implement me")
201+
}
202+
199203
// SetStatus GitLab implementation is using https://docs.gitlab.com/15.11/ee/api/status_checks.html (external status checks)
200204
// https://docs.gitlab.com/ee/user/project/merge_requests/status_checks.html#add-a-status-check-service
201205
// only supported by 'Ultimate' plan

libs/ci/mocks.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ci
22

3+
import "fmt"
4+
35
type MockPullRequestManager struct {
46
ChangedFiles []string
57
Teams []string
@@ -25,6 +27,10 @@ func (t MockPullRequestManager) PublishIssue(title string, body string) (int64,
2527
return 0, nil
2628
}
2729

30+
func (t MockPullRequestManager) UpdateIssue(ID int64, title string, body string) (int64, error) {
31+
return 0, fmt.Errorf("implement me")
32+
}
33+
2834
func (t MockPullRequestManager) SetStatus(prNumber int, status string, statusContext string) error {
2935
return nil
3036
}

libs/comment_utils/reporting/reporting_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (t MockCiService) PublishIssue(title string, body string) (int64, error) {
4747
return 0, fmt.Errorf("implement me")
4848
}
4949

50+
func (svc MockCiService) UpdateIssue(ID int64, title string, body string) (int64, error) {
51+
return 0, fmt.Errorf("implement me")
52+
}
53+
5054
func (t MockCiService) SetStatus(prNumber int, status string, statusContext string) error {
5155
return nil
5256
}

0 commit comments

Comments
 (0)