-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
92 lines (70 loc) · 2.18 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)
// NewServer creates a new Server instance.
func NewServer(
logger log.Logger, addr string, service Service,
) Server {
return Server{
logger: logger,
address: addr,
service: service,
}
}
// Server is the HTTP server used to serve requests.
type Server struct {
address string
logger log.Logger
service Service
}
// Open will setup a tcp listener and serve the http requests.
func (s Server) Open() error {
// Init the mux router
router := mux.NewRouter()
// Route handles & endpoints
router.HandleFunc("/repo-prs", s.GetPRsByRepository).Methods("POST")
router.HandleFunc("/user-prs", s.GetPRsByUsername).Methods("POST")
// serve the app
return http.ListenAndServe(s.address, router)
}
// GetPRsByUsername post a message with all the PRs in which the username is missing reviews
func (s Server) GetPRsByUsername(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
channelID := r.FormValue("channel_id")
channelName := r.FormValue("channel_name")
username := r.FormValue("text")
if channelName == DM { // This should be done so we can publish both IM and public messages
channelID = r.FormValue("user_id")
}
prs, err := s.service.GithubClient.GetPRsByUser(ctx, username)
if err != nil {
fmt.Println(err)
}
err = s.service.SlackClient.SendMessage(ctx, channelID, prs, nil, &username)
if err != nil {
json.NewEncoder(w).Encode("Slack message not posted")
}
}
// GetPRsByRepository post a message with all the PRs from a repository that are missing reviews
func (s Server) GetPRsByRepository(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
channelID := r.FormValue("channel_id")
channelName := r.FormValue("channel_name")
repository := r.FormValue("text")
if channelName == DM { // This should be done so we can publish both IM and public messages
channelID = r.FormValue("user_id")
}
prs, err := s.service.GithubClient.GetPRsByRepository(ctx, repository)
if err != nil {
fmt.Println(err)
}
err = s.service.SlackClient.SendMessage(ctx, channelID, prs, &repository, nil)
if err != nil {
json.NewEncoder(w).Encode("Slack message not posted")
}
}