Skip to content

Commit

Permalink
Allow to omit channel topic update
Browse files Browse the repository at this point in the history
  • Loading branch information
timoreimann committed Jan 7, 2021
1 parent a1d22f7 commit 871f1c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ Right now, the only supported target is Slack: given a list of PageDuty schedule

You will need to create a Slack app with the following scopes

| Scope | Optional | Used for |
|--------------------|----------|-----------------------------------|
| `users:read` | no | |
| `channels:read` | no | |
| `channels:manage` | no | |
| `groups:read` | yes | interacting with private channels |
| `groups:write` | yes | interacting with private channels |
| `usergroups:read` | yes | managing user groups |
| `usergroups:write` | yes | managing user groups |
| Scope | Optional | Used for |
|--------------------|----------|------------------------------------|
| `users:read` | no | |
| `channels:read` | yes | managing topics (public channels) |
| `channels:manage` | yes | managing topics (public channels) |
| `groups:read` | yes | managing topics (private channels) |
| `groups:write` | yes | managing topics (private channels) |
| `usergroups:read` | yes | managing user groups |
| `usergroups:write` | yes | managing user groups |

and invite it to the target channel.

Expand Down
17 changes: 7 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ func parseSchedule(schedule string) (ConfigSchedule, error) {
delete(kvs, "name")
}

if id == "" && name == "" {
return ConfigSchedule{}, errors.New(`one of "id" or "name" must be given`)
}

if id != "" && name != "" {
return ConfigSchedule{}, errors.New(`"id" and "name" cannot be specified simultaneously`)
}
Expand Down Expand Up @@ -242,12 +238,13 @@ func validateConfig(cfg *config) error {
}
}

if sync.Channel.ID == "" && sync.Channel.Name == "" {
return fmt.Errorf("slack sync %q invalid: must specify either channel ID or channel name", sync.Name)
}

if sync.Template == "" {
return fmt.Errorf("slack sync %q invalid: template is missing", sync.Name)
channelGiven := sync.Channel.ID != "" || sync.Channel.Name != ""
if sync.Template != "" {
if !channelGiven {
return fmt.Errorf("slack sync %q invalid: must specify either channel ID or channel name when topic is given", sync.Name)
}
} else if channelGiven {
return fmt.Errorf("slack sync %q invalid: must specify template when either channel ID or channel name is given", sync.Name)
}
}

Expand Down
5 changes: 0 additions & 5 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ func TestParseSchedule(t *testing.T) {
inSchedule: "name=schedule1;name=schedule2",
wantErrStr: `multiple values for key "name" not allowed`,
},
{
name: "none of id or name specifiers given",
inSchedule: "foo=bar",
wantErrStr: `one of "id" or "name" must be given`,
},
{
name: "id and name specifiers given",
inSchedule: "id=schedule;name=schedule",
Expand Down
50 changes: 29 additions & 21 deletions syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type runSlackSync struct {
name string
pdSchedules pdSchedules
slackChannelID string
//userGroupsBySchedule map[string]UserGroups
tmpl *template.Template
dryRun bool
}
Expand All @@ -32,12 +31,22 @@ func (sp syncerParams) createSlackSyncs(ctx context.Context, cfg config) ([]runS
dryRun: cfgSlSync.DryRun,
}

slChannel, err := sp.slClient.getChannel(ctx, cfgSlSync.Channel.Name, cfgSlSync.Channel.ID)
if err != nil {
return nil, fmt.Errorf("failed to create slack sync %q: failed to get Slack channel: %s", slSync.name, err)
if cfgSlSync.Template == "" {
fmt.Printf("Slack sync %s: skipping topic handling because template is undefined\n", slSync.name)
} else {
var err error
slSync.tmpl, err = template.New("topic").Parse(cfgSlSync.Template)
if err != nil {
return nil, fmt.Errorf("failed to create slack sync %q: failed to parse template %q: %s", slSync.name, cfgSlSync.Template, err)
}

slChannel, err := sp.slClient.getChannel(ctx, cfgSlSync.Channel.Name, cfgSlSync.Channel.ID)
if err != nil {
return nil, fmt.Errorf("failed to create slack sync %q: failed to get Slack channel: %s", slSync.name, err)
}
slSync.slackChannelID = slChannel.ID
fmt.Printf("Slack sync %s: found Slack channel %q (ID %s)\n", slSync.name, slChannel.Name, slChannel.ID)
}
slSync.slackChannelID = slChannel.ID
fmt.Printf("Slack sync %s: found Slack channel %q (ID %s)\n", slSync.name, slChannel.Name, slChannel.ID)

pdSchedules := pdSchedules{}
fmt.Printf("Slack sync %s: Getting PagerDuty schedules\n", slSync.name)
Expand Down Expand Up @@ -72,11 +81,6 @@ func (sp syncerParams) createSlackSyncs(ctx context.Context, cfg config) ([]runS
slSync.pdSchedules = pdSchedules
fmt.Printf("Slack sync %s: found %d PagerDuty schedule(s)\n", slSync.name, len(pdSchedules))

slSync.tmpl, err = template.New("topic").Parse(cfgSlSync.Template)
if err != nil {
return nil, fmt.Errorf("failed to create slack sync %q: failed to parse template %q: %s", slSync.name, cfgSlSync.Template, err)
}

slSyncs = append(slSyncs, slSync)
}

Expand Down Expand Up @@ -134,17 +138,21 @@ func (s *syncer) runSlackSync(ctx context.Context, slackSync runSlackSync) error
return fmt.Errorf("failed to update on-call user group members: %s", err)
}

var buf bytes.Buffer
fmt.Printf("Executing template with Slack user IDs by schedule name: %s\n", slackUserIDByScheduleName)
err := slackSync.tmpl.Execute(&buf, slackUserIDByScheduleName)
if err != nil {
return fmt.Errorf("failed to render template: %s", err)
}
if slackSync.tmpl == nil {
fmt.Println("Skipping topic update")
} else {
var buf bytes.Buffer
fmt.Printf("Executing template with Slack user IDs by schedule name: %s\n", slackUserIDByScheduleName)
err := slackSync.tmpl.Execute(&buf, slackUserIDByScheduleName)
if err != nil {
return fmt.Errorf("failed to render template: %s", err)
}

topic := buf.String()
err = s.slClient.updateTopic(ctx, slackSync.slackChannelID, topic, slackSync.dryRun)
if err != nil {
return fmt.Errorf("failed to update topic: %s", err)
topic := buf.String()
err = s.slClient.updateTopic(ctx, slackSync.slackChannelID, topic, slackSync.dryRun)
if err != nil {
return fmt.Errorf("failed to update topic: %s", err)
}
}

return nil
Expand Down

0 comments on commit 871f1c0

Please sign in to comment.