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.

455 lines
11 KiB

Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
Shows total tracked time in issue and milestone list (#3341) * Show total tracked time in issue and milestone list Show total tracked time at issue page Signed-off-by: Jonas Franz <info@jonasfranz.software> * Optimizing TotalTimes by using SumInt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixing wrong total times for milestones caused by a missing JOIN Adding unit tests for total times Signed-off-by: Jonas Franz <info@jonasfranz.software> * Logging error instead of ignoring it Signed-off-by: Jonas Franz <info@jonasfranz.software> * Correcting spelling mistakes Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change error message to a short version Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add error handling to TotalTimes Add variable for totalTimes Signed-off-by: Jonas Franz <info@jonasfranz.de> * Introduce TotalTrackedTimes as variable of issue Load TotalTrackedTimes by loading attributes of IssueList Load TotalTrackedTimes by loading attributes of single issue Add Sec2Time as helper to use it in templates Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fixed test + gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Load TotalTrackedTimes via MilestoneList instead of single requests Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add documentation for MilestoneList Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change comment from SQL query to description Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit test by using int64 instead of int Signed-off-by: Jonas Franz <info@jonasfranz.software> * Check if timetracker is enabled Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix test by enabling timetracking Signed-off-by: Jonas Franz <info@jonasfranz.de>
6 years ago
  1. // Copyright 2017 The Gitea 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. "code.gitea.io/gitea/modules/log"
  8. "code.gitea.io/gitea/modules/setting"
  9. "code.gitea.io/gitea/modules/util"
  10. api "code.gitea.io/sdk/gitea"
  11. "github.com/go-xorm/xorm"
  12. )
  13. // Milestone represents a milestone of repository.
  14. type Milestone struct {
  15. ID int64 `xorm:"pk autoincr"`
  16. RepoID int64 `xorm:"INDEX"`
  17. Name string
  18. Content string `xorm:"TEXT"`
  19. RenderedContent string `xorm:"-"`
  20. IsClosed bool
  21. NumIssues int
  22. NumClosedIssues int
  23. NumOpenIssues int `xorm:"-"`
  24. Completeness int // Percentage(1-100).
  25. IsOverdue bool `xorm:"-"`
  26. DeadlineString string `xorm:"-"`
  27. DeadlineUnix util.TimeStamp
  28. ClosedDateUnix util.TimeStamp
  29. TotalTrackedTime int64 `xorm:"-"`
  30. }
  31. // BeforeUpdate is invoked from XORM before updating this object.
  32. func (m *Milestone) BeforeUpdate() {
  33. if m.NumIssues > 0 {
  34. m.Completeness = m.NumClosedIssues * 100 / m.NumIssues
  35. } else {
  36. m.Completeness = 0
  37. }
  38. }
  39. // AfterLoad is invoked from XORM after setting the value of a field of
  40. // this object.
  41. func (m *Milestone) AfterLoad() {
  42. m.NumOpenIssues = m.NumIssues - m.NumClosedIssues
  43. if m.DeadlineUnix.Year() == 9999 {
  44. return
  45. }
  46. m.DeadlineString = m.DeadlineUnix.Format("2006-01-02")
  47. if util.TimeStampNow() >= m.DeadlineUnix {
  48. m.IsOverdue = true
  49. }
  50. }
  51. // State returns string representation of milestone status.
  52. func (m *Milestone) State() api.StateType {
  53. if m.IsClosed {
  54. return api.StateClosed
  55. }
  56. return api.StateOpen
  57. }
  58. // APIFormat returns this Milestone in API format.
  59. func (m *Milestone) APIFormat() *api.Milestone {
  60. apiMilestone := &api.Milestone{
  61. ID: m.ID,
  62. State: m.State(),
  63. Title: m.Name,
  64. Description: m.Content,
  65. OpenIssues: m.NumOpenIssues,
  66. ClosedIssues: m.NumClosedIssues,
  67. }
  68. if m.IsClosed {
  69. apiMilestone.Closed = m.ClosedDateUnix.AsTimePtr()
  70. }
  71. if m.DeadlineUnix.Year() < 9999 {
  72. apiMilestone.Deadline = m.DeadlineUnix.AsTimePtr()
  73. }
  74. return apiMilestone
  75. }
  76. // NewMilestone creates new milestone of repository.
  77. func NewMilestone(m *Milestone) (err error) {
  78. sess := x.NewSession()
  79. defer sess.Close()
  80. if err = sess.Begin(); err != nil {
  81. return err
  82. }
  83. if _, err = sess.Insert(m); err != nil {
  84. return err
  85. }
  86. if _, err = sess.Exec("UPDATE `repository` SET num_milestones = num_milestones + 1 WHERE id = ?", m.RepoID); err != nil {
  87. return err
  88. }
  89. return sess.Commit()
  90. }
  91. func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
  92. m := &Milestone{
  93. ID: id,
  94. RepoID: repoID,
  95. }
  96. has, err := e.Get(m)
  97. if err != nil {
  98. return nil, err
  99. } else if !has {
  100. return nil, ErrMilestoneNotExist{id, repoID}
  101. }
  102. return m, nil
  103. }
  104. // GetMilestoneByRepoID returns the milestone in a repository.
  105. func GetMilestoneByRepoID(repoID, id int64) (*Milestone, error) {
  106. return getMilestoneByRepoID(x, repoID, id)
  107. }
  108. // MilestoneList is a list of milestones offering additional functionality
  109. type MilestoneList []*Milestone
  110. func (milestones MilestoneList) loadTotalTrackedTimes(e Engine) error {
  111. type totalTimesByMilestone struct {
  112. MilestoneID int64
  113. Time int64
  114. }
  115. if len(milestones) == 0 {
  116. return nil
  117. }
  118. var trackedTimes = make(map[int64]int64, len(milestones))
  119. // Get total tracked time by milestone_id
  120. rows, err := e.Table("issue").
  121. Join("INNER", "milestone", "issue.milestone_id = milestone.id").
  122. Join("LEFT", "tracked_time", "tracked_time.issue_id = issue.id").
  123. Select("milestone_id, sum(time) as time").
  124. In("milestone_id", milestones.getMilestoneIDs()).
  125. GroupBy("milestone_id").
  126. Rows(new(totalTimesByMilestone))
  127. if err != nil {
  128. return err
  129. }
  130. defer rows.Close()
  131. for rows.Next() {
  132. var totalTime totalTimesByMilestone
  133. err = rows.Scan(&totalTime)
  134. if err != nil {
  135. return err
  136. }
  137. trackedTimes[totalTime.MilestoneID] = totalTime.Time
  138. }
  139. for _, milestone := range milestones {
  140. milestone.TotalTrackedTime = trackedTimes[milestone.ID]
  141. }
  142. return nil
  143. }
  144. // LoadTotalTrackedTimes loads for every milestone in the list the TotalTrackedTime by a batch request
  145. func (milestones MilestoneList) LoadTotalTrackedTimes() error {
  146. return milestones.loadTotalTrackedTimes(x)
  147. }
  148. func (milestones MilestoneList) getMilestoneIDs() []int64 {
  149. var ids = make([]int64, 0, len(milestones))
  150. for _, ms := range milestones {
  151. ids = append(ids, ms.ID)
  152. }
  153. return ids
  154. }
  155. // GetMilestonesByRepoID returns all milestones of a repository.
  156. func GetMilestonesByRepoID(repoID int64) (MilestoneList, error) {
  157. miles := make([]*Milestone, 0, 10)
  158. return miles, x.Where("repo_id = ?", repoID).Asc("deadline_unix").Find(&miles)
  159. }
  160. // GetMilestones returns a list of milestones of given repository and status.
  161. func GetMilestones(repoID int64, page int, isClosed bool, sortType string) (MilestoneList, error) {
  162. miles := make([]*Milestone, 0, setting.UI.IssuePagingNum)
  163. sess := x.Where("repo_id = ? AND is_closed = ?", repoID, isClosed)
  164. if page > 0 {
  165. sess = sess.Limit(setting.UI.IssuePagingNum, (page-1)*setting.UI.IssuePagingNum)
  166. }
  167. switch sortType {
  168. case "furthestduedate":
  169. sess.Desc("deadline_unix")
  170. case "leastcomplete":
  171. sess.Asc("completeness")
  172. case "mostcomplete":
  173. sess.Desc("completeness")
  174. case "leastissues":
  175. sess.Asc("num_issues")
  176. case "mostissues":
  177. sess.Desc("num_issues")
  178. default:
  179. sess.Asc("deadline_unix")
  180. }
  181. return miles, sess.Find(&miles)
  182. }
  183. func updateMilestone(e Engine, m *Milestone) error {
  184. _, err := e.ID(m.ID).AllCols().Update(m)
  185. return err
  186. }
  187. // UpdateMilestone updates information of given milestone.
  188. func UpdateMilestone(m *Milestone) error {
  189. return updateMilestone(x, m)
  190. }
  191. func countRepoMilestones(e Engine, repoID int64) (int64, error) {
  192. return e.
  193. Where("repo_id=?", repoID).
  194. Count(new(Milestone))
  195. }
  196. func countRepoClosedMilestones(e Engine, repoID int64) (int64, error) {
  197. return e.
  198. Where("repo_id=? AND is_closed=?", repoID, true).
  199. Count(new(Milestone))
  200. }
  201. // CountRepoClosedMilestones returns number of closed milestones in given repository.
  202. func CountRepoClosedMilestones(repoID int64) (int64, error) {
  203. return countRepoClosedMilestones(x, repoID)
  204. }
  205. // MilestoneStats returns number of open and closed milestones of given repository.
  206. func MilestoneStats(repoID int64) (open int64, closed int64, err error) {
  207. open, err = x.
  208. Where("repo_id=? AND is_closed=?", repoID, false).
  209. Count(new(Milestone))
  210. if err != nil {
  211. return 0, 0, nil
  212. }
  213. closed, err = CountRepoClosedMilestones(repoID)
  214. return open, closed, err
  215. }
  216. // ChangeMilestoneStatus changes the milestone open/closed status.
  217. func ChangeMilestoneStatus(m *Milestone, isClosed bool) (err error) {
  218. repo, err := GetRepositoryByID(m.RepoID)
  219. if err != nil {
  220. return err
  221. }
  222. sess := x.NewSession()
  223. defer sess.Close()
  224. if err = sess.Begin(); err != nil {
  225. return err
  226. }
  227. m.IsClosed = isClosed
  228. if err = updateMilestone(sess, m); err != nil {
  229. return err
  230. }
  231. numMilestones, err := countRepoMilestones(sess, repo.ID)
  232. if err != nil {
  233. return err
  234. }
  235. numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID)
  236. if err != nil {
  237. return err
  238. }
  239. repo.NumMilestones = int(numMilestones)
  240. repo.NumClosedMilestones = int(numClosedMilestones)
  241. if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
  242. return err
  243. }
  244. return sess.Commit()
  245. }
  246. func changeMilestoneIssueStats(e *xorm.Session, issue *Issue) error {
  247. if issue.MilestoneID == 0 {
  248. return nil
  249. }
  250. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  251. if err != nil {
  252. return err
  253. }
  254. if issue.IsClosed {
  255. m.NumOpenIssues--
  256. m.NumClosedIssues++
  257. } else {
  258. m.NumOpenIssues++
  259. m.NumClosedIssues--
  260. }
  261. return updateMilestone(e, m)
  262. }
  263. func changeMilestoneAssign(e *xorm.Session, doer *User, issue *Issue, oldMilestoneID int64) error {
  264. if oldMilestoneID > 0 {
  265. m, err := getMilestoneByRepoID(e, issue.RepoID, oldMilestoneID)
  266. if err != nil {
  267. return err
  268. }
  269. m.NumIssues--
  270. if issue.IsClosed {
  271. m.NumClosedIssues--
  272. }
  273. if err = updateMilestone(e, m); err != nil {
  274. return err
  275. }
  276. }
  277. if issue.MilestoneID > 0 {
  278. m, err := getMilestoneByRepoID(e, issue.RepoID, issue.MilestoneID)
  279. if err != nil {
  280. return err
  281. }
  282. m.NumIssues++
  283. if issue.IsClosed {
  284. m.NumClosedIssues++
  285. }
  286. if err = updateMilestone(e, m); err != nil {
  287. return err
  288. }
  289. }
  290. if err := issue.loadRepo(e); err != nil {
  291. return err
  292. }
  293. if oldMilestoneID > 0 || issue.MilestoneID > 0 {
  294. if _, err := createMilestoneComment(e, doer, issue.Repo, issue, oldMilestoneID, issue.MilestoneID); err != nil {
  295. return err
  296. }
  297. }
  298. return updateIssueCols(e, issue, "milestone_id")
  299. }
  300. // ChangeMilestoneAssign changes assignment of milestone for issue.
  301. func ChangeMilestoneAssign(issue *Issue, doer *User, oldMilestoneID int64) (err error) {
  302. sess := x.NewSession()
  303. defer sess.Close()
  304. if err = sess.Begin(); err != nil {
  305. return err
  306. }
  307. if err = changeMilestoneAssign(sess, doer, issue, oldMilestoneID); err != nil {
  308. return err
  309. }
  310. if err = sess.Commit(); err != nil {
  311. return fmt.Errorf("Commit: %v", err)
  312. }
  313. var hookAction api.HookIssueAction
  314. if issue.MilestoneID > 0 {
  315. hookAction = api.HookIssueMilestoned
  316. } else {
  317. hookAction = api.HookIssueDemilestoned
  318. }
  319. if err = issue.LoadAttributes(); err != nil {
  320. return err
  321. }
  322. mode, _ := AccessLevel(doer.ID, issue.Repo)
  323. if issue.IsPull {
  324. err = issue.PullRequest.LoadIssue()
  325. if err != nil {
  326. log.Error(2, "LoadIssue: %v", err)
  327. return
  328. }
  329. err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
  330. Action: hookAction,
  331. Index: issue.Index,
  332. PullRequest: issue.PullRequest.APIFormat(),
  333. Repository: issue.Repo.APIFormat(mode),
  334. Sender: doer.APIFormat(),
  335. })
  336. } else {
  337. err = PrepareWebhooks(issue.Repo, HookEventIssues, &api.IssuePayload{
  338. Action: hookAction,
  339. Index: issue.Index,
  340. Issue: issue.APIFormat(),
  341. Repository: issue.Repo.APIFormat(mode),
  342. Sender: doer.APIFormat(),
  343. })
  344. }
  345. if err != nil {
  346. log.Error(2, "PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
  347. } else {
  348. go HookQueue.Add(issue.RepoID)
  349. }
  350. return nil
  351. }
  352. // DeleteMilestoneByRepoID deletes a milestone from a repository.
  353. func DeleteMilestoneByRepoID(repoID, id int64) error {
  354. m, err := GetMilestoneByRepoID(repoID, id)
  355. if err != nil {
  356. if IsErrMilestoneNotExist(err) {
  357. return nil
  358. }
  359. return err
  360. }
  361. repo, err := GetRepositoryByID(m.RepoID)
  362. if err != nil {
  363. return err
  364. }
  365. sess := x.NewSession()
  366. defer sess.Close()
  367. if err = sess.Begin(); err != nil {
  368. return err
  369. }
  370. if _, err = sess.ID(m.ID).Delete(new(Milestone)); err != nil {
  371. return err
  372. }
  373. numMilestones, err := countRepoMilestones(sess, repo.ID)
  374. if err != nil {
  375. return err
  376. }
  377. numClosedMilestones, err := countRepoClosedMilestones(sess, repo.ID)
  378. if err != nil {
  379. return err
  380. }
  381. repo.NumMilestones = int(numMilestones)
  382. repo.NumClosedMilestones = int(numClosedMilestones)
  383. if _, err = sess.ID(repo.ID).Cols("num_milestones, num_closed_milestones").Update(repo); err != nil {
  384. return err
  385. }
  386. if _, err = sess.Exec("UPDATE `issue` SET milestone_id = 0 WHERE milestone_id = ?", m.ID); err != nil {
  387. return err
  388. }
  389. return sess.Commit()
  390. }