Skip to content

Commit

Permalink
Merge pull request #11 from kothar/privacy_setting
Browse files Browse the repository at this point in the history
Implement new privacy settings
  • Loading branch information
kothar authored Jan 2, 2025
2 parents ca4a5f1 + 95d6239 commit 6ffc3f4
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 216 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
cmd/asana/asana
/.idea
/.idea

.DS_Store
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.23.0
12 changes: 6 additions & 6 deletions asana.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ import (
"os"
"strings"

"github.com/rs/xid"

"dario.cat/mergo"
"github.com/google/go-querystring/query"
"github.com/imdario/mergo"
"github.com/pkg/errors"
"github.com/rs/xid"
)

const (
Expand All @@ -34,9 +33,10 @@ func (f Feature) String() string {
}

const (
NewTaskSubtypes Feature = "new_task_subtypes"
NewSections Feature = "new_sections"
StringIDs Feature = "string_ids"
NewTaskSubtypes Feature = "new_task_subtypes"
NewSections Feature = "new_sections"
StringIDs Feature = "string_ids"
ProjectPrivacySetting Feature = "project_privacy_setting"
)

// Client is the root client for the Asana API. The nested HTTPClient should provide
Expand Down
284 changes: 144 additions & 140 deletions cmd/asana/main.go
Original file line number Diff line number Diff line change
@@ -1,176 +1,180 @@
package main

import (
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"log"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"

"bitbucket.org/mikehouston/asana-go"
"encoding/json"
"fmt"
"github.com/jessevdk/go-flags"
"log"
"mime"
"net/http"
"net/url"
"os"
"path/filepath"

"bitbucket.org/mikehouston/asana-go"
)

var (
True = true
)

var options struct {
Token string `long:"token" description:"Personal Access Token used to authorize access to the API" env:"ASANA_TOKEN" required:"true"`
Token string `long:"token" description:"Personal Access Token used to authorize access to the API" env:"ASANA_TOKEN" required:"true"`

Workspace []string `long:"workspace" short:"w" description:"Workspace to access"`
Project []string `long:"project" short:"p" description:"Project to access"`
Task []string `long:"task" short:"t" description:"Task to access"`
Workspace []string `long:"workspace" short:"w" description:"Workspace to access"`
Project []string `long:"project" short:"p" description:"Project to access"`
Task []string `long:"task" short:"t" description:"Task to access"`

Attach string `long:"attach" description:"Attach a file to a task"`
AddSection string `long:"add-section" description:"Add a new section to a project"`
Attach string `long:"attach" description:"Attach a file to a task"`
AddSection string `long:"add-section" description:"Add a new section to a project"`

Stories bool `long:"stories" description:"List stories for a task"`
Clean bool `long:"clean" description:"Clean all stories from a task"`
Stories bool `long:"stories" description:"List stories for a task"`
Clean bool `long:"clean" description:"Clean all stories from a task"`

Debug bool `short:"d" long:"debug" description:"Show debug information"`
Verbose []bool `short:"v" long:"verbose" description:"Show verbose output"`
Debug bool `short:"d" long:"debug" description:"Show debug information"`
Verbose []bool `short:"v" long:"verbose" description:"Show verbose output"`
}

func authenticate(req *http.Request) (*url.URL, error) {
req.Header.Add("Authorization", "Bearer "+options.Token)
return nil, nil
req.Header.Add("Authorization", "Bearer "+options.Token)
return nil, nil
}

func check(err error) {
if err != nil {
log.Fatal(err)
}
if err != nil {
log.Fatal(err)
}
}

func main() {
if _, err := flags.Parse(&options); err != nil {
return
}

// Create a client
client := asana.NewClient(&http.Client{
Transport: &http.Transport{
Proxy: authenticate,
},
})
if options.Debug {
client.Debug = true
client.DefaultOptions.Pretty = true
}
client.Verbose = options.Verbose
client.DefaultOptions.Enable = []asana.Feature{asana.StringIDs, asana.NewSections, asana.NewTaskSubtypes}

// Load a task object
if options.Task == nil {

// Load a project object
if options.Project == nil {

// Load a workspace object
if options.Workspace == nil {
check(ListWorkspaces(client))
return
}

for _, w := range options.Workspace {
workspace := &asana.Workspace{ID: w}
check(ListProjects(client, workspace))
}
return
}

for _, p := range options.Project {
project := &asana.Project{ID: p}

if options.AddSection != "" {
request := &asana.SectionBase{
Name: options.AddSection,
}

_, err := project.CreateSection(client, request)
check(err)
return
}

fmtProject(client, project)
}
return
}

for _, t := range options.Task {
task := &asana.Task{ID: t}
check(task.Fetch(client))

fmt.Printf("Task %s: %q\n", task.ID, task.Name)
if options.Attach != "" {
addAttachment(task, client)
return
}
if options.Stories {
listStories(task, client)
}
if options.Clean {
cleanStories(task, client)
}

fmtTask(task, client)
}
if _, err := flags.Parse(&options); err != nil {
return
}

// Create a client
client := asana.NewClient(&http.Client{
Transport: &http.Transport{
Proxy: authenticate,
},
})
if options.Debug {
client.DefaultOptions.Debug = &True
client.DefaultOptions.Pretty = &True
}
client.Verbose = options.Verbose
client.DefaultOptions.Enable = []asana.Feature{asana.StringIDs, asana.NewSections, asana.NewTaskSubtypes, asana.ProjectPrivacySetting}

// Load a task object
if options.Task == nil {

// Load a project object
if options.Project == nil {

// Load a workspace object
if options.Workspace == nil {
check(ListWorkspaces(client))
return
}

for _, w := range options.Workspace {
workspace := &asana.Workspace{ID: w}
check(ListProjects(client, workspace))
}
return
}

for _, p := range options.Project {
project := &asana.Project{ID: p}

if options.AddSection != "" {
request := &asana.SectionBase{
Name: options.AddSection,
}

_, err := project.CreateSection(client, request)
check(err)
return
}

fmtProject(client, project)
}
return
}

for _, t := range options.Task {
task := &asana.Task{ID: t}
check(task.Fetch(client))

fmt.Printf("Task %s: %q\n", task.ID, task.Name)
if options.Attach != "" {
addAttachment(task, client)
return
}
if options.Stories {
listStories(task, client)
}
if options.Clean {
cleanStories(task, client)
}

fmtTask(task, client)
}
}

func listStories(task *asana.Task, client *asana.Client) {
stories, _, _ := task.Stories(client)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")

for _, s := range stories {
fmt.Printf("Story %s (%s):\n", s.ID, s.CreatedBy.Name)
check(enc.Encode(s))
}
stories, _, _ := task.Stories(client)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")

for _, s := range stories {
fmt.Printf("Story %s (%s):\n", s.ID, s.CreatedBy.Name)
check(enc.Encode(s))
}
}

func cleanStories(task *asana.Task, client *asana.Client) {
stories, _, _ := task.Stories(client)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
stories, _, _ := task.Stories(client)
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")

for _, s := range stories {
check(s.Delete(client))
}
for _, s := range stories {
check(s.Delete(client))
}
}

func fmtProject(client *asana.Client, project *asana.Project) {
fmt.Println("\nSections:")
check(ListSections(client, project))
fmt.Println("\nTasks:")
check(ListTasks(client, project))
fmt.Println("\nSections:")
check(ListSections(client, project))
fmt.Println("\nTasks:")
check(ListTasks(client, project))
}

func fmtTask(task *asana.Task, client *asana.Client) {
fmt.Printf(" Completed: %v\n", task.Completed)
if task.Completed != nil && !*task.Completed {
fmt.Printf(" Due: %s\n", task.DueAt)
}
if task.Notes != "" {
fmt.Printf(" Notes: %q\n", task.Notes)
}
// Get subtasks
subtasks, nextPage, err := task.Subtasks(client)
check(err)
_ = nextPage
for _, subtask := range subtasks {
fmt.Printf(" Subtask %s: %q\n", subtask.ID, subtask.Name)
}
fmt.Printf(" Completed: %v\n", task.Completed)
if task.Completed != nil && !*task.Completed {
fmt.Printf(" Due: %s\n", task.DueAt)
}
if task.Notes != "" {
fmt.Printf(" Notes: %q\n", task.Notes)
}
// Get subtasks
subtasks, nextPage, err := task.Subtasks(client)
check(err)
_ = nextPage
for _, subtask := range subtasks {
fmt.Printf(" Subtask %s: %q\n", subtask.ID, subtask.Name)
}
}

func addAttachment(task *asana.Task, client *asana.Client) {
f, err := os.Open(options.Attach)
check(err)
defer f.Close()
a, err := task.CreateAttachment(client, &asana.NewAttachment{
Reader: f,
FileName: f.Name(),
ContentType: mime.TypeByExtension(filepath.Ext(f.Name())),
})
check(err)
fmt.Printf("Attachment added: %+v", a)
f, err := os.Open(options.Attach)
check(err)
defer f.Close()
a, err := task.CreateAttachment(client, &asana.NewAttachment{
Reader: f,
FileName: f.Name(),
ContentType: mime.TypeByExtension(filepath.Ext(f.Name())),
})
check(err)
fmt.Printf("Attachment added: %+v", a)
}
2 changes: 1 addition & 1 deletion customfields.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ type AddProjectLocalCustomFieldRequest struct {
}

func (p *Project) AddProjectLocalCustomField(client *Client, request *AddProjectLocalCustomFieldRequest) (*CustomFieldSetting, error) {
client.trace("Attach custom field %q to project %q", request.CustomField, p.ID)
client.trace("Attach custom field %q to project %q", request.CustomField.Name, p.ID)

// Custom request encoding
m := map[string]interface{}{}
Expand Down
Loading

0 comments on commit 6ffc3f4

Please sign in to comment.