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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 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,110 @@ 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

if r.Method != http.MethodGet {
http.Error(w, fmt.Sprintf("Request: %s is not allowed, must be GET", r.Method), http.StatusMethodNotAllowed)
return
}

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) {
if r.Method != http.MethodPost {
http.Error(w, fmt.Sprintf("Request: %s is not allowed, must be POST", r.Method), http.StatusMethodNotAllowed)
return
}

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
Loading