Skip to content

Commit

Permalink
fix: add check for empty appeal duration (#376)
Browse files Browse the repository at this point in the history
* refactor: add func to check if appeal duration is empty

* fix: add check for empty appeal duration
  • Loading branch information
bsushmith committed Mar 13, 2023
1 parent 931558d commit 3b152ad
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
9 changes: 8 additions & 1 deletion core/appeal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
AuditKeyDeleteApprover = "appeal.deleteApprover"

RevokeReasonForExtension = "Automatically revoked for grant extension"
PermanentDuration = "Permanent"
)

var TimeNow = time.Now
Expand Down Expand Up @@ -785,6 +786,12 @@ func (s *Service) getPoliciesMap(ctx context.Context) (map[string]map[uint]*doma
func getApprovalNotifications(appeal *domain.Appeal) []domain.Notification {
notifications := []domain.Notification{}
approval := appeal.GetNextPendingApproval()

duration := PermanentDuration
if !appeal.IsDurationEmpty() {
duration = appeal.Options.Duration
}

if approval != nil {
for _, approver := range approval.Approvers {
notifications = append(notifications, domain.Notification{
Expand All @@ -804,7 +811,7 @@ func getApprovalNotifications(appeal *domain.Appeal) []domain.Notification {
"approval_step": approval.Name,
"actor": approver,
"details": appeal.Details,
"duration": appeal.Options.Duration,
"duration": duration,
},
},
})
Expand Down
23 changes: 18 additions & 5 deletions domain/appeal.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ func (a *Appeal) Cancel() {
func (a *Appeal) Approve() error {
a.Status = AppealStatusApproved

if a.Options == nil || a.Options.Duration == "" {
return nil
}

duration, err := time.ParseDuration(a.Options.Duration)
duration, err := a.GetDuration()
if err != nil {
return err
}
Expand All @@ -102,6 +98,23 @@ func (a *Appeal) Approve() error {
return nil
}

func (a *Appeal) GetDuration() (time.Duration, error) {
if a.IsDurationEmpty() {
return 0 * time.Second, nil
}

duration, err := time.ParseDuration(a.Options.Duration)
if err != nil {
return 0 * time.Second, err
}

return duration, nil
}

func (a *Appeal) IsDurationEmpty() bool {
return a.Options == nil || a.Options.Duration == "" || a.Options.Duration == "0h"
}

func (a *Appeal) Reject() {
a.Status = AppealStatusRejected
}
Expand Down

0 comments on commit 3b152ad

Please sign in to comment.