Skip to content

Commit

Permalink
fix drift notification provider
Browse files Browse the repository at this point in the history
  • Loading branch information
motatoes committed Jun 5, 2024
1 parent 49443b9 commit 2fa89dc
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cli/cmd/digger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"github.com/diggerhq/digger/cli/pkg/azure"
"github.com/diggerhq/digger/cli/pkg/digger"
"github.com/diggerhq/digger/cli/pkg/drift"
"github.com/diggerhq/digger/cli/pkg/github"
"github.com/diggerhq/digger/cli/pkg/usage"
comment_updater "github.com/diggerhq/digger/libs/comment_utils/summary"
"github.com/spf13/cobra"
Expand All @@ -21,7 +23,7 @@ var defaultCmd = &cobra.Command{
switch ci {
case digger.GitHub:
logLeader = os.Getenv("GITHUB_ACTOR")
gitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderBasic{})
github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderBasic{}, drift.DriftNotificationProviderBasic{})
case digger.GitLab:
logLeader = os.Getenv("CI_PROJECT_NAME")
gitLabCI(lock, PolicyChecker, BackendApi, ReportStrategy)
Expand Down
25 changes: 25 additions & 0 deletions cli/pkg/drift/Provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package drift

import (
"fmt"
core_drift "github.com/diggerhq/digger/cli/pkg/core/drift"
"github.com/diggerhq/digger/libs/orchestrator"
"os"
)

type DriftNotificationProvider interface {
Get(prService orchestrator.PullRequestService) (core_drift.Notification, error)
}

type DriftNotificationProviderBasic struct{}

func (d DriftNotificationProviderBasic) Get(prService orchestrator.PullRequestService) (core_drift.Notification, error) {
slackNotificationUrl := os.Getenv("INPUT_DRIFT_DETECTION_SLACK_NOTIFICATION_URL")
var notification core_drift.Notification
if slackNotificationUrl != "" {
notification = SlackNotification{slackNotificationUrl}
} else {
return nil, fmt.Errorf("could not identify drift mode, please specify slack or github")
}
return notification, nil
}
14 changes: 5 additions & 9 deletions cli/pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/diggerhq/digger/cli/pkg/backend"
core_backend "github.com/diggerhq/digger/cli/pkg/core/backend"
core_drift "github.com/diggerhq/digger/cli/pkg/core/drift"
core_policy "github.com/diggerhq/digger/cli/pkg/core/policy"
"github.com/diggerhq/digger/cli/pkg/digger"
"github.com/diggerhq/digger/cli/pkg/drift"
Expand All @@ -32,7 +31,7 @@ import (
"time"
)

func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backendApi core_backend.Api, reportingStrategy reporting.ReportStrategy, commentUpdaterProvider comment_updater.CommentUpdaterProvider) {
func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backendApi core_backend.Api, reportingStrategy reporting.ReportStrategy, commentUpdaterProvider comment_updater.CommentUpdaterProvider, driftNotifcationProvider drift.DriftNotificationProvider) {
log.Printf("Using GitHub.\n")
githubActor := os.Getenv("GITHUB_ACTOR")
if githubActor != "" {
Expand Down Expand Up @@ -301,15 +300,12 @@ func GitHubCI(lock core_locking.Lock, policyChecker core_policy.Checker, backend
CommandEnvProvider: CommandEnvProvider,
}

slackNotificationUrl := os.Getenv("INPUT_DRIFT_DETECTION_SLACK_NOTIFICATION_URL")
var notification core_drift.Notification
if slackNotificationUrl != "" {
notification = drift.SlackNotification{slackNotificationUrl}
} else {
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Could not identify drift mode, please specify slack webhook url"), 8)
notification, err := driftNotifcationProvider.Get(githubPrService)
if err != nil {
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("could not get drift notification type: %v", err), 8)
}

err := digger.RunJob(job, ghRepository, githubActor, &githubPrService, policyChecker, nil, backendApi, &notification, currentDir)
err = digger.RunJob(job, ghRepository, githubActor, &githubPrService, policyChecker, nil, backendApi, &notification, currentDir)
if err != nil {
usage.ReportErrorAndExit(githubActor, fmt.Sprintf("Failed to run commands. %s", err), 8)
}
Expand Down
3 changes: 2 additions & 1 deletion ee/cli/cmd/digger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/diggerhq/digger/cli/pkg/github"
"github.com/diggerhq/digger/cli/pkg/usage"
"github.com/diggerhq/digger/ee/cli/pkg/comment_updater"
"github.com/diggerhq/digger/ee/cli/pkg/drift"
"github.com/spf13/cobra"
"log"
"os"
Expand All @@ -21,7 +22,7 @@ var defaultCmd = &cobra.Command{
switch ci {
case digger.GitHub:
logLeader = os.Getenv("GITHUB_ACTOR")
github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderAdvanced{})
github.GitHubCI(lock, PolicyChecker, BackendApi, ReportStrategy, comment_updater.CommentUpdaterProviderAdvanced{}, drift.DriftNotificationProviderAdvanced{})
case digger.None:
print("No CI detected.")
os.Exit(10)
Expand Down
25 changes: 25 additions & 0 deletions ee/cli/pkg/drift/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package drift

import (
"fmt"
core_drift "github.com/diggerhq/digger/cli/pkg/core/drift"
"github.com/diggerhq/digger/cli/pkg/drift"
"github.com/diggerhq/digger/libs/orchestrator"
"os"
)

type DriftNotificationProviderAdvanced struct{}

func (d DriftNotificationProviderAdvanced) Get(prService orchestrator.PullRequestService) (core_drift.Notification, error) {
slackNotificationUrl := os.Getenv("INPUT_DRIFT_DETECTION_SLACK_NOTIFICATION_URL")
DriftAsGithubIssues := os.Getenv("INPUT_DRIFT_GITHUB_ISSUES")
var notification core_drift.Notification
if slackNotificationUrl != "" {
notification = drift.SlackNotification{slackNotificationUrl}
} else if DriftAsGithubIssues != "" {
notification = GithubIssueNotification{GithubService: &prService}
} else {
return nil, fmt.Errorf("could not identify drift mode, please specify slack or github")
}
return notification, nil
}

0 comments on commit 2fa89dc

Please sign in to comment.