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.

456 lines
12 KiB

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
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
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 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
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "sort"
  8. "strings"
  9. "code.gitea.io/git"
  10. "code.gitea.io/gitea/modules/process"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/util"
  13. api "code.gitea.io/sdk/gitea"
  14. "github.com/go-xorm/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. LowerTagName string
  25. Target string
  26. Title string
  27. Sha1 string `xorm:"VARCHAR(40)"`
  28. NumCommits int64
  29. NumCommitsBehind int64 `xorm:"-"`
  30. Note string `xorm:"TEXT"`
  31. IsDraft bool `xorm:"NOT NULL DEFAULT false"`
  32. IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
  33. IsTag bool `xorm:"NOT NULL DEFAULT false"`
  34. Attachments []*Attachment `xorm:"-"`
  35. CreatedUnix util.TimeStamp `xorm:"INDEX"`
  36. }
  37. func (r *Release) loadAttributes(e Engine) error {
  38. var err error
  39. if r.Repo == nil {
  40. r.Repo, err = GetRepositoryByID(r.RepoID)
  41. if err != nil {
  42. return err
  43. }
  44. }
  45. if r.Publisher == nil {
  46. r.Publisher, err = GetUserByID(r.PublisherID)
  47. if err != nil {
  48. return err
  49. }
  50. }
  51. return GetReleaseAttachments(r)
  52. }
  53. // LoadAttributes load repo and publisher attributes for a release
  54. func (r *Release) LoadAttributes() error {
  55. return r.loadAttributes(x)
  56. }
  57. // APIURL the api url for a release. release must have attributes loaded
  58. func (r *Release) APIURL() string {
  59. return fmt.Sprintf("%sapi/v1/%s/releases/%d",
  60. setting.AppURL, r.Repo.FullName(), r.ID)
  61. }
  62. // ZipURL the zip url for a release. release must have attributes loaded
  63. func (r *Release) ZipURL() string {
  64. return fmt.Sprintf("%s/archive/%s.zip", r.Repo.HTMLURL(), r.TagName)
  65. }
  66. // TarURL the tar.gz url for a release. release must have attributes loaded
  67. func (r *Release) TarURL() string {
  68. return fmt.Sprintf("%s/archive/%s.tar.gz", r.Repo.HTMLURL(), r.TagName)
  69. }
  70. // APIFormat convert a Release to api.Release
  71. func (r *Release) APIFormat() *api.Release {
  72. assets := make([]*api.Attachment, 0)
  73. for _, att := range r.Attachments {
  74. assets = append(assets, att.APIFormat())
  75. }
  76. return &api.Release{
  77. ID: r.ID,
  78. TagName: r.TagName,
  79. Target: r.Target,
  80. Note: r.Note,
  81. URL: r.APIURL(),
  82. TarURL: r.TarURL(),
  83. ZipURL: r.ZipURL(),
  84. IsDraft: r.IsDraft,
  85. IsPrerelease: r.IsPrerelease,
  86. CreatedAt: r.CreatedUnix.AsTime(),
  87. PublishedAt: r.CreatedUnix.AsTime(),
  88. Publisher: r.Publisher.APIFormat(),
  89. Attachments: assets,
  90. }
  91. }
  92. // IsReleaseExist returns true if release with given tag name already exists.
  93. func IsReleaseExist(repoID int64, tagName string) (bool, error) {
  94. if len(tagName) == 0 {
  95. return false, nil
  96. }
  97. return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
  98. }
  99. func createTag(gitRepo *git.Repository, rel *Release) error {
  100. // Only actual create when publish.
  101. if !rel.IsDraft {
  102. if !gitRepo.IsTagExist(rel.TagName) {
  103. commit, err := gitRepo.GetBranchCommit(rel.Target)
  104. if err != nil {
  105. return fmt.Errorf("GetBranchCommit: %v", err)
  106. }
  107. // Trim '--' prefix to prevent command line argument vulnerability.
  108. rel.TagName = strings.TrimPrefix(rel.TagName, "--")
  109. if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
  110. if strings.Contains(err.Error(), "is not a valid tag name") {
  111. return ErrInvalidTagName{rel.TagName}
  112. }
  113. return err
  114. }
  115. rel.LowerTagName = strings.ToLower(rel.TagName)
  116. }
  117. commit, err := gitRepo.GetTagCommit(rel.TagName)
  118. if err != nil {
  119. return fmt.Errorf("GetTagCommit: %v", err)
  120. }
  121. rel.Sha1 = commit.ID.String()
  122. rel.CreatedUnix = util.TimeStamp(commit.Author.When.Unix())
  123. rel.NumCommits, err = commit.CommitsCount()
  124. if err != nil {
  125. return fmt.Errorf("CommitsCount: %v", err)
  126. }
  127. } else {
  128. rel.CreatedUnix = util.TimeStampNow()
  129. }
  130. return nil
  131. }
  132. func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
  133. // Check attachments
  134. var attachments = make([]*Attachment, 0)
  135. for _, uuid := range attachmentUUIDs {
  136. attach, err := getAttachmentByUUID(x, uuid)
  137. if err != nil {
  138. if IsErrAttachmentNotExist(err) {
  139. continue
  140. }
  141. return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  142. }
  143. attachments = append(attachments, attach)
  144. }
  145. for i := range attachments {
  146. attachments[i].ReleaseID = releaseID
  147. // No assign value could be 0, so ignore AllCols().
  148. if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {
  149. return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  150. }
  151. }
  152. return
  153. }
  154. // CreateRelease creates a new release of repository.
  155. func CreateRelease(gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) error {
  156. isExist, err := IsReleaseExist(rel.RepoID, rel.TagName)
  157. if err != nil {
  158. return err
  159. } else if isExist {
  160. return ErrReleaseAlreadyExist{rel.TagName}
  161. }
  162. if err = createTag(gitRepo, rel); err != nil {
  163. return err
  164. }
  165. rel.LowerTagName = strings.ToLower(rel.TagName)
  166. _, err = x.InsertOne(rel)
  167. if err != nil {
  168. return err
  169. }
  170. err = addReleaseAttachments(rel.ID, attachmentUUIDs)
  171. return err
  172. }
  173. // GetRelease returns release by given ID.
  174. func GetRelease(repoID int64, tagName string) (*Release, error) {
  175. isExist, err := IsReleaseExist(repoID, tagName)
  176. if err != nil {
  177. return nil, err
  178. } else if !isExist {
  179. return nil, ErrReleaseNotExist{0, tagName}
  180. }
  181. rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
  182. _, err = x.Get(rel)
  183. return rel, err
  184. }
  185. // GetReleaseByID returns release with given ID.
  186. func GetReleaseByID(id int64) (*Release, error) {
  187. rel := new(Release)
  188. has, err := x.
  189. ID(id).
  190. Get(rel)
  191. if err != nil {
  192. return nil, err
  193. } else if !has {
  194. return nil, ErrReleaseNotExist{id, ""}
  195. }
  196. return rel, nil
  197. }
  198. // FindReleasesOptions describes the conditions to Find releases
  199. type FindReleasesOptions struct {
  200. IncludeDrafts bool
  201. IncludeTags bool
  202. TagNames []string
  203. }
  204. func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
  205. var cond = builder.NewCond()
  206. cond = cond.And(builder.Eq{"repo_id": repoID})
  207. if !opts.IncludeDrafts {
  208. cond = cond.And(builder.Eq{"is_draft": false})
  209. }
  210. if !opts.IncludeTags {
  211. cond = cond.And(builder.Eq{"is_tag": false})
  212. }
  213. if len(opts.TagNames) > 0 {
  214. cond = cond.And(builder.In("tag_name", opts.TagNames))
  215. }
  216. return cond
  217. }
  218. // GetReleasesByRepoID returns a list of releases of repository.
  219. func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions, page, pageSize int) (rels []*Release, err error) {
  220. if page <= 0 {
  221. page = 1
  222. }
  223. err = x.
  224. Desc("created_unix", "id").
  225. Limit(pageSize, (page-1)*pageSize).
  226. Where(opts.toConds(repoID)).
  227. Find(&rels)
  228. return rels, err
  229. }
  230. // GetReleaseCountByRepoID returns the count of releases of repository
  231. func GetReleaseCountByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
  232. return x.Where(opts.toConds(repoID)).Count(&Release{})
  233. }
  234. type releaseMetaSearch struct {
  235. ID []int64
  236. Rel []*Release
  237. }
  238. func (s releaseMetaSearch) Len() int {
  239. return len(s.ID)
  240. }
  241. func (s releaseMetaSearch) Swap(i, j int) {
  242. s.ID[i], s.ID[j] = s.ID[j], s.ID[i]
  243. s.Rel[i], s.Rel[j] = s.Rel[j], s.Rel[i]
  244. }
  245. func (s releaseMetaSearch) Less(i, j int) bool {
  246. return s.ID[i] < s.ID[j]
  247. }
  248. // GetReleaseAttachments retrieves the attachments for releases
  249. func GetReleaseAttachments(rels ...*Release) (err error) {
  250. if len(rels) == 0 {
  251. return
  252. }
  253. // To keep this efficient as possible sort all releases by id,
  254. // select attachments by release id,
  255. // then merge join them
  256. // Sort
  257. var sortedRels = releaseMetaSearch{ID: make([]int64, len(rels)), Rel: make([]*Release, len(rels))}
  258. var attachments []*Attachment
  259. for index, element := range rels {
  260. element.Attachments = []*Attachment{}
  261. sortedRels.ID[index] = element.ID
  262. sortedRels.Rel[index] = element
  263. }
  264. sort.Sort(sortedRels)
  265. // Select attachments
  266. err = x.
  267. Asc("release_id").
  268. In("release_id", sortedRels.ID).
  269. Find(&attachments, Attachment{})
  270. if err != nil {
  271. return err
  272. }
  273. // merge join
  274. var currentIndex = 0
  275. for _, attachment := range attachments {
  276. for sortedRels.ID[currentIndex] < attachment.ReleaseID {
  277. currentIndex++
  278. }
  279. sortedRels.Rel[currentIndex].Attachments = append(sortedRels.Rel[currentIndex].Attachments, attachment)
  280. }
  281. return
  282. }
  283. type releaseSorter struct {
  284. rels []*Release
  285. }
  286. func (rs *releaseSorter) Len() int {
  287. return len(rs.rels)
  288. }
  289. func (rs *releaseSorter) Less(i, j int) bool {
  290. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  291. if diffNum != 0 {
  292. return diffNum > 0
  293. }
  294. return rs.rels[i].CreatedUnix > rs.rels[j].CreatedUnix
  295. }
  296. func (rs *releaseSorter) Swap(i, j int) {
  297. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  298. }
  299. // SortReleases sorts releases by number of commits and created time.
  300. func SortReleases(rels []*Release) {
  301. sorter := &releaseSorter{rels: rels}
  302. sort.Sort(sorter)
  303. }
  304. // UpdateRelease updates information of a release.
  305. func UpdateRelease(gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) (err error) {
  306. if err = createTag(gitRepo, rel); err != nil {
  307. return err
  308. }
  309. rel.LowerTagName = strings.ToLower(rel.TagName)
  310. _, err = x.ID(rel.ID).AllCols().Update(rel)
  311. if err != nil {
  312. return err
  313. }
  314. err = addReleaseAttachments(rel.ID, attachmentUUIDs)
  315. return err
  316. }
  317. // DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
  318. func DeleteReleaseByID(id int64, u *User, delTag bool) error {
  319. rel, err := GetReleaseByID(id)
  320. if err != nil {
  321. return fmt.Errorf("GetReleaseByID: %v", err)
  322. }
  323. repo, err := GetRepositoryByID(rel.RepoID)
  324. if err != nil {
  325. return fmt.Errorf("GetRepositoryByID: %v", err)
  326. }
  327. has, err := HasAccess(u.ID, repo, AccessModeWrite)
  328. if err != nil {
  329. return fmt.Errorf("HasAccess: %v", err)
  330. } else if !has {
  331. return fmt.Errorf("DeleteReleaseByID: permission denied")
  332. }
  333. if delTag {
  334. _, stderr, err := process.GetManager().ExecDir(-1, repo.RepoPath(),
  335. fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID),
  336. "git", "tag", "-d", rel.TagName)
  337. if err != nil && !strings.Contains(stderr, "not found") {
  338. return fmt.Errorf("git tag -d: %v - %s", err, stderr)
  339. }
  340. if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
  341. return fmt.Errorf("Delete: %v", err)
  342. }
  343. } else {
  344. rel.IsTag = true
  345. rel.IsDraft = false
  346. rel.IsPrerelease = false
  347. rel.Title = ""
  348. rel.Note = ""
  349. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  350. return fmt.Errorf("Update: %v", err)
  351. }
  352. }
  353. return nil
  354. }
  355. // SyncReleasesWithTags synchronizes release table with repository tags
  356. func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
  357. existingRelTags := make(map[string]struct{})
  358. opts := FindReleasesOptions{IncludeDrafts: true, IncludeTags: true}
  359. for page := 1; ; page++ {
  360. rels, err := GetReleasesByRepoID(repo.ID, opts, page, 100)
  361. if err != nil {
  362. return fmt.Errorf("GetReleasesByRepoID: %v", err)
  363. }
  364. if len(rels) == 0 {
  365. break
  366. }
  367. for _, rel := range rels {
  368. if rel.IsDraft {
  369. continue
  370. }
  371. commitID, err := gitRepo.GetTagCommitID(rel.TagName)
  372. if err != nil {
  373. return fmt.Errorf("GetTagCommitID: %v", err)
  374. }
  375. if !gitRepo.IsTagExist(rel.TagName) || commitID != rel.Sha1 {
  376. if err := pushUpdateDeleteTag(repo, gitRepo, rel.TagName); err != nil {
  377. return fmt.Errorf("pushUpdateDeleteTag: %v", err)
  378. }
  379. } else {
  380. existingRelTags[strings.ToLower(rel.TagName)] = struct{}{}
  381. }
  382. }
  383. }
  384. tags, err := gitRepo.GetTags()
  385. if err != nil {
  386. return fmt.Errorf("GetTags: %v", err)
  387. }
  388. for _, tagName := range tags {
  389. if _, ok := existingRelTags[strings.ToLower(tagName)]; !ok {
  390. if err := pushUpdateAddTag(repo, gitRepo, tagName); err != nil {
  391. return fmt.Errorf("pushUpdateAddTag: %v", err)
  392. }
  393. }
  394. }
  395. return nil
  396. }