diff --git a/Makefile b/Makefile index 0d9420f..4012a96 100644 --- a/Makefile +++ b/Makefile @@ -8,9 +8,9 @@ target ?= opsgenie-scheduler-creator authorname ?= Marcin Woźniak authoremail ?= y0rune@aol.com license ?= MIT -year ?= 2023 +year ?= $(shell date +%Y) copyright ?= Copyright (c) $(year) -gofile ?= main.go +gofile ?= main.go compile: @echo "Compiling..." diff --git a/README.md b/README.md index 831c1f0..58c9e89 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,8 @@ make build # ID of schedule (default "XXXXXXXXXXXXXXX") -scheduleName string # Name of schedule (default "Test Schedule") + -scheduleRotationStartEndTime int + # Start Time of the rotation (default 9) -scheduleTeam string # Name of the team in the schedule (default "TestTeam") -scheduleTimezone string diff --git a/go.mod b/go.mod index fd19619..f628a29 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module opsgenie-scheduler-creator -go 1.19 +go 1.21.5 require ( github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.22 diff --git a/main.go b/main.go index 69c2365..f4a9119 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ const staticScheduleTimezone string = "Europe/Warsaw" const staticScheduleTeam string = "TestTeam" const staticScheduleYear int = 2022 const staticScheduleEnabledFlag bool = true +const staticRotationStartEndTime int = 9 const staticTeamID string = "XXXXXXXXXXXXXXX" const staticTeamName string = "Team Test" @@ -141,12 +142,13 @@ func scheduleCreator(scheduleClient schedule.Client, scheduleName string, schedu return *scheduleResult } -func restrictionCreator(scheduleClient schedule.Client, scheduleID string, year int) { +func restrictionCreator(scheduleClient schedule.Client, scheduleID string, year int, startEndTime int) { month := time.Month(1) firstMonday := getFirstMonday(year, month) numberOfWeeks := getNumberOfWeeks(year) - nextMonday := time.Date(year, month, int(firstMonday), 1, 0, 0, 0, time.UTC) + // Set the next Monday at 9:00 AM + nextMonday := time.Date(year, month, int(firstMonday), startEndTime, 0, 0, 0, time.UTC) for week := 1; week <= numberOfWeeks; week++ { monday := nextMonday nextMonday = nextMonday.AddDate(0, 0, 7) @@ -195,6 +197,18 @@ func deleteSchedule(scheduleClient schedule.Client, scheduleID string) { time.Sleep(10 * time.Second) } +func getSchedule(scheduleClient schedule.Client, scheduleID string) *schedule.GetResult { + scheduleResult, err := scheduleClient.Get(nil, &schedule.GetRequest{ + IdentifierType: schedule.Id, + IdentifierValue: scheduleID, + }) + + if err != nil { + fmt.Printf("Schedule %s can NOT be get.\n", scheduleID) + } + return scheduleResult +} + func getListRotation(scheduleClient schedule.Client, scheduleID string) *schedule.ListRotationsResult { scheduleResult, err := scheduleClient.ListRotations(nil, &schedule.ListRotationsRequest{ ScheduleIdentifierType: schedule.Id, @@ -261,6 +275,7 @@ func main() { scheduleTeam := flag.String("scheduleTeam", staticScheduleTeam, "# Name of the team in the schedule") scheduleYear := flag.Int("scheduleYear", staticScheduleYear, "# Year of the schedule") scheduleEnabledFlag := flag.Bool("scheduleEnabledFlag", staticScheduleEnabledFlag, "# Schedule is enabled") + scheduleRotationStartEndTime := flag.Int("scheduleRotationStartEndTime", staticRotationStartEndTime, "# Start Time of the rotation") // Team Values teamName := flag.String("teamName", staticTeamName, "# Name of team") @@ -287,7 +302,7 @@ func main() { if *scheduleName != staticScheduleName && *scheduleID == staticScheduleID { createdSchedule := scheduleCreator(*scheduleClient, *scheduleName, *scheduleTimezone, *scheduleTeam, *scheduleEnabledFlag) - restrictionCreator(*scheduleClient, createdSchedule.Id, *scheduleYear) + restrictionCreator(*scheduleClient, createdSchedule.Id, *scheduleYear, *scheduleRotationStartEndTime) scheduleID = &createdSchedule.Id } diff --git a/main_test.go b/main_test.go index d53b77a..a8d71cd 100644 --- a/main_test.go +++ b/main_test.go @@ -25,7 +25,10 @@ const scheduleTimezone string = "Europe/Warsaw" const scheduleTeam string = "TestTeam" const scheduleYear int = 2022 const scheduleEnabledFlag bool = false +const scheduleStartEndTimeOfRotation int = 9 const expetedNameOfRotation string = "w21-23.5-30.5" +const expectedStartDateOfRotation string = "2022-03-14" +const expectedStartEndTimeOfRotation string = "09" const teamName string = "TestTeam" const teamDesc string = "Test" @@ -87,12 +90,29 @@ func TestOneCreateSchedule(t *testing.T) { } func TestOneCreateRestriction(t *testing.T) { - restrictionCreator(*scheduleClient, scheduleTest.Id, scheduleYear) + restrictionCreator(*scheduleClient, scheduleTest.Id, scheduleYear, scheduleStartEndTimeOfRotation) listRotation := getListRotation(*scheduleClient, scheduleTest.Id) + + // w21-23.5-30.5 if (listRotation.Rotations[20].Name) != expetedNameOfRotation { t.Fatalf("Schedule has been NOT created correctly.") } + + // w11-14.3-21.3 + if (listRotation.Rotations[10].StartDate).Format("2006-01-02") != expectedStartDateOfRotation { + t.Fatalf("Schedule has been NOT created correctly.") + } + + // w11-14.3-21.3 + if (listRotation.Rotations[10].StartDate).Format("15") != expectedStartEndTimeOfRotation { + t.Fatalf("Schedule has been NOT created correctly.") + } + + // w49-5.12-12.12 + if (listRotation.Rotations[49].StartDate).Format("15") != expectedStartEndTimeOfRotation { + t.Fatalf("Schedule has been NOT created correctly.") + } } func TestOneDeleteSchedule(t *testing.T) { diff --git a/release/opsgenie-scheduler-creator-darwin-amd64 b/release/opsgenie-scheduler-creator-darwin-amd64 new file mode 100755 index 0000000..fffa3a1 Binary files /dev/null and b/release/opsgenie-scheduler-creator-darwin-amd64 differ diff --git a/release/opsgenie-scheduler-creator-linux-amd64 b/release/opsgenie-scheduler-creator-linux-amd64 new file mode 100755 index 0000000..7a9cde8 Binary files /dev/null and b/release/opsgenie-scheduler-creator-linux-amd64 differ diff --git a/release/opsgenie-scheduler-creator-linux-arm b/release/opsgenie-scheduler-creator-linux-arm new file mode 100755 index 0000000..3f4a15a Binary files /dev/null and b/release/opsgenie-scheduler-creator-linux-arm differ diff --git a/release/opsgenie-scheduler-creator-linux-arm64 b/release/opsgenie-scheduler-creator-linux-arm64 new file mode 100755 index 0000000..fd509e8 Binary files /dev/null and b/release/opsgenie-scheduler-creator-linux-arm64 differ