// Handle recommended message // CAT URL: // http://cat.eng.vmware.com/api/v2.0/recommendation/?format=json&limit=1&order_by=-updated&branch__name=main&sla__name=VA_Bats&site=mbu func (plugin *recommended) Handle(req *comm.Request) error { branch := defaultBranch if len(req.Arguments) > 2 { branch = req.Arguments[2] } jRecommended := RecommendedJSON{} { url := plugin.config.URLs.BaseCatURL + plugin.config.URLs.BaseRecommendedURL params := DefaultRecommendedParams() params["branch__name"] = branch p := params.AsUrlValues() s := napping.Session{} r, err := s.Get(url, &p, &jRecommended, nil) if err != nil { return err } if r.Status() != 200 { return fmt.Errorf("expect response status code 200. Actual %d %s", r.Status(), params) } if len(jRecommended.Objects) == 0 { return fmt.Errorf("recommended change list for %s not found", params["branch__name"]) } if len(jRecommended.Objects) > 1 { return fmt.Errorf("expected one object. Actual %d %s", len(jRecommended.Objects), params) } } // CAT URL: // http://cat.eng.vmware.com/api/v2.0/build/<id>/?format=json jCurrBuild := CurrBuildJSON{} { url := plugin.config.URLs.BaseCatURL + jRecommended.Objects[0].CurrBuild params := DefaultCurrBuildParams() p := params.AsUrlValues() s := napping.Session{} r, err := s.Get(url, &p, &jCurrBuild, nil) if err != nil { return err } if r.Status() != 200 { return fmt.Errorf("expect response status code 200. Actual %d\n", r.Status()) } } t := comm.Mention(req.User) t += fmt.Sprintf("recommended changeset for *%s* ", branch) t += fmt.Sprintf("is *%s*", comm.P4WebBranchURL(branch, jCurrBuild.Changeset)) iop := &comm.Interop{ Request: req, Response: &comm.Response{ Channel: req.Channel, Text: t, Parameters: comm.DefaultMessageParameters()}} comm.SB.ChanResponse <- iop.Response comm.SB.ChanPersist <- iop return nil }
// Run recommendedPush func (plugin *recommendedPush) Run() (err error) { pollInterval := common.DefaultPollInterval var channels []string var groups []string pollInterval, err = time.ParseDuration(plugin.config.PushPluginConfig.PollInterval) if err != nil { return err } channels = plugin.config.PushPluginConfig.Channels groups = plugin.config.PushPluginConfig.Groups if len(channels) == 0 && len(groups) == 0 { return fmt.Errorf("no channels or groups specified") } log.Printf("%s poll interval %d", plugin.Name(), pollInterval) log.Printf("%s channels %v", plugin.Name(), channels) log.Printf("%s groups %v", plugin.Name(), groups) for _, b := range plugin.config.Branches { common.RandomDelay() go func(branch string) { pumpName := fmt.Sprintf("%s/%s", plugin.Name(), branch) log.Printf("Starting pump for %s", pumpName) currentChangeset := unknownStatus for { select { case <-time.After(pollInterval): comm.SB.ChanHeartbeat <- pumpName pa := &common.PushActions{ PluginName: plugin.Name()} jRecommended := RecommendedJSON{} { url := plugin.config.URLs.BaseCatURL + plugin.config.URLs.BaseRecommendedURL params := DefaultRecommendedParams() params["branch__name"] = branch p := params.AsUrlValues() s := napping.Session{} r, err := s.Get(url, &p, &jRecommended, nil) if err != nil { pa.Response = fmt.Sprintf("Error getting %s %s [%s]", url, params, err) pa.IsError = 1 comm.SB.ChanPersist <- pa continue } if r.Status() != 200 { pa.Response = fmt.Sprintf("Expect response status code 200. Actual %d. %s", r.Status(), params) pa.IsError = 1 comm.SB.ChanPersist <- pa continue } if len(jRecommended.Objects) == 0 { // build not found continue } if len(jRecommended.Objects) > 1 { pa.Response = fmt.Sprintf("Expected one object. Actual %d %s\n", len(jRecommended.Objects), params) pa.IsError = 1 comm.SB.ChanPersist <- pa continue } } jCurrBuild := CurrBuildJSON{} { url := plugin.config.URLs.BaseCatURL + jRecommended.Objects[0].CurrBuild params := DefaultCurrBuildParams() p := params.AsUrlValues() s := napping.Session{} r, err := s.Get(url, &p, &jCurrBuild, nil) if err != nil { pa.Response = fmt.Sprintf("Error getting %s [%s]", url, err) pa.IsError = 1 comm.SB.ChanPersist <- pa continue } if r.Status() != 200 { pa.Response = fmt.Sprintf("Expect response status code 200. Actual %d. %s", r.Status(), params) pa.IsError = 1 comm.SB.ChanPersist <- pa continue } } if currentChangeset != unknownStatus && currentChangeset != jCurrBuild.Changeset { text := fmt.Sprintf("recommendation for *%s* ", branch) text += fmt.Sprintf( "changed from %s to %s", comm.P4WebBranchURL(branch, currentChangeset), comm.P4WebBranchURL(branch, jCurrBuild.Changeset)) pa.Response = text pa.Before = currentChangeset pa.After = jCurrBuild.Changeset for _, c := range channels { cname, err := comm.GetChannelByName(c) if err != nil { log.Print(err) continue } comm.SB.ChanResponse <- &comm.Response{ Channel: cname, Text: text, Parameters: comm.DefaultMessageParameters()} pa.Channel = cname comm.SB.ChanPersist <- pa } for _, g := range groups { gname, err := comm.GetGroupByName(g) if err != nil { log.Print(err) continue } comm.SB.ChanResponse <- &comm.Response{ Channel: gname, Text: text, Parameters: comm.DefaultMessageParameters()} pa.Channel = gname comm.SB.ChanPersist <- pa } } currentChangeset = jCurrBuild.Changeset } } }(b) } return }