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

deduplicatoin of github issues drift #1664

Merged
merged 5 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions cli/pkg/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func (svc BitbucketAPI) PublishIssue(title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (svc BitbucketAPI) UpdateIssue(ID int64, title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (b BitbucketAPI) EditComment(prNumber int, id string, comment string) error {
url := fmt.Sprintf("%s/repositories/%s/%s/pullrequests/%d/comments/%s", bitbucketBaseURL, b.RepoWorkspace, b.RepoName, prNumber, id)

Expand Down
5 changes: 5 additions & 0 deletions cli/pkg/digger/digger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package digger

import (
"fmt"
"github.com/diggerhq/digger/libs/ci"
"github.com/diggerhq/digger/libs/execution"
orchestrator "github.com/diggerhq/digger/libs/scheduler"
Expand Down Expand Up @@ -97,6 +98,10 @@ func (m *MockPRManager) PublishIssue(title string, body string) (int64, error) {
return 0, nil
}

func (m *MockPRManager) UpdateIssue(ID int64, title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (m *MockPRManager) SetStatus(prNumber int, status string, statusContext string) error {
m.Commands = append(m.Commands, RunInfo{"SetStatus", strconv.Itoa(prNumber) + " " + status + " " + statusContext, time.Now()})
return nil
Expand Down
28 changes: 26 additions & 2 deletions ee/cli/pkg/drift/github_issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package drift
import (
"fmt"
orchestrator "github.com/diggerhq/digger/libs/ci"
"github.com/samber/lo"
"log"
)

type GithubIssueNotification struct {
Expand All @@ -11,8 +13,30 @@ type GithubIssueNotification struct {
}

func (ghi GithubIssueNotification) Send(projectName string, plan string) error {
log.Printf("Info: Sending drift notification regarding project: %v", projectName)
title := fmt.Sprintf("Drift detected in project: %v", projectName)
message := fmt.Sprintf(":bangbang: Drift detected in digger project %v details below: \n\n```\n%v\n```", projectName, plan)
_, err := (*ghi.GithubService).PublishIssue(title, message)
return err
existingIssues, err := (*ghi.GithubService).ListIssues()
if err != nil {
log.Printf("failed to retrive issues: %v", err)
return fmt.Errorf("failed to retrive issues: %v", err)
}

theIssue, exists := lo.Find(existingIssues, func(item *orchestrator.Issue) bool {
return item.Title == title
})
if exists {
_, err := (*ghi.GithubService).UpdateIssue(theIssue.ID, theIssue.Title, message)
if err != nil {
log.Printf("error while updating issue: %v", err)
}
return err
} else {
_, err := (*ghi.GithubService).PublishIssue(title, message)
if err != nil {
log.Printf("error while publishing issue: %v", err)
}
return err
}
return nil
}
4 changes: 4 additions & 0 deletions libs/ci/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ func (svc *AzureReposService) PublishIssue(title string, body string) (int64, er
return 0, fmt.Errorf("implement me")
}

func (svc *AzureReposService) UpdateIssue(ID int64, title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (a *AzureReposService) SetStatus(prNumber int, status string, statusContext string) error {
var gitStatusState git.GitStatusState
if status == "success" {
Expand Down
1 change: 1 addition & 0 deletions libs/ci/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type PullRequestService interface {
PublishComment(prNumber int, comment string) (*Comment, error)
ListIssues() ([]*Issue, error)
PublishIssue(title string, body string) (int64, error)
UpdateIssue(ID int64, title string, body string) (int64, error)
EditComment(prNumber int, id string, comment string) error
CreateCommentReaction(id string, reaction string) error
GetComments(prNumber int) ([]Comment, error)
Expand Down
8 changes: 8 additions & 0 deletions libs/ci/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ func (svc GithubService) PublishIssue(title string, body string) (int64, error)
return *githubissue.ID, err
}

func (svc GithubService) UpdateIssue(ID int64, title string, body string) (int64, error) {
githubissue, _, err := svc.Client.Issues.Edit(context.Background(), svc.Owner, svc.RepoName, int(ID), &github.IssueRequest{Title: &title, Body: &body})
if err != nil {
return 0, fmt.Errorf("could not edit issue: %v", err)
}
return *githubissue.ID, err
}

func (svc GithubService) PublishComment(prNumber int, comment string) (*ci.Comment, error) {
githubComment, _, err := svc.Client.Issues.CreateComment(context.Background(), svc.Owner, svc.RepoName, prNumber, &github.IssueComment{Body: &comment})
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions libs/ci/github/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (t MockCiService) PublishIssue(title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (svc MockCiService) UpdateIssue(ID int64, title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (t MockCiService) SetStatus(prNumber int, status string, statusContext string) error {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions libs/ci/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ func (svc GitLabService) PublishIssue(title string, body string) (int64, error)
return 0, fmt.Errorf("implement me")
}

func (svc GitLabService) UpdateIssue(ID int64, title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

// SetStatus GitLab implementation is using https://docs.gitlab.com/15.11/ee/api/status_checks.html (external status checks)
// https://docs.gitlab.com/ee/user/project/merge_requests/status_checks.html#add-a-status-check-service
// only supported by 'Ultimate' plan
Expand Down
6 changes: 6 additions & 0 deletions libs/ci/mocks.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ci

import "fmt"

type MockPullRequestManager struct {
ChangedFiles []string
Teams []string
Expand All @@ -25,6 +27,10 @@ func (t MockPullRequestManager) PublishIssue(title string, body string) (int64,
return 0, nil
}

func (t MockPullRequestManager) UpdateIssue(ID int64, title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (t MockPullRequestManager) SetStatus(prNumber int, status string, statusContext string) error {
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions libs/comment_utils/reporting/reporting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (t MockCiService) PublishIssue(title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (svc MockCiService) UpdateIssue(ID int64, title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (t MockCiService) SetStatus(prNumber int, status string, statusContext string) error {
return nil
}
Expand Down
9 changes: 8 additions & 1 deletion libs/orchestrator/mock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package orchestrator

import "github.com/diggerhq/digger/libs/ci"
import (
"fmt"
"github.com/diggerhq/digger/libs/ci"
)

type MockGithubPullrequestManager struct {
commands []string
Expand Down Expand Up @@ -30,6 +33,10 @@ func (mockGithubPullrequestManager *MockGithubPullrequestManager) PublishIssue(t
return 0, nil
}

func (mockGithubPullrequestManager *MockGithubPullrequestManager) UpdateIssue(ID int64, title string, body string) (int64, error) {
return 0, fmt.Errorf("implement me")
}

func (mockGithubPullrequestManager *MockGithubPullrequestManager) SetStatus(prNumber int, status string, statusContext string) error {
mockGithubPullrequestManager.commands = append(mockGithubPullrequestManager.commands, "SetStatus")
return nil
Expand Down
Loading