123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- /*
- * Copyright 2023 Girish M
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package main
- import (
- "encoding/json"
- "fmt"
- "strconv"
- "strings"
- "time"
- "github.com/senseyeio/duration"
- "github.com/mattermost/mattermost/server/public/model"
- "github.com/mattermost/mattermost/server/public/plugin"
- )
- func getProjectOptAttachmentJSON(pluginURL string, action string, options []Option) []byte {
- attachments := OptAttachments{Attachments: []Attachment{
- {
- Actions: []Action{
- {
- Name: "Type to search for a project...",
- Integration: Integration{
- URL: pluginURL + "/projSel",
- Context: Context{
- Action: action,
- },
- },
- Type: "select",
- Options: options,
- },
- {
- Name: "Cancel Project search",
- Integration: Integration{
- URL: pluginURL + "/bye",
- },
- },
- },
- },
- },
- }
- attachmentsJSON, _ := json.Marshal(attachments)
- return attachmentsJSON
- }
- func getWPOptAttachmentJSON(pluginURL string, action string, options []Option) []byte {
- attachments := OptAttachments{Attachments: []Attachment{
- {
- Actions: []Action{
- {
- Name: "Type to search for a work package...",
- Integration: Integration{
- URL: pluginURL + "/wpSel",
- Context: Context{
- Action: action,
- },
- },
- Type: "select",
- Options: options,
- },
- {
- Name: "Cancel WP search",
- Integration: Integration{
- URL: pluginURL + "/createTimeLog",
- },
- },
- },
- },
- },
- }
- attachmentsJSON, _ := json.Marshal(attachments)
- return attachmentsJSON
- }
- func getWPOptJSON(pluginURL string, action string, options []Option) []byte {
- attachments := OptAttachments{Attachments: []Attachment{
- {
- Actions: []Action{
- {
- Name: "Type to search for a work package...",
- Integration: Integration{
- URL: pluginURL + "/delWP",
- Context: Context{
- Action: action,
- },
- },
- Type: "select",
- Options: options,
- },
- {
- Name: "Cancel search",
- Integration: Integration{
- URL: pluginURL + "/bye",
- },
- },
- },
- },
- },
- }
- attachmentsJSON, _ := json.Marshal(attachments)
- return attachmentsJSON
- }
- func getTimeLogOptJSON(pluginURL string, action string, options []Option) []byte {
- attachments := OptAttachments{Attachments: []Attachment{
- {
- Actions: []Action{
- {
- Name: "Type to search for a time log...",
- Integration: Integration{
- URL: pluginURL + "/delTimeLog",
- Context: Context{
- Action: action,
- },
- },
- Type: "select",
- Options: options,
- },
- {
- Name: "Cancel search",
- Integration: Integration{
- URL: pluginURL + "/bye",
- },
- },
- },
- },
- },
- }
- attachmentsJSON, _ := json.Marshal(attachments)
- return attachmentsJSON
- }
- func getCnfDelBtnJSON(url string, action string) []byte {
- attachments := OptAttachments{Attachments: []Attachment{
- {
- Actions: []Action{
- {
- Name: "Yes, Delete!",
- Integration: Integration{
- URL: url,
- Context: Context{
- Action: action,
- },
- },
- },
- {
- Name: "No, go back.",
- Integration: Integration{
- URL: url,
- Context: Context{
- Action: "",
- },
- },
- },
- },
- },
- },
- }
- attachmentsJSON, _ := json.Marshal(attachments)
- return attachmentsJSON
- }
- func getTimeLogDelMsgJSON(pluginURL string) []byte {
- attachments := OptAttachments{Attachments: []Attachment{
- {
- Actions: []Action{
- {
- Name: "View time logs",
- Integration: Integration{
- URL: pluginURL + "/getTimeLog",
- Context: Context{
- Action: "getTimeLog",
- },
- },
- },
- },
- },
- }}
- attachmentsJSON, _ := json.Marshal(attachments)
- return attachmentsJSON
- }
- func getCreatePostMsg(userID string, channelID string, msg string) *model.Post {
- var post = &model.Post{
- UserId: userID,
- ChannelId: channelID,
- Message: msg,
- }
- return post
- }
- func getOptArrayForProjectElements(elements []Element) []Option {
- var options []Option
- for _, element := range elements {
- id := strconv.Itoa(element.ID)
- options = append(options, Option{
- Text: element.Name,
- Value: "opt" + id,
- })
- }
- return options
- }
- func getOptArrayForWPElements(elements []Element) []Option {
- var options []Option
- for _, element := range elements {
- id := strconv.Itoa(element.ID)
- options = append(options, Option{
- Text: element.Subject,
- Value: element.Subject + "|:-" + id,
- })
- }
- return options
- }
- func getOptArrayForTimeLogElements(elements []TimeElement) []Option {
- var options []Option
- for _, element := range elements {
- id := strconv.Itoa(element.ID)
- text := element.Comment.Raw + "-"
- text += element.SpentOn + "-"
- d, _ := duration.ParseISO8601(element.Hours)
- loggedTime := convDurationToHoursMin(d)
- text += loggedTime + "-"
- text += element.Links.WorkPackage.Title + "-"
- text += element.Links.Activity.Title + "-"
- text += element.Links.Project.Title
- options = append(options, Option{
- Text: text,
- Value: text + "|:-" + id,
- })
- }
- return options
- }
- func getOptArrayForAllowedValues(allowedValues []AllowedValues) []*model.PostActionOptions {
- var postActionOptions []*model.PostActionOptions
- for _, value := range allowedValues {
- id := strconv.Itoa(value.ID)
- postActionOptions = append(postActionOptions, &model.PostActionOptions{
- Text: value.Name,
- Value: "opt" + id,
- })
- }
- return postActionOptions
- }
- func getOptArrayForTypes(types []TypeElement) []*model.PostActionOptions {
- var postActionOptions []*model.PostActionOptions
- for _, value := range types {
- id := strconv.Itoa(value.ID)
- postActionOptions = append(postActionOptions, &model.PostActionOptions{
- Text: value.Name,
- Value: "opt" + id,
- })
- }
- return postActionOptions
- }
- func getOptArrayForAvailableAssignees(assignees []AvailableAssigneesElement) []*model.PostActionOptions {
- var postActionOptions []*model.PostActionOptions
- for _, value := range assignees {
- id := strconv.Itoa(value.ID)
- name := value.FirstName + " " + value.LastName
- postActionOptions = append(postActionOptions, &model.PostActionOptions{
- Text: name,
- Value: name + "|:-" + id,
- })
- }
- return postActionOptions
- }
- func GetAttachmentJSON(pluginURL string) string {
- return `{
- "attachments": [
- {
- "text": "What would you like me to do?",
- "actions": [
- {
- "name": "Log time",
- "integration": {
- "url": "` + pluginURL + `/createTimeLog",
- "context": {
- "action": "showSelWP"
- }
- }
- },
- {
- "name": "Create Work Package",
- "integration": {
- "url": "` + pluginURL + `/createWP",
- "context": {
- "action": "createWP"
- }
- }
- },
- {
- "name": "View time logs",
- "integration": {
- "url": "` + pluginURL + `/getTimeLog",
- "context": {
- "action": "getTimeLog"
- }
- }
- },
- {
- "name": "Delete time log",
- "integration": {
- "url": "` + pluginURL + `/delTimeLog",
- "context": {
- "action": "delTimeLog"
- }
- }
- },
- {
- "name": "Delete Work Package",
- "integration": {
- "url": "` + pluginURL + `/delWP",
- "context": {
- "action": ""
- }
- }
- },
- {
- "name": "Subscribe to notifications",
- "integration": {
- "url": "` + pluginURL + `/subscribe",
- "context": {
- "action": ""
- }
- }
- },
- {
- "name": "Bye :wave:",
- "integration": {
- "url": "` + pluginURL + `/bye",
- "context": {
- "action": "bye"
- }
- }
- }
- ]
- }
- ]
- }`
- }
- func GetTimeEntriesBodyJSON(submission map[string]interface{}, loggedHours string, billableHours string) ([]byte, error) {
- var timeEntriesBody TimeEntryPostBody
- timeEntriesBody.Links.Project.Href = apiVersionStr + "projects/" + projectID
- timeEntriesBody.Links.WorkPackage.Href = apiVersionStr + "work_packages/" + timeLogID
- if submission["activity"] != nil {
- activityID = strings.Split(submission["activity"].(string), "opt")[1]
- timeEntriesBody.Links.Activity.Href = apiVersionStr + "time_entries/activities/" + activityID
- }
- timeEntriesBody.SpentOn = submission["spent_on"].(string)
- timeEntriesBody.Comment.Raw = submission["comments"].(string)
- spentHoursFloat, _ := strconv.ParseFloat(loggedHours, 64)
- loggedHoursDuration := time.Duration(spentHoursFloat*3600) * time.Second
- timeEntriesBody.Hours = fmt.Sprintf("PT%fH", loggedHoursDuration.Hours())
- timeEntriesBody.CustomField = billableHours
- return json.Marshal(timeEntriesBody)
- }
- func GetWPBodyJSON(submission map[string]interface{}) ([]byte, error) {
- var workPackagePostBody WorkPackagePostBody
- workPackagePostBody.Links.Project.Href = apiVersionStr + "projects/" + projectID
- typeID = strings.Split(submission["type"].(string), "opt")[1]
- workPackagePostBody.Links.Type.Href = apiVersionStr + "types/" + typeID
- workPackagePostBody.Subject = submission["subject"].(string)
- assignee := strings.Split(submission["assignee"].(string), "|:-")
- if submission["assignee"] != nil {
- workPackagePostBody.Assignee.Href = apiVersionStr + "users/" + assignee[1]
- workPackagePostBody.Assignee.Title = assignee[0]
- }
- return json.Marshal(workPackagePostBody)
- }
- func getUpdatePostMsg(userID string, channelID string, msg string) *model.Post {
- var post = &model.Post{
- Id: menuPost.Id,
- UserId: userID,
- ChannelId: channelID,
- Message: msg,
- }
- return post
- }
- func setOPStr(p plugin.MattermostPlugin) {
- opURL, _ := p.API.KVGet("opURL")
- apiKey, _ := p.API.KVGet("apiKey")
- OpURLStr = string(opURL)
- APIKeyStr = string(apiKey)
- p.API.LogInfo("opURLStr: " + OpURLStr + " apiKeyStr: " + APIKeyStr)
- }
- func checkDate(dateStr string) bool {
- layout := "2006-01-02"
- date, err := time.Parse(layout, dateStr)
- if err != nil {
- return false
- }
- currentDate := time.Now()
- oneYearAgo := currentDate.AddDate(-1, 0, 0)
- if date.After(oneYearAgo) && date.Before(currentDate) {
- return true
- }
- return false
- }
- func checkHours(billableHours string, hoursLogged string) bool {
- hoursLoggedFloat, _ := strconv.ParseFloat(hoursLogged, 64)
- billableHoursFloat, _ := strconv.ParseFloat(billableHours, 64)
- return billableHoursFloat <= hoursLoggedFloat
- }
- func convDurationToHoursMin(d duration.Duration) string {
- var loggedTime = ""
- if d.TH != 0 {
- hours := strconv.Itoa(d.TH)
- if d.TH > 1 {
- loggedTime = hours + " hours "
- } else {
- loggedTime = hours + " hour "
- }
- }
- if d.TM != 0 {
- minutes := strconv.Itoa(d.TM)
- if d.TM > 1 {
- loggedTime = loggedTime + minutes + " minutes"
- } else {
- loggedTime = loggedTime + minutes + " minute"
- }
- }
- return loggedTime
- }
- func convHoursToHoursMin(hours float64) string {
- seconds := int(hours * 3600)
- minutes := (seconds / 60) % 60
- hour := int(hours)
- if hour > 0 {
- return strconv.Itoa(hour) + " hours " + strconv.Itoa(minutes) + " minutes"
- }
- if minutes > 1 {
- return strconv.Itoa(minutes) + " minutes"
- }
- return strconv.Itoa(minutes) + " minute"
- }
|