Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions cla-backend-go/github/github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2597,26 +2597,67 @@ func GetReturnURL(ctx context.Context, installationID, repositoryID int64, pullR
"pullRequestID": pullRequestID,
}

client, err := NewGithubAppClient(installationID)
if installationID <= 0 {
err := errors.New("invalid installation ID")
log.WithFields(f).WithError(err).Warn("invalid installation ID")
return "", err
}
if repositoryID <= 0 {
err := errors.New("invalid repository ID")
log.WithFields(f).WithError(err).Warn("invalid repository ID")
return "", err
}
if pullRequestID <= 0 {
err := errors.New("invalid pull request ID")
log.WithFields(f).WithError(err).Warn("invalid pull request ID")
return "", err
}

client, err := NewGithubAppClient(installationID)
if err != nil {
log.WithFields(f).WithError(err).Warn("unable to create Github client")
return "", err
}

log.WithFields(f).Debugf("getting github repository by id: %d", repositoryID)
repo, _, err := client.Repositories.GetByID(ctx, repositoryID)
repo, resp, err := client.Repositories.GetByID(ctx, repositoryID)
if err != nil {
if ok, wrapped := CheckAndWrapForKnownErrors(resp, err); ok {
log.WithFields(f).WithError(wrapped).Warnf("unable to get repository by ID: %d", repositoryID)
return "", wrapped
}
log.WithFields(f).WithError(err).Warnf("unable to get repository by ID: %d", repositoryID)
return "", err
}
if repo == nil || repo.Owner == nil || repo.Owner.Login == nil || repo.Name == nil {
err := fmt.Errorf("missing repository owner or name for repository ID %d", repositoryID)
log.WithFields(f).WithError(err).Warn("invalid repository metadata")
return "", err
}

owner := repo.GetOwner().GetLogin()
name := repo.GetName()
if owner == "" || name == "" {
err := fmt.Errorf("invalid repository owner/name for repository ID %d", repositoryID)
log.WithFields(f).WithError(err).Warn("invalid repository metadata")
return "", err
}

log.WithFields(f).Debugf("getting pull request by id: %d", pullRequestID)
pullRequest, _, err := client.PullRequests.Get(ctx, *repo.Owner.Login, *repo.Name, pullRequestID)
pullRequest, resp, err := client.PullRequests.Get(ctx, owner, name, pullRequestID)
if err != nil {
if ok, wrapped := CheckAndWrapForKnownErrors(resp, err); ok {
log.WithFields(f).WithError(wrapped).Warnf("unable to get pull request by ID: %d", pullRequestID)
return "", wrapped
}
log.WithFields(f).WithError(err).Warnf("unable to get pull request by ID: %d", pullRequestID)
return "", err
}
if pullRequest == nil || pullRequest.HTMLURL == nil || *pullRequest.HTMLURL == "" {
err := fmt.Errorf("missing html url for pull request %d/%s/%s", pullRequestID, owner, name)
log.WithFields(f).WithError(err).Warn("invalid pull request metadata")
return "", err
}

log.WithFields(f).Debugf("returning pull request html url: %s", *pullRequest.HTMLURL)

Expand Down
23 changes: 22 additions & 1 deletion cla-backend-go/v2/sign/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,22 @@ func getUserName(user *v1Models.User) string {
return ""
}

// metadataStringValue extracts a string value from metadata and validates it.
func metadataStringValue(metadata map[string]interface{}, key string) (string, error) {
if metadata == nil {
return "", fmt.Errorf("missing %s in metadata", key)
}
v, ok := metadata[key]
if !ok || v == nil {
return "", fmt.Errorf("missing %s in metadata", key)
}
s := strings.TrimSpace(fmt.Sprintf("%v", v))
if s == "" || s == "<nil>" {
return "", fmt.Errorf("missing %s in metadata", key)
}
return s, nil
}

func (s *service) getIndividualSignatureCallbackURLGitlab(ctx context.Context, userID string, metadata map[string]interface{}) (string, error) {
f := logrus.Fields{
"functionName": "sign.getIndividualSignatureCallbackURLGitlab",
Expand All @@ -1572,13 +1588,15 @@ func (s *service) getIndividualSignatureCallbackURLGitlab(ctx context.Context, u
if found, ok := metadata["repository_id"].(string); ok {
repositoryID = found
} else {
err = errors.New("missing repository_id in metadata")
log.WithFields(f).WithError(err).Warnf("unable to get repository ID for user: %s", userID)
return "", err
}

if found, ok := metadata["merge_request_id"].(string); ok {
mergeRequestID = found
} else {
err = errors.New("missing merge_request_id in metadata")
log.WithFields(f).WithError(err).Warnf("unable to get merge request ID for user: %s", userID)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
return "", err
}
Expand All @@ -1599,6 +1617,7 @@ func (s *service) getIndividualSignatureCallbackURLGitlab(ctx context.Context, u
}

if gitlabOrg.OrganizationID == "" {
err = errors.New("missing organization ID for GitLab repository")
log.WithFields(f).WithError(err).Warnf("unable to get organization ID for repository ID: %s", repositoryID)
return "", err
}
Expand Down Expand Up @@ -1631,6 +1650,7 @@ func (s *service) getIndividualSignatureCallbackURL(ctx context.Context, userID
if found, ok := metadata["repository_id"].(string); ok {
repositoryID = found
} else {
err = errors.New("missing repository_id in metadata")
log.WithFields(f).WithError(err).Warnf("unable to get repository ID for user: %s", userID)
return "", err
}
Expand All @@ -1640,6 +1660,7 @@ func (s *service) getIndividualSignatureCallbackURL(ctx context.Context, userID
if found, ok := metadata["pull_request_id"].(string); ok {
pullRequestID = found
} else {
err = errors.New("missing pull_request_id in metadata")
log.WithFields(f).WithError(err).Warnf("unable to get pull request ID for user: %s", userID)
return "", err
}
Expand Down Expand Up @@ -1682,7 +1703,7 @@ func (s *service) getInstallationIDFromRepositoryID(ctx context.Context, reposit

installationId = githubOrg.OrganizationInstallationID
if installationId == 0 {
err = fmt.Errorf("installation ID missing for repository ID: %s", repositoryID)
err = fmt.Errorf("missing installation ID for repository ID in metadata: %s", repositoryID)
log.WithFields(f).WithError(err).Warnf("unable to get installation ID for repository ID: %s", repositoryID)
return 0, err
}
Expand Down
Loading