// List ... func (p *ProjectAPI) List() { var projectList []models.Project projectName := p.GetString("project_name") if len(projectName) > 0 { projectName = "%" + projectName + "%" } var public int var err error isPublic := p.GetString("is_public") if len(isPublic) > 0 { public, err = strconv.Atoi(isPublic) if err != nil { log.Errorf("Error parsing public property: %v, error: %v", isPublic, err) p.CustomAbort(http.StatusBadRequest, "invalid project Id") } } isAdmin := false if public == 1 { projectList, err = dao.GetPublicProjects(projectName) } else { //if the request is not for public projects, user must login or provide credential p.userID = p.ValidateUser() isAdmin, err = dao.IsAdminRole(p.userID) if err != nil { log.Errorf("Error occured in check admin, error: %v", err) p.CustomAbort(http.StatusInternalServerError, "Internal error.") } if isAdmin { projectList, err = dao.GetAllProjects(projectName) } else { projectList, err = dao.GetUserRelevantProjects(p.userID, projectName) } } if err != nil { log.Errorf("Error occured in get projects info, error: %v", err) p.CustomAbort(http.StatusInternalServerError, "Internal error.") } for i := 0; i < len(projectList); i++ { if public != 1 { if isAdmin { projectList[i].Role = models.PROJECTADMIN } if projectList[i].Role == models.PROJECTADMIN { projectList[i].Togglable = true } } projectList[i].RepoCount = getRepoCountByProject(projectList[i].Name) } p.Data["json"] = projectList p.ServeJSON() }
// Get total projects and repos of the user func (s *StatisticAPI) Get() { isAdmin, err := dao.IsAdminRole(s.userID) if err != nil { log.Errorf("Error occured in check admin, error: %v", err) s.CustomAbort(http.StatusInternalServerError, "Internal error.") } var projectList []models.Project if isAdmin { projectList, err = dao.GetAllProjects("") } else { projectList, err = dao.GetUserRelevantProjects(s.userID, "") } if err != nil { log.Errorf("Error occured in QueryProject, error: %v", err) s.CustomAbort(http.StatusInternalServerError, "Internal error.") } proMap := map[string]int{} proMap["my_project_count"] = 0 proMap["my_repo_count"] = 0 proMap["public_project_count"] = 0 proMap["public_repo_count"] = 0 var publicProjects []models.Project publicProjects, err = dao.GetPublicProjects("") if err != nil { log.Errorf("Error occured in QueryPublicProject, error: %v", err) s.CustomAbort(http.StatusInternalServerError, "Internal error.") } proMap["public_project_count"] = len(publicProjects) for i := 0; i < len(publicProjects); i++ { proMap["public_repo_count"] += getRepoCountByProject(publicProjects[i].Name) } if isAdmin { proMap["total_project_count"] = len(projectList) proMap["total_repo_count"] = getTotalRepoCount() } for i := 0; i < len(projectList); i++ { if isAdmin { projectList[i].Role = models.PROJECTADMIN } if projectList[i].Role == models.PROJECTADMIN || projectList[i].Role == models.DEVELOPER || projectList[i].Role == models.GUEST { proMap["my_project_count"]++ proMap["my_repo_count"] += getRepoCountByProject(projectList[i].Name) } } s.Data["json"] = proMap s.ServeJSON() }