本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

383 lines
10KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "sort"
  9. "strings"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/structs"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. "xorm.io/builder"
  16. )
  17. // Release represents a release of repository.
  18. type Release struct {
  19. ID int64 `xorm:"pk autoincr"`
  20. RepoID int64 `xorm:"INDEX UNIQUE(n)"`
  21. Repo *Repository `xorm:"-"`
  22. PublisherID int64 `xorm:"INDEX"`
  23. Publisher *User `xorm:"-"`
  24. TagName string `xorm:"INDEX UNIQUE(n)"`
  25. OriginalAuthor string
  26. OriginalAuthorID int64 `xorm:"index"`
  27. LowerTagName string
  28. Target string
  29. Title string
  30. Sha1 string `xorm:"VARCHAR(40)"`
  31. NumCommits int64
  32. NumCommitsBehind int64 `xorm:"-"`
  33. Note string `xorm:"TEXT"`
  34. IsDraft bool `xorm:"NOT NULL DEFAULT false"`
  35. IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
  36. IsTag bool `xorm:"NOT NULL DEFAULT false"`
  37. Attachments []*Attachment `xorm:"-"`
  38. CreatedUnix timeutil.TimeStamp `xorm:"INDEX"`
  39. }
  40. func (r *Release) loadAttributes(e Engine) error {
  41. var err error
  42. if r.Repo == nil {
  43. r.Repo, err = GetRepositoryByID(r.RepoID)
  44. if err != nil {
  45. return err
  46. }
  47. }
  48. if r.Publisher == nil {
  49. r.Publisher, err = getUserByID(e, r.PublisherID)
  50. if err != nil {
  51. return err
  52. }
  53. }
  54. return getReleaseAttachments(e, r)
  55. }
  56. // LoadAttributes load repo and publisher attributes for a release
  57. func (r *Release) LoadAttributes() error {
  58. return r.loadAttributes(x)
  59. }
  60. // APIURL the api url for a release. release must have attributes loaded
  61. func (r *Release) APIURL() string {
  62. return fmt.Sprintf("%sapi/v1/repos/%s/releases/%d",
  63. setting.AppURL, r.Repo.FullName(), r.ID)
  64. }
  65. // ZipURL the zip url for a release. release must have attributes loaded
  66. func (r *Release) ZipURL() string {
  67. return fmt.Sprintf("%s/archive/%s.zip", r.Repo.HTMLURL(), r.TagName)
  68. }
  69. // TarURL the tar.gz url for a release. release must have attributes loaded
  70. func (r *Release) TarURL() string {
  71. return fmt.Sprintf("%s/archive/%s.tar.gz", r.Repo.HTMLURL(), r.TagName)
  72. }
  73. // APIFormat convert a Release to api.Release
  74. func (r *Release) APIFormat() *api.Release {
  75. assets := make([]*api.Attachment, 0)
  76. for _, att := range r.Attachments {
  77. assets = append(assets, att.APIFormat())
  78. }
  79. return &api.Release{
  80. ID: r.ID,
  81. TagName: r.TagName,
  82. Target: r.Target,
  83. Title: r.Title,
  84. Note: r.Note,
  85. URL: r.APIURL(),
  86. TarURL: r.TarURL(),
  87. ZipURL: r.ZipURL(),
  88. IsDraft: r.IsDraft,
  89. IsPrerelease: r.IsPrerelease,
  90. CreatedAt: r.CreatedUnix.AsTime(),
  91. PublishedAt: r.CreatedUnix.AsTime(),
  92. Publisher: r.Publisher.APIFormat(),
  93. Attachments: assets,
  94. }
  95. }
  96. // IsReleaseExist returns true if release with given tag name already exists.
  97. func IsReleaseExist(repoID int64, tagName string) (bool, error) {
  98. if len(tagName) == 0 {
  99. return false, nil
  100. }
  101. return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
  102. }
  103. // InsertRelease inserts a release
  104. func InsertRelease(rel *Release) error {
  105. _, err := x.Insert(rel)
  106. return err
  107. }
  108. // UpdateRelease updates all columns of a release
  109. func UpdateRelease(rel *Release) error {
  110. _, err := x.ID(rel.ID).AllCols().Update(rel)
  111. return err
  112. }
  113. // AddReleaseAttachments adds a release attachments
  114. func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
  115. // Check attachments
  116. var attachments = make([]*Attachment, 0)
  117. for _, uuid := range attachmentUUIDs {
  118. attach, err := getAttachmentByUUID(x, uuid)
  119. if err != nil {
  120. if IsErrAttachmentNotExist(err) {
  121. continue
  122. }
  123. return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  124. }
  125. attachments = append(attachments, attach)
  126. }
  127. for i := range attachments {
  128. attachments[i].ReleaseID = releaseID
  129. // No assign value could be 0, so ignore AllCols().
  130. if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {
  131. return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  132. }
  133. }
  134. return
  135. }
  136. // GetRelease returns release by given ID.
  137. func GetRelease(repoID int64, tagName string) (*Release, error) {
  138. isExist, err := IsReleaseExist(repoID, tagName)
  139. if err != nil {
  140. return nil, err
  141. } else if !isExist {
  142. return nil, ErrReleaseNotExist{0, tagName}
  143. }
  144. rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
  145. _, err = x.Get(rel)
  146. return rel, err
  147. }
  148. // GetReleaseByID returns release with given ID.
  149. func GetReleaseByID(id int64) (*Release, error) {
  150. rel := new(Release)
  151. has, err := x.
  152. ID(id).
  153. Get(rel)
  154. if err != nil {
  155. return nil, err
  156. } else if !has {
  157. return nil, ErrReleaseNotExist{id, ""}
  158. }
  159. return rel, nil
  160. }
  161. // FindReleasesOptions describes the conditions to Find releases
  162. type FindReleasesOptions struct {
  163. IncludeDrafts bool
  164. IncludeTags bool
  165. TagNames []string
  166. }
  167. func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
  168. var cond = builder.NewCond()
  169. cond = cond.And(builder.Eq{"repo_id": repoID})
  170. if !opts.IncludeDrafts {
  171. cond = cond.And(builder.Eq{"is_draft": false})
  172. }
  173. if !opts.IncludeTags {
  174. cond = cond.And(builder.Eq{"is_tag": false})
  175. }
  176. if len(opts.TagNames) > 0 {
  177. cond = cond.And(builder.In("tag_name", opts.TagNames))
  178. }
  179. return cond
  180. }
  181. // GetReleasesByRepoID returns a list of releases of repository.
  182. func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions, page, pageSize int) (rels []*Release, err error) {
  183. if page <= 0 {
  184. page = 1
  185. }
  186. err = x.
  187. Desc("created_unix", "id").
  188. Limit(pageSize, (page-1)*pageSize).
  189. Where(opts.toConds(repoID)).
  190. Find(&rels)
  191. return rels, err
  192. }
  193. // GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
  194. func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) {
  195. err = x.
  196. Desc("created_unix").
  197. In("tag_name", tagNames).
  198. Find(&rels, Release{RepoID: repoID})
  199. return rels, err
  200. }
  201. // GetReleaseCountByRepoID returns the count of releases of repository
  202. func GetReleaseCountByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
  203. return x.Where(opts.toConds(repoID)).Count(&Release{})
  204. }
  205. type releaseMetaSearch struct {
  206. ID []int64
  207. Rel []*Release
  208. }
  209. func (s releaseMetaSearch) Len() int {
  210. return len(s.ID)
  211. }
  212. func (s releaseMetaSearch) Swap(i, j int) {
  213. s.ID[i], s.ID[j] = s.ID[j], s.ID[i]
  214. s.Rel[i], s.Rel[j] = s.Rel[j], s.Rel[i]
  215. }
  216. func (s releaseMetaSearch) Less(i, j int) bool {
  217. return s.ID[i] < s.ID[j]
  218. }
  219. // GetReleaseAttachments retrieves the attachments for releases
  220. func GetReleaseAttachments(rels ...*Release) (err error) {
  221. return getReleaseAttachments(x, rels...)
  222. }
  223. func getReleaseAttachments(e Engine, rels ...*Release) (err error) {
  224. if len(rels) == 0 {
  225. return
  226. }
  227. // To keep this efficient as possible sort all releases by id,
  228. // select attachments by release id,
  229. // then merge join them
  230. // Sort
  231. var sortedRels = releaseMetaSearch{ID: make([]int64, len(rels)), Rel: make([]*Release, len(rels))}
  232. var attachments []*Attachment
  233. for index, element := range rels {
  234. element.Attachments = []*Attachment{}
  235. sortedRels.ID[index] = element.ID
  236. sortedRels.Rel[index] = element
  237. }
  238. sort.Sort(sortedRels)
  239. // Select attachments
  240. err = e.
  241. Asc("release_id").
  242. In("release_id", sortedRels.ID).
  243. Find(&attachments, Attachment{})
  244. if err != nil {
  245. return err
  246. }
  247. // merge join
  248. var currentIndex = 0
  249. for _, attachment := range attachments {
  250. for sortedRels.ID[currentIndex] < attachment.ReleaseID {
  251. currentIndex++
  252. }
  253. sortedRels.Rel[currentIndex].Attachments = append(sortedRels.Rel[currentIndex].Attachments, attachment)
  254. }
  255. return
  256. }
  257. type releaseSorter struct {
  258. rels []*Release
  259. }
  260. func (rs *releaseSorter) Len() int {
  261. return len(rs.rels)
  262. }
  263. func (rs *releaseSorter) Less(i, j int) bool {
  264. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  265. if diffNum != 0 {
  266. return diffNum > 0
  267. }
  268. return rs.rels[i].CreatedUnix > rs.rels[j].CreatedUnix
  269. }
  270. func (rs *releaseSorter) Swap(i, j int) {
  271. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  272. }
  273. // SortReleases sorts releases by number of commits and created time.
  274. func SortReleases(rels []*Release) {
  275. sorter := &releaseSorter{rels: rels}
  276. sort.Sort(sorter)
  277. }
  278. // DeleteReleaseByID deletes a release from database by given ID.
  279. func DeleteReleaseByID(id int64) error {
  280. _, err := x.ID(id).Delete(new(Release))
  281. return err
  282. }
  283. // SyncReleasesWithTags synchronizes release table with repository tags
  284. func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
  285. existingRelTags := make(map[string]struct{})
  286. opts := FindReleasesOptions{IncludeDrafts: true, IncludeTags: true}
  287. for page := 1; ; page++ {
  288. rels, err := GetReleasesByRepoID(repo.ID, opts, page, 100)
  289. if err != nil {
  290. return fmt.Errorf("GetReleasesByRepoID: %v", err)
  291. }
  292. if len(rels) == 0 {
  293. break
  294. }
  295. for _, rel := range rels {
  296. if rel.IsDraft {
  297. continue
  298. }
  299. commitID, err := gitRepo.GetTagCommitID(rel.TagName)
  300. if err != nil && !git.IsErrNotExist(err) {
  301. return fmt.Errorf("GetTagCommitID: %v", err)
  302. }
  303. if git.IsErrNotExist(err) || commitID != rel.Sha1 {
  304. if err := PushUpdateDeleteTag(repo, rel.TagName); err != nil {
  305. return fmt.Errorf("PushUpdateDeleteTag: %v", err)
  306. }
  307. } else {
  308. existingRelTags[strings.ToLower(rel.TagName)] = struct{}{}
  309. }
  310. }
  311. }
  312. tags, err := gitRepo.GetTags()
  313. if err != nil {
  314. return fmt.Errorf("GetTags: %v", err)
  315. }
  316. for _, tagName := range tags {
  317. if _, ok := existingRelTags[strings.ToLower(tagName)]; !ok {
  318. if err := PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
  319. return fmt.Errorf("pushUpdateAddTag: %v", err)
  320. }
  321. }
  322. }
  323. return nil
  324. }
  325. // UpdateReleasesMigrationsByType updates all migrated repositories' releases from gitServiceType to replace originalAuthorID to posterID
  326. func UpdateReleasesMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID string, posterID int64) error {
  327. _, err := x.Table("release").
  328. Where("repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
  329. And("original_author_id = ?", originalAuthorID).
  330. Update(map[string]interface{}{
  331. "publisher_id": posterID,
  332. "original_author": "",
  333. "original_author_id": 0,
  334. })
  335. return err
  336. }
上海开阖软件有限公司 沪ICP备12045867号-1