// Post handles POST request, and records audit log or refreshes cache based on event. func (n *NotificationHandler) Post() { var notification models.Notification err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), ¬ification) if err != nil { log.Errorf("failed to decode notification: %v", err) return } events, err := filterEvents(¬ification) if err != nil { log.Errorf("failed to filter events: %v", err) return } for _, event := range events { repository := event.Target.Repository project, _ := utils.ParseRepository(repository) tag := event.Target.Tag action := event.Action user := event.Actor.Name if len(user) == 0 { user = "******" } go func() { if err := dao.AccessLog(user, project, repository, tag, action); err != nil { log.Errorf("failed to add access log: %v", err) } }() if action == "push" { go func() { exist := dao.RepositoryExists(repository) if exist { return } log.Debugf("Add repository %s into DB.", repository) repoRecord := models.RepoRecord{Name: repository, OwnerName: user, ProjectName: project} if err := dao.AddRepository(repoRecord); err != nil { log.Errorf("Error happens when adding repository: %v", err) } if err := cache.RefreshCatalogCache(); err != nil { log.Errorf("failed to refresh cache: %v", err) } }() go api.TriggerReplicationByRepository(repository, []string{tag}, models.RepOpTransfer) } if action == "pull" { go func() { log.Debugf("Increase the repository %s pull count.", repository) if err := dao.IncreasePullCount(repository); err != nil { log.Errorf("Error happens when increasing pull count: %v", repository) } }() } } }
func CommonAddRepository() { commonRepository := &models.RepoRecord{ RepositoryID: "1", Name: TestRepoName, OwnerName: AdminName, OwnerID: 1, ProjectName: DefaultProjectName, ProjectID: 1, PullCount: 1, } _ = dao.AddRepository(*commonRepository) }
// SyncRegistry syncs the repositories of registry with database. func SyncRegistry() error { log.Debugf("Start syncing repositories from registry to DB... ") reposInRegistry, err := catalog() if err != nil { log.Error(err) return err } var repoRecordsInDB []models.RepoRecord repoRecordsInDB, err = dao.GetAllRepositories() if err != nil { log.Errorf("error occurred while getting all registories. %v", err) return err } var reposInDB []string for _, repoRecordInDB := range repoRecordsInDB { reposInDB = append(reposInDB, repoRecordInDB.Name) } var reposToAdd []string var reposToDel []string reposToAdd, reposToDel, err = diffRepos(reposInRegistry, reposInDB) if err != nil { return err } if len(reposToAdd) > 0 { log.Debugf("Start adding repositories into DB... ") for _, repoToAdd := range reposToAdd { project, _ := utils.ParseRepository(repoToAdd) user, err := dao.GetAccessLogCreator(repoToAdd) if err != nil { log.Errorf("Error happens when getting the repository owner from access log: %v", err) } if len(user) == 0 { user = "******" } pullCount, err := dao.CountPull(repoToAdd) if err != nil { log.Errorf("Error happens when counting pull count from access log: %v", err) } repoRecord := models.RepoRecord{Name: repoToAdd, OwnerName: user, ProjectName: project, PullCount: pullCount} if err := dao.AddRepository(repoRecord); err != nil { log.Errorf("Error happens when adding the missing repository: %v", err) } else { log.Debugf("Add repository: %s success.", repoToAdd) } } } if len(reposToDel) > 0 { log.Debugf("Start deleting repositories from DB... ") for _, repoToDel := range reposToDel { if err := dao.DeleteRepository(repoToDel); err != nil { log.Errorf("Error happens when deleting the repository: %v", err) } else { log.Debugf("Delete repository: %s success.", repoToDel) } } } log.Debugf("Sync repositories from registry to DB is done.") return nil }