@@ -8,6 +8,14 @@ import ( | |||||
"xorm.io/builder" | "xorm.io/builder" | ||||
) | ) | ||||
const ( | |||||
TechHide = 1 | |||||
TechShow = 2 | |||||
TechMigrateFailed = 3 | |||||
) | |||||
const DefaultTechStatus = 2 | |||||
type TechConvergeBaseInfo struct { | type TechConvergeBaseInfo struct { | ||||
ID int64 `xorm:"pk autoincr"` | ID int64 `xorm:"pk autoincr"` | ||||
ProjectNumber string `xorm:"UNIQUE NOT NULL"` //项目立项编号 | ProjectNumber string `xorm:"UNIQUE NOT NULL"` //项目立项编号 | ||||
@@ -66,6 +74,16 @@ type RepoConvergeInfo struct { | |||||
BaseInfo *TechConvergeBaseInfo `xorm:"-"` | BaseInfo *TechConvergeBaseInfo `xorm:"-"` | ||||
} | } | ||||
func (r *RepoConvergeInfo) InsertOrUpdate() error { | |||||
if r.ID != 0 { | |||||
_, err := x.ID(r.ID).Update(r) | |||||
return err | |||||
} else { | |||||
_, err := x.InsertOne(r) | |||||
return err | |||||
} | |||||
} | |||||
func GetTechConvergeBaseInfoByProjectNumber(projectNumber string) (*TechConvergeBaseInfo, error) { | func GetTechConvergeBaseInfoByProjectNumber(projectNumber string) (*TechConvergeBaseInfo, error) { | ||||
tb := &TechConvergeBaseInfo{ProjectNumber: projectNumber} | tb := &TechConvergeBaseInfo{ProjectNumber: projectNumber} | ||||
return getTechConvergeBaseInfo(tb) | return getTechConvergeBaseInfo(tb) | ||||
@@ -139,3 +157,15 @@ func FindTech(opt FindTechOpt) ([]*TechConvergeBaseInfo, error) { | |||||
} | } | ||||
return r, nil | return r, nil | ||||
} | } | ||||
func GetTechByTechNo(techNo string) (*TechConvergeBaseInfo, error) { | |||||
var tech *TechConvergeBaseInfo | |||||
has, err := x.Where("project_number = ?", techNo).Get(&tech) | |||||
if err != nil { | |||||
return nil, err | |||||
} else if !has { | |||||
return nil, ErrTechConvergeBaseInfoNotExist{} | |||||
} | |||||
return tech, nil | |||||
} |
@@ -0,0 +1,11 @@ | |||||
package structs | |||||
type TechRepo struct { | |||||
Url string `json:"url"` | |||||
TechNo string `json:"no"` | |||||
Institution string `json:"institution"` | |||||
uid string `json:"uid"` //启智项目uid | |||||
repo_name string `json:"repo_name"` //启智项目名称 | |||||
topics string `json:"topics"` //关键词 | |||||
description string `json:"description"` //简介 | |||||
} |
@@ -540,7 +540,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
m.Group("/tech", func() { | m.Group("/tech", func() { | ||||
m.Get("", tech.FindTech) | m.Get("", tech.FindTech) | ||||
m.Post("/basic", tech.ImportBasicInfo) | m.Post("/basic", tech.ImportBasicInfo) | ||||
m.Post("/openi", tech.CommitOpenIRepo) | |||||
m.Post("/openi", bind(api.TechRepo{}), tech.CommitOpenIRepo) | |||||
}, reqToken()) | }, reqToken()) | ||||
@@ -1,9 +1,42 @@ | |||||
package tech | package tech | ||||
import ( | import ( | ||||
"code.gitea.io/gitea/models" | |||||
"code.gitea.io/gitea/modules/context" | "code.gitea.io/gitea/modules/context" | ||||
api "code.gitea.io/gitea/modules/structs" | |||||
"code.gitea.io/gitea/routers/response" | |||||
"code.gitea.io/gitea/services/repository" | |||||
"net/http" | |||||
) | ) | ||||
func CommitOpenIRepo(ctx *context.APIContext) { | |||||
func CommitOpenIRepo(ctx *context.APIContext, form api.TechRepo) { | |||||
//解析项目路径,查找项目 | |||||
repo, err := repository.FindRepoByUrl(form.Url) | |||||
if err != nil { | |||||
ctx.JSON(http.StatusOK, response.OuterServerError(err.Error())) | |||||
return | |||||
} | |||||
//查找项目编号 | |||||
tech, err := models.GetTechByTechNo(form.TechNo) | |||||
if err != nil { | |||||
ctx.JSON(http.StatusOK, response.OuterServerError(err.Error())) | |||||
return | |||||
} | |||||
//写入数据库 | |||||
rci := &models.RepoConvergeInfo{ | |||||
RepoID: repo.ID, | |||||
Url: form.Url, | |||||
BaseInfoID: tech.ID, | |||||
Institution: form.Institution, | |||||
UID: ctx.User.ID, | |||||
Status: models.DefaultTechStatus, | |||||
} | |||||
err = rci.InsertOrUpdate() | |||||
if err != nil { | |||||
ctx.JSON(http.StatusOK, response.OuterServerError(err.Error())) | |||||
return | |||||
} | |||||
ctx.JSON(http.StatusOK, response.OuterSuccess()) | |||||
return | |||||
} | } |
@@ -0,0 +1,40 @@ | |||||
package repository | |||||
import ( | |||||
"code.gitea.io/gitea/models" | |||||
"code.gitea.io/gitea/modules/setting" | |||||
"net/url" | |||||
"strings" | |||||
) | |||||
func FindRepoByUrl(url string) (*models.Repository, error) { | |||||
ownerName, repoName := parseOpenIUrl(url) | |||||
if ownerName == "" || repoName == "" { | |||||
return nil, nil | |||||
} | |||||
r, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) | |||||
if err != nil { | |||||
return nil, err | |||||
} | |||||
return r, nil | |||||
} | |||||
//parseOpenIUrl parse openI repo url,return ownerName and repoName | |||||
func parseOpenIUrl(u string) (string, string) { | |||||
url, err := url.Parse(u) | |||||
if err != nil { | |||||
return "", "" | |||||
} | |||||
if url.Host != setting.AppURL { | |||||
return "", "" | |||||
} | |||||
array := strings.Split(url.Path, "/") | |||||
if len(array) < 3 { | |||||
return "", "" | |||||
} | |||||
ownerName := array[1] | |||||
repoName := array[2] | |||||
return ownerName, repoName | |||||
} |