github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/api/queries_comments.go (about)

     1  package api
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/shurcooL/githubv4"
     7  )
     8  
     9  type Comments struct {
    10  	Nodes      []Comment
    11  	TotalCount int
    12  	PageInfo   struct {
    13  		HasNextPage bool
    14  		EndCursor   string
    15  	}
    16  }
    17  
    18  func (cs Comments) CurrentUserComments() []Comment {
    19  	var comments []Comment
    20  	for _, c := range cs.Nodes {
    21  		if c.ViewerDidAuthor {
    22  			comments = append(comments, c)
    23  		}
    24  	}
    25  	return comments
    26  }
    27  
    28  type Comment struct {
    29  	ID                  string         `json:"id"`
    30  	Author              Author         `json:"author"`
    31  	AuthorAssociation   string         `json:"authorAssociation"`
    32  	Body                string         `json:"body"`
    33  	CreatedAt           time.Time      `json:"createdAt"`
    34  	IncludesCreatedEdit bool           `json:"includesCreatedEdit"`
    35  	IsMinimized         bool           `json:"isMinimized"`
    36  	MinimizedReason     string         `json:"minimizedReason"`
    37  	ReactionGroups      ReactionGroups `json:"reactionGroups"`
    38  	URL                 string         `json:"url,omitempty"`
    39  	ViewerDidAuthor     bool           `json:"viewerDidAuthor"`
    40  }
    41  
    42  type CommentCreateInput struct {
    43  	Body      string
    44  	SubjectId string
    45  }
    46  
    47  type CommentUpdateInput struct {
    48  	Body      string
    49  	CommentId string
    50  }
    51  
    52  func CommentCreate(client *Client, repoHost string, params CommentCreateInput) (string, error) {
    53  	var mutation struct {
    54  		AddComment struct {
    55  			CommentEdge struct {
    56  				Node struct {
    57  					URL string
    58  				}
    59  			}
    60  		} `graphql:"addComment(input: $input)"`
    61  	}
    62  
    63  	variables := map[string]interface{}{
    64  		"input": githubv4.AddCommentInput{
    65  			Body:      githubv4.String(params.Body),
    66  			SubjectID: githubv4.ID(params.SubjectId),
    67  		},
    68  	}
    69  
    70  	err := client.Mutate(repoHost, "CommentCreate", &mutation, variables)
    71  	if err != nil {
    72  		return "", err
    73  	}
    74  
    75  	return mutation.AddComment.CommentEdge.Node.URL, nil
    76  }
    77  
    78  func CommentUpdate(client *Client, repoHost string, params CommentUpdateInput) (string, error) {
    79  	var mutation struct {
    80  		UpdateIssueComment struct {
    81  			IssueComment struct {
    82  				URL string
    83  			}
    84  		} `graphql:"updateIssueComment(input: $input)"`
    85  	}
    86  
    87  	variables := map[string]interface{}{
    88  		"input": githubv4.UpdateIssueCommentInput{
    89  			Body: githubv4.String(params.Body),
    90  			ID:   githubv4.ID(params.CommentId),
    91  		},
    92  	}
    93  
    94  	err := client.Mutate(repoHost, "CommentUpdate", &mutation, variables)
    95  	if err != nil {
    96  		return "", err
    97  	}
    98  
    99  	return mutation.UpdateIssueComment.IssueComment.URL, nil
   100  }
   101  
   102  func (c Comment) Identifier() string {
   103  	return c.ID
   104  }
   105  
   106  func (c Comment) AuthorLogin() string {
   107  	return c.Author.Login
   108  }
   109  
   110  func (c Comment) Association() string {
   111  	return c.AuthorAssociation
   112  }
   113  
   114  func (c Comment) Content() string {
   115  	return c.Body
   116  }
   117  
   118  func (c Comment) Created() time.Time {
   119  	return c.CreatedAt
   120  }
   121  
   122  func (c Comment) HiddenReason() string {
   123  	return c.MinimizedReason
   124  }
   125  
   126  func (c Comment) IsEdited() bool {
   127  	return c.IncludesCreatedEdit
   128  }
   129  
   130  func (c Comment) IsHidden() bool {
   131  	return c.IsMinimized
   132  }
   133  
   134  func (c Comment) Link() string {
   135  	return c.URL
   136  }
   137  
   138  func (c Comment) Reactions() ReactionGroups {
   139  	return c.ReactionGroups
   140  }
   141  
   142  func (c Comment) Status() string {
   143  	return ""
   144  }