// Handle If query matches, grab the job name from the query and queue that build func (task JenkinsRunBuildTask) Handle(query types.Query) (bool, <-chan types.Answer) { matches := task.queryRegexp.FindStringSubmatch(query.Statement) if matches == nil { return false, nil } jobName := matches[1] uri := fmt.Sprintf("%s/job/%s/build", task.jenkinsURL, jobName) body := strings.NewReader("") c1 := make(chan types.Answer) go func() { resp, err := http.Post(uri, " text/plain", body) jobURL := resp.Header.Get("Location") if err != nil { c1 <- types.Answer(err.Error()) } else { var responseText string if resp.StatusCode == 201 { responseText = fmt.Sprintf("A new build for %s is queued (%s)", jobName, jobURL) } else if resp.StatusCode == 404 { responseText = fmt.Sprintf("No job with name %s exists", jobName) } else { responseText = fmt.Sprintf("Unknown response (%s) from jenkins", resp.Status) } c1 <- types.Answer(responseText) } resp.Body.Close() close(c1) }() return true, c1 }
// Handle If query matches, grab the job name from the query and queue that build func (task JenkinsStatusTask) Handle(query types.Query) (bool, <-chan types.Answer) { matches := task.queryRegexp.FindStringSubmatch(query.Statement) if matches == nil { return false, nil } jobName := matches[1] uri := fmt.Sprintf("%s/job/%s/api/json", task.jenkinsURL, jobName) c1 := make(chan types.Answer) go func() { var jsonResp statusTaskResponse resp, err := http.Get(uri) if err != nil { log.Fatal(err) } body, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() json.Unmarshal(body, &jsonResp) c1 <- types.Answer(fmt.Sprintf("Status: %s", jsonResp.Color)) c1 <- types.Answer(fmt.Sprintf("Url: %s", jsonResp.URL)) c1 <- types.Answer(fmt.Sprintf("Health report: %s", jsonResp.HealthReport[0]["description"])) close(c1) }() return true, c1 }
func (tt SimpsonsTask) Handle(query types.Query) (bool, <-chan types.Answer) { if !strings.Contains(query.Statement, "simpsons") { return false, nil } c1 := make(chan types.Answer) rand.Seed(8) // Try changing this number! go func() { //time.Sleep(time.Millisecond * 200) c1 <- types.Answer(tt.quotes[rand.Intn(len(tt.quotes))]) close(c1) }() return true, c1 }
// Handle If query matches, grab the job name from the query and queue that build func (task JenkinsListTask) Handle(query types.Query) (bool, <-chan types.Answer) { if query.Statement != "jenkins list" { return false, nil } c1 := make(chan types.Answer) go func() { var jsonResp listTaskResponse resp, _ := http.Get(fmt.Sprintf("%s/%s", task.jenkinsURL, "/api/json")) body, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() json.Unmarshal(body, &jsonResp) c1 <- types.Answer("Here is the jobs available in Jenkins: ") for _, m := range jsonResp.Jobs { c1 <- types.Answer(fmt.Sprintf("%s %s", m["name"], m["url"])) } close(c1) }() return true, c1 }
func (task ServerStatusTask) Handle(query types.Query) (bool, <-chan types.Answer) { if !task.queryRegexp.MatchString(query.Statement) { return false, nil } c1 := make(chan types.Answer) go func(cmd string) { out, err := exec.Command(cmd).Output() if err != nil { log.Fatal(err) } c1 <- types.Answer(fmt.Sprintf("My status is: %s", strings.Trim(string(out), " \n"))) close(c1) }("uptime") return true, c1 }
//Handle Run the task if query matches func (task HackerNewsTopTask) Handle(query types.Query) (bool, <-chan types.Answer) { if !strings.Contains(query.Statement, "hackernews top") && !strings.Contains(query.Statement, "hn top") { return false, nil } c1 := make(chan types.Answer) go func() { ids, err := task.getTopIds() if err != nil { log.Fatal(err) return } stories, err := task.getStories(ids[:5]) if err != nil { log.Fatal(err) return } for _, story := range stories { c1 <- types.Answer(story) } close(c1) }() return true, c1 }