-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from jvmistica/feat/execute-trello-functions
feat: execute trello functions
- Loading branch information
Showing
11 changed files
with
676 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ repos: | |
- id: go-fmt | ||
args: [-w] | ||
- id: go-mod-tidy | ||
- id: golangci-lint-repo-mod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/jvmistica/gcal/pkg/gcal" | ||
// "github.com/jvmistica/gcal/pkg/trello" | ||
"github.com/jvmistica/gcal/pkg/suggestion" | ||
) | ||
|
||
var ( | ||
defaultCalendarID = "en.austrian#[email protected]" | ||
key = os.Getenv("GCP_API_KEY") | ||
suggestion = "Leave Suggestions" | ||
q1 = "Jan - Mar" | ||
q2 = "Apr - Jun" | ||
q3 = "Jul - Sep" | ||
q4 = "Oct - Dec" | ||
gcpAPIKey = os.Getenv("GCP_API_KEY") | ||
) | ||
|
||
// go run main.go -start=2023-05-01T00:00:00Z -end=2023-05-31T00:00:00Z | ||
func main() { | ||
// Parse command-line arguments | ||
calendarID := flag.String("calendarId", defaultCalendarID, "the calendarID") | ||
start := flag.String("start", "", "the start date") | ||
end := flag.String("end", "", "the end date") | ||
flag.Parse() | ||
|
||
events, suggestions, err := gcal.GetCalendarEvents(key, *start, *end, *calendarID) | ||
if err != nil { | ||
log.Fatal(err) | ||
func init() { | ||
gcpAPIKey := os.Getenv("GCP_API_KEY") | ||
if gcpAPIKey == "" { | ||
log.Fatal("missing environment variable GCP_API_KEY") | ||
} | ||
|
||
e, err := json.MarshalIndent(events, "", " ") | ||
if err != nil { | ||
log.Fatal(err) | ||
trelloAPIKey := os.Getenv("TRELLO_API_KEY") | ||
if trelloAPIKey == "" { | ||
log.Fatal("missing environment variable TRELLO_API_KEY") | ||
} | ||
|
||
fmt.Println("No leaves") | ||
fmt.Println(string(e)) | ||
|
||
s, err := json.MarshalIndent(suggestions, "", " ") | ||
if err != nil { | ||
log.Fatal(err) | ||
trelloAPIToken := os.Getenv("TRELLO_API_TOKEN") | ||
if trelloAPIToken == "" { | ||
log.Fatal("missing environment variable TRELLO_API_TOKEN") | ||
} | ||
fmt.Println("Suggestions") | ||
fmt.Println(string(s)) | ||
|
||
// boardID, err := trello.CreateBoard("Holidays") | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
// fmt.Println("Board ID:", boardID) | ||
|
||
// suggestListID, err := trello.CreateList(boardID, suggestion, "1") | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
|
||
// q1ListID, err := trello.CreateList(boardID, q1, "2") | ||
// if err != nil { | ||
// log.Fatal(err) | ||
// } | ||
|
||
// // q2ListID, err := trello.CreateList(boardID, q2, "3") | ||
// // if err != nil { | ||
// // log.Fatal(err) | ||
// // } | ||
|
||
// // q3ListID, err := trello.CreateList(boardID, q3, "4") | ||
// // if err != nil { | ||
// // log.Fatal(err) | ||
// // } | ||
} | ||
|
||
// // q4ListID, err := trello.CreateList(boardID, q4, "5") | ||
// // if err != nil { | ||
// // log.Fatal(err) | ||
// // } | ||
func main() { | ||
calendarID := flag.String("calendarId", defaultCalendarID, "the calendarID") | ||
start := flag.String("start", "", "the start date") | ||
end := flag.String("end", "", "the end date") | ||
flag.Parse() | ||
|
||
// _, _ = trello.CreateCard(q1ListID, string(e)) | ||
// _, _ = trello.CreateCard(suggestListID, string(s)) | ||
if err := suggestion.GenerateSuggestions(gcpAPIKey, *start, *end, *calendarID); err != nil { | ||
log.Fatalf("failed to generate suggestions - %s", err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
@@ -12,11 +13,10 @@ import ( | |
) | ||
|
||
var ( | ||
defaultCalendarID = "en.austrian#[email protected]" | ||
defaultTimeFormat = "2006-01-02T00:00:00Z" | ||
defaultResultDir = "./pkg/gcal/data/%s" | ||
DefaultTimeFormat = "2006-01-02" | ||
DefaultFilePath = "./pkg/gcal/data/%s.json" | ||
|
||
defaultMinDaysWithoutLeave = 3 | ||
key = os.Getenv("GCP_API_KEY") | ||
eventsListURL = "https://www.googleapis.com/calendar/v3/calendars/%s/events?" | ||
) | ||
|
||
|
@@ -51,21 +51,21 @@ type Vacation struct { | |
Count int | ||
} | ||
|
||
// GetCalendarEvents | ||
// GetCalendarEvents returns all holidays, weekends, and suggested vacation leaves | ||
func GetCalendarEvents(key, start, end, calendarID string) ([]*Vacation, []*Suggestion, error) { | ||
var events *Events | ||
filePath := fmt.Sprintf(defaultResultDir, fmt.Sprintf("%s.%s", calendarID, "json")) // change filePath | ||
filePath := fmt.Sprintf(DefaultFilePath, calendarID) | ||
|
||
if _, err := os.Stat(filePath); os.IsNotExist(err) { | ||
fmt.Println("Initiating GET request..") | ||
log.Print("Initiating GET request..") | ||
|
||
var err error | ||
events, err = queryCalendarAPI(events, key, calendarID, start, end, filePath) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} else { | ||
fmt.Println("Skipping GET request..") | ||
log.Print("Skipping GET request..") | ||
|
||
data, err := os.ReadFile(filePath) | ||
if err != nil { | ||
|
@@ -98,7 +98,7 @@ func GetCalendarEvents(key, start, end, calendarID string) ([]*Vacation, []*Sugg | |
func getHolidays(events *Events) ([]time.Time, error) { | ||
var holidays []time.Time | ||
for _, item := range events.Items { | ||
start, err := time.Parse("2006-01-02", item.Start.Date) | ||
start, err := time.Parse(DefaultTimeFormat, item.Start.Date) | ||
if err != nil { | ||
return holidays, err | ||
} | ||
|
@@ -111,17 +111,17 @@ func getHolidays(events *Events) ([]time.Time, error) { | |
// getWeekends returns a list of dates that fall on Saturdays and Sundays | ||
func getWeekends(startDate, endDate string) ([]time.Time, error) { | ||
var weekends []time.Time | ||
start, err := time.Parse(defaultTimeFormat, startDate) | ||
start, err := time.Parse(DefaultTimeFormat, startDate) | ||
if err != nil { | ||
return weekends, err | ||
} | ||
|
||
end, err := time.Parse(defaultTimeFormat, endDate) | ||
end, err := time.Parse(DefaultTimeFormat, endDate) | ||
if err != nil { | ||
return weekends, err | ||
} | ||
|
||
for d := start; d.After(end) == false; d = d.AddDate(0, 0, 1) { | ||
for d := start; !d.After(end); d = d.AddDate(0, 0, 1) { | ||
if d.Weekday().String() == "Saturday" || d.Weekday().String() == "Sunday" { | ||
weekends = append(weekends, d) | ||
} | ||
|
@@ -216,7 +216,7 @@ func formatFreeTime(holidays, weekends []time.Time) []time.Time { | |
// queryCalendarAPI gets the list of holidays from the Calendar API and writes it into a JSON file | ||
func queryCalendarAPI(events *Events, key, calendarID, start, end, filePath string) (*Events, error) { | ||
id := url.QueryEscape(calendarID) | ||
query := fmt.Sprintf("key=%s&timeMin=%s&timeMax=%s", key, start, end) | ||
query := fmt.Sprintf("key=%s&timeMin=%sT00:00:00Z&timeMax=%sT00:00:00Z", key, start, end) | ||
url := fmt.Sprintf(eventsListURL+query, id) | ||
|
||
f, err := os.Create(filePath) | ||
|
@@ -249,7 +249,9 @@ func queryCalendarAPI(events *Events, key, calendarID, start, end, filePath stri | |
return nil, err | ||
} | ||
|
||
f.Write(s) | ||
if _, err := f.Write(s); err != nil { | ||
return nil, err | ||
} | ||
|
||
return events, nil | ||
} |
Oops, something went wrong.