// Creates a comment on an issue
//
// https://developer.github.com/v3/issues/comments/#create-a-comment
191380
func (c *IssueCommentsService) Create(uri *Hyperlink, uriParams M, requestParams interface{}) (comment *IssueComment, result *Result) {
	url, err := ExpandWithDefault(uri, &IssueCommentsURL, uriParams)
	if err != nil {
		return nil, &Result{Err: err}
	}

	result = c.client.post(url, requestParams, &comment)
	return
}
711044
func (s *IssueService) create(req *restful.Request, resp *restful.Response) {
	// TODO (m0sth8): Check permissions
	raw := &TargetIssueEntity{}

	if err := req.ReadEntity(raw); err != nil {
		logrus.Error(stackerr.Wrap(err))
		resp.WriteServiceError(
			http.StatusBadRequest,
			services.WrongEntityErr,
		)
		return
	}
	// check target field, it must be present
	if !s.IsId(raw.Target) {
		resp.WriteServiceError(
			http.StatusBadRequest,
			services.NewBadReq("Target is wrong"),
		)
		return
	}
	// validate other fields
	if err := validator.WithTag("creating").Validate(raw); err != nil {
		resp.WriteServiceError(
			http.StatusBadRequest,
			services.NewBadReq("Validation error: %s", err.Error()),
		)
		return
	}

	mgr := s.Manager()
	defer mgr.Close()

	// load target and project
	t, err := mgr.Targets.GetById(mgr.ToId(raw.Target))
	if err != nil {
		if mgr.IsNotFound(err) {
			resp.WriteServiceError(http.StatusBadRequest, services.NewBadReq("Target not found"))
			return
		}
		logrus.Error(stackerr.Wrap(err))
		resp.WriteServiceError(http.StatusInternalServerError, services.DbErr)
		return
	}

	//	current user should have a permission to create issue there
	u := filters.GetUser(req)

	if sErr := services.Must(services.HasProjectIdPermission(mgr, u, t.Project)); sErr != nil {
		sErr.Write(resp)
		return
	}

	newObj := &issue.TargetIssue{
		Project: t.Project,
		Target:  t.Id,
	}
	updateTargetIssue(raw, newObj)
	newObj.AddUserReportActivity(u.Id)

	obj, err := mgr.Issues.Create(newObj)
	if err != nil {
		if mgr.IsDup(err) {
			resp.WriteServiceError(
				http.StatusConflict,
				services.DuplicateErr)
			return
		}
		logrus.Error(stackerr.Wrap(err))
		resp.WriteServiceError(http.StatusInternalServerError, services.DbErr)
		return
	}
	// TODO (m0sth8): extract to worker
	func(mgr *manager.Manager) {
		tgt, err := mgr.Targets.GetById(obj.Target)
		if err != nil {
			logrus.Error(stackerr.Wrap(err))
			return
		}
		err = mgr.Targets.UpdateSummary(tgt)
		if err != nil {
			logrus.Error(stackerr.Wrap(err))
			return
		}
	}(s.Manager())
	resp.WriteHeader(http.StatusCreated)
	resp.WriteEntity(obj)
}
499735
func (s *GistsService) DeleteComment(ctx context.Context, gistID string, commentID int64) (*Response, error) {
	u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID)
	req, err := s.client.NewRequest("DELETE", u, nil)
	if err != nil {
		return nil, err
	}

	return s.client.Do(ctx, req, nil)
}
1155080
func EditPostComment() echo.HandlerFunc {

	// swagger:route PUT /projects/{id}/posts/{pid}/comments/{cid} project post comment EditProjectPostComment
	//
	// Update the speficied comment on the specified project post
	//
	// Consumes:
	// - application/json
	//
	//	Produces:
	//	- application/json
	//
	//	Security:
	//		oauth: project_comments:write
	//
	//	Responses:
	//		default: ProjectsIdPostsPidCommentsCid

	return func(c echo.Context) error {
		if !rest.IsGranted("project_comments:write", c) {
			return rest.InvalidScopeResponse("project_comments:write", c)
		}

		// Read a rest.Message from the body request.
		message := rest.NewMessage{}
		if err := c.Bind(&message); err != nil {
			return err
		}
		comment := c.Get("comment").(*nerdz.ProjectPostComment)

		// Update filds
		comment.Message = message.Message
		if comment.Lang != "" {
			comment.Lang = message.Lang
		}

		// Edit
		me := c.Get("me").(*nerdz.User)
		if err := me.Edit(comment); err != nil {
			return err
		}

		// Extract the TO from the comment and return selected fields.
		return rest.SelectFields(comment.GetTO(me), c)
	}
}
537818
func (comment *UserPostComment) Post() (ExistingPost, error) {
	var post *UserPost
	var err error
	if post, err = NewUserPost(comment.Hpid); err != nil {
		return nil, err
	}
	return ExistingPost(post), nil
}
846291
func (g *Generator) Comments(path string) string {
	loc, ok := g.file.comments[path]
	if !ok {
		return ""
	}
	text := strings.TrimSuffix(loc.GetLeadingComments(), "\n")
	return text
}
460440
func NewComment(text string, idx file.Idx) *Comment {
	comment := &Comment{
		Begin:    idx,
		Text:     text,
		Position: TBD,
	}

	return comment
}
1155786
func NewCommentCreated(commentID string) Message {
	return Message{MessageID: uuid.NewV4(), MessageType: "comment.create", TargetID: commentID}
}
1073491
func EditPostComment() echo.HandlerFunc {

	// swagger:route PUT /me/posts/{pid}/comments/{cid} me post comment EditMeComment
	//
	// Update the speficied post on the specified user board
	//
	// Consumes:
	// - application/json
	//
	//	Produces:
	//	- application/json
	//
	//	Security:
	//		oauth: profile_comments:write
	//
	//	Responses:
	//		default: MePostsPidCommentsCid

	return func(c echo.Context) error {
		return user.EditPostComment()(c)
	}
}
1014534
func (s *IssuesService) Create(ctx context.Context, owner string, repo string, issue *IssueRequest) (*Issue, *Response, error) {
	u := fmt.Sprintf("repos/%v/%v/issues", owner, repo)
	req, err := s.client.NewRequest("POST", u, issue)
	if err != nil {
		return nil, nil, err
	}

	// TODO: remove custom Accept header when this API fully launches.
	req.Header.Set("Accept", mediaTypeLabelDescriptionSearchPreview)

	i := new(Issue)
	resp, err := s.client.Do(ctx, req, i)
	if err != nil {
		return nil, resp, err
	}

	return i, resp, nil
}
555170
func (f *File) HeaderComment(comment string) {
	f.headers = append(f.headers, comment)
}
911731
func (i *IssuesService) Create(uri *Hyperlink, uriParams M, requestParams interface{}) (issue *Issue, result *Result) {
	url, err := ExpandWithDefault(uri, &RepoIssuesURL, uriParams)
	if err != nil {
		return nil, &Result{Err: err}
	}

	result = i.client.post(url, requestParams, &issue)
	return
}
297051
func (s Session) SubmissionsComments(submissionID string) ([]*Comment, error) {
	redditURL := "https://www.reddit.com"
	redditURL += "/comments/" + submissionID
	redditURL += ".json"

	req := request{
		url:       redditURL,
		useragent: s.useragent,
	}
	body, err := req.getResponse()
	if err != nil {
		return nil, err
	}

	var comments interface{}
	err = json.NewDecoder(body).Decode(&comments)
	if err != nil {
		return nil, err
	}
	helper := new(helper)
	helper.buildComments(comments)

	return helper.comments, nil
}
800205
func Comment(prefix string) Option {
	return func(ini *INI) error {
		ini.comment = []byte(prefix)
		return nil
	}
}
525909
func (post *ProjectPost) CommentsCount() (count uint8) {
	Db().Model(ProjectPostComment{}).Where(&ProjectPostComment{Hpid: post.ID()}).Count(&count)
	return
}
1352611
func parseComment(t *thing) (*Comment, error) {
	// Reddit makes the replies field a string if it is empty, just to make
	// it harder for programmers who like static type systems.
	value, present := t.Data["replies"]
	if present {
		if str, ok := value.(string); ok && str == "" {
			delete(t.Data, "replies")
		}
	}

	c := &comment{}
	if err := mapstructure.Decode(t.Data, c); err != nil {
		return nil, mapDecodeError(err, t.Data)
	}

	var err error
	if c.Replies.Kind == listingKind {
		c.Comment.Replies, _, _, err = parseListing(&c.Replies)
	}

	c.Comment.Deleted = c.Comment.Body == deletedKey
	return &c.Comment, err
}
1118394
func (s *IssueService) UpdateComment(issueID string, comment *Comment) (*Comment, *Response, error) {
	reqBody := struct {
		Body string `json:"body"`
	}{
		Body: comment.Body,
	}
	apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s/comment/%s", issueID, comment.ID)
	req, err := s.client.NewRequest("PUT", apiEndpoint, reqBody)
	if err != nil {
		return nil, nil, err
	}

	responseComment := new(Comment)
	resp, err := s.client.Do(req, responseComment)
	if err != nil {
		return nil, resp, err
	}

	return responseComment, resp, nil
}