github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/prow/github/types.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package github
    18  
    19  import (
    20  	"strings"
    21  )
    22  
    23  // These are possible State entries for a Status.
    24  const (
    25  	StatusPending = "pending"
    26  	StatusSuccess = "success"
    27  	StatusError   = "error"
    28  	StatusFailure = "failure"
    29  )
    30  
    31  // Possible contents for reactions.
    32  const (
    33  	ReactionThumbsUp   = "+1"
    34  	ReactionThumbsDown = "-1"
    35  	ReactionLaugh      = "laugh"
    36  	ReactionConfused   = "confused"
    37  	ReactionHeart      = "heart"
    38  	ReactionHooray     = "hooray"
    39  )
    40  
    41  type Reaction struct {
    42  	Content string `json:"content"`
    43  }
    44  
    45  // Status is used to set a commit status line.
    46  type Status struct {
    47  	State       string `json:"state"`
    48  	TargetURL   string `json:"target_url,omitempty"`
    49  	Description string `json:"description,omitempty"`
    50  	Context     string `json:"context,omitempty"`
    51  }
    52  
    53  // CombinedStatus is the latest statuses for a ref.
    54  type CombinedStatus struct {
    55  	Statuses []Status `json:"statuses"`
    56  }
    57  
    58  // User is a GitHub user account.
    59  type User struct {
    60  	Login string `json:"login"`
    61  	Name  string `json:"name"`
    62  	Email string `json:"email"`
    63  	ID    int    `json:"id"`
    64  }
    65  
    66  // NormLogin normalizes GitHub login strings
    67  var NormLogin = strings.ToLower
    68  
    69  // PullRequestEventAction enumerates the triggers for this
    70  // webhook payload type. See also:
    71  // https://developer.github.com/v3/activity/events/types/#pullrequestevent
    72  type PullRequestEventAction string
    73  
    74  const (
    75  	PullRequestActionAssigned             PullRequestEventAction = "assigned"
    76  	PullRequestActionUnassigned                                  = "unassigned"
    77  	PullRequestActionReviewRequested                             = "review_requested"
    78  	PullRequestActionReviewRequestRemoved                        = "review_request_removed"
    79  	PullRequestActionLabeled                                     = "labeled"
    80  	PullRequestActionUnlabeled                                   = "unlabeled"
    81  	PullRequestActionOpened                                      = "opened"
    82  	PullRequestActionEdited                                      = "edited"
    83  	PullRequestActionClosed                                      = "closed"
    84  	PullRequestActionReopened                                    = "reopened"
    85  	PullRequestActionSynchronize                                 = "synchronize"
    86  )
    87  
    88  // PullRequestEvent is what GitHub sends us when a PR is changed.
    89  type PullRequestEvent struct {
    90  	Action      PullRequestEventAction `json:"action"`
    91  	Number      int                    `json:"number"`
    92  	PullRequest PullRequest            `json:"pull_request"`
    93  	Repo        Repo                   `json:"repository"`
    94  	Label       Label                  `json:"label"`
    95  }
    96  
    97  // PullRequest contains information about a PullRequest.
    98  type PullRequest struct {
    99  	Number             int               `json:"number"`
   100  	HTMLURL            string            `json:"html_url"`
   101  	User               User              `json:"user"`
   102  	Base               PullRequestBranch `json:"base"`
   103  	Head               PullRequestBranch `json:"head"`
   104  	Title              string            `json:"title"`
   105  	Body               string            `json:"body"`
   106  	RequestedReviewers []User            `json:"requested_reviewers"`
   107  	Assignees          []User            `json:"assignees"`
   108  	State              string            `json:"state"`
   109  	Merged             bool              `json:"merged"`
   110  	// ref https://developer.github.com/v3/pulls/#get-a-single-pull-request
   111  	// If Merged is true, MergeSHA is the SHA of the merge commit, or squashed commit
   112  	// If Merged is false, MergeSHA is a commit SHA that github created to test if
   113  	// the PR can be merged automatically.
   114  	MergeSHA *string `json:"merge_commit_sha"`
   115  }
   116  
   117  // PullRequestBranch contains information about a particular branch in a PR.
   118  type PullRequestBranch struct {
   119  	Ref  string `json:"ref"`
   120  	SHA  string `json:"sha"`
   121  	Repo Repo   `json:"repo"`
   122  }
   123  
   124  type Label struct {
   125  	URL   string `json:"url"`
   126  	Name  string `json:"name"`
   127  	Color string `json:"color"`
   128  }
   129  
   130  // PullRequestFileStatus enumerates the statuses for this webhook payload type.
   131  type PullRequestFileStatus string
   132  
   133  const (
   134  	PullRequestFileModified PullRequestFileStatus = "modified"
   135  	PullRequestFileAdded                          = "added"
   136  	PullRequestFileRemoved                        = "removed"
   137  	PullRequestFileRenamed                        = "renamed"
   138  )
   139  
   140  // PullRequestChange contains information about what a PR changed.
   141  type PullRequestChange struct {
   142  	SHA       string `json:"sha"`
   143  	Filename  string `json:"filename"`
   144  	Status    string `json:"status"`
   145  	Additions int    `json:"additions"`
   146  	Deletions int    `json:"deletions"`
   147  	Changes   int    `json:"changes"`
   148  	Patch     string `json:"patch"`
   149  }
   150  
   151  // Repo contains general repository information.
   152  type Repo struct {
   153  	Owner    User   `json:"owner"`
   154  	Name     string `json:"name"`
   155  	FullName string `json:"full_name"`
   156  	HTMLURL  string `json:"html_url"`
   157  }
   158  
   159  // IssueEventAction enumerates the triggers for this
   160  // webhook payload type. See also:
   161  // https://developer.github.com/v3/activity/events/types/#issuesevent
   162  type IssueEventAction string
   163  
   164  const (
   165  	IssueActionAssigned     IssueEventAction = "assigned"
   166  	IssueActionUnassigned                    = "unassigned"
   167  	IssueActionLabeled                       = "labeled"
   168  	IssueActionUnlabeled                     = "unlabeled"
   169  	IssueActionOpened                        = "opened"
   170  	IssueActionEdited                        = "edited"
   171  	IssueActionMilestoned                    = "milestoned"
   172  	IssueActionDemilestoned                  = "demilestoned"
   173  	IssueActionClosed                        = "closed"
   174  	IssueActionReopened                      = "reopened"
   175  )
   176  
   177  type IssueEvent struct {
   178  	Action IssueEventAction `json:"action"`
   179  	Issue  Issue            `json:"issue"`
   180  	Repo   Repo             `json:"repository"`
   181  }
   182  
   183  // IssueCommentEventAction enumerates the triggers for this
   184  // webhook payload type. See also:
   185  // https://developer.github.com/v3/activity/events/types/#issuecommentevent
   186  type IssueCommentEventAction string
   187  
   188  const (
   189  	IssueCommentActionCreated IssueCommentEventAction = "created"
   190  	IssueCommentActionEdited                          = "edited"
   191  	IssueCommentActionDeleted                         = "deleted"
   192  )
   193  
   194  type IssueCommentEvent struct {
   195  	Action  IssueCommentEventAction `json:"action"`
   196  	Issue   Issue                   `json:"issue"`
   197  	Comment IssueComment            `json:"comment"`
   198  	Repo    Repo                    `json:"repository"`
   199  }
   200  
   201  type Issue struct {
   202  	User      User    `json:"user"`
   203  	Number    int     `json:"number"`
   204  	Title     string  `json:"title"`
   205  	State     string  `json:"state"`
   206  	HTMLURL   string  `json:"html_url"`
   207  	Labels    []Label `json:"labels"`
   208  	Assignees []User  `json:"assignees"`
   209  	Body      string  `json:"body"`
   210  
   211  	// This will be non-nil if it is a pull request.
   212  	PullRequest *struct{} `json:"pull_request,omitempty"`
   213  }
   214  
   215  func (i Issue) IsAssignee(login string) bool {
   216  	for _, assignee := range i.Assignees {
   217  		if NormLogin(login) == NormLogin(assignee.Login) {
   218  			return true
   219  		}
   220  	}
   221  	return false
   222  }
   223  
   224  func (i Issue) IsAuthor(login string) bool {
   225  	return NormLogin(i.User.Login) == NormLogin(login)
   226  }
   227  
   228  func (i Issue) IsPullRequest() bool {
   229  	return i.PullRequest != nil
   230  }
   231  
   232  func (i Issue) HasLabel(labelToFind string) bool {
   233  	for _, label := range i.Labels {
   234  		if strings.ToLower(label.Name) == strings.ToLower(labelToFind) {
   235  			return true
   236  		}
   237  	}
   238  	return false
   239  }
   240  
   241  type IssueComment struct {
   242  	ID      int    `json:"id,omitempty"`
   243  	Body    string `json:"body"`
   244  	User    User   `json:"user,omitempty"`
   245  	HTMLURL string `json:"html_url,omitempty"`
   246  }
   247  
   248  type StatusEvent struct {
   249  	SHA         string `json:"sha,omitempty"`
   250  	State       string `json:"state,omitempty"`
   251  	Description string `json:"description,omitempty"`
   252  	TargetURL   string `json:"target_url,omitempty"`
   253  	ID          int    `json:"id,omitempty"`
   254  	Name        string `json:"name,omitempty"`
   255  	Context     string `json:"context,omitempty"`
   256  	Sender      User   `json:"sender,omitempty"`
   257  	Repo        Repo   `json:"repository,omitempty"`
   258  }
   259  
   260  // IssuesSearchResult represents the result of an issues search.
   261  type IssuesSearchResult struct {
   262  	Total  int     `json:"total_count,omitempty"`
   263  	Issues []Issue `json:"items,omitempty"`
   264  }
   265  
   266  type PushEvent struct {
   267  	Ref     string   `json:"ref"`
   268  	Before  string   `json:"before"`
   269  	After   string   `json:"after"`
   270  	Compare string   `json:"compare"`
   271  	Commits []Commit `json:"commits"`
   272  	// Pusher is the user that pushed the commit, valid in a webhook event.
   273  	Pusher User `json:"pusher"`
   274  	// Sender contains more information that Pusher about the user.
   275  	Sender User `json:"sender"`
   276  	Repo   Repo `json:"repository"`
   277  }
   278  
   279  func (pe PushEvent) Branch() string {
   280  	refs := strings.Split(pe.Ref, "/")
   281  	return refs[len(refs)-1]
   282  }
   283  
   284  type Commit struct {
   285  	ID       string   `json:"id"`
   286  	Message  string   `json:"message"`
   287  	Added    []string `json:"added"`
   288  	Removed  []string `json:"removed"`
   289  	Modified []string `json:"modified"`
   290  }
   291  
   292  // ReviewEventAction enumerates the triggers for this
   293  // webhook payload type. See also:
   294  // https://developer.github.com/v3/activity/events/types/#pullrequestreviewevent
   295  type ReviewEventAction string
   296  
   297  const (
   298  	ReviewActionSubmitted ReviewEventAction = "submitted"
   299  	ReviewActionEdited                      = "edited"
   300  	ReviewActionDismissed                   = "dismissed"
   301  )
   302  
   303  // ReviewEvent is what GitHub sends us when a PR review is changed.
   304  type ReviewEvent struct {
   305  	Action      ReviewEventAction `json:"action"`
   306  	PullRequest PullRequest       `json:"pull_request"`
   307  	Repo        Repo              `json:"repository"`
   308  	Review      Review            `json:"review"`
   309  }
   310  
   311  // Review describes a Pull Request review.
   312  type Review struct {
   313  	ID      int    `json:"id"`
   314  	User    User   `json:"user"`
   315  	Body    string `json:"body"`
   316  	State   string `json:"state"`
   317  	HTMLURL string `json:"html_url"`
   318  }
   319  
   320  // ReviewCommentEventAction enumerates the triggers for this
   321  // webhook payload type. See also:
   322  // https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent
   323  type ReviewCommentEventAction string
   324  
   325  const (
   326  	ReviewCommentActionCreated ReviewCommentEventAction = "created"
   327  	ReviewCommentActionEdited                           = "edited"
   328  	ReviewCommentActionDeleted                          = "deleted"
   329  )
   330  
   331  // ReviewCommentEvent is what GitHub sends us when a PR review comment is changed.
   332  type ReviewCommentEvent struct {
   333  	Action      ReviewCommentEventAction `json:"action"`
   334  	PullRequest PullRequest              `json:"pull_request"`
   335  	Repo        Repo                     `json:"repository"`
   336  	Comment     ReviewComment            `json:"comment"`
   337  }
   338  
   339  // ReviewComment describes a Pull Request review.
   340  type ReviewComment struct {
   341  	ID       int    `json:"id"`
   342  	ReviewID int    `json:"pull_request_review_id"`
   343  	User     User   `json:"user"`
   344  	Body     string `json:"body"`
   345  	Path     string `json:"path"`
   346  	HTMLURL  string `json:"html_url"`
   347  	// Position will be nil if the code has changed such that the comment is no
   348  	// longer relevant.
   349  	Position *int `json:"position"`
   350  }
   351  
   352  // ReviewAction is the action that a review can be made with.
   353  type ReviewAction string
   354  
   355  // Possible review actions. Leave Action blank for a pending review.
   356  const (
   357  	Approve        ReviewAction = "APPROVE"
   358  	RequestChanges              = "REQUEST_CHANGES"
   359  	Comment                     = "COMMENT"
   360  )
   361  
   362  // DraftReview is what we give GitHub when we want to make a PR Review. This is
   363  // different than what we receive when we ask for a Review.
   364  type DraftReview struct {
   365  	// If unspecified, defaults to the most recent commit in the PR.
   366  	CommitSHA string `json:"commit_id,omitempty"`
   367  	Body      string `json:"body"`
   368  	// If unspecified, defaults to PENDING.
   369  	Action   ReviewAction         `json:"event,omitempty"`
   370  	Comments []DraftReviewComment `json:"comments,omitempty"`
   371  }
   372  
   373  // DraftReviewComment is a comment in a draft review.
   374  type DraftReviewComment struct {
   375  	Path string `json:"path"`
   376  	// Position in the patch, not the line number in the file.
   377  	Position int    `json:"position"`
   378  	Body     string `json:"body"`
   379  }
   380  
   381  // Content is some base64 encoded github file content
   382  type Content struct {
   383  	Content string `json:"content"`
   384  	SHA     string `json:"sha"`
   385  }
   386  
   387  // Team is a github organizational team
   388  type Team struct {
   389  	ID   int    `json:"id"`
   390  	Name string `json:"name"`
   391  }
   392  
   393  // TeamMember is a member of an organizational team
   394  type TeamMember struct {
   395  	Login string `json:"login"`
   396  }
   397  
   398  type GenericCommentEventAction string
   399  
   400  // Comments indicate values that are coerced to the specified value.
   401  const (
   402  	GenericCommentActionCreated GenericCommentEventAction = "created" // "opened", "submitted"
   403  	GenericCommentActionEdited                            = "edited"
   404  	GenericCommentActionDeleted                           = "deleted" // "dismissed"
   405  )
   406  
   407  type GenericCommentEvent struct {
   408  	IsPR        bool
   409  	Action      GenericCommentEventAction
   410  	Body        string
   411  	HTMLURL     string
   412  	Number      int
   413  	Repo        Repo
   414  	User        User
   415  	IssueAuthor User
   416  	Assignees   []User
   417  	IssueState  string
   418  }