From e0599c37b0e942420bc0142d77bae764a396f126 Mon Sep 17 00:00:00 2001 From: Harold Wanyama Date: Thu, 14 Mar 2024 04:56:30 +0000 Subject: [PATCH] [#4240] Bug/LDAP Gerrit LDAP group addition - Handled nil pointer execption for auth user - Handled email send for icla - Added ldap group for corporate callback Signed-off-by: Harold Wanyama --- cla-backend-go/gerrits/lf_group.go | 2 -- cla-backend-go/gerrits/service.go | 2 -- cla-backend-go/v2/sign/service.go | 37 ++++++++++++++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/cla-backend-go/gerrits/lf_group.go b/cla-backend-go/gerrits/lf_group.go index d3eeb980e..ae9c455fe 100644 --- a/cla-backend-go/gerrits/lf_group.go +++ b/cla-backend-go/gerrits/lf_group.go @@ -238,8 +238,6 @@ func (lfg *LFGroup) AddUserToGroup(ctx context.Context, authUser *auth.User, cla "claGroupID": claGroupID, "groupName": groupName, "userName": userName, - "authUserName": authUser.UserName, - "authUserEmail": authUser.Email, } log.WithFields(f).Debug("adding user to group...") diff --git a/cla-backend-go/gerrits/service.go b/cla-backend-go/gerrits/service.go index 3ca90164a..ecc72b236 100644 --- a/cla-backend-go/gerrits/service.go +++ b/cla-backend-go/gerrits/service.go @@ -329,8 +329,6 @@ func (s service) AddUserToGroup(ctx context.Context, authUser *auth.User, claGro utils.XREQUESTID: ctx.Value(utils.XREQUESTID), "claGroupID": claGroupID, "userName": userName, - "authUserName": authUser.UserName, - "authUserEmail": authUser.Email, } log.WithFields(f).Debug("querying for CLA Group gerrits...") diff --git a/cla-backend-go/v2/sign/service.go b/cla-backend-go/v2/sign/service.go index ce0c208c4..33b08a1c9 100644 --- a/cla-backend-go/v2/sign/service.go +++ b/cla-backend-go/v2/sign/service.go @@ -931,14 +931,16 @@ func (s *service) SignedIndividualCallbackGerrit(ctx context.Context, payload [] return err } - // send email to user - log.WithFields(f).Debugf("sending email to user... ") + log.WithFields(f).Debugf("getting claGroupID: %s", signature.ProjectID) + claGroup, err := s.claGroupService.GetCLAGroup(ctx, signature.ProjectID) if err != nil { log.WithFields(f).WithError(err).Warnf("unable to lookup CLA Group by ID: %s", signature.ProjectID) return err } + log.WithFields(f).Debugf("claGroup: %+s found", claGroup.ProjectID) + subject := fmt.Sprintf("EasyCLA: Individual CLA Signed for %s", claGroup.ProjectName) pdfLink := fmt.Sprintf("%s/v3/signatures/%s/%s/icla/pdf", s.ClaV1ApiURL, signature.ProjectID, signature.SignatureReferenceID) emailParams := emails.DocumentSignedTemplateParams{ @@ -957,7 +959,7 @@ func (s *service) SignedIndividualCallbackGerrit(ctx context.Context, payload [] recipients := []string{utils.GetBestEmail(claUser)} - body, err := emails.RenderDocumentSignedTemplate(s.emailTemplateService, claGroup.Version, claGroup.ProjectID, emailParams) + body, err := emails.RenderDocumentSignedTemplate(s.emailTemplateService, claGroup.Version, claGroup.ProjectExternalID, emailParams) if err != nil { log.WithFields(f).WithError(err).Warnf("unable to render document signed template for project version: %s, project ID: %s", claGroup.Version, claGroup.ProjectID) return err @@ -1004,13 +1006,15 @@ func (s *service) SignedIndividualCallbackGerrit(ctx context.Context, payload [] }) // Add User to Gerrit Group - log.WithFields(f).Debugf("adding user to ldap group...") if claUser.LfUsername != "" { + log.WithFields(f).Debugf("adding user to gerrit group: %s", claUser.LfUsername) err = s.gerritService.AddUserToGroup(ctx, nil, signature.ProjectID, claUser.LfUsername, utils.ClaTypeICLA) if err != nil { log.WithFields(f).WithError(err).Warnf("unable to add user to gerrit group") return err } + } else { + log.WithFields(f).Warnf("user LF username is empty") } } else { @@ -1188,6 +1192,31 @@ func (s *service) SignedCorporateCallback(ctx context.Context, payload []byte, c CompanySFID: companyModel.CompanyExternalID, }) + // Check if project is a gerrit instance + var gerrits []*v1Models.Gerrit + gerritList, err := s.gerritService.GetClaGroupGerrits(ctx, projectID) + if err != nil { + log.WithFields(f).WithError(err).Warnf("unable to get gerrit instances for project: %s", projectID) + gerrits = []*v1Models.Gerrit{} + } else { + log.WithFields(f).Debugf("gerrit instances found for project: %s", projectID) + gerrits = gerritList.List + } + + // Add User to Gerrit Group + if len(gerrits) > 0 { + if user.LfUsername != "" { + log.WithFields(f).Debugf("adding user to gerrit group: %s", user.LfUsername) + err = s.gerritService.AddUserToGroup(ctx, nil, projectID, user.LfUsername, utils.ClaTypeCCLA) + if err != nil { + log.WithFields(f).WithError(err).Warnf("unable to add user to gerrit group") + return err + } + } else { + log.WithFields(f).Warnf("user LF username is empty") + } + } + return nil }