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.

331 lines
9.0 KiB

10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
6 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
9 years ago
  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/setting"
  11. "code.gitea.io/gitea/modules/structs"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/timeutil"
  14. "xorm.io/builder"
  15. )
  16. // Release represents a release of repository.
  17. type Release struct {
  18. ID int64 `xorm:"pk autoincr"`
  19. RepoID int64 `xorm:"INDEX UNIQUE(n)"`
  20. Repo *Repository `xorm:"-"`
  21. PublisherID int64 `xorm:"INDEX"`
  22. Publisher *User `xorm:"-"`
  23. TagName string `xorm:"INDEX UNIQUE(n)"`
  24. OriginalAuthor string
  25. OriginalAuthorID int64 `xorm:"index"`
  26. LowerTagName string
  27. Target string
  28. Title string
  29. Sha1 string `xorm:"VARCHAR(40)"`
  30. NumCommits int64
  31. NumCommitsBehind int64 `xorm:"-"`
  32. Note string `xorm:"TEXT"`
  33. IsDraft bool `xorm:"NOT NULL DEFAULT false"`
  34. IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
  35. IsTag bool `xorm:"NOT NULL DEFAULT false"`
  36. Attachments []*Attachment `xorm:"-"`
  37. CreatedUnix timeutil.TimeStamp `xorm:"INDEX"`
  38. }
  39. func (r *Release) loadAttributes(e Engine) error {
  40. var err error
  41. if r.Repo == nil {
  42. r.Repo, err = GetRepositoryByID(r.RepoID)
  43. if err != nil {
  44. return err
  45. }
  46. }
  47. if r.Publisher == nil {
  48. r.Publisher, err = getUserByID(e, r.PublisherID)
  49. if err != nil {
  50. return err
  51. }
  52. }
  53. return getReleaseAttachments(e, r)
  54. }
  55. // LoadAttributes load repo and publisher attributes for a release
  56. func (r *Release) LoadAttributes() error {
  57. return r.loadAttributes(x)
  58. }
  59. // APIURL the api url for a release. release must have attributes loaded
  60. func (r *Release) APIURL() string {
  61. return fmt.Sprintf("%sapi/v1/repos/%s/releases/%d",
  62. setting.AppURL, r.Repo.FullName(), r.ID)
  63. }
  64. // ZipURL the zip url for a release. release must have attributes loaded
  65. func (r *Release) ZipURL() string {
  66. return fmt.Sprintf("%s/archive/%s.zip", r.Repo.HTMLURL(), r.TagName)
  67. }
  68. // TarURL the tar.gz url for a release. release must have attributes loaded
  69. func (r *Release) TarURL() string {
  70. return fmt.Sprintf("%s/archive/%s.tar.gz", r.Repo.HTMLURL(), r.TagName)
  71. }
  72. // APIFormat convert a Release to api.Release
  73. func (r *Release) APIFormat() *api.Release {
  74. assets := make([]*api.Attachment, 0)
  75. for _, att := range r.Attachments {
  76. assets = append(assets, att.APIFormat())
  77. }
  78. return &api.Release{
  79. ID: r.ID,
  80. TagName: r.TagName,
  81. Target: r.Target,
  82. Title: r.Title,
  83. Note: r.Note,
  84. URL: r.APIURL(),
  85. TarURL: r.TarURL(),
  86. ZipURL: r.ZipURL(),
  87. IsDraft: r.IsDraft,
  88. IsPrerelease: r.IsPrerelease,
  89. CreatedAt: r.CreatedUnix.AsTime(),
  90. PublishedAt: r.CreatedUnix.AsTime(),
  91. Publisher: r.Publisher.APIFormat(),
  92. Attachments: assets,
  93. }
  94. }
  95. // IsReleaseExist returns true if release with given tag name already exists.
  96. func IsReleaseExist(repoID int64, tagName string) (bool, error) {
  97. if len(tagName) == 0 {
  98. return false, nil
  99. }
  100. return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
  101. }
  102. // InsertRelease inserts a release
  103. func InsertRelease(rel *Release) error {
  104. _, err := x.Insert(rel)
  105. return err
  106. }
  107. // UpdateRelease updates all columns of a release
  108. func UpdateRelease(rel *Release) error {
  109. _, err := x.ID(rel.ID).AllCols().Update(rel)
  110. return err
  111. }
  112. // AddReleaseAttachments adds a release attachments
  113. func AddReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
  114. // Check attachments
  115. attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs)
  116. if err != nil {
  117. return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
  118. }
  119. for i := range attachments {
  120. attachments[i].ReleaseID = releaseID
  121. // No assign value could be 0, so ignore AllCols().
  122. if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {
  123. return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  124. }
  125. }
  126. return
  127. }
  128. // GetRelease returns release by given ID.
  129. func GetRelease(repoID int64, tagName string) (*Release, error) {
  130. isExist, err := IsReleaseExist(repoID, tagName)
  131. if err != nil {
  132. return nil, err
  133. } else if !isExist {
  134. return nil, ErrReleaseNotExist{0, tagName}
  135. }
  136. rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
  137. _, err = x.Get(rel)
  138. return rel, err
  139. }
  140. // GetReleaseByID returns release with given ID.
  141. func GetReleaseByID(id int64) (*Release, error) {
  142. rel := new(Release)
  143. has, err := x.
  144. ID(id).
  145. Get(rel)
  146. if err != nil {
  147. return nil, err
  148. } else if !has {
  149. return nil, ErrReleaseNotExist{id, ""}
  150. }
  151. return rel, nil
  152. }
  153. // FindReleasesOptions describes the conditions to Find releases
  154. type FindReleasesOptions struct {
  155. IncludeDrafts bool
  156. IncludeTags bool
  157. TagNames []string
  158. }
  159. func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
  160. var cond = builder.NewCond()
  161. cond = cond.And(builder.Eq{"repo_id": repoID})
  162. if !opts.IncludeDrafts {
  163. cond = cond.And(builder.Eq{"is_draft": false})
  164. }
  165. if !opts.IncludeTags {
  166. cond = cond.And(builder.Eq{"is_tag": false})
  167. }
  168. if len(opts.TagNames) > 0 {
  169. cond = cond.And(builder.In("tag_name", opts.TagNames))
  170. }
  171. return cond
  172. }
  173. // GetReleasesByRepoID returns a list of releases of repository.
  174. func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions, page, pageSize int) (rels []*Release, err error) {
  175. if page <= 0 {
  176. page = 1
  177. }
  178. err = x.
  179. Desc("created_unix", "id").
  180. Limit(pageSize, (page-1)*pageSize).
  181. Where(opts.toConds(repoID)).
  182. Find(&rels)
  183. return rels, err
  184. }
  185. // GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
  186. func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) {
  187. err = x.
  188. Desc("created_unix").
  189. In("tag_name", tagNames).
  190. Find(&rels, Release{RepoID: repoID})
  191. return rels, err
  192. }
  193. // GetReleaseCountByRepoID returns the count of releases of repository
  194. func GetReleaseCountByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
  195. return x.Where(opts.toConds(repoID)).Count(&Release{})
  196. }
  197. type releaseMetaSearch struct {
  198. ID []int64
  199. Rel []*Release
  200. }
  201. func (s releaseMetaSearch) Len() int {
  202. return len(s.ID)
  203. }
  204. func (s releaseMetaSearch) Swap(i, j int) {
  205. s.ID[i], s.ID[j] = s.ID[j], s.ID[i]
  206. s.Rel[i], s.Rel[j] = s.Rel[j], s.Rel[i]
  207. }
  208. func (s releaseMetaSearch) Less(i, j int) bool {
  209. return s.ID[i] < s.ID[j]
  210. }
  211. // GetReleaseAttachments retrieves the attachments for releases
  212. func GetReleaseAttachments(rels ...*Release) (err error) {
  213. return getReleaseAttachments(x, rels...)
  214. }
  215. func getReleaseAttachments(e Engine, rels ...*Release) (err error) {
  216. if len(rels) == 0 {
  217. return
  218. }
  219. // To keep this efficient as possible sort all releases by id,
  220. // select attachments by release id,
  221. // then merge join them
  222. // Sort
  223. var sortedRels = releaseMetaSearch{ID: make([]int64, len(rels)), Rel: make([]*Release, len(rels))}
  224. var attachments []*Attachment
  225. for index, element := range rels {
  226. element.Attachments = []*Attachment{}
  227. sortedRels.ID[index] = element.ID
  228. sortedRels.Rel[index] = element
  229. }
  230. sort.Sort(sortedRels)
  231. // Select attachments
  232. err = e.
  233. Asc("release_id").
  234. In("release_id", sortedRels.ID).
  235. Find(&attachments, Attachment{})
  236. if err != nil {
  237. return err
  238. }
  239. // merge join
  240. var currentIndex = 0
  241. for _, attachment := range attachments {
  242. for sortedRels.ID[currentIndex] < attachment.ReleaseID {
  243. currentIndex++
  244. }
  245. sortedRels.Rel[currentIndex].Attachments = append(sortedRels.Rel[currentIndex].Attachments, attachment)
  246. }
  247. return
  248. }
  249. type releaseSorter struct {
  250. rels []*Release
  251. }
  252. func (rs *releaseSorter) Len() int {
  253. return len(rs.rels)
  254. }
  255. func (rs *releaseSorter) Less(i, j int) bool {
  256. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  257. if diffNum != 0 {
  258. return diffNum > 0
  259. }
  260. return rs.rels[i].CreatedUnix > rs.rels[j].CreatedUnix
  261. }
  262. func (rs *releaseSorter) Swap(i, j int) {
  263. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  264. }
  265. // SortReleases sorts releases by number of commits and created time.
  266. func SortReleases(rels []*Release) {
  267. sorter := &releaseSorter{rels: rels}
  268. sort.Sort(sorter)
  269. }
  270. // DeleteReleaseByID deletes a release from database by given ID.
  271. func DeleteReleaseByID(id int64) error {
  272. _, err := x.ID(id).Delete(new(Release))
  273. return err
  274. }
  275. // UpdateReleasesMigrationsByType updates all migrated repositories' releases from gitServiceType to replace originalAuthorID to posterID
  276. func UpdateReleasesMigrationsByType(gitServiceType structs.GitServiceType, originalAuthorID string, posterID int64) error {
  277. _, err := x.Table("release").
  278. Where("repo_id IN (SELECT id FROM repository WHERE original_service_type = ?)", gitServiceType).
  279. And("original_author_id = ?", originalAuthorID).
  280. Update(map[string]interface{}{
  281. "publisher_id": posterID,
  282. "original_author": "",
  283. "original_author_id": 0,
  284. })
  285. return err
  286. }