Skip to content

Commit

Permalink
Merge pull request #26 from jvmistica/feat/execute-trello-functions
Browse files Browse the repository at this point in the history
feat: execute trello functions
  • Loading branch information
jvmistica committed Jun 13, 2023
2 parents 21724f5 + d5ab787 commit 2c2084b
Show file tree
Hide file tree
Showing 11 changed files with 676 additions and 210 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ repos:
- id: go-fmt
args: [-w]
- id: go-mod-tidy
- id: golangci-lint-repo-mod
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ test:
cover:
@go tool cover -html cover.out -o cover.html

run:
@go run main.go

build:
@go build -v ./...

Expand Down
85 changes: 21 additions & 64 deletions main.go
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())
}
}
16 changes: 15 additions & 1 deletion pkg/gcal/data/en.austrian#[email protected]
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
{
"summary": "Holidays in Austria",
"nextSyncToken": "CMDu0emHs_8CEAAYASCn_tSAAg==",
"nextSyncToken": "CICx5O3Wu_8CEAAYASDHsumAAg==",
"items": [
{
"summary": "Corpus Christi",
"description": "Public holiday",
"start": {
"date": "2023-06-08"
}
},
{
"summary": "Father's Day",
"description": "Observance\nTo hide observances, go to Google Calendar Settings \u003e Holidays in Austria",
"start": {
"date": "2023-06-11"
}
},
{
"summary": "Assumption of Mary",
"description": "Public holiday",
Expand Down
30 changes: 16 additions & 14 deletions pkg/gcal/gcal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
Expand All @@ -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?"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit 2c2084b

Please sign in to comment.