Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2600] Bug/ Add|Update Repository #2605

Merged
merged 1 commit into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions cla-backend-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/communitybridge/easycla v1.0.99 h1:PkmkMV7cLH2Q2YNSFiGGmlyrHBXVYdsWMwbXNuMAyqw=
github.com/communitybridge/easycla v1.0.106 h1:NLYUZUZtp9DQ0dHEQkhz9h9EMzLRmuh9udsPZk8oLoQ=
github.com/communitybridge/easycla v1.0.107 h1:dktHAji1yJ1nMEu54z4paPWOM4Q7A9rryc0OCADfAcY=
github.com/communitybridge/easycla v1.0.117 h1:o+rdmcNgZeMQ/N8HV/d5apNIBrkYH7eyM9UUYnEzewo=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
Expand Down
120 changes: 120 additions & 0 deletions cla-backend-go/repositories/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@ const (
RepositoryNameIndex = "repository-name-index"
)

// ErrRepositoryDoesNotExist ...
var ErrRepositoryDoesNotExist = errors.New("repository does not exist")

// Repository defines functions of Repositories
type Repository interface {
AddGithubRepository(ctx context.Context, externalProjectID string, projectSFID string, input *models.GithubRepositoryInput) (*models.GithubRepository, error)
UpdateGithubRepository(ctx context.Context, repositoryID string, input *models.GithubRepositoryInput) (*models.GithubRepository, error)
UpdateClaGroupID(ctx context.Context, repositoryID, claGroupID string) error
EnableRepository(ctx context.Context, repositoryID string) error
EnableRepositoryWithCLAGroupID(ctx context.Context, repositoryID, claGroupID string) error
Expand Down Expand Up @@ -142,6 +146,122 @@ func (r repo) AddGithubRepository(ctx context.Context, externalProjectID string,
return repository.toModel(), nil
}

// UpdateGithubRepository updates the repository record for given ID
func (r *repo) UpdateGithubRepository(ctx context.Context, repositoryID string, input *models.GithubRepositoryInput) (*models.GithubRepository, error) {

externalID := utils.StringValue(input.RepositoryExternalID)
projectSFID := utils.StringValue(input.RepositoryProjectID)
repositoryName := utils.StringValue(input.RepositoryName)
repositoryOrganizationName := utils.StringValue(input.RepositoryOrganizationName)
repositoryType := utils.StringValue(input.RepositoryType)
repositoryURL := utils.StringValue(input.RepositoryURL)

f := logrus.Fields{
"functionName": "repositories.repository.UpdateGitHubRepository",
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"repositoryID": repositoryID,
"externalProjectID": externalID,
"projectSFID": projectSFID,
"repositoryName": repositoryName,
"repositoryOrganizationName": repositoryOrganizationName,
"repositoryType": repositoryType,
"repositoryURL": repositoryURL,
}

log.WithFields(f).Debugf("updating Repository : %s... ", repositoryID)

repoModel, repoErr := r.GetRepository(ctx, repositoryID)
if repoErr != nil {
log.WithFields(f).Warnf("update error locating the repository ID : %s , error: %+v ", repositoryID, repoErr)
return nil, repoErr
}

if repoModel == nil {
log.WithFields(f).Warnf("Repository does not exist for repo: %s ", repositoryID)
return nil, ErrRepositoryDoesNotExist
}

expressionAttributeNames := map[string]*string{}
expressionAttributeValues := map[string]*dynamodb.AttributeValue{}
updateExpression := "SET "

if projectSFID != "" && repoModel.ProjectSFID != projectSFID {
log.WithFields(f).Debugf("adding projectSFID : %s ", projectSFID)
expressionAttributeNames["#P"] = aws.String("project_sfid")
expressionAttributeValues[":p"] = &dynamodb.AttributeValue{S: aws.String(projectSFID)}
updateExpression = updateExpression + " #P = :p, "
}

if externalID != "" && repoModel.RepositoryExternalID != externalID {
log.WithFields(f).Debugf("adding externalID : %s ", externalID)
expressionAttributeNames["#E"] = aws.String("repository_external_id")
expressionAttributeValues[":e"] = &dynamodb.AttributeValue{S: aws.String(externalID)}
updateExpression = updateExpression + " #E = :e, "
}

if repositoryName != "" && repoModel.RepositoryName != repositoryName {
log.WithFields(f).Debugf("adding repositoryName : %s ", repositoryName)
expressionAttributeNames["#N"] = aws.String("repository_name")
expressionAttributeValues[":n"] = &dynamodb.AttributeValue{S: aws.String(repositoryName)}
updateExpression = updateExpression + " #N = :n, "
}

if repositoryOrganizationName != "" && repoModel.RepositoryOrganizationName != repositoryOrganizationName {
log.WithFields(f).Debugf("adding repositoryOrganizationName : %s ", repositoryOrganizationName)
expressionAttributeNames["#O"] = aws.String("repository_organization_name")
expressionAttributeValues[":o"] = &dynamodb.AttributeValue{S: aws.String(repositoryOrganizationName)}
updateExpression = updateExpression + " #O = :o, "
}

if repositoryType != "" && repoModel.RepositoryType != repositoryType {
log.WithFields(f).Debugf("adding repositoryType : %s ", repositoryType)
expressionAttributeNames["#T"] = aws.String("repository_type")
expressionAttributeValues[":t"] = &dynamodb.AttributeValue{S: aws.String(repositoryType)}
updateExpression = updateExpression + " #T = :t, "
}

if repositoryURL != "" && repoModel.RepositoryURL != repositoryURL {
log.WithFields(f).Debugf("adding repositoryURL : %s ", repositoryURL)
expressionAttributeNames["#U"] = aws.String("repository_url")
expressionAttributeValues[":u"] = &dynamodb.AttributeValue{S: aws.String(repositoryURL)}
updateExpression = updateExpression + " #U = :u, "
}

if input.Enabled != nil && repoModel.Enabled != *input.Enabled {
log.WithFields(f).Debugf("adding enabled flag: %+v", *input.Enabled)
expressionAttributeNames["#EN"] = aws.String("enabled")
expressionAttributeValues[":en"] = &dynamodb.AttributeValue{BOOL: input.Enabled}
updateExpression = updateExpression + " #EN = :en, "
}

_, currentTimeString := utils.CurrentTime()
log.WithFields(f).Debugf("adding date_modified: %s", currentTimeString)
expressionAttributeNames["#M"] = aws.String("date_modified")
expressionAttributeValues[":m"] = &dynamodb.AttributeValue{S: aws.String(currentTimeString)}
updateExpression = updateExpression + " #M = :m "

// Assemble the query input parameters
updateInput := &dynamodb.UpdateItemInput{
Key: map[string]*dynamodb.AttributeValue{
"repository_id": {
S: aws.String(repositoryID),
},
},
ExpressionAttributeNames: expressionAttributeNames,
ExpressionAttributeValues: expressionAttributeValues,
UpdateExpression: &updateExpression,
TableName: aws.String(r.repositoryTableName),
}

_, updateErr := r.dynamoDBClient.UpdateItem(updateInput)
if updateErr != nil {
log.WithFields(f).Warnf("error updatingRepository by repositoryID: %s, error: %v", repositoryID, updateErr)
return nil, updateErr
}

return r.GetRepository(ctx, repositoryID)
}

// UpdateClaGroupID updates the claGroupID of the repository
func (r *repo) UpdateClaGroupID(ctx context.Context, repositoryID, claGroupID string) error {
return r.setClaGroupIDGithubRepository(ctx, repositoryID, claGroupID)
Expand Down
3 changes: 3 additions & 0 deletions cla-backend-go/swagger/common/github-repository-input.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ properties:
type: string
repositoryUrl:
type: string
enabled:
type: boolean
default: true
20 changes: 20 additions & 0 deletions cla-backend-go/v2/repositories/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"strconv"

"github.com/jinzhu/copier"
"github.com/sirupsen/logrus"

"github.com/go-openapi/swag"
Expand Down Expand Up @@ -138,6 +139,25 @@ func (s *service) AddGithubRepository(ctx context.Context, projectSFID string, i
}

// We already have an existing repository model with the same name
if existingRepositoryModel != nil && !existingRepositoryModel.Enabled {
msg := fmt.Sprintf("Github repository : %s disabled and shall get re-enabled... ", utils.StringValue(ghRepo.FullName))
log.WithFields(f).Debug(msg)
var v1Input v1Models.GithubRepositoryInput
err := copier.Copy(&v1Input, &input)
if err != nil {
log.WithFields(f).Error("unable to create v1GithubRepository input")
return nil, err
}
// Enabled repository
*v1Input.Enabled = true
// Update Repo details in case of any changes
updatedRepository, updateErr := s.repo.UpdateGithubRepository(ctx, existingRepositoryModel.RepositoryID, &v1Input)
if updateErr != nil {
return nil, updateErr
}
return updatedRepository, nil
}

if existingRepositoryModel != nil {
msg := fmt.Sprintf("GitHub repository already exists with repository name: %s", utils.StringValue(ghRepo.FullName))
log.WithFields(f).Warn(msg)
Expand Down