@@ -41,6 +41,8 @@ import (
4141var (
4242 ErrProjectNotFound = errors .New ("project not found" )
4343 ErrCLAUserNotFound = errors .New ("claUser not found" )
44+ ErrNoValidEmail = errors .New ("user with no valid email" )
45+ ErrNoLfUsername = errors .New ("user has no LF username" )
4446)
4547
4648// constants
@@ -192,6 +194,8 @@ func (s *service) GetCompanyProjectContributors(projectSFID string, companySFID
192194
193195func (s * service ) CreateCompany (companyName string , companyWebsite string , userID string , LFXPortalURL string ) (* models.CompanyOutput , error ) {
194196
197+ var lfUser * v2UserServiceModels.User
198+
195199 // Get EasyCLA User
196200 claUser , claUserErr := s .userRepo .GetUser (userID )
197201 if claUserErr != nil {
@@ -207,8 +211,51 @@ func (s *service) CreateCompany(companyName string, companyWebsite string, userI
207211 }
208212
209213 acsClient := acs_service .GetClient ()
214+ userClient := v2UserService .GetClient ()
215+
216+ // Ensure to send user invite with public email (GH)
217+ filteredEmails := []string {}
218+ for _ , email := range claUser .Emails {
219+ if ! (strings .Contains (email , "noreply.github.com" )) {
220+ filteredEmails = append (filteredEmails , email )
221+ }
222+ }
223+
224+ var userEmail * string
225+
226+ if len (filteredEmails ) > 0 {
227+ // Check if userEmail is an LF user
228+ for i := range filteredEmails {
229+ found , lfErr := userClient .SearchUserByEmail (filteredEmails [i ])
230+ if lfErr != nil {
231+ userEmail = & filteredEmails [i ]
232+ log .Debugf ("User email :%s is not associated with LF" , filteredEmails [i ])
233+ continue
234+ }
235+ lfUser = found
236+ log .Debugf ("User email : %s has an LFx account" , filteredEmails [i ])
237+ break
238+ }
239+ }
240+
241+ if lfUser != nil || claUser .LfEmail != "" {
242+ // Set lf email
243+ var email string
244+ if claUser .LfEmail != "" {
245+ email = claUser .LfEmail
246+ } else {
247+ email = * lfUser .Emails [0 ].EmailAddress
248+ }
249+ // set lf username
250+ var lfUsername string
251+ if claUser .LfUsername != "" {
252+ log .Debugf ("Setting username : %s" , claUser .LfUsername )
253+ lfUsername = claUser .LfUsername
254+ } else {
255+ log .Debugf ("Setting username : %s" , lfUser .Username )
256+ lfUsername = lfUser .Username
257+ }
210258
211- if claUser .LfUsername != "" {
212259 // Get Role ID
213260 roleID , designeeErr := acsClient .GetRoleID ("company-owner" )
214261 if designeeErr != nil {
@@ -217,32 +264,27 @@ func (s *service) CreateCompany(companyName string, companyWebsite string, userI
217264 return nil , designeeErr
218265 }
219266
220- err = orgClient .CreateOrgUserRoleOrgScope (claUser .LfEmail , org .ID , roleID )
267+ if lfUsername == "" {
268+ msg := fmt .Sprintf ("EasyCLA - Bad Request - User with lfEmail: %s has no username required for setting scope " , email )
269+ log .Warn (msg )
270+ return nil , ErrNoLfUsername
271+ }
272+ err = orgClient .CreateOrgUserRoleOrgScope (email , org .ID , roleID )
221273 if err != nil {
222- log .Warnf ("Organization Service - Failed to assign company-owner role to user: %s, error: %+v " , claUser . LfUsername , err )
274+ log .Warnf ("Organization Service - Failed to assign company-owner role to user: %s, error: %+v " , email , err )
223275 return nil , err
224276 }
225- log .Debugf ("User :%s has been assigned the company-owner role to organization: %s " , claUser . LfUsername , org .Name )
277+ log .Debugf ("User :%s has been assigned the company-owner role to organization: %s " , email , org .Name )
226278 //Send Email to User with instructions to complete Company profile
227- log .Debugf ("Sending Email to user :%s to complete setup for newly created Org : %s " , claUser .Username , org .Name )
228- sendEmailToUserCompanyProfile (org .Name , claUser .Username , claUser .LfUsername , LFXPortalURL )
229-
230- } else {
231- // Ensure to send user invite with public email (GH)
232- filteredEmails := []string {}
233- for _ , email := range claUser .Emails {
234- if ! (strings .Contains (email , "noreply.github.com" )) {
235- filteredEmails = append (filteredEmails , email )
236- }
237- }
238-
239- if len (filteredEmails ) > 0 {
240- // Send User invite
241- acsErr := acsClient .SendUserInvite (& filteredEmails [0 ], "contributor" , "organization" , org .ID , "userinvite" )
242- if acsErr != nil {
243- return nil , acsErr
244- }
245- log .Debugf ("ACS Service - User invite initiated for user with email : %s" , filteredEmails [0 ])
279+ log .Debugf ("Sending Email to user :%s to complete setup for newly created Org : %s " , email , org .Name )
280+ sendEmailToUserCompanyProfile (org .Name , email , lfUsername , LFXPortalURL )
281+
282+ } else if userEmail != nil {
283+ // Send User invite
284+ log .Debugf ("User invite initiated for user with email : %s" , * userEmail )
285+ acsErr := acsClient .SendUserInvite (userEmail , "contributor" , "organization" , org .ID , "userinvite" )
286+ if acsErr != nil {
287+ return nil , acsErr
246288 }
247289 }
248290
0 commit comments