func Tasks(params url.Values, withCompleted bool, withProject bool) []Task_t { params.Add("opt_fields", "name,completed,due_on,assignee.name,projects.name,tags.name") var tasks Tasks_t if withProject { uri := "/api/1.0/projects/" + strconv.Itoa(config.Load().Project) + "/tasks" err := json.Unmarshal(Get(uri, params), &tasks) utils.Check(err) } else { params.Add("workspace", strconv.Itoa(config.Load().Workspace)) params.Add("assignee", "me") params.Add("limit", "100") err := json.Unmarshal(Get("/api/1.0/tasks", params), &tasks) utils.Check(err) } var tasks_with_due []Task_t for _, t := range tasks.Data { if !withCompleted && t.Completed { continue } if strings.HasSuffix(t.Name, ":") { continue } if HasTagsBlacklisted(t) { continue } tasks_with_due = append(tasks_with_due, t) } return tasks_with_due }
func Task(taskId string, verbose bool) (Task_t, []Story_t) { var ( err error t map[string]Task_t ss map[string][]Story_t stories []Story_t ) task_chan, stories_chan := make(chan []byte), make(chan []byte) go func() { task_chan <- Get("/api/1.0/tasks/"+taskId, nil) }() stories = nil if verbose { go func() { stories_chan <- Get("/api/1.0/tasks/"+taskId+"/stories", nil) }() err = json.Unmarshal(<-stories_chan, &ss) utils.Check(err) stories = ss["data"] } err = json.Unmarshal(<-task_chan, &t) utils.Check(err) return t["data"], stories }
func FindUserId(name string) string { var users map[string][]User_t uri := "/api/1.0/workspaces/" + strconv.Itoa(config.Load().Workspace) + "/typeahead?type=user&query=" + name err := json.Unmarshal(Get(uri, nil), &users) utils.Check(err) return strconv.Itoa(users["data"][0].Id) }
func Projects() []Project_t { var projects map[string][]Project_t uri := "/api/1.0/workspaces/" + strconv.Itoa(config.Load().Workspace) + "/projects" err := json.Unmarshal(Get(uri, nil), &projects) utils.Check(err) return projects["data"] }
func CreateTask(withProject bool, name string) Task_t { workspace := strconv.Itoa(config.Load().Workspace) data := `{ "data" : { "assignee": "me", "workspace": ` + workspace + `, "name": "` + name + `" } }` respBody := Post("/tasks", data) var output map[string]Task_t err := json.Unmarshal(respBody, &output) utils.Check(err) newTask := output["data"] if withProject { p := strconv.Itoa(config.Load().Project) t := strconv.Itoa(newTask.Id) uri := "/tasks/" + t + "/addProject" dd := `{ "data": { "project": ` + p + ` } }` Post(uri, dd) } return newTask }
func FindTaskId(index string, autoFirst bool, withProject bool) string { if index == "" { if autoFirst == false { log.Fatal("fatal: Task index is required.") } else { index = "0" } } var id string txt, err := ioutil.ReadFile(utils.CacheFile()) if err != nil { // cache file not exist ind, parseErr := strconv.Atoi(index) utils.Check(parseErr) task := Tasks(url.Values{}, false, withProject)[ind] id = strconv.Itoa(task.Id) } else { lines := regexp.MustCompile("\n").Split(string(txt), -1) for i, line := range lines { if index == strconv.Itoa(i) { line = regexp.MustCompile("^[0-9]*:").ReplaceAllString(line, "") // remove index id = regexp.MustCompile("^[0-9]*").FindString(line) } } } return id }
func Tags() Tags_t { var tags Tags_t uri := "/api/1.0/workspaces/" + strconv.Itoa(config.Load().Workspace) + "/tags" err := json.Unmarshal(Get(uri, nil), &tags) utils.Check(err) return tags }
func Update(taskId string, key string, value string) Task_t { respBody := Put("/tasks/"+taskId, `{"data":{"`+key+`":"`+value+`"}}`) var output map[string]Task_t err := json.Unmarshal(respBody, &output) utils.Check(err) return output["data"] }
func Comment(c *cli.Context, withProject bool) { taskId := api.FindTaskId(c.Args().First(), false, withProject) task, stories := api.Task(taskId, true) tmpFile := os.TempDir() + "/asana_comment.txt" f, err := os.Create(tmpFile) utils.Check(err) defer f.Close() err = template(f, task, stories) utils.Check(err) cmd := exec.Command(os.Getenv("EDITOR"), tmpFile) cmd.Stdin, cmd.Stdout = os.Stdin, os.Stdout err = cmd.Run() txt, err := ioutil.ReadFile(tmpFile) utils.Check(err) isForClose := getIsForClose(string(txt)) asignee := getAsignee(string(txt)) postComment := trim(string(txt)) if postComment != "" { commented := api.CommentTo(taskId, postComment) fmt.Println("Commented on Task: \"" + task.Name + "\"\n") fmt.Println(commented) if isForClose { result := api.Update(taskId, "completed", "true") fmt.Println("Task closed \"" + task.Name + "\"\n") fmt.Println(result) } if asignee != "" { result := api.Update(taskId, "assignee", api.FindUserId(strings.Replace(asignee, "@", "", -1))) fmt.Println("New asignee \"" + asignee + "\"\n") fmt.Println(result) } } else { fmt.Println("Aborting comment due to empty content.") } }
func fire(req *http.Request) []byte { client := &http.Client{} req.Header.Set("User-Agent", UserAgent) req.SetBasicAuth(config.Load().Api_key, "") resp, err := client.Do(req) utils.Check(err) body, err := ioutil.ReadAll(resp.Body) utils.Check(err) if resp.StatusCode >= 300 { println(resp.Status) } return body }
func CommentTo(taskId string, comment string) string { respBody := Post("/tasks/"+taskId+"/stories", `{"data":{"text":"`+comment+`"}}`) var output map[string]Commented_t err := json.Unmarshal(respBody, &output) utils.Check(err) return output["data"].Text }
func Load() Conf { var dat []byte var err error dat, err = ioutil.ReadFile(utils.Home() + "/.asana.yml") if err != nil { fmt.Println("Config file isn't set.\n ==> $ asana config") os.Exit(1) } conf := Conf{} err = yaml.Unmarshal(dat, &conf) utils.Check(err) return conf }
func CreateTag(tagname string) Tag_t { workspace := strconv.Itoa(config.Load().Workspace) data := `{ "data": { "workspace": ` + workspace + `, "name": "` + tagname + `" } }` respBody := Post("/tags", data) var output map[string]Tag_t err := json.Unmarshal(respBody, &output) utils.Check(err) newTag := output["data"] return newTag }
func Me() Me_t { var me map[string]Me_t err := json.Unmarshal(Get("/api/1.0/users/me", nil), &me) utils.Check(err) return me["data"] }
func Put(path string, data string) []byte { req, err := http.NewRequest("PUT", PostBase+path, strings.NewReader(data)) utils.Check(err) return fire(req) }
func Get(path string, params url.Values) []byte { req, err := http.NewRequest("GET", getURL(path, params), nil) utils.Check(err) return fire(req) }