Skip to content

Commit ffb2092

Browse files
API ported
Signed-off-by: Lukasz Gryglicki <[email protected]>
1 parent 1f2f678 commit ffb2092

File tree

1 file changed

+105
-27
lines changed

1 file changed

+105
-27
lines changed

cla-backend-go/v2/sign/service.go

Lines changed: 105 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@ func (s *service) getIndividualSignatureCallbackURLGitlab(ctx context.Context, u
15401540
if found, ok := metadata["merge_request_id"].(string); ok {
15411541
mergeRequestID = found
15421542
} else {
1543-
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)
15441544
return "", err
15451545
}
15461546

@@ -1608,11 +1608,28 @@ func (s *service) getIndividualSignatureCallbackURL(ctx context.Context, userID
16081608
log.WithFields(f).Debugf("found pull request ID: %s", pullRequestID)
16091609

16101610
// Get installation ID through a helper function
1611-
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)
16121629
githubRepository, err := s.repositoryService.GetRepositoryByExternalID(ctx, repositoryID)
16131630
if err != nil {
16141631
log.WithFields(f).WithError(err).Warnf("unable to get installation ID for repository ID: %s", repositoryID)
1615-
return "", err
1632+
return 0, err
16161633
}
16171634

16181635
// Get github organization
@@ -1621,17 +1638,16 @@ func (s *service) getIndividualSignatureCallbackURL(ctx context.Context, userID
16211638

16221639
if err != nil {
16231640
log.WithFields(f).WithError(err).Warnf("unable to get github organization for repository ID: %s", repositoryID)
1624-
return "", err
1641+
return 0, err
16251642
}
16261643

16271644
installationId = githubOrg.OrganizationInstallationID
16281645
if installationId == 0 {
1629-
log.WithFields(f).WithError(err).Warnf("unable to get installation ID for repository ID: %s", repositoryID)
1630-
return "", err
1646+
log.WithFields(f).Warnf("unable to get installation ID for repository ID: %s", repositoryID)
1647+
return 0, err
16311648
}
16321649

1633-
callbackURL := fmt.Sprintf("%s/v4/signed/individual/%d/%s/%s", s.ClaV4ApiURL, installationId, repositoryID, pullRequestID)
1634-
return callbackURL, nil
1650+
return installationId, nil
16351651
}
16361652

16371653
//nolint:gocyclo
@@ -2771,6 +2787,60 @@ func claSignatoryEmailContent(params ClaSignatoryEmailParams) (string, string) {
27712787
return emailSubject, emailBody
27722788
}
27732789

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+
27742844
func (s *service) GetUserActiveSignature(ctx context.Context, userID string) (*models.UserActiveSignature, error) {
27752845
f := logrus.Fields{
27762846
"functionName": "sign.GetUserActiveSignature",
@@ -2782,40 +2852,48 @@ func (s *service) GetUserActiveSignature(ctx context.Context, userID string) (*m
27822852
log.WithFields(f).WithError(err).Warnf("unable to get active signature meta data for user: %s", userID)
27832853
return nil, err
27842854
}
2785-
27862855
log.WithFields(f).Debugf("active signature metadata: %+v", activeSignatureMetadata)
2787-
isGitlab := false
2788-
var mergeRequestId *string
2789-
if mrId, ok := activeSignatureMetadata["merge_request_id"].(string); ok {
2856+
var (
2857+
mergeRequestId *string
2858+
isGitlab bool
2859+
returnURL string
2860+
pullRequestId string
2861+
repositoryId string
2862+
)
2863+
if mrId, ok := activeSignatureMetadata["merge_request_id"]; ok && mrId != nil {
2864+
mrStr := fmt.Sprintf("%v", mrId)
2865+
mergeRequestId = &mrStr
27902866
isGitlab = true
2791-
mergeRequestId = &mrId
27922867
}
2793-
log.WithFields(f).Debugf("generating signature callback url gitlab=%v...", isGitlab)
2794-
2795-
var callBackURL string
27962868
if isGitlab {
2797-
callBackURL, err = s.getIndividualSignatureCallbackURLGitlab(ctx, userID, activeSignatureMetadata)
2798-
if err != nil {
2799-
log.WithFields(f).WithError(err).Warnf("unable to get gitlab signature callback url for user: %s", userID)
2800-
return nil, err
2869+
var ok bool
2870+
returnURL, ok = activeSignatureMetadata["return_url"].(string)
2871+
if !ok {
2872+
log.WithFields(f).Warnf("missing return_url in metadata while merge_request_id is present: %+v", activeSignatureMetadata)
28012873
}
28022874
} else {
2803-
callBackURL, err = s.getIndividualSignatureCallbackURL(ctx, userID, activeSignatureMetadata)
2875+
returnURL, err = s.getActiveSignatureReturnURL(ctx, userID, activeSignatureMetadata)
28042876
if err != nil {
2805-
log.WithFields(f).WithError(err).Warnf("unable to get github signature callback url for user: %s", userID)
2877+
log.WithFields(f).WithError(err).Warnf("unable to get active signature return url for user: %s", userID)
28062878
return nil, err
28072879
}
28082880
}
2809-
log.WithFields(f).Debugf("signature callback url: %s", callBackURL)
2810-
projectId, _ := activeSignatureMetadata["project_id"].(string)
2811-
pullRequestId, _ := activeSignatureMetadata["pull_request_id"].(string)
2812-
repositoryId, _ := activeSignatureMetadata["repository_id"].(string)
2881+
projectId, ok := activeSignatureMetadata["project_id"].(string)
2882+
if !ok {
2883+
log.WithFields(f).Warnf("missing project_id in metadata: %+v", activeSignatureMetadata)
2884+
}
2885+
if val, ok := activeSignatureMetadata["pull_request_id"]; ok && val != nil {
2886+
pullRequestId = fmt.Sprintf("%v", val)
2887+
}
2888+
if val, ok := activeSignatureMetadata["repository_id"]; ok && val != nil {
2889+
repositoryId = fmt.Sprintf("%v", val)
2890+
}
28132891
return &models.UserActiveSignature{
28142892
MergeRequestID: mergeRequestId,
28152893
ProjectID: projectId,
28162894
PullRequestID: pullRequestId,
28172895
RepositoryID: repositoryId,
2818-
ReturnURL: strfmt.URI(callBackURL),
2896+
ReturnURL: strfmt.URI(returnURL),
28192897
UserID: userID,
28202898
}, nil
28212899
}

0 commit comments

Comments
 (0)