mirror of
https://github.com/go-gitea/gitea.git
synced 2025-08-22 16:27:23 +00:00
fix: return error when converting project to api project
This commit is contained in:
parent
852c0df382
commit
dbfecced25
@ -17,7 +17,7 @@ import (
|
||||
|
||||
func innerCreateProject(
|
||||
ctx *context.APIContext,
|
||||
project_type project_model.Type,
|
||||
projectType project_model.Type,
|
||||
) {
|
||||
form := web.GetForm(ctx).(*api.NewProjectPayload)
|
||||
project := &project_model.Project{
|
||||
@ -27,14 +27,14 @@ func innerCreateProject(
|
||||
Description: form.Description,
|
||||
CreatorID: ctx.Doer.ID,
|
||||
BoardType: project_model.BoardType(form.BoardType),
|
||||
Type: project_type,
|
||||
Type: projectType,
|
||||
}
|
||||
|
||||
if ctx.ContextUser != nil {
|
||||
project.OwnerID = ctx.ContextUser.ID
|
||||
}
|
||||
|
||||
if project_type == project_model.TypeRepository {
|
||||
if projectType == project_model.TypeRepository {
|
||||
project.RepoID = ctx.Repo.Repository.ID
|
||||
}
|
||||
|
||||
@ -49,7 +49,13 @@ func innerCreateProject(
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusCreated, convert.ToAPIProject(ctx, project))
|
||||
projectResponse, err := convert.ToAPIProject(ctx, project)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "NewProject", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusCreated, projectResponse)
|
||||
}
|
||||
|
||||
func CreateUserProject(ctx *context.APIContext) {
|
||||
@ -165,7 +171,12 @@ func GetProject(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, convert.ToAPIProject(ctx, project))
|
||||
projectResponse, err := convert.ToAPIProject(ctx, project)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "GetProjectByID", err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, projectResponse)
|
||||
}
|
||||
|
||||
func UpdateProject(ctx *context.APIContext) {
|
||||
@ -215,7 +226,12 @@ func UpdateProject(ctx *context.APIContext) {
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateProject", err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, convert.ToAPIProject(ctx, project))
|
||||
projectResponse, err := convert.ToAPIProject(ctx, project)
|
||||
if err != nil {
|
||||
ctx.Error(http.StatusInternalServerError, "UpdateProject", err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, projectResponse)
|
||||
}
|
||||
|
||||
func DeleteProject(ctx *context.APIContext) {
|
||||
@ -242,7 +258,6 @@ func DeleteProject(ctx *context.APIContext) {
|
||||
}
|
||||
|
||||
ctx.Status(http.StatusNoContent)
|
||||
|
||||
}
|
||||
|
||||
func ListUserProjects(ctx *context.APIContext) {
|
||||
|
@ -10,8 +10,7 @@ import (
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
)
|
||||
|
||||
func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Project {
|
||||
|
||||
func ToAPIProject(ctx context.Context, project *project_model.Project) (*api.Project, error) {
|
||||
apiProject := &api.Project{
|
||||
Title: project.Title,
|
||||
Description: project.Description,
|
||||
@ -23,7 +22,10 @@ func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Proj
|
||||
}
|
||||
|
||||
// try to laod the repo
|
||||
project.LoadRepo(ctx)
|
||||
err := project.LoadRepo(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if project.Repo != nil {
|
||||
apiProject.Repo = &api.RepositoryMeta{
|
||||
ID: project.RepoID,
|
||||
@ -33,7 +35,10 @@ func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Proj
|
||||
}
|
||||
}
|
||||
|
||||
project.LoadCreator(ctx)
|
||||
err = project.LoadCreator(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if project.Creator != nil {
|
||||
apiProject.Creator = &api.User{
|
||||
ID: project.Creator.ID,
|
||||
@ -42,7 +47,10 @@ func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Proj
|
||||
}
|
||||
}
|
||||
|
||||
project.LoadOwner(ctx)
|
||||
err = project.LoadOwner(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if project.Owner != nil {
|
||||
apiProject.Owner = &api.User{
|
||||
ID: project.Owner.ID,
|
||||
@ -51,13 +59,20 @@ func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Proj
|
||||
}
|
||||
}
|
||||
|
||||
return apiProject
|
||||
return apiProject, nil
|
||||
}
|
||||
|
||||
func ToAPIProjectList(ctx context.Context, projects []*project_model.Project) ([]*api.Project, error) {
|
||||
result := make([]*api.Project, len(projects))
|
||||
var err error
|
||||
for i := range projects {
|
||||
result[i] = ToAPIProject(ctx, projects[i])
|
||||
result[i], err = ToAPIProject(ctx, projects[i])
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user