Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Attach to Github Issue" similar to Jira plugin #120

Merged
merged 11 commits into from
Sep 24, 2019
Merged
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ require (

// Workaround for https://github.com/golang/go/issues/30831 and fallout.
replace github.com/golang/lint => github.com/golang/lint v0.0.0-20190227174305-8f45f776aaf1

replace git.apache.org/thrift.git => github.com/apache/thrift v0.12.0
105 changes: 105 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req
p.getReviews(w, r)
case "/api/v1/yourprs":
p.getYourPrs(w, r)
case "/api/v1/searchissues":
p.searchIssues(w, r)
case "/api/v1/yourassignments":
p.getYourAssignments(w, r)
case "/api/v1/createissuecomment":
p.createIssueComment(w, r)
case "/api/v1/mentions":
p.getMentions(w, r)
case "/api/v1/unreads":
Expand Down Expand Up @@ -225,6 +229,13 @@ type ConnectedResponse struct {
Settings *UserSettings `json:"settings"`
}

type CreateIssueCommentRequest struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
Number int `json:"number"`
Comment string `json:"comment"`
}

type GitHubUserRequest struct {
UserID string `json:"user_id"`
}
Expand Down Expand Up @@ -480,6 +491,100 @@ func (p *Plugin) getYourPrs(w http.ResponseWriter, r *http.Request) {
w.Write(resp)
}

func (p *Plugin) searchIssues(w http.ResponseWriter, r *http.Request) {
config := p.getConfiguration()
niklabh marked this conversation as resolved.
Show resolved Hide resolved

userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}

ctx := context.Background()

var githubClient *github.Client
username := ""

searchTerm := r.FormValue("term")

if info, err := p.getGitHubUserInfo(userID); err != nil {
writeAPIError(w, err)
return
} else {
githubClient = p.githubConnect(*info.Token)
username = info.GitHubUsername
}

result, _, err := githubClient.Search.Issues(ctx, getIssuesSearchQuery(username, config.GitHubOrg, searchTerm), &github.SearchOptions{})
if err != nil {
mlog.Error(err.Error())
}

resp, _ := json.Marshal(result.Issues)
w.Write(resp)
}

func (p *Plugin) createIssueComment(w http.ResponseWriter, r *http.Request) {
userID := r.Header.Get("Mattermost-User-ID")
if userID == "" {
http.Error(w, "Not authorized", http.StatusUnauthorized)
return
}

req := &CreateIssueCommentRequest{}
dec := json.NewDecoder(r.Body)
if err := dec.Decode(&req); err != nil {
if err != nil {
mlog.Error("Error decoding JSON body", mlog.Err(err))
}
writeAPIError(w, &APIErrorResponse{ID: "", Message: "Please provide a JSON object.", StatusCode: http.StatusBadRequest})
return
}

if req.Owner == "" {
writeAPIError(w, &APIErrorResponse{ID: "", Message: "Please provide a valid repo owner.", StatusCode: http.StatusBadRequest})
return
}

if req.Repo == "" {
writeAPIError(w, &APIErrorResponse{ID: "", Message: "Please provide a valid repo.", StatusCode: http.StatusBadRequest})
return
}

if req.Number == 0 {
writeAPIError(w, &APIErrorResponse{ID: "", Message: "Please provide a valid issue number.", StatusCode: http.StatusBadRequest})
return
}

if req.Comment == "" {
writeAPIError(w, &APIErrorResponse{ID: "", Message: "Please provide a valid non empty comment.", StatusCode: http.StatusBadRequest})
return
}

ctx := context.Background()

var githubClient *github.Client

if info, err := p.getGitHubUserInfo(userID); err != nil {
writeAPIError(w, err)
return
} else {
githubClient = p.githubConnect(*info.Token)
}

comment := &github.IssueComment{
Body: &req.Comment,
}

result, _, err := githubClient.Issues.CreateComment(ctx, req.Owner, req.Repo, req.Number, comment)
if err != nil {
mlog.Error(err.Error())
}

resp, _ := json.Marshal(result)
w.Write(resp)
}

func (p *Plugin) getYourAssignments(w http.ResponseWriter, r *http.Request) {
config := p.getConfiguration()

Expand Down
10 changes: 10 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ func getYourAssigneeSearchQuery(username, org string) string {
return buildSearchQuery("is:open assignee:%v archived:false %v", username, org)
}

func getIssuesSearchQuery(username, org, searchTerm string) string {
query := "is:open is:issue assignee:%v archived:false %v %v"
orgField := ""
if len(org) != 0 {
orgField = fmt.Sprintf("org:%v", org)
}

return fmt.Sprintf(query, username, orgField, searchTerm)
}

func buildSearchQuery(query, username, org string) string {
orgField := ""
if len(org) != 0 {
Expand Down