@@ -90,6 +90,7 @@ type Service interface {
9090 SignedIndividualCallbackGitlab (ctx context.Context , payload []byte , userID , organizationID , repositoryID , mergeRequestID string ) error
9191 SignedIndividualCallbackGerrit (ctx context.Context , payload []byte , userID string ) error
9292 SignedCorporateCallback (ctx context.Context , payload []byte , companyID , projectID string ) error
93+ GetUserActiveSignature (ctx context.Context , userID string ) (* models.UserActiveSignature , error )
9394}
9495
9596// service
@@ -1539,7 +1540,7 @@ func (s *service) getIndividualSignatureCallbackURLGitlab(ctx context.Context, u
15391540 if found , ok := metadata ["merge_request_id" ].(string ); ok {
15401541 mergeRequestID = found
15411542 } else {
1542- log .WithFields (f ).WithError (err ).Warnf ("unable to get pull request ID for user: %s" , userID )
1543+ log .WithFields (f ).WithError (err ).Warnf ("unable to get merge request ID for user: %s" , userID )
15431544 return "" , err
15441545 }
15451546
@@ -1607,11 +1608,28 @@ func (s *service) getIndividualSignatureCallbackURL(ctx context.Context, userID
16071608 log .WithFields (f ).Debugf ("found pull request ID: %s" , pullRequestID )
16081609
16091610 // Get installation ID through a helper function
1610- log .WithFields (f ).Debugf ("getting repository..." )
1611+ installationId , err = s .getInstallationIDFromRepositoryID (ctx , repositoryID )
1612+ if err != nil {
1613+ log .WithFields (f ).WithError (err ).Warnf ("unable to get github organization for repository ID: %s" , repositoryID )
1614+ return "" , err
1615+ }
1616+
1617+ callbackURL := fmt .Sprintf ("%s/v4/signed/individual/%d/%s/%s" , s .ClaV4ApiURL , installationId , repositoryID , pullRequestID )
1618+ return callbackURL , nil
1619+ }
1620+
1621+ func (s * service ) getInstallationIDFromRepositoryID (ctx context.Context , repositoryID string ) (int64 , error ) {
1622+ var installationId int64
1623+ f := logrus.Fields {
1624+ "functionName" : "sign.getInstallationIDFromRepositoryID" ,
1625+ "repositoryID" : repositoryID ,
1626+ }
1627+ // Get installation ID through a helper function
1628+ log .WithFields (f ).Debugf ("getting repository for ID=%s..." , repositoryID )
16111629 githubRepository , err := s .repositoryService .GetRepositoryByExternalID (ctx , repositoryID )
16121630 if err != nil {
16131631 log .WithFields (f ).WithError (err ).Warnf ("unable to get installation ID for repository ID: %s" , repositoryID )
1614- return "" , err
1632+ return 0 , err
16151633 }
16161634
16171635 // Get github organization
@@ -1620,17 +1638,16 @@ func (s *service) getIndividualSignatureCallbackURL(ctx context.Context, userID
16201638
16211639 if err != nil {
16221640 log .WithFields (f ).WithError (err ).Warnf ("unable to get github organization for repository ID: %s" , repositoryID )
1623- return "" , err
1641+ return 0 , err
16241642 }
16251643
16261644 installationId = githubOrg .OrganizationInstallationID
16271645 if installationId == 0 {
1628- log .WithFields (f ).WithError ( err ). Warnf ("unable to get installation ID for repository ID: %s" , repositoryID )
1629- return "" , err
1646+ log .WithFields (f ).Warnf ("unable to get installation ID for repository ID: %s" , repositoryID )
1647+ return 0 , err
16301648 }
16311649
1632- callbackURL := fmt .Sprintf ("%s/v4/signed/individual/%d/%s/%s" , s .ClaV4ApiURL , installationId , repositoryID , pullRequestID )
1633- return callbackURL , nil
1650+ return installationId , nil
16341651}
16351652
16361653//nolint:gocyclo
@@ -2769,3 +2786,117 @@ func claSignatoryEmailContent(params ClaSignatoryEmailParams) (string, string) {
27692786
27702787 return emailSubject , emailBody
27712788}
2789+
2790+ func (s * service ) getActiveSignatureReturnURL (ctx context.Context , userID string , metadata map [string ]interface {}) (string , error ) {
2791+
2792+ f := logrus.Fields {
2793+ "functionName" : "sign.getActiveSignatureReturnURL" ,
2794+ }
2795+
2796+ var returnURL , rId string
2797+ var err , err2 error
2798+ var pullRequestID int
2799+ var repositoryID int64
2800+ var installationID int64
2801+
2802+ if found , ok := metadata ["pull_request_id" ]; ok && found != nil {
2803+ prId := fmt .Sprintf ("%v" , found )
2804+ pullRequestID , err2 = strconv .Atoi (prId )
2805+ if err2 != nil {
2806+ log .WithFields (f ).WithError (err2 ).Warnf ("unable to get pull request ID for user: %s" , userID )
2807+ return "" , err2
2808+ }
2809+ } else {
2810+ err2 = errors .New ("missing pull_request_id in metadata" )
2811+ log .WithFields (f ).WithError (err2 ).Warnf ("unable to get pull request ID for user: %s" , userID )
2812+ return "" , err2
2813+ }
2814+
2815+ if found , ok := metadata ["repository_id" ]; ok && found != nil {
2816+ rId = fmt .Sprintf ("%v" , found )
2817+ repositoryID , err2 = strconv .ParseInt (rId , 10 , 64 )
2818+ if err2 != nil {
2819+ log .WithFields (f ).WithError (err2 ).Warnf ("unable to get repository ID for user: %s" , userID )
2820+ return "" , err2
2821+ }
2822+ } else {
2823+ err2 = errors .New ("missing repository_id in metadata" )
2824+ log .WithFields (f ).WithError (err2 ).Warnf ("unable to get repository ID for user: %s" , userID )
2825+ return "" , err2
2826+ }
2827+
2828+ // Get installation ID through a helper function
2829+ installationID , err = s .getInstallationIDFromRepositoryID (ctx , rId )
2830+ if err != nil {
2831+ log .WithFields (f ).WithError (err ).Warnf ("unable to get github organization for repository ID: %v" , repositoryID )
2832+ return "" , err
2833+ }
2834+
2835+ returnURL , err = github .GetReturnURL (ctx , installationID , repositoryID , pullRequestID )
2836+
2837+ if err != nil {
2838+ return "" , err
2839+ }
2840+
2841+ return returnURL , nil
2842+ }
2843+
2844+ func (s * service ) GetUserActiveSignature (ctx context.Context , userID string ) (* models.UserActiveSignature , error ) {
2845+ f := logrus.Fields {
2846+ "functionName" : "sign.GetUserActiveSignature" ,
2847+ utils .XREQUESTID : ctx .Value (utils .XREQUESTID ),
2848+ "userID" : userID ,
2849+ }
2850+ activeSignatureMetadata , err := s .storeRepository .GetActiveSignatureMetaData (ctx , userID )
2851+ if err != nil {
2852+ log .WithFields (f ).WithError (err ).Warnf ("unable to get active signature meta data for user: %s" , userID )
2853+ return nil , err
2854+ }
2855+ log .WithFields (f ).Debugf ("active signature metadata: %+v" , activeSignatureMetadata )
2856+ if len (activeSignatureMetadata ) == 0 {
2857+ return nil , nil
2858+ }
2859+ var (
2860+ mergeRequestId * string
2861+ isGitlab bool
2862+ returnURL string
2863+ pullRequestId string
2864+ repositoryId string
2865+ )
2866+ if mrId , ok := activeSignatureMetadata ["merge_request_id" ]; ok && mrId != nil {
2867+ mrStr := fmt .Sprintf ("%v" , mrId )
2868+ mergeRequestId = & mrStr
2869+ isGitlab = true
2870+ }
2871+ if isGitlab {
2872+ var ok bool
2873+ returnURL , ok = activeSignatureMetadata ["return_url" ].(string )
2874+ if ! ok {
2875+ log .WithFields (f ).Warnf ("missing return_url in metadata while merge_request_id is present: %+v" , activeSignatureMetadata )
2876+ }
2877+ } else {
2878+ returnURL , err = s .getActiveSignatureReturnURL (ctx , userID , activeSignatureMetadata )
2879+ if err != nil {
2880+ log .WithFields (f ).WithError (err ).Warnf ("unable to get active signature return url for user: %s" , userID )
2881+ return nil , err
2882+ }
2883+ }
2884+ projectId , ok := activeSignatureMetadata ["project_id" ].(string )
2885+ if ! ok {
2886+ log .WithFields (f ).Warnf ("missing project_id in metadata: %+v" , activeSignatureMetadata )
2887+ }
2888+ if val , ok := activeSignatureMetadata ["pull_request_id" ]; ok && val != nil {
2889+ pullRequestId = fmt .Sprintf ("%v" , val )
2890+ }
2891+ if val , ok := activeSignatureMetadata ["repository_id" ]; ok && val != nil {
2892+ repositoryId = fmt .Sprintf ("%v" , val )
2893+ }
2894+ return & models.UserActiveSignature {
2895+ MergeRequestID : mergeRequestId ,
2896+ ProjectID : projectId ,
2897+ PullRequestID : pullRequestId ,
2898+ RepositoryID : repositoryId ,
2899+ ReturnURL : strfmt .URI (returnURL ),
2900+ UserID : userID ,
2901+ }, nil
2902+ }
0 commit comments