feat(clbot): insert Unicode ZWS to avoid pinging users

Change-Id: I732bf6daa01f5b2099c4f4cbdd21a2ceedc79cb1
Reviewed-on: https://cl.tvl.fyi/c/depot/+/385
Reviewed-by: q3k <q3k@q3k.org>
Reviewed-by: lukegb <lukegb@tvl.fyi>
This commit is contained in:
eta 2020-06-15 17:59:22 +01:00
parent 70731b9ee0
commit 4c22cf3169

View file

@ -63,7 +63,9 @@ func callOnShutdown(f func()) {
shutdownFuncs = append(shutdownFuncs, f) shutdownFuncs = append(shutdownFuncs, f)
} }
const ignoreBotChar = "\xe2\x80\x8b" // Unicode U+200B zero-width-space, to avoid triggering other bots
// or highlighting people on IRC.
const zeroWidthSpace = "\u200b"
func runIRC(ctx context.Context, ircCfg irc.ClientConfig, sendMsg <-chan string) { func runIRC(ctx context.Context, ircCfg irc.ClientConfig, sendMsg <-chan string) {
bo := backoffutil.NewDefaultBackOff() bo := backoffutil.NewDefaultBackOff()
@ -100,7 +102,7 @@ func runIRC(ctx context.Context, ircCfg irc.ClientConfig, sendMsg <-chan string)
return return
case msg := <-sendMsg: case msg := <-sendMsg:
log.Infof("sending message %q to %v", msg, *ircChannel) log.Infof("sending message %q to %v", msg, *ircChannel)
ircClient.Writef("PRIVMSG %s :%s%s", *ircChannel, ignoreBotChar, msg) ircClient.Writef("PRIVMSG %s :%s%s", *ircChannel, zeroWidthSpace, msg)
} }
} }
}() }()
@ -132,6 +134,12 @@ func username(p gerritevents.PatchSet) string {
return "UNKNOWN USER" return "UNKNOWN USER"
} }
// noping inserts a Unicode zero-width space between the first and rest characters of `user`
// in an effort to avoid pinging that user on IRC.
func noping(user string) string {
return fmt.Sprintf("%s%s%s", user[0], zeroWidthSpace, user[1:])
}
func patchSetURL(c gerritevents.Change, p gerritevents.PatchSet) string { func patchSetURL(c gerritevents.Change, p gerritevents.PatchSet) string {
return fmt.Sprintf("https://cl.tvl.fyi/%d", c.Number) return fmt.Sprintf("https://cl.tvl.fyi/%d", c.Number)
} }
@ -190,12 +198,12 @@ func main() {
if e.Change.Project != "depot" || e.Change.Branch != "master" || e.PatchSet.Number != 1 { if e.Change.Project != "depot" || e.Change.Branch != "master" || e.PatchSet.Number != 1 {
continue continue
} }
parsedMsg = fmt.Sprintf("CL/%d: %q proposed by %s - %s", e.Change.Number, e.Change.Subject, username(e.PatchSet), patchSetURL(e.Change, e.PatchSet)) parsedMsg = fmt.Sprintf("CL/%d: %q proposed by %s - %s", e.Change.Number, e.Change.Subject, noping(username(e.PatchSet)), patchSetURL(e.Change, e.PatchSet))
case *gerritevents.ChangeMerged: case *gerritevents.ChangeMerged:
if e.Change.Project != "depot" || e.Change.Branch != "master" { if e.Change.Project != "depot" || e.Change.Branch != "master" {
continue continue
} }
parsedMsg = fmt.Sprintf("CL/%d: %q submitted by %s - %s", e.Change.Number, e.Change.Subject, username(e.PatchSet), patchSetURL(e.Change, e.PatchSet)) parsedMsg = fmt.Sprintf("CL/%d: %q submitted by %s - %s", e.Change.Number, e.Change.Subject, noping(username(e.PatchSet)), patchSetURL(e.Change, e.PatchSet))
} }
if parsedMsg != "" { if parsedMsg != "" {
sendMsgChan <- parsedMsg sendMsgChan <- parsedMsg