Skip to content

Commit a4af881

Browse files
Added optional authenticated user parameter to CreateOrgNotification function
1 parent 12efd7b commit a4af881

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

notifications.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -624,16 +624,22 @@ func forwardNotificationRequest(ctx context.Context, title, description, referen
624624
return nil
625625
}
626626

627-
func CreateOrgNotification(ctx context.Context, title, description, referenceUrl, orgId string, adminsOnly bool) error {
627+
func CreateOrgNotification(ctx context.Context, title, description, referenceUrl, orgId string, adminsOnly bool, authenticatedUser ...*User) error {
628628
if len(orgId) == 0 {
629629
log.Printf("[ERROR] No org ID provided to create notification '%s'", title)
630-
return errors.New("No org ID provided")
630+
return errors.New("no org ID provided")
631+
}
632+
633+
// Extract authenticated user if provided
634+
var requestingUser *User
635+
if len(authenticatedUser) > 0 && authenticatedUser[0] != nil {
636+
requestingUser = authenticatedUser[0]
631637
}
632638

633639
// Since we use a static workflow name, this should be effective.
634640
if strings.Contains(title, "Ops Dashboard Workflow") {
635641
log.Printf("[INFO] Skipping create notification for health check workflow")
636-
return errors.New("Health check workflow detected")
642+
return errors.New("health check workflow detected")
637643
}
638644

639645
if project.Environment == "" {
@@ -745,14 +751,14 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
745751
}
746752
}
747753

754+
// Look for admin with API key
748755
for _, user := range authOrg.Users {
749756
if user.Role == "admin" && len(user.Id) > 0 && len(selectedApikey) == 0 {
750-
// Checking if it's the right active org
751-
// FIXME: Should it need to be in the active org? Shouldn't matter? :thinking:
752757
foundUser, err := GetUser(ctx, user.Id)
753758
if err == nil {
754759
if len(foundUser.ApiKey) > 0 {
755760
selectedApikey = foundUser.ApiKey
761+
log.Printf("[DEBUG] Using admin's API key for notification workflow")
756762
break
757763
} else {
758764
// Admin exists but has no API key - auto-generate one
@@ -770,6 +776,12 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
770776
}
771777
}
772778

779+
// If no admin key found, use requesting user's key
780+
if len(selectedApikey) == 0 && requestingUser != nil && len(requestingUser.ApiKey) > 0 {
781+
selectedApikey = requestingUser.ApiKey
782+
log.Printf("[INFO] No admin API key available. Using requesting user's (%s) API key for notification workflow", requestingUser.Username)
783+
}
784+
773785
if len(matchingNotifications) > 0 {
774786
// FIXME: This may have bugs for old workflows with new users (not being rediscovered)
775787
if project.Environment != "cloud" {
@@ -856,12 +868,14 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
856868
}
857869

858870
selectedApikey := ""
871+
// Look for admin with API key
859872
for _, user := range filteredUsers {
860873
if user.Role == "admin" && len(selectedApikey) == 0 {
861874
foundUser, err := GetUser(ctx, user.Id)
862875
if err == nil {
863876
if len(foundUser.ApiKey) > 0 {
864877
selectedApikey = foundUser.ApiKey
878+
log.Printf("[DEBUG] Using admin's API key for notification workflow")
865879
break
866880
} else {
867881
// Admin exists but has no API key - auto-generate one
@@ -879,9 +893,15 @@ func CreateOrgNotification(ctx context.Context, title, description, referenceUrl
879893
}
880894
}
881895

896+
// If no admin key found, use requesting user's key
897+
if len(selectedApikey) == 0 && requestingUser != nil && len(requestingUser.ApiKey) > 0 {
898+
selectedApikey = requestingUser.ApiKey
899+
log.Printf("[INFO] No admin API key available. Using requesting user's (%s) API key for notification workflow", requestingUser.Username)
900+
}
901+
882902
if len(org.Defaults.NotificationWorkflow) > 0 {
883903
if len(selectedApikey) == 0 {
884-
log.Printf("[ERROR] Didn't find an apikey to use when sending notifications for org %s to workflow %s", org.Id, org.Defaults.NotificationWorkflow)
904+
log.Printf("[ERROR] No API key available to trigger notification workflow for org %s to workflow %s", org.Id, org.Defaults.NotificationWorkflow)
885905
if debug {
886906
log.Printf("\n\n\n")
887907
}
@@ -1152,6 +1172,7 @@ func HandleCreateNotification(resp http.ResponseWriter, request *http.Request) {
11521172
notification.ReferenceUrl,
11531173
orgId,
11541174
false,
1175+
&user, // Pass authenticated user for API key fallback
11551176
)
11561177

11571178
DeleteCache(ctx, fmt.Sprintf("%s_%s", "notifications", user.ActiveOrg.Id))

0 commit comments

Comments
 (0)