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.

518 lines
14 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
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
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 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
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
5 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/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/process"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/util"
  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. LowerTagName string
  26. Target string
  27. Title string
  28. Sha1 string `xorm:"VARCHAR(40)"`
  29. NumCommits int64
  30. NumCommitsBehind int64 `xorm:"-"`
  31. Note string `xorm:"TEXT"`
  32. IsDraft bool `xorm:"NOT NULL DEFAULT false"`
  33. IsPrerelease bool `xorm:"NOT NULL DEFAULT false"`
  34. IsTag bool `xorm:"NOT NULL DEFAULT false"`
  35. Attachments []*Attachment `xorm:"-"`
  36. CreatedUnix util.TimeStamp `xorm:"INDEX"`
  37. }
  38. func (r *Release) loadAttributes(e Engine) error {
  39. var err error
  40. if r.Repo == nil {
  41. r.Repo, err = GetRepositoryByID(r.RepoID)
  42. if err != nil {
  43. return err
  44. }
  45. }
  46. if r.Publisher == nil {
  47. r.Publisher, err = getUserByID(e, r.PublisherID)
  48. if err != nil {
  49. return err
  50. }
  51. }
  52. return getReleaseAttachments(e, r)
  53. }
  54. // LoadAttributes load repo and publisher attributes for a release
  55. func (r *Release) LoadAttributes() error {
  56. return r.loadAttributes(x)
  57. }
  58. // APIURL the api url for a release. release must have attributes loaded
  59. func (r *Release) APIURL() string {
  60. return fmt.Sprintf("%sapi/v1/%s/releases/%d",
  61. setting.AppURL, r.Repo.FullName(), r.ID)
  62. }
  63. // ZipURL the zip url for a release. release must have attributes loaded
  64. func (r *Release) ZipURL() string {
  65. return fmt.Sprintf("%s/archive/%s.zip", r.Repo.HTMLURL(), r.TagName)
  66. }
  67. // TarURL the tar.gz url for a release. release must have attributes loaded
  68. func (r *Release) TarURL() string {
  69. return fmt.Sprintf("%s/archive/%s.tar.gz", r.Repo.HTMLURL(), r.TagName)
  70. }
  71. // APIFormat convert a Release to api.Release
  72. func (r *Release) APIFormat() *api.Release {
  73. assets := make([]*api.Attachment, 0)
  74. for _, att := range r.Attachments {
  75. assets = append(assets, att.APIFormat())
  76. }
  77. return &api.Release{
  78. ID: r.ID,
  79. TagName: r.TagName,
  80. Target: r.Target,
  81. Title: r.Title,
  82. Note: r.Note,
  83. URL: r.APIURL(),
  84. TarURL: r.TarURL(),
  85. ZipURL: r.ZipURL(),
  86. IsDraft: r.IsDraft,
  87. IsPrerelease: r.IsPrerelease,
  88. CreatedAt: r.CreatedUnix.AsTime(),
  89. PublishedAt: r.CreatedUnix.AsTime(),
  90. Publisher: r.Publisher.APIFormat(),
  91. Attachments: assets,
  92. }
  93. }
  94. // IsReleaseExist returns true if release with given tag name already exists.
  95. func IsReleaseExist(repoID int64, tagName string) (bool, error) {
  96. if len(tagName) == 0 {
  97. return false, nil
  98. }
  99. return x.Get(&Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)})
  100. }
  101. func createTag(gitRepo *git.Repository, rel *Release) error {
  102. // Only actual create when publish.
  103. if !rel.IsDraft {
  104. if !gitRepo.IsTagExist(rel.TagName) {
  105. commit, err := gitRepo.GetCommit(rel.Target)
  106. if err != nil {
  107. return fmt.Errorf("GetCommit: %v", err)
  108. }
  109. // Trim '--' prefix to prevent command line argument vulnerability.
  110. rel.TagName = strings.TrimPrefix(rel.TagName, "--")
  111. if err = gitRepo.CreateTag(rel.TagName, commit.ID.String()); err != nil {
  112. if strings.Contains(err.Error(), "is not a valid tag name") {
  113. return ErrInvalidTagName{rel.TagName}
  114. }
  115. return err
  116. }
  117. rel.LowerTagName = strings.ToLower(rel.TagName)
  118. }
  119. commit, err := gitRepo.GetTagCommit(rel.TagName)
  120. if err != nil {
  121. return fmt.Errorf("GetTagCommit: %v", err)
  122. }
  123. rel.Sha1 = commit.ID.String()
  124. rel.CreatedUnix = util.TimeStamp(commit.Author.When.Unix())
  125. rel.NumCommits, err = commit.CommitsCount()
  126. if err != nil {
  127. return fmt.Errorf("CommitsCount: %v", err)
  128. }
  129. } else {
  130. rel.CreatedUnix = util.TimeStampNow()
  131. }
  132. return nil
  133. }
  134. func addReleaseAttachments(releaseID int64, attachmentUUIDs []string) (err error) {
  135. // Check attachments
  136. var attachments = make([]*Attachment, 0)
  137. for _, uuid := range attachmentUUIDs {
  138. attach, err := getAttachmentByUUID(x, uuid)
  139. if err != nil {
  140. if IsErrAttachmentNotExist(err) {
  141. continue
  142. }
  143. return fmt.Errorf("getAttachmentByUUID [%s]: %v", uuid, err)
  144. }
  145. attachments = append(attachments, attach)
  146. }
  147. for i := range attachments {
  148. attachments[i].ReleaseID = releaseID
  149. // No assign value could be 0, so ignore AllCols().
  150. if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {
  151. return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  152. }
  153. }
  154. return
  155. }
  156. // CreateRelease creates a new release of repository.
  157. func CreateRelease(gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) error {
  158. isExist, err := IsReleaseExist(rel.RepoID, rel.TagName)
  159. if err != nil {
  160. return err
  161. } else if isExist {
  162. return ErrReleaseAlreadyExist{rel.TagName}
  163. }
  164. if err = createTag(gitRepo, rel); err != nil {
  165. return err
  166. }
  167. rel.LowerTagName = strings.ToLower(rel.TagName)
  168. _, err = x.InsertOne(rel)
  169. if err != nil {
  170. return err
  171. }
  172. err = addReleaseAttachments(rel.ID, attachmentUUIDs)
  173. if err != nil {
  174. return err
  175. }
  176. if !rel.IsDraft {
  177. if err := rel.LoadAttributes(); err != nil {
  178. log.Error("LoadAttributes: %v", err)
  179. } else {
  180. mode, _ := AccessLevel(rel.Publisher, rel.Repo)
  181. if err := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{
  182. Action: api.HookReleasePublished,
  183. Release: rel.APIFormat(),
  184. Repository: rel.Repo.APIFormat(mode),
  185. Sender: rel.Publisher.APIFormat(),
  186. }); err != nil {
  187. log.Error("PrepareWebhooks: %v", err)
  188. } else {
  189. go HookQueue.Add(rel.Repo.ID)
  190. }
  191. }
  192. }
  193. return nil
  194. }
  195. // GetRelease returns release by given ID.
  196. func GetRelease(repoID int64, tagName string) (*Release, error) {
  197. isExist, err := IsReleaseExist(repoID, tagName)
  198. if err != nil {
  199. return nil, err
  200. } else if !isExist {
  201. return nil, ErrReleaseNotExist{0, tagName}
  202. }
  203. rel := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
  204. _, err = x.Get(rel)
  205. return rel, err
  206. }
  207. // GetReleaseByID returns release with given ID.
  208. func GetReleaseByID(id int64) (*Release, error) {
  209. rel := new(Release)
  210. has, err := x.
  211. ID(id).
  212. Get(rel)
  213. if err != nil {
  214. return nil, err
  215. } else if !has {
  216. return nil, ErrReleaseNotExist{id, ""}
  217. }
  218. return rel, nil
  219. }
  220. // FindReleasesOptions describes the conditions to Find releases
  221. type FindReleasesOptions struct {
  222. IncludeDrafts bool
  223. IncludeTags bool
  224. TagNames []string
  225. }
  226. func (opts *FindReleasesOptions) toConds(repoID int64) builder.Cond {
  227. var cond = builder.NewCond()
  228. cond = cond.And(builder.Eq{"repo_id": repoID})
  229. if !opts.IncludeDrafts {
  230. cond = cond.And(builder.Eq{"is_draft": false})
  231. }
  232. if !opts.IncludeTags {
  233. cond = cond.And(builder.Eq{"is_tag": false})
  234. }
  235. if len(opts.TagNames) > 0 {
  236. cond = cond.And(builder.In("tag_name", opts.TagNames))
  237. }
  238. return cond
  239. }
  240. // GetReleasesByRepoID returns a list of releases of repository.
  241. func GetReleasesByRepoID(repoID int64, opts FindReleasesOptions, page, pageSize int) (rels []*Release, err error) {
  242. if page <= 0 {
  243. page = 1
  244. }
  245. err = x.
  246. Desc("created_unix", "id").
  247. Limit(pageSize, (page-1)*pageSize).
  248. Where(opts.toConds(repoID)).
  249. Find(&rels)
  250. return rels, err
  251. }
  252. // GetReleasesByRepoIDAndNames returns a list of releases of repository according repoID and tagNames.
  253. func GetReleasesByRepoIDAndNames(repoID int64, tagNames []string) (rels []*Release, err error) {
  254. err = x.
  255. Desc("created_unix").
  256. In("tag_name", tagNames).
  257. Find(&rels, Release{RepoID: repoID})
  258. return rels, err
  259. }
  260. // GetReleaseCountByRepoID returns the count of releases of repository
  261. func GetReleaseCountByRepoID(repoID int64, opts FindReleasesOptions) (int64, error) {
  262. return x.Where(opts.toConds(repoID)).Count(&Release{})
  263. }
  264. type releaseMetaSearch struct {
  265. ID []int64
  266. Rel []*Release
  267. }
  268. func (s releaseMetaSearch) Len() int {
  269. return len(s.ID)
  270. }
  271. func (s releaseMetaSearch) Swap(i, j int) {
  272. s.ID[i], s.ID[j] = s.ID[j], s.ID[i]
  273. s.Rel[i], s.Rel[j] = s.Rel[j], s.Rel[i]
  274. }
  275. func (s releaseMetaSearch) Less(i, j int) bool {
  276. return s.ID[i] < s.ID[j]
  277. }
  278. // GetReleaseAttachments retrieves the attachments for releases
  279. func GetReleaseAttachments(rels ...*Release) (err error) {
  280. return getReleaseAttachments(x, rels...)
  281. }
  282. func getReleaseAttachments(e Engine, rels ...*Release) (err error) {
  283. if len(rels) == 0 {
  284. return
  285. }
  286. // To keep this efficient as possible sort all releases by id,
  287. // select attachments by release id,
  288. // then merge join them
  289. // Sort
  290. var sortedRels = releaseMetaSearch{ID: make([]int64, len(rels)), Rel: make([]*Release, len(rels))}
  291. var attachments []*Attachment
  292. for index, element := range rels {
  293. element.Attachments = []*Attachment{}
  294. sortedRels.ID[index] = element.ID
  295. sortedRels.Rel[index] = element
  296. }
  297. sort.Sort(sortedRels)
  298. // Select attachments
  299. err = e.
  300. Asc("release_id").
  301. In("release_id", sortedRels.ID).
  302. Find(&attachments, Attachment{})
  303. if err != nil {
  304. return err
  305. }
  306. // merge join
  307. var currentIndex = 0
  308. for _, attachment := range attachments {
  309. for sortedRels.ID[currentIndex] < attachment.ReleaseID {
  310. currentIndex++
  311. }
  312. sortedRels.Rel[currentIndex].Attachments = append(sortedRels.Rel[currentIndex].Attachments, attachment)
  313. }
  314. return
  315. }
  316. type releaseSorter struct {
  317. rels []*Release
  318. }
  319. func (rs *releaseSorter) Len() int {
  320. return len(rs.rels)
  321. }
  322. func (rs *releaseSorter) Less(i, j int) bool {
  323. diffNum := rs.rels[i].NumCommits - rs.rels[j].NumCommits
  324. if diffNum != 0 {
  325. return diffNum > 0
  326. }
  327. return rs.rels[i].CreatedUnix > rs.rels[j].CreatedUnix
  328. }
  329. func (rs *releaseSorter) Swap(i, j int) {
  330. rs.rels[i], rs.rels[j] = rs.rels[j], rs.rels[i]
  331. }
  332. // SortReleases sorts releases by number of commits and created time.
  333. func SortReleases(rels []*Release) {
  334. sorter := &releaseSorter{rels: rels}
  335. sort.Sort(sorter)
  336. }
  337. // UpdateRelease updates information of a release.
  338. func UpdateRelease(doer *User, gitRepo *git.Repository, rel *Release, attachmentUUIDs []string) (err error) {
  339. if err = createTag(gitRepo, rel); err != nil {
  340. return err
  341. }
  342. rel.LowerTagName = strings.ToLower(rel.TagName)
  343. _, err = x.ID(rel.ID).AllCols().Update(rel)
  344. if err != nil {
  345. return err
  346. }
  347. err = rel.loadAttributes(x)
  348. if err != nil {
  349. return err
  350. }
  351. err = addReleaseAttachments(rel.ID, attachmentUUIDs)
  352. mode, _ := AccessLevel(doer, rel.Repo)
  353. if err1 := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{
  354. Action: api.HookReleaseUpdated,
  355. Release: rel.APIFormat(),
  356. Repository: rel.Repo.APIFormat(mode),
  357. Sender: rel.Publisher.APIFormat(),
  358. }); err1 != nil {
  359. log.Error("PrepareWebhooks: %v", err)
  360. } else {
  361. go HookQueue.Add(rel.Repo.ID)
  362. }
  363. return err
  364. }
  365. // DeleteReleaseByID deletes a release and corresponding Git tag by given ID.
  366. func DeleteReleaseByID(id int64, u *User, delTag bool) error {
  367. rel, err := GetReleaseByID(id)
  368. if err != nil {
  369. return fmt.Errorf("GetReleaseByID: %v", err)
  370. }
  371. repo, err := GetRepositoryByID(rel.RepoID)
  372. if err != nil {
  373. return fmt.Errorf("GetRepositoryByID: %v", err)
  374. }
  375. if delTag {
  376. _, stderr, err := process.GetManager().ExecDir(-1, repo.RepoPath(),
  377. fmt.Sprintf("DeleteReleaseByID (git tag -d): %d", rel.ID),
  378. git.GitExecutable, "tag", "-d", rel.TagName)
  379. if err != nil && !strings.Contains(stderr, "not found") {
  380. return fmt.Errorf("git tag -d: %v - %s", err, stderr)
  381. }
  382. if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
  383. return fmt.Errorf("Delete: %v", err)
  384. }
  385. } else {
  386. rel.IsTag = true
  387. rel.IsDraft = false
  388. rel.IsPrerelease = false
  389. rel.Title = ""
  390. rel.Note = ""
  391. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  392. return fmt.Errorf("Update: %v", err)
  393. }
  394. }
  395. rel.Repo = repo
  396. if err = rel.LoadAttributes(); err != nil {
  397. return fmt.Errorf("LoadAttributes: %v", err)
  398. }
  399. mode, _ := AccessLevel(u, rel.Repo)
  400. if err := PrepareWebhooks(rel.Repo, HookEventRelease, &api.ReleasePayload{
  401. Action: api.HookReleaseDeleted,
  402. Release: rel.APIFormat(),
  403. Repository: rel.Repo.APIFormat(mode),
  404. Sender: rel.Publisher.APIFormat(),
  405. }); err != nil {
  406. log.Error("PrepareWebhooks: %v", err)
  407. } else {
  408. go HookQueue.Add(rel.Repo.ID)
  409. }
  410. return nil
  411. }
  412. // SyncReleasesWithTags synchronizes release table with repository tags
  413. func SyncReleasesWithTags(repo *Repository, gitRepo *git.Repository) error {
  414. existingRelTags := make(map[string]struct{})
  415. opts := FindReleasesOptions{IncludeDrafts: true, IncludeTags: true}
  416. for page := 1; ; page++ {
  417. rels, err := GetReleasesByRepoID(repo.ID, opts, page, 100)
  418. if err != nil {
  419. return fmt.Errorf("GetReleasesByRepoID: %v", err)
  420. }
  421. if len(rels) == 0 {
  422. break
  423. }
  424. for _, rel := range rels {
  425. if rel.IsDraft {
  426. continue
  427. }
  428. commitID, err := gitRepo.GetTagCommitID(rel.TagName)
  429. if err != nil && !git.IsErrNotExist(err) {
  430. return fmt.Errorf("GetTagCommitID: %v", err)
  431. }
  432. if git.IsErrNotExist(err) || commitID != rel.Sha1 {
  433. if err := PushUpdateDeleteTag(repo, rel.TagName); err != nil {
  434. return fmt.Errorf("PushUpdateDeleteTag: %v", err)
  435. }
  436. } else {
  437. existingRelTags[strings.ToLower(rel.TagName)] = struct{}{}
  438. }
  439. }
  440. }
  441. tags, err := gitRepo.GetTags()
  442. if err != nil {
  443. return fmt.Errorf("GetTags: %v", err)
  444. }
  445. for _, tagName := range tags {
  446. if _, ok := existingRelTags[strings.ToLower(tagName)]; !ok {
  447. if err := PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
  448. return fmt.Errorf("pushUpdateAddTag: %v", err)
  449. }
  450. }
  451. }
  452. return nil
  453. }