mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-16 20:01:35 +01:00
Merge pull request #2579 from ywk253100/170620_pm
Using project manager instead of DAO project methods
This commit is contained in:
commit
38b09f9f3f
@ -684,6 +684,7 @@ func TestCountPull(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func TestProjectExists(t *testing.T) {
|
||||
var exists bool
|
||||
var err error
|
||||
@ -702,7 +703,7 @@ func TestProjectExists(t *testing.T) {
|
||||
t.Errorf("The project with name: %s, does not exist", currentProject.Name)
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
func TestGetProjectById(t *testing.T) {
|
||||
id := currentProject.ProjectID
|
||||
p, err := GetProjectByID(id)
|
||||
@ -768,13 +769,14 @@ func TestToggleProjectPublicity(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
func TestIsProjectPublic(t *testing.T) {
|
||||
|
||||
if isPublic := IsProjectPublic(projectName); isPublic {
|
||||
t.Errorf("project, id: %d, its publicity is not false after turning off", currentProject.ProjectID)
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
func TestGetUserProjectRoles(t *testing.T) {
|
||||
r, err := GetUserProjectRoles(currentUser.UserID, currentProject.ProjectID)
|
||||
if err != nil {
|
||||
@ -791,6 +793,7 @@ func TestGetUserProjectRoles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func TestProjectPermission(t *testing.T) {
|
||||
roleCode, err := GetPermission(currentUser.Username, currentProject.Name)
|
||||
if err != nil {
|
||||
@ -800,7 +803,7 @@ func TestProjectPermission(t *testing.T) {
|
||||
t.Errorf("The expected role code is MDRWS,but actual: %s", roleCode)
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
func TestGetTotalOfProjects(t *testing.T) {
|
||||
total, err := GetTotalOfProjects(nil)
|
||||
if err != nil {
|
||||
|
@ -21,7 +21,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/vmware/harbor/src/common/utils/log"
|
||||
//"github.com/vmware/harbor/src/common/utils/log"
|
||||
)
|
||||
|
||||
//TODO:transaction, return err
|
||||
@ -50,6 +50,7 @@ func AddProject(project models.Project) (int64, error) {
|
||||
return projectID, err
|
||||
}
|
||||
|
||||
/*
|
||||
// IsProjectPublic ...
|
||||
func IsProjectPublic(projectName string) bool {
|
||||
project, err := GetProjectByName(projectName)
|
||||
@ -85,6 +86,7 @@ func ProjectExists(nameOrID interface{}) (bool, error) {
|
||||
return num > 0, nil
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
// GetProjectByID ...
|
||||
func GetProjectByID(id int64) (*models.Project, error) {
|
||||
@ -125,6 +127,7 @@ func GetProjectByName(name string) (*models.Project, error) {
|
||||
return &p[0], nil
|
||||
}
|
||||
|
||||
/*
|
||||
// GetPermission gets roles that the user has according to the project.
|
||||
func GetPermission(username, projectName string) (string, error) {
|
||||
o := GetOrmer()
|
||||
@ -147,6 +150,7 @@ func GetPermission(username, projectName string) (string, error) {
|
||||
|
||||
return r[0].RoleCode, nil
|
||||
}
|
||||
*/
|
||||
|
||||
// ToggleProjectPublicity toggles the publicity of the project.
|
||||
func ToggleProjectPublicity(projectID int64, publicity int) error {
|
||||
|
@ -167,14 +167,14 @@ func (ra *RepositoryAPI) Delete() {
|
||||
repoName := ra.GetString(":splat")
|
||||
|
||||
projectName, _ := utils.ParseRepository(repoName)
|
||||
exist, err := ra.ProjectMgr.Exist(projectName)
|
||||
project, err := ra.ProjectMgr.Get(projectName)
|
||||
if err != nil {
|
||||
ra.HandleInternalServerError(fmt.Sprintf("failed to check the existence of project %s: %v",
|
||||
ra.HandleInternalServerError(fmt.Sprintf("failed to get the project %s: %v",
|
||||
projectName, err))
|
||||
return
|
||||
}
|
||||
|
||||
if !exist {
|
||||
if project == nil {
|
||||
ra.HandleNotFound(fmt.Sprintf("project %s not found", projectName))
|
||||
return
|
||||
}
|
||||
@ -252,21 +252,10 @@ func (ra *RepositoryAPI) Delete() {
|
||||
ra.CustomAbort(http.StatusInternalServerError, "internal error")
|
||||
}
|
||||
log.Infof("delete tag: %s:%s", repoName, t)
|
||||
go TriggerReplicationByRepository(repoName, []string{t}, models.RepOpDelete)
|
||||
|
||||
go TriggerReplicationByRepository(project.ProjectID, repoName, []string{t}, models.RepOpDelete)
|
||||
|
||||
go func(tag string) {
|
||||
project, err := ra.ProjectMgr.Get(projectName)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get the project %s: %v",
|
||||
projectName, err)
|
||||
return
|
||||
}
|
||||
|
||||
if project == nil {
|
||||
log.Errorf("project %s not found", projectName)
|
||||
return
|
||||
}
|
||||
|
||||
if err := dao.AddAccessLog(models.AccessLog{
|
||||
Username: ra.SecurityCtx.GetUsername(),
|
||||
ProjectID: project.ProjectID,
|
||||
@ -280,7 +269,7 @@ func (ra *RepositoryAPI) Delete() {
|
||||
}(t)
|
||||
}
|
||||
|
||||
exist, err = repositoryExist(repoName, rc)
|
||||
exist, err := repositoryExist(repoName, rc)
|
||||
if err != nil {
|
||||
log.Errorf("failed to check the existence of repository %s: %v", repoName, err)
|
||||
ra.CustomAbort(http.StatusInternalServerError, "")
|
||||
|
@ -99,28 +99,9 @@ func TriggerReplication(policyID int64, repository string,
|
||||
return requestAsUI("POST", url, bytes.NewBuffer(b), http.StatusOK)
|
||||
}
|
||||
|
||||
// GetPoliciesByRepository returns policies according the repository
|
||||
func GetPoliciesByRepository(repository string) ([]*models.RepPolicy, error) {
|
||||
repository = strings.TrimSpace(repository)
|
||||
repository = strings.TrimRight(repository, "/")
|
||||
projectName, _ := utils.ParseRepository(repository)
|
||||
|
||||
project, err := dao.GetProjectByName(projectName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
policies, err := dao.GetRepPolicyByProject(project.ProjectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return policies, nil
|
||||
}
|
||||
|
||||
// TriggerReplicationByRepository triggers the replication according to the repository
|
||||
func TriggerReplicationByRepository(repository string, tags []string, operation string) {
|
||||
policies, err := GetPoliciesByRepository(repository)
|
||||
func TriggerReplicationByRepository(projectID int64, repository string, tags []string, operation string) {
|
||||
policies, err := dao.GetRepPolicyByProject(projectID)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get policies for repository %s: %v", repository, err)
|
||||
return
|
||||
|
@ -28,13 +28,11 @@ import (
|
||||
"github.com/vmware/harbor/src/ui/api"
|
||||
"github.com/vmware/harbor/src/ui/config"
|
||||
"github.com/vmware/harbor/src/ui/projectmanager/pms"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// NotificationHandler handles request on /service/notifications/, which listens to registry's events.
|
||||
type NotificationHandler struct {
|
||||
beego.Controller
|
||||
api.BaseController
|
||||
}
|
||||
|
||||
const manifestPattern = `^application/vnd.docker.distribution.manifest.v\d\+(json|prettyjws)`
|
||||
@ -58,7 +56,6 @@ func (n *NotificationHandler) Post() {
|
||||
|
||||
for _, event := range events {
|
||||
repository := event.Target.Repository
|
||||
|
||||
project, _ := utils.ParseRepository(repository)
|
||||
tag := event.Target.Tag
|
||||
action := event.Action
|
||||
@ -68,12 +65,13 @@ func (n *NotificationHandler) Post() {
|
||||
user = "anonymous"
|
||||
}
|
||||
|
||||
pro, err := n.ProjectMgr.Get(project)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get project by name %s: %v", project, err)
|
||||
return
|
||||
}
|
||||
|
||||
go func() {
|
||||
pro, err := dao.GetProjectByName(project)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get project by name %s: %v", project, err)
|
||||
return
|
||||
}
|
||||
if err := dao.AddAccessLog(models.AccessLog{
|
||||
Username: user,
|
||||
ProjectID: pro.ProjectID,
|
||||
@ -85,6 +83,7 @@ func (n *NotificationHandler) Post() {
|
||||
log.Errorf("failed to add access log: %v", err)
|
||||
}
|
||||
}()
|
||||
|
||||
if action == "push" {
|
||||
go func() {
|
||||
exist := dao.RepositoryExists(repository)
|
||||
@ -92,11 +91,6 @@ func (n *NotificationHandler) Post() {
|
||||
return
|
||||
}
|
||||
log.Debugf("Add repository %s into DB.", repository)
|
||||
pro, err := dao.GetProjectByName(project)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get project %s: %v", project, err)
|
||||
return
|
||||
}
|
||||
repoRecord := models.RepoRecord{
|
||||
Name: repository,
|
||||
ProjectID: pro.ProjectID,
|
||||
@ -105,7 +99,8 @@ func (n *NotificationHandler) Post() {
|
||||
log.Errorf("Error happens when adding repository: %v", err)
|
||||
}
|
||||
}()
|
||||
go api.TriggerReplicationByRepository(repository, []string{tag}, models.RepOpTransfer)
|
||||
|
||||
go api.TriggerReplicationByRepository(pro.ProjectID, repository, []string{tag}, models.RepOpTransfer)
|
||||
if autoScanEnabled(project) {
|
||||
if err := api.TriggerImageScan(repository, tag); err != nil {
|
||||
log.Warningf("Failed to scan image, repository: %s, tag: %s, error: %v", repository, tag, err)
|
||||
|
Loading…
Reference in New Issue
Block a user