misc.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright 2023 Girish M
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package main
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "strconv"
  21. "strings"
  22. "time"
  23. "github.com/senseyeio/duration"
  24. "github.com/mattermost/mattermost/server/public/model"
  25. "github.com/mattermost/mattermost/server/public/plugin"
  26. )
  27. func getProjectOptAttachmentJSON(pluginURL string, action string, options []Option) []byte {
  28. attachments := OptAttachments{Attachments: []Attachment{
  29. {
  30. Actions: []Action{
  31. {
  32. Name: "Type to search for a project...",
  33. Integration: Integration{
  34. URL: pluginURL + "/projSel",
  35. Context: Context{
  36. Action: action,
  37. },
  38. },
  39. Type: "select",
  40. Options: options,
  41. },
  42. {
  43. Name: "Cancel Project search",
  44. Integration: Integration{
  45. URL: pluginURL + "/bye",
  46. },
  47. },
  48. },
  49. },
  50. },
  51. }
  52. attachmentsJSON, _ := json.Marshal(attachments)
  53. return attachmentsJSON
  54. }
  55. func getWPOptAttachmentJSON(pluginURL string, action string, options []Option) []byte {
  56. attachments := OptAttachments{Attachments: []Attachment{
  57. {
  58. Actions: []Action{
  59. {
  60. Name: "Type to search for a work package...",
  61. Integration: Integration{
  62. URL: pluginURL + "/wpSel",
  63. Context: Context{
  64. Action: action,
  65. },
  66. },
  67. Type: "select",
  68. Options: options,
  69. },
  70. {
  71. Name: "Cancel WP search",
  72. Integration: Integration{
  73. URL: pluginURL + "/createTimeLog",
  74. },
  75. },
  76. },
  77. },
  78. },
  79. }
  80. attachmentsJSON, _ := json.Marshal(attachments)
  81. return attachmentsJSON
  82. }
  83. func getWPOptJSON(pluginURL string, action string, options []Option) []byte {
  84. attachments := OptAttachments{Attachments: []Attachment{
  85. {
  86. Actions: []Action{
  87. {
  88. Name: "Type to search for a work package...",
  89. Integration: Integration{
  90. URL: pluginURL + "/delWP",
  91. Context: Context{
  92. Action: action,
  93. },
  94. },
  95. Type: "select",
  96. Options: options,
  97. },
  98. {
  99. Name: "Cancel search",
  100. Integration: Integration{
  101. URL: pluginURL + "/bye",
  102. },
  103. },
  104. },
  105. },
  106. },
  107. }
  108. attachmentsJSON, _ := json.Marshal(attachments)
  109. return attachmentsJSON
  110. }
  111. func getTimeLogOptJSON(pluginURL string, action string, options []Option) []byte {
  112. attachments := OptAttachments{Attachments: []Attachment{
  113. {
  114. Actions: []Action{
  115. {
  116. Name: "Type to search for a time log...",
  117. Integration: Integration{
  118. URL: pluginURL + "/delTimeLog",
  119. Context: Context{
  120. Action: action,
  121. },
  122. },
  123. Type: "select",
  124. Options: options,
  125. },
  126. {
  127. Name: "Cancel search",
  128. Integration: Integration{
  129. URL: pluginURL + "/bye",
  130. },
  131. },
  132. },
  133. },
  134. },
  135. }
  136. attachmentsJSON, _ := json.Marshal(attachments)
  137. return attachmentsJSON
  138. }
  139. func getCnfDelBtnJSON(url string, action string) []byte {
  140. attachments := OptAttachments{Attachments: []Attachment{
  141. {
  142. Actions: []Action{
  143. {
  144. Name: "Yes, Delete!",
  145. Integration: Integration{
  146. URL: url,
  147. Context: Context{
  148. Action: action,
  149. },
  150. },
  151. },
  152. {
  153. Name: "No, go back.",
  154. Integration: Integration{
  155. URL: url,
  156. Context: Context{
  157. Action: "",
  158. },
  159. },
  160. },
  161. },
  162. },
  163. },
  164. }
  165. attachmentsJSON, _ := json.Marshal(attachments)
  166. return attachmentsJSON
  167. }
  168. func getTimeLogDelMsgJSON(pluginURL string) []byte {
  169. attachments := OptAttachments{Attachments: []Attachment{
  170. {
  171. Actions: []Action{
  172. {
  173. Name: "View time logs",
  174. Integration: Integration{
  175. URL: pluginURL + "/getTimeLog",
  176. Context: Context{
  177. Action: "getTimeLog",
  178. },
  179. },
  180. },
  181. },
  182. },
  183. }}
  184. attachmentsJSON, _ := json.Marshal(attachments)
  185. return attachmentsJSON
  186. }
  187. func getCreatePostMsg(userID string, channelID string, msg string) *model.Post {
  188. var post = &model.Post{
  189. UserId: userID,
  190. ChannelId: channelID,
  191. Message: msg,
  192. }
  193. return post
  194. }
  195. func getOptArrayForProjectElements(elements []Element) []Option {
  196. var options []Option
  197. for _, element := range elements {
  198. id := strconv.Itoa(element.ID)
  199. options = append(options, Option{
  200. Text: element.Name,
  201. Value: "opt" + id,
  202. })
  203. }
  204. return options
  205. }
  206. func getOptArrayForWPElements(elements []Element) []Option {
  207. var options []Option
  208. for _, element := range elements {
  209. id := strconv.Itoa(element.ID)
  210. options = append(options, Option{
  211. Text: element.Subject,
  212. Value: element.Subject + "|:-" + id,
  213. })
  214. }
  215. return options
  216. }
  217. func getOptArrayForTimeLogElements(elements []TimeElement) []Option {
  218. var options []Option
  219. for _, element := range elements {
  220. id := strconv.Itoa(element.ID)
  221. text := element.Comment.Raw + "-"
  222. text += element.SpentOn + "-"
  223. d, _ := duration.ParseISO8601(element.Hours)
  224. loggedTime := convDurationToHoursMin(d)
  225. text += loggedTime + "-"
  226. text += element.Links.WorkPackage.Title + "-"
  227. text += element.Links.Activity.Title + "-"
  228. text += element.Links.Project.Title
  229. options = append(options, Option{
  230. Text: text,
  231. Value: text + "|:-" + id,
  232. })
  233. }
  234. return options
  235. }
  236. func getOptArrayForAllowedValues(allowedValues []AllowedValues) []*model.PostActionOptions {
  237. var postActionOptions []*model.PostActionOptions
  238. for _, value := range allowedValues {
  239. id := strconv.Itoa(value.ID)
  240. postActionOptions = append(postActionOptions, &model.PostActionOptions{
  241. Text: value.Name,
  242. Value: "opt" + id,
  243. })
  244. }
  245. return postActionOptions
  246. }
  247. func getOptArrayForTypes(types []TypeElement) []*model.PostActionOptions {
  248. var postActionOptions []*model.PostActionOptions
  249. for _, value := range types {
  250. id := strconv.Itoa(value.ID)
  251. postActionOptions = append(postActionOptions, &model.PostActionOptions{
  252. Text: value.Name,
  253. Value: "opt" + id,
  254. })
  255. }
  256. return postActionOptions
  257. }
  258. func getOptArrayForAvailableAssignees(assignees []AvailableAssigneesElement) []*model.PostActionOptions {
  259. var postActionOptions []*model.PostActionOptions
  260. for _, value := range assignees {
  261. id := strconv.Itoa(value.ID)
  262. name := value.FirstName + " " + value.LastName
  263. postActionOptions = append(postActionOptions, &model.PostActionOptions{
  264. Text: name,
  265. Value: name + "|:-" + id,
  266. })
  267. }
  268. return postActionOptions
  269. }
  270. func GetAttachmentJSON(pluginURL string) string {
  271. return `{
  272. "attachments": [
  273. {
  274. "text": "What would you like me to do?",
  275. "actions": [
  276. {
  277. "name": "Log time",
  278. "integration": {
  279. "url": "` + pluginURL + `/createTimeLog",
  280. "context": {
  281. "action": "showSelWP"
  282. }
  283. }
  284. },
  285. {
  286. "name": "Create Work Package",
  287. "integration": {
  288. "url": "` + pluginURL + `/createWP",
  289. "context": {
  290. "action": "createWP"
  291. }
  292. }
  293. },
  294. {
  295. "name": "View time logs",
  296. "integration": {
  297. "url": "` + pluginURL + `/getTimeLog",
  298. "context": {
  299. "action": "getTimeLog"
  300. }
  301. }
  302. },
  303. {
  304. "name": "Delete time log",
  305. "integration": {
  306. "url": "` + pluginURL + `/delTimeLog",
  307. "context": {
  308. "action": "delTimeLog"
  309. }
  310. }
  311. },
  312. {
  313. "name": "Delete Work Package",
  314. "integration": {
  315. "url": "` + pluginURL + `/delWP",
  316. "context": {
  317. "action": ""
  318. }
  319. }
  320. },
  321. {
  322. "name": "Subscribe to notifications",
  323. "integration": {
  324. "url": "` + pluginURL + `/subscribe",
  325. "context": {
  326. "action": ""
  327. }
  328. }
  329. },
  330. {
  331. "name": "Bye :wave:",
  332. "integration": {
  333. "url": "` + pluginURL + `/bye",
  334. "context": {
  335. "action": "bye"
  336. }
  337. }
  338. }
  339. ]
  340. }
  341. ]
  342. }`
  343. }
  344. func GetTimeEntriesBodyJSON(submission map[string]interface{}, loggedHours string, billableHours string) ([]byte, error) {
  345. var timeEntriesBody TimeEntryPostBody
  346. timeEntriesBody.Links.Project.Href = apiVersionStr + "projects/" + projectID
  347. timeEntriesBody.Links.WorkPackage.Href = apiVersionStr + "work_packages/" + timeLogID
  348. if submission["activity"] != nil {
  349. activityID = strings.Split(submission["activity"].(string), "opt")[1]
  350. timeEntriesBody.Links.Activity.Href = apiVersionStr + "time_entries/activities/" + activityID
  351. }
  352. timeEntriesBody.SpentOn = submission["spent_on"].(string)
  353. timeEntriesBody.Comment.Raw = submission["comments"].(string)
  354. spentHoursFloat, _ := strconv.ParseFloat(loggedHours, 64)
  355. loggedHoursDuration := time.Duration(spentHoursFloat*3600) * time.Second
  356. timeEntriesBody.Hours = fmt.Sprintf("PT%fH", loggedHoursDuration.Hours())
  357. timeEntriesBody.CustomField = billableHours
  358. return json.Marshal(timeEntriesBody)
  359. }
  360. func GetWPBodyJSON(submission map[string]interface{}) ([]byte, error) {
  361. var workPackagePostBody WorkPackagePostBody
  362. workPackagePostBody.Links.Project.Href = apiVersionStr + "projects/" + projectID
  363. typeID = strings.Split(submission["type"].(string), "opt")[1]
  364. workPackagePostBody.Links.Type.Href = apiVersionStr + "types/" + typeID
  365. workPackagePostBody.Subject = submission["subject"].(string)
  366. assignee := strings.Split(submission["assignee"].(string), "|:-")
  367. if submission["assignee"] != nil {
  368. workPackagePostBody.Assignee.Href = apiVersionStr + "users/" + assignee[1]
  369. workPackagePostBody.Assignee.Title = assignee[0]
  370. }
  371. return json.Marshal(workPackagePostBody)
  372. }
  373. func getUpdatePostMsg(userID string, channelID string, msg string) *model.Post {
  374. var post = &model.Post{
  375. Id: menuPost.Id,
  376. UserId: userID,
  377. ChannelId: channelID,
  378. Message: msg,
  379. }
  380. return post
  381. }
  382. func setOPStr(p plugin.MattermostPlugin) {
  383. opURL, _ := p.API.KVGet("opURL")
  384. apiKey, _ := p.API.KVGet("apiKey")
  385. OpURLStr = string(opURL)
  386. APIKeyStr = string(apiKey)
  387. p.API.LogInfo("opURLStr: " + OpURLStr + " apiKeyStr: " + APIKeyStr)
  388. }
  389. func checkDate(dateStr string) bool {
  390. layout := "2006-01-02"
  391. date, err := time.Parse(layout, dateStr)
  392. if err != nil {
  393. return false
  394. }
  395. currentDate := time.Now()
  396. oneYearAgo := currentDate.AddDate(-1, 0, 0)
  397. if date.After(oneYearAgo) && date.Before(currentDate) {
  398. return true
  399. }
  400. return false
  401. }
  402. func checkHours(billableHours string, hoursLogged string) bool {
  403. hoursLoggedFloat, _ := strconv.ParseFloat(hoursLogged, 64)
  404. billableHoursFloat, _ := strconv.ParseFloat(billableHours, 64)
  405. return billableHoursFloat <= hoursLoggedFloat
  406. }
  407. func convDurationToHoursMin(d duration.Duration) string {
  408. var loggedTime = ""
  409. if d.TH != 0 {
  410. hours := strconv.Itoa(d.TH)
  411. if d.TH > 1 {
  412. loggedTime = hours + " hours "
  413. } else {
  414. loggedTime = hours + " hour "
  415. }
  416. }
  417. if d.TM != 0 {
  418. minutes := strconv.Itoa(d.TM)
  419. if d.TM > 1 {
  420. loggedTime = loggedTime + minutes + " minutes"
  421. } else {
  422. loggedTime = loggedTime + minutes + " minute"
  423. }
  424. }
  425. return loggedTime
  426. }
  427. func convHoursToHoursMin(hours float64) string {
  428. seconds := int(hours * 3600)
  429. minutes := (seconds / 60) % 60
  430. hour := int(hours)
  431. if hour > 0 {
  432. return strconv.Itoa(hour) + " hours " + strconv.Itoa(minutes) + " minutes"
  433. }
  434. if minutes > 1 {
  435. return strconv.Itoa(minutes) + " minutes"
  436. }
  437. return strconv.Itoa(minutes) + " minute"
  438. }