github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/core/scm.go (about)

     1  package core
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"time"
     7  )
     8  
     9  //go:generate mockgen -package mock -destination ../mock/scm_mock.go . SCMService,Client,GitRepoService,UserService,ContentService,GitService,WebhookService
    10  
    11  // SCMService to interact with given SCM provider
    12  type SCMService interface {
    13  	Client(scm SCMProvider) (Client, error)
    14  }
    15  
    16  // Token for OAuth authorization
    17  type Token struct {
    18  	Token   string
    19  	Refresh string
    20  	Expires time.Time
    21  }
    22  
    23  // Hook defines a created webhook
    24  type Hook struct {
    25  	ID string
    26  }
    27  
    28  // HookEvent defines a hook post from SCM
    29  type HookEvent interface{}
    30  
    31  // PullRequestHook event
    32  type PullRequestHook struct {
    33  	Number int
    34  	Merged bool
    35  	// Commit SHA of source branch head
    36  	Commit string
    37  	Source string
    38  	Target string
    39  }
    40  
    41  // PullRequest object
    42  type PullRequest struct {
    43  	Number int
    44  	Commit string
    45  	Source string
    46  	Target string
    47  }
    48  
    49  // Commit object
    50  type Commit struct {
    51  	Sha             string `json:"sha"`
    52  	Message         string `json:"message"`
    53  	Committer       string `json:"committer"`
    54  	CommitterAvater string `json:"committerAvatar"`
    55  }
    56  
    57  // Client connects to a SCM provider
    58  type Client interface {
    59  	Repositories() GitRepoService
    60  	Users() UserService
    61  	Git() GitService
    62  	Contents() ContentService
    63  	PullRequests() PullRequestService
    64  	Webhooks() WebhookService
    65  	Token(user *User) Token
    66  }
    67  
    68  // GitRepoService provides operations with SCM
    69  type GitRepoService interface {
    70  	NewReportID(repo *Repo) string
    71  	// List repositories from SCM context
    72  	List(ctx context.Context, user *User) ([]*Repo, error)
    73  	Find(ctx context.Context, user *User, name string) (*Repo, error)
    74  	CloneURL(ctx context.Context, user *User, name string) (string, error)
    75  	CreateHook(ctx context.Context, user *User, name string) (*Hook, error)
    76  	RemoveHook(ctx context.Context, user *User, name string, hook *Hook) error
    77  	IsAdmin(ctx context.Context, user *User, name string) bool
    78  }
    79  
    80  // UserService defines operations with SCM
    81  type UserService interface {
    82  	Find(ctx context.Context, token *Token) (*User, error)
    83  	Create(ctx context.Context, token *Token) (*User, error)
    84  	Update(ctx context.Context, token *Token) (*User, error)
    85  	Bind(ctx context.Context, user *User, token *Token) (*User, error)
    86  }
    87  
    88  // GitService provides Git operations
    89  type GitService interface {
    90  	GitRepository(ctx context.Context, user *User, repo string) (GitRepository, error)
    91  	FindCommit(ctx context.Context, user *User, repo *Repo) string
    92  	// ListCommits in the repository default branch
    93  	ListCommits(ctx context.Context, user *User, repo string) ([]*Commit, error)
    94  	// ListCommitsByRef in the repository reference. The reference could be branch name.
    95  	ListCommitsByRef(ctx context.Context, user *User, repo, ref string) ([]*Commit, error)
    96  	ListBranches(ctx context.Context, user *User, repo string) ([]string, error)
    97  }
    98  
    99  // ContentService provides information of source codes
   100  type ContentService interface {
   101  	ListAllFiles(ctx context.Context, user *User, repo, ref string) ([]string, error)
   102  	Find(ctx context.Context, user *User, repo, path, ref string) ([]byte, error)
   103  }
   104  
   105  // PullRequestService provides operation to repository issue, basically to pull request
   106  type PullRequestService interface {
   107  	Find(ctx context.Context, user *User, repo string, number int) (*PullRequest, error)
   108  	CreateComment(ctx context.Context, user *User, repo string, number int, body string) (int, error)
   109  	RemoveComment(ctx context.Context, user *User, repo string, number int, id int) error
   110  	ListChanges(ctx context.Context, user *User, repo string, number int) ([]*FileChange, error)
   111  }
   112  
   113  // WebhookService provides webhook parsing
   114  type WebhookService interface {
   115  	Parse(req *http.Request) (HookEvent, error)
   116  	IsWebhookNotSupport(err error) bool
   117  }