Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

telegram: align chatIDs and chatIDsFile format handling #1316

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions internal/notif/telegram/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ func (c *Client) Send(entry model.NotifEntry) error {
return errors.Wrap(err, "cannot retrieve token secret for Telegram notifier")
}

var cids []interface{}
for _, cid := range c.cfg.ChatIDs {
cids = append(cids, cid)
}
cids := c.cfg.ChatIDs
cidsRaw, err := utl.GetSecret("", c.cfg.ChatIDsFile)
if err != nil {
return errors.Wrap(err, "cannot retrieve chat IDs secret for Telegram notifier")
Expand Down Expand Up @@ -124,41 +121,32 @@ func (c *Client) Send(entry model.NotifEntry) error {
return nil
}

func parseChatIDs(entries []interface{}) ([]chatID, error) {
func parseChatIDs(entries []string) ([]chatID, error) {
var chatIDs []chatID
for _, entry := range entries {
switch v := entry.(type) {
case int:
chatIDs = append(chatIDs, chatID{id: int64(v)})
case int64:
chatIDs = append(chatIDs, chatID{id: v})
case string:
parts := strings.Split(v, ":")
if len(parts) < 1 || len(parts) > 2 {
return nil, errors.Errorf("invalid chat ID %q", v)
}
id, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return nil, errors.Wrap(err, "invalid chat ID")
}
var topics []int64
if len(parts) == 2 {
topicParts := strings.Split(parts[1], ";")
for _, topicPart := range topicParts {
topic, err := strconv.ParseInt(topicPart, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id)
}
topics = append(topics, topic)
parts := strings.Split(entry, ":")
if len(parts) < 1 || len(parts) > 2 {
return nil, errors.Errorf("invalid chat ID %q", entry)
}
id, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return nil, errors.Wrap(err, "invalid chat ID")
}
var topics []int64
if len(parts) == 2 {
topicParts := strings.Split(parts[1], ";")
for _, topicPart := range topicParts {
topic, err := strconv.ParseInt(topicPart, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "invalid topic %q for chat ID %d", topicPart, id)
}
topics = append(topics, topic)
}
chatIDs = append(chatIDs, chatID{
id: id,
topics: topics,
})
default:
return nil, errors.Errorf("invalid chat ID %v (type=%T)", entry, entry)
}
chatIDs = append(chatIDs, chatID{
id: id,
topics: topics,
})
}
return chatIDs, nil
}
Expand Down
28 changes: 8 additions & 20 deletions internal/notif/telegram/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
func TestParseChatIDs(t *testing.T) {
tests := []struct {
name string
entries []interface{}
entries []string
expected []chatID
err error
}{
{
name: "valid integers",
entries: []interface{}{8547439, 1234567},
name: "valid chat IDS",
entries: []string{"8547439", "1234567"},
expected: []chatID{
{id: 8547439},
{id: 1234567},
Expand All @@ -25,7 +25,7 @@ func TestParseChatIDs(t *testing.T) {
},
{
name: "valid strings with topics",
entries: []interface{}{"567891234:25", "891256734:25;12"},
entries: []string{"567891234:25", "891256734:25;12"},
expected: []chatID{
{id: 567891234, topics: []int64{25}},
{id: 891256734, topics: []int64{25, 12}},
Expand All @@ -34,37 +34,25 @@ func TestParseChatIDs(t *testing.T) {
},
{
name: "invalid format",
entries: []interface{}{"invalid_format"},
entries: []string{"invalid_format"},
expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`),
},
{
name: "invalid type",
entries: []interface{}{true},
expected: nil,
err: errors.New("invalid chat ID true (type=bool)"),
},
{
name: "empty string",
entries: []interface{}{""},
entries: []string{""},
expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "": invalid syntax`),
},
{
name: "string with invalid topic",
entries: []interface{}{"567891234:invalid"},
entries: []string{"567891234:invalid"},
expected: nil,
err: errors.New(`invalid topic "invalid" for chat ID 567891234: strconv.ParseInt: parsing "invalid": invalid syntax`),
},
{
name: "mixed valid and invalid entries",
entries: []interface{}{8547439, "567891234:25", "invalid_format", true},
expected: nil,
err: errors.New(`invalid chat ID: strconv.ParseInt: parsing "invalid_format": invalid syntax`),
},
{
name: "invalid format with too many parts",
entries: []interface{}{"567891234:25:extra"},
entries: []string{"567891234:25:extra"},
expected: nil,
err: errors.New(`invalid chat ID "567891234:25:extra"`),
},
Expand Down
Loading