| 1 | package opsgeniescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ |
| 2 | |
| 3 | import ( |
| 4 | "errorsscip-go gomod github.com/golang/go/src . errors/" |
| 5 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 6 | "regexpscip-go gomod github.com/golang/go/src . regexp/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 8 | ) |
| 9 | |
| 10 | // Entity represents an OpsGenie entity (e.g., user, team) with type and identifier. |
| 11 | // Example JSON: { "username":"[email protected]", "type":"user" }. |
| 12 | type Entityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity# struct { |
| 13 | Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Type. string `json:"type"` |
| 14 | IDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#ID. string `json:"id,omitempty"` |
| 15 | Namescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Name. string `json:"name,omitempty"` |
| 16 | Usernamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Username. string `json:"username,omitempty"` |
| 17 | } |
| 18 | |
| 19 | // EntityPartsCount is the expected number of parts in an entity string (type:identifier). |
| 20 | const ( |
| 21 | EntityPartsCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/EntityPartsCount. = 2 // Expected number of parts in an entity string (type:identifier) |
| 22 | ) |
| 23 | |
| 24 | // ErrInvalidEntityFormat indicates that the entity string does not have two elements separated by a colon. |
| 25 | var ( |
| 26 | ErrInvalidEntityFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrInvalidEntityFormat. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().( |
| 27 | "invalid entity, should have two elements separated by colon", |
| 28 | ) |
| 29 | ErrInvalidEntityIDNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrInvalidEntityIDName. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("invalid entity, cannot parse id/name") |
| 30 | ErrUnexpectedEntityTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrUnexpectedEntityType. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("invalid entity, unexpected entity type") |
| 31 | ErrMissingEntityIdentityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrMissingEntityIdentity. = errorsscip-go gomod github.com/golang/go/src . errors/.Newscip-go gomod github.com/golang/go/src . errors/New().("invalid entity, should have either ID, name or username") |
| 32 | ) |
| 33 | |
| 34 | // GetPropValue serializes an entity back into a string in the format "type:identifier". |
| 35 | func (e *Entityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#) GetPropValuescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#GetPropValue().() (string, error) { |
| 36 | var identifier string |
| 37 | |
| 38 | switch { |
| 39 | case e.IDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#ID. != "": |
| 40 | identifier = e.IDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#ID. |
| 41 | case e.Namescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Name. != "": |
| 42 | identifier = e.Namescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Name. |
| 43 | case e.Usernamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Username. != "": |
| 44 | identifier = e.Usernamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Username. |
| 45 | default: |
| 46 | return "", ErrMissingEntityIdentityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrMissingEntityIdentity. |
| 47 | } |
| 48 | |
| 49 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Sprintfscip-go gomod github.com/golang/go/src . fmt/Sprintf().("%s:%s", e.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Type., identifier), nil |
| 50 | } |
| 51 | |
| 52 | // SetFromProp deserializes an entity from a string in the format "type:identifier". |
| 53 | func (e *Entityscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#) SetFromPropscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#SetFromProp().(propValue string) error { |
| 54 | elements := stringsscip-go gomod github.com/golang/go/src . strings/.Splitscip-go gomod github.com/golang/go/src . strings/Split().(propValue, ":") |
| 55 | |
| 56 | if len(elements) != EntityPartsCountscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/EntityPartsCount. { |
| 57 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", ErrInvalidEntityFormatscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrInvalidEntityFormat., propValue) |
| 58 | } |
| 59 | |
| 60 | e.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Type. = elements[0] |
| 61 | identifier := elements[1] |
| 62 | |
| 63 | isID, err := isOpsGenieIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/isOpsGenieID().(identifier) |
| 64 | if err != nil { |
| 65 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", ErrInvalidEntityIDNamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrInvalidEntityIDName., identifier) |
| 66 | } |
| 67 | |
| 68 | switch { |
| 69 | case isID: |
| 70 | e.IDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#ID. = identifier |
| 71 | case e.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Type. == "team": |
| 72 | e.Namescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Name. = identifier |
| 73 | case e.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Type. == "user": |
| 74 | e.Usernamescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Username. = identifier |
| 75 | default: |
| 76 | return fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("%w: %q", ErrUnexpectedEntityTypescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/ErrUnexpectedEntityType., e.Typescip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/Entity#Type.) |
| 77 | } |
| 78 | |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | // isOpsGenieID checks if a string matches the OpsGenie ID format (e.g., 4513b7ea-3b91-438f-b7e4-e3e54af9147c). |
| 83 | func isOpsGenieIDscip-go gomod github.com/nicholas-fedor/shoutrrr . `github.com/nicholas-fedor/shoutrrr/pkg/services/incident/opsgenie`/isOpsGenieID().(str string) (bool, error) { |
| 84 | matched, err := regexpscip-go gomod github.com/golang/go/src . regexp/.MatchStringscip-go gomod github.com/golang/go/src . regexp/MatchString().( |
| 85 | `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`, |
| 86 | str, |
| 87 | ) |
| 88 | if err != nil { |
| 89 | return false, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("matching OpsGenie ID format for %q: %w", str, err) |
| 90 | } |
| 91 | |
| 92 | return matched, nil |
| 93 | } |