Skip to content

Commit

Permalink
Merge pull request #38 from y0rune/add-start-end-time
Browse files Browse the repository at this point in the history
Added staticStartEndTime and give opportunity change startEndTime
  • Loading branch information
y0rune authored Jan 6, 2024
2 parents 67db663 + 44be8f2 commit 5394f32
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ target ?= opsgenie-scheduler-creator
authorname ?= Marcin Woźniak
authoremail ?= [email protected]
license ?= MIT
year ?= 2023
year ?= $(shell date +%Y)
copyright ?= Copyright (c) $(year)
gofile ?= main.go
gofile ?= main.go

compile:
@echo "Compiling..."
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand Down
22 changes: 21 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
Binary file added release/opsgenie-scheduler-creator-darwin-amd64
Binary file not shown.
Binary file added release/opsgenie-scheduler-creator-linux-amd64
Binary file not shown.
Binary file added release/opsgenie-scheduler-creator-linux-arm
Binary file not shown.
Binary file added release/opsgenie-scheduler-creator-linux-arm64
Binary file not shown.

0 comments on commit 5394f32

Please sign in to comment.