...

Source file src/edge-infra.dev/pkg/f8n/devinfra/jack/plugin/interfaces.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/jack/plugin

     1  package plugin
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/url"
     7  
     8  	"github.com/google/go-github/v47/github"
     9  	githubv4 "github.com/shurcooL/githubv4"
    10  	"github.com/shurcooL/graphql"
    11  )
    12  
    13  // TODO(ss185994): most of the params here are unused
    14  type GithubClient struct {
    15  	Client              *github.Client
    16  	GithubIssues        IssueServiceInterface
    17  	GithubOrganizations OrganizationsServiceInterface
    18  	GithubProjects      ProjectsServiceInterface
    19  	GithubChecks        ChecksServiceInterface
    20  	GithubActions       ActionsServiceInterface
    21  	GithubRepositories  RepositoriesServiceInterface
    22  	GithubPullRequests  PullRequestServiceInterface
    23  }
    24  
    25  func NewGithubClient(client *github.Client) *GithubClient {
    26  	return &GithubClient{
    27  		Client:              client,
    28  		GithubIssues:        client.Issues,
    29  		GithubOrganizations: client.Organizations,
    30  		GithubProjects:      client.Projects,
    31  		GithubChecks:        client.Checks,
    32  		GithubActions:       client.Actions,
    33  		GithubRepositories:  client.Repositories,
    34  		GithubPullRequests:  client.PullRequests,
    35  	}
    36  }
    37  
    38  type GithubClientInterface interface {
    39  	Issues() IssueServiceInterface
    40  	Organizations() OrganizationsServiceInterface
    41  	Projects() ProjectsServiceInterface
    42  	Checks() ChecksServiceInterface
    43  	Actions() ActionsServiceInterface
    44  	Repositories() RepositoriesServiceInterface
    45  	PullRequests() PullRequestServiceInterface
    46  }
    47  
    48  //
    49  // The functions from here until MockGithubClient actually interact with the Github API
    50  // issue mocks
    51  //
    52  //
    53  
    54  type IssueServiceInterface interface {
    55  	AddLabelsToIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*github.Label, *github.Response, error)
    56  	RemoveLabelForIssue(ctx context.Context, owner string, repo string, number int, label string) (*github.Response, error)
    57  	Edit(ctx context.Context, owner string, repo string, number int, issue *github.IssueRequest) (*github.Issue, *github.Response, error)
    58  	Get(ctx context.Context, owner string, repo string, number int) (*github.Issue, *github.Response, error)
    59  	CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
    60  	AddAssignees(ctx context.Context, owner string, repo string, number int, assignees []string) (*github.Issue, *github.Response, error)
    61  	EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *github.IssueComment) (*github.IssueComment, *github.Response, error)
    62  	ListComments(ctx context.Context, owner string, repo string, number int, opts *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error)
    63  	EditMilestone(ctx context.Context, owner string, repo string, number int, milestone *github.Milestone) (*github.Milestone, *github.Response, error)
    64  	CreateMilestone(ctx context.Context, owner string, repo string, milestone *github.Milestone) (*github.Milestone, *github.Response, error)
    65  	ListMilestones(ctx context.Context, owner string, repo string, opt *github.MilestoneListOptions) ([]*github.Milestone, *github.Response, error)
    66  	ListByRepo(ctx context.Context, owner string, repo string, opts *github.IssueListByRepoOptions) ([]*github.Issue, *github.Response, error)
    67  }
    68  
    69  func (gc *GithubClient) Issues() IssueServiceInterface {
    70  	return gc.GithubIssues
    71  }
    72  
    73  type IssueService struct {
    74  	gis *github.IssuesService
    75  }
    76  
    77  func (is *IssueService) AddLabelsToIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*github.Label, *github.Response, error) {
    78  	return is.gis.AddLabelsToIssue(ctx, owner, repo, number, labels)
    79  }
    80  
    81  func (is *IssueService) RemoveLabelForIssue(ctx context.Context, owner string, repo string, number int, label string) (*github.Response, error) {
    82  	return is.gis.RemoveLabelForIssue(ctx, owner, repo, number, label)
    83  }
    84  
    85  func (is *IssueService) Edit(ctx context.Context, owner string, repo string, number int, issue *github.IssueRequest) (*github.Issue, *github.Response, error) {
    86  	return is.gis.Edit(ctx, owner, repo, number, issue)
    87  }
    88  
    89  func (is *IssueService) Get(ctx context.Context, owner string, repo string, number int) (*github.Issue, *github.Response, error) {
    90  	fmt.Println("in get")
    91  	return is.gis.Get(ctx, owner, repo, number)
    92  }
    93  
    94  func (is *IssueService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
    95  	return is.gis.CreateComment(ctx, owner, repo, number, comment)
    96  }
    97  
    98  func (is *IssueService) AddAssignees(ctx context.Context, owner string, repo string, number int, assignees []string) (*github.Issue, *github.Response, error) {
    99  	return is.gis.AddAssignees(ctx, owner, repo, number, assignees)
   100  }
   101  
   102  func (is *IssueService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
   103  	return is.gis.EditComment(ctx, owner, repo, commentID, comment)
   104  }
   105  
   106  func (is *IssueService) ListComments(ctx context.Context, owner string, repo string, number int, opts *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error) {
   107  	return is.gis.ListComments(ctx, owner, repo, number, opts)
   108  }
   109  
   110  func (is *IssueService) EditMilestone(ctx context.Context, owner string, repo string, number int, milestone *github.Milestone) (*github.Milestone, *github.Response, error) {
   111  	return is.gis.EditMilestone(ctx, owner, repo, number, milestone)
   112  }
   113  
   114  func (is *IssueService) CreateMilestone(ctx context.Context, owner string, repo string, milestone *github.Milestone) (*github.Milestone, *github.Response, error) {
   115  	return is.gis.CreateMilestone(ctx, owner, repo, milestone)
   116  }
   117  
   118  func (is *IssueService) ListMilestones(ctx context.Context, owner string, repo string, opt *github.MilestoneListOptions) ([]*github.Milestone, *github.Response, error) {
   119  	return is.gis.ListMilestones(ctx, owner, repo, opt)
   120  }
   121  func (is *IssueService) ListByRepo(ctx context.Context, owner string, repo string, opts *github.IssueListByRepoOptions) ([]*github.Issue, *github.Response, error) {
   122  	return is.gis.ListByRepo(ctx, owner, repo, opts)
   123  }
   124  
   125  //
   126  //  org mocks
   127  //
   128  //
   129  
   130  type OrganizationsServiceInterface interface {
   131  	ListProjects(ctx context.Context, org string, opts *github.ProjectListOptions) ([]*github.Project, *github.Response, error)
   132  }
   133  
   134  func (gc *GithubClient) Organizations() OrganizationsServiceInterface {
   135  	return gc.GithubOrganizations
   136  }
   137  
   138  type OrganizationsService struct {
   139  	gos *github.OrganizationsService
   140  }
   141  
   142  func (os *OrganizationsService) ListProjects(ctx context.Context, org string, opts *github.ProjectListOptions) ([]*github.Project, *github.Response, error) {
   143  	return os.gos.ListProjects(ctx, org, opts)
   144  }
   145  
   146  //
   147  // projects mocks
   148  //
   149  //
   150  
   151  type ProjectsServiceInterface interface {
   152  	GetProject(ctx context.Context, id int64) (*github.Project, *github.Response, error)
   153  	ListProjectColumns(ctx context.Context, projectID int64, opts *github.ListOptions) ([]*github.ProjectColumn, *github.Response, error)
   154  	CreateProjectCard(ctx context.Context, columnID int64, opts *github.ProjectCardOptions) (*github.ProjectCard, *github.Response, error)
   155  }
   156  
   157  func (gc *GithubClient) Projects() ProjectsServiceInterface {
   158  	return gc.GithubProjects
   159  }
   160  
   161  type ProjectsService struct {
   162  	gps *github.ProjectsService
   163  }
   164  
   165  func (ps *ProjectsService) GetProject(ctx context.Context, id int64) (*github.Project, *github.Response, error) {
   166  	return ps.gps.GetProject(ctx, id)
   167  }
   168  
   169  func (ps *ProjectsService) ListProjectColumns(ctx context.Context, projectID int64, opts *github.ListOptions) ([]*github.ProjectColumn, *github.Response, error) {
   170  	return ps.gps.ListProjectColumns(ctx, projectID, opts)
   171  }
   172  
   173  func (ps *ProjectsService) CreateProjectCard(ctx context.Context, columnID int64, opts *github.ProjectCardOptions) (*github.ProjectCard, *github.Response, error) {
   174  	return ps.gps.CreateProjectCard(ctx, columnID, opts)
   175  }
   176  
   177  //
   178  //  check mocks
   179  //
   180  //
   181  
   182  type ActionsServiceInterface interface {
   183  	GetWorkflowRunByID(ctx context.Context, owner string, repo string, runID int64) (*github.WorkflowRun, *github.Response, error)
   184  	GetWorkflowRunLogs(ctx context.Context, owner string, repo string, runID int64, followRedirects bool) (*url.URL, *github.Response, error)
   185  }
   186  
   187  func (gc *GithubClient) Actions() ActionsServiceInterface {
   188  	return gc.GithubActions
   189  }
   190  
   191  type ActionsService struct {
   192  	gas *github.ActionsService
   193  }
   194  
   195  func (as *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner string, repo string, runID int64, followRedirects bool) (*url.URL, *github.Response, error) {
   196  	return as.gas.GetWorkflowRunLogs(ctx, owner, repo, runID, followRedirects)
   197  }
   198  
   199  func (as *ActionsService) GetWorkflowRunByID(ctx context.Context, owner string, repo string, runID int64) (*github.WorkflowRun, *github.Response, error) {
   200  	return as.gas.GetWorkflowRunByID(ctx, owner, repo, runID)
   201  }
   202  
   203  //
   204  //  actions mocks
   205  //
   206  //
   207  
   208  type ChecksServiceInterface interface {
   209  	GetCheckSuite(ctx context.Context, owner string, repo string, checkSuiteID int64) (*github.CheckSuite, *github.Response, error)
   210  	ListCheckRunsCheckSuite(ctx context.Context, owner string, repo string, checkSuiteID int64, opts *github.ListCheckRunsOptions) (*github.ListCheckRunsResults, *github.Response, error)
   211  }
   212  
   213  func (gc *GithubClient) Checks() ChecksServiceInterface {
   214  	return gc.GithubChecks
   215  }
   216  
   217  type ChecksService struct {
   218  	gcs *github.ChecksService
   219  }
   220  
   221  func (os *ChecksService) GetCheckSuite(ctx context.Context, owner string, repo string, checkSuiteID int64) (*github.CheckSuite, *github.Response, error) {
   222  	return os.gcs.GetCheckSuite(ctx, owner, repo, checkSuiteID)
   223  }
   224  
   225  func (os *ChecksService) ListCheckRunsCheckSuite(ctx context.Context, owner string, repo string, checkSuiteID int64, opts *github.ListCheckRunsOptions) (*github.ListCheckRunsResults, *github.Response, error) {
   226  	return os.gcs.ListCheckRunsCheckSuite(ctx, owner, repo, checkSuiteID, opts)
   227  }
   228  
   229  //
   230  //  repos mocks
   231  //
   232  //
   233  
   234  type RepositoriesServiceInterface interface {
   235  	GetCommit(ctx context.Context, owner string, repo string, sha string, opts *github.ListOptions) (*github.RepositoryCommit, *github.Response, error)
   236  	GetContents(ctx context.Context, owner string, repo string, path string, opts *github.RepositoryContentGetOptions) (fileContent *github.RepositoryContent, directoryContent []*github.RepositoryContent, resp *github.Response, err error)
   237  	Get(ctx context.Context, owner, repo string) (*github.Repository, *github.Response, error)
   238  }
   239  
   240  func (gc *GithubClient) Repositories() RepositoriesServiceInterface {
   241  	return gc.GithubRepositories
   242  }
   243  
   244  type RepositoriesService struct {
   245  	grs *github.RepositoriesService
   246  }
   247  
   248  func (rs *RepositoriesService) GetCommit(ctx context.Context, owner string, repo string, sha string, opts *github.ListOptions) (*github.RepositoryCommit, *github.Response, error) {
   249  	return rs.grs.GetCommit(ctx, owner, repo, sha, opts)
   250  }
   251  
   252  func (rs *RepositoriesService) GetContents(ctx context.Context, owner string, repo string, path string, opts *github.RepositoryContentGetOptions) (fileContent *github.RepositoryContent, directoryContent []*github.RepositoryContent, resp *github.Response, err error) {
   253  	return rs.grs.GetContents(ctx, owner, repo, path, opts)
   254  }
   255  
   256  func (rs *RepositoriesService) Get(ctx context.Context, owner, repo string) (*github.Repository, *github.Response, error) {
   257  	return rs.grs.Get(ctx, owner, repo)
   258  }
   259  
   260  //
   261  //  pull request mocks
   262  //
   263  //
   264  
   265  type PullRequestServiceInterface interface {
   266  	Get(ctx context.Context, owner string, repo string, number int) (*github.PullRequest, *github.Response, error)
   267  	List(ctx context.Context, owner string, repo string, opts *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error)
   268  }
   269  
   270  func (gc *GithubClient) PullRequests() PullRequestServiceInterface {
   271  	return gc.GithubPullRequests
   272  }
   273  
   274  type PullRequestsService struct {
   275  	gprs *github.PullRequestsService
   276  }
   277  
   278  func (prs *PullRequestsService) Get(ctx context.Context, owner string, repo string, number int) (*github.PullRequest, *github.Response, error) {
   279  	return prs.gprs.Get(ctx, owner, repo, number)
   280  }
   281  
   282  func (prs *PullRequestsService) List(ctx context.Context, owner string, repo string, opts *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) {
   283  	return prs.gprs.List(ctx, owner, repo, opts)
   284  }
   285  
   286  //
   287  // v4 mocks
   288  // from github.com/shurcooL/githubv4
   289  // and github.com/shurcooL/graphql
   290  //
   291  //
   292  
   293  type GithubV4Client struct {
   294  	Clientv4 *githubv4.Client
   295  }
   296  
   297  func NewGithubV4Client(clientv4 *githubv4.Client) *GithubV4Client {
   298  	return &GithubV4Client{
   299  		Clientv4: clientv4,
   300  	}
   301  }
   302  
   303  type GithubV4ClientInterface interface {
   304  	Query(ctx context.Context, q interface{}, variables map[string]interface{}) error
   305  	Mutate(ctx context.Context, m interface{}, input Input, variables map[string]interface{}) error
   306  }
   307  
   308  type Input interface{}
   309  
   310  type AddProjectNextItemInput struct {
   311  	ProjectID graphql.ID `json:"projectId"`
   312  	ContentID graphql.ID `json:"contentId"`
   313  }
   314  
   315  func (cv4 *GithubV4Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error {
   316  	return cv4.Clientv4.Query(ctx, q, variables)
   317  }
   318  
   319  func (cv4 *GithubV4Client) Mutate(ctx context.Context, m interface{}, input Input, variables map[string]interface{}) error {
   320  	return cv4.Clientv4.Mutate(ctx, m, input, variables)
   321  }
   322  
   323  //
   324  // The functions from here onward are mocks of Github functions used for our tests.
   325  // mock mocks
   326  //
   327  //
   328  
   329  type MockGithubClient struct {
   330  	Client              *github.Client
   331  	GithubIssues        IssueServiceInterface
   332  	GithubOrganizations OrganizationsServiceInterface
   333  	GithubProjects      ProjectsServiceInterface
   334  	GithubChecks        ChecksServiceInterface
   335  	GithubActions       ActionsServiceInterface
   336  	GithubRepositories  RepositoriesServiceInterface
   337  	GithubPullRequests  PullRequestServiceInterface
   338  }
   339  
   340  // issue mocks
   341  func (mgc *MockGithubClient) Issues() IssueServiceInterface {
   342  	return mgc.GithubIssues
   343  }
   344  
   345  type MockIssueService struct {
   346  	// gis *github.IssuesService
   347  	// add other fields that define an issue
   348  	Title        string
   349  	Labels       []string
   350  	Assignees    []*github.User
   351  	Body         string
   352  	Comments     int
   353  	ID           int
   354  	RelatedIssue github.Issue
   355  	Milestone    *github.Milestone
   356  }
   357  
   358  // nolint:revive
   359  func (is *MockIssueService) AddLabelsToIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*github.Label, *github.Response, error) {
   360  	fmt.Print("adding")
   361  	fmt.Printf("%+v", is)
   362  	is.Labels = append(is.Labels, labels...)
   363  	fmt.Printf("%+v", is.Labels)
   364  	var l []*github.Label
   365  
   366  	l = append(l, &github.Label{Name: github.String(labels[0])})
   367  	return l, nil, nil
   368  }
   369  
   370  // nolint:revive
   371  func (is *MockIssueService) RemoveLabelForIssue(ctx context.Context, owner string, repo string, number int, label string) (*github.Response, error) {
   372  	fmt.Print("removing")
   373  	fmt.Printf("labels: %+v", is.Labels)
   374  
   375  	newitems := []string{}
   376  	for _, i := range is.Labels {
   377  		if i != label {
   378  			newitems = append(newitems, i)
   379  		}
   380  	}
   381  	// return newitems
   382  
   383  	is.Labels = newitems
   384  	fmt.Printf("remaining labels: %+v", is.Labels)
   385  
   386  	return nil, nil
   387  }
   388  
   389  // nolint:revive
   390  func (is *MockIssueService) Edit(ctx context.Context, owner string, repo string, number int, issue *github.IssueRequest) (*github.Issue, *github.Response, error) {
   391  	fmt.Print("editing")
   392  	if issue.Body != nil {
   393  		is.Body = *issue.Body
   394  	}
   395  	if issue.Labels != nil {
   396  		is.Labels = *issue.Labels
   397  	}
   398  	return nil, nil, nil
   399  }
   400  
   401  // nolint:revive
   402  func (is *MockIssueService) UpdateTitle(ctx context.Context, owner string, repo string, number int, issue *github.IssueRequest) (*github.Issue, *github.Response, error) {
   403  	fmt.Print("editing title")
   404  
   405  	if issue.Title != nil {
   406  		is.Title = *issue.Title
   407  	}
   408  
   409  	return nil, nil, nil
   410  }
   411  
   412  // nolint:revive
   413  func (is *MockIssueService) Get(ctx context.Context, owner string, repo string, number int) (*github.Issue, *github.Response, error) {
   414  	fmt.Print("getting")
   415  	issue := &github.Issue{}
   416  	var labels []*github.Label
   417  	if is.RelatedIssue.Number != nil {
   418  		return &is.RelatedIssue, nil, nil
   419  	}
   420  	issue.Assignees = is.Assignees
   421  	for i := range is.Labels {
   422  		labels = append(labels, &github.Label{Name: &is.Labels[i]})
   423  	}
   424  	issue.Labels = labels
   425  	//issue.Body = github.String(is.Body)
   426  	return issue, nil, nil
   427  }
   428  
   429  // nolint:revive
   430  func (is *MockIssueService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
   431  	fmt.Print("creating comment")
   432  	is.Comments++
   433  	return nil, nil, nil
   434  }
   435  
   436  // nolint:revive
   437  func (is *MockIssueService) AddAssignees(ctx context.Context, owner string, repo string, number int, assignees []string) (*github.Issue, *github.Response, error) {
   438  	fmt.Print("adding assignee")
   439  	fmt.Printf("%+v", is)
   440  	users := []*github.User{}
   441  	for i := range assignees {
   442  		users = append(users, &github.User{Login: &assignees[i]})
   443  	}
   444  	is.Assignees = users
   445  	return nil, nil, nil
   446  }
   447  
   448  // nolint:revive
   449  func (is *MockIssueService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *github.IssueComment) (*github.IssueComment, *github.Response, error) {
   450  	fmt.Print("EditComment")
   451  	return nil, nil, nil
   452  }
   453  
   454  // nolint:revive
   455  func (is *MockIssueService) ListComments(ctx context.Context, owner string, repo string, number int, opts *github.IssueListCommentsOptions) ([]*github.IssueComment, *github.Response, error) {
   456  	fmt.Print("ListComments")
   457  	return nil, nil, nil
   458  }
   459  
   460  // nolint:revive
   461  func (is *MockIssueService) EditMilestone(ctx context.Context, owner string, repo string, number int, milestone *github.Milestone) (*github.Milestone, *github.Response, error) {
   462  	if milestone.State != nil {
   463  		is.Milestone.State = milestone.State
   464  	}
   465  	return nil, nil, nil
   466  }
   467  
   468  // nolint:revive
   469  func (is *MockIssueService) CreateMilestone(ctx context.Context, owner string, repo string, milestone *github.Milestone) (*github.Milestone, *github.Response, error) {
   470  	is.Milestone = milestone
   471  	return milestone, nil, nil
   472  }
   473  
   474  // nolint:revive
   475  func (is *MockIssueService) ListMilestones(ctx context.Context, owner string, repo string, opt *github.MilestoneListOptions) ([]*github.Milestone, *github.Response, error) {
   476  	return nil, nil, nil
   477  }
   478  
   479  // nolint:revive
   480  func (is *MockIssueService) ListByRepo(ctx context.Context, owner string, repo string, opts *github.IssueListByRepoOptions) ([]*github.Issue, *github.Response, error) {
   481  	return nil, nil, nil
   482  }
   483  
   484  // organization mocks
   485  func (mgc *MockGithubClient) Organizations() OrganizationsServiceInterface {
   486  	return mgc.GithubOrganizations
   487  }
   488  
   489  type MockOrganizationsService struct {
   490  	// gis *github.IssuesService
   491  	// add other fields that define an issue
   492  	Login    string
   493  	Name     string
   494  	URL      string
   495  	Projects []int
   496  }
   497  
   498  // nolint:revive
   499  func (mos *MockOrganizationsService) ListProjects(ctx context.Context, org string, opts *github.ProjectListOptions) ([]*github.Project, *github.Response, error) {
   500  	fmt.Print("listing projects")
   501  	if len(mos.Projects) <= 0 {
   502  		return nil, nil, nil
   503  	}
   504  	var projects []*github.Project
   505  	for i := range mos.Projects {
   506  		projects = append(projects, &github.Project{
   507  			ID:       github.Int64(int64(mos.Projects[i])),
   508  			Number:   github.Int(mos.Projects[i]),
   509  			OwnerURL: github.String(org),
   510  			NodeID:   github.String("nodeID"),
   511  			Name:     github.String("test"),
   512  		})
   513  	}
   514  	return projects, nil, nil
   515  }
   516  
   517  // project mocks
   518  func (mgc *MockGithubClient) Projects() ProjectsServiceInterface {
   519  	return mgc.GithubProjects
   520  }
   521  
   522  type MockProjectsService struct {
   523  	// gis *github.IssuesService
   524  	// add other fields that define an issue
   525  	ID       int64
   526  	Number   int
   527  	OwnerURL string
   528  	NodeID   string
   529  	Name     string
   530  	Cards    int
   531  	Columns  int
   532  }
   533  
   534  // nolint:revive
   535  func (mps *MockProjectsService) GetProject(ctx context.Context, id int64) (*github.Project, *github.Response, error) {
   536  	fmt.Print("getting projects")
   537  	return &github.Project{
   538  		ID:       github.Int64(mps.ID),
   539  		Number:   github.Int(mps.Number),
   540  		OwnerURL: github.String(mps.OwnerURL),
   541  		NodeID:   github.String(mps.NodeID),
   542  	}, nil, nil
   543  }
   544  
   545  // nolint:revive
   546  func (mps *MockProjectsService) ListProjectColumns(ctx context.Context, projectID int64, opts *github.ListOptions) ([]*github.ProjectColumn, *github.Response, error) {
   547  	fmt.Print("listing project columns")
   548  	var columns []*github.ProjectColumn
   549  	for i := 1; i <= mps.Columns; i++ {
   550  		columns = append(columns, &github.ProjectColumn{ID: github.Int64(mps.ID)})
   551  	}
   552  	return columns, nil, nil
   553  }
   554  
   555  // nolint:revive
   556  func (mps *MockProjectsService) CreateProjectCard(ctx context.Context, columnID int64, opts *github.ProjectCardOptions) (*github.ProjectCard, *github.Response, error) {
   557  	fmt.Print("creating project cards")
   558  	mps.Cards++
   559  	return &github.ProjectCard{ColumnID: github.Int64(columnID)}, nil, nil
   560  }
   561  
   562  // checks mocks
   563  func (mgc *MockGithubClient) Checks() ChecksServiceInterface {
   564  	return mgc.GithubChecks
   565  }
   566  
   567  type MockChecksService struct {
   568  	// gis *github.IssuesService
   569  	// add other fields that define an issue
   570  }
   571  
   572  // nolint:revive
   573  func (mcs *MockChecksService) GetCheckSuite(ctx context.Context, owner string, repo string, checkSuiteID int64) (*github.CheckSuite, *github.Response, error) {
   574  	fmt.Print("GetCheckSuite")
   575  	return nil, nil, nil
   576  }
   577  
   578  // nolint:revive
   579  func (mcs *MockChecksService) ListCheckRunsCheckSuite(ctx context.Context, owner string, repo string, checkSuiteID int64, opts *github.ListCheckRunsOptions) (*github.ListCheckRunsResults, *github.Response, error) {
   580  	fmt.Print("ListCheckRunsCheckSuite")
   581  	return nil, nil, nil
   582  }
   583  
   584  // actions mocks
   585  func (mgc *MockGithubClient) Actions() ActionsServiceInterface {
   586  	return mgc.GithubActions
   587  }
   588  
   589  type MockActionsService struct {
   590  	// gis *github.IssuesService
   591  	// add other fields that define an issue
   592  }
   593  
   594  // nolint:revive
   595  func (mas *MockActionsService) GetWorkflowRunLogs(ctx context.Context, owner string, repo string, runID int64, followRedirects bool) (*url.URL, *github.Response, error) {
   596  	fmt.Print("GetWorkflowRunLogs")
   597  	return nil, nil, nil
   598  }
   599  
   600  // nolint:revive
   601  func (mas *MockActionsService) GetWorkflowRunByID(ctx context.Context, owner string, repo string, runID int64) (*github.WorkflowRun, *github.Response, error) {
   602  	fmt.Print("GetWorkflowRunByID")
   603  	return nil, nil, nil
   604  }
   605  
   606  //
   607  //  repos mocks
   608  //
   609  //
   610  
   611  func (mgc *MockGithubClient) Repositories() RepositoriesServiceInterface {
   612  	return mgc.GithubRepositories
   613  }
   614  
   615  type MockRepositoriesService struct {
   616  	// grs *github.RepositoriesService
   617  }
   618  
   619  // nolint:revive
   620  func (mrs *MockRepositoriesService) GetCommit(ctx context.Context, owner string, repo string, sha string, opts *github.ListOptions) (*github.RepositoryCommit, *github.Response, error) {
   621  	fmt.Print("GetCommit")
   622  	return nil, nil, nil
   623  }
   624  
   625  // nolint:revive
   626  func (mrs *MockRepositoriesService) GetContents(ctx context.Context, owner string, repo string, path string, opts *github.RepositoryContentGetOptions) (fileContent *github.RepositoryContent, directoryContent []*github.RepositoryContent, resp *github.Response, err error) {
   627  	fmt.Print("GetContents")
   628  	return nil, nil, nil, nil
   629  }
   630  
   631  // nolint:revive
   632  func (mrs *MockRepositoriesService) Get(ctx context.Context, owner, repo string) (*github.Repository, *github.Response, error) {
   633  	fmt.Print("Get")
   634  	return nil, nil, nil
   635  }
   636  
   637  //
   638  //  pull requests mock
   639  //
   640  //
   641  
   642  func (mgc *MockGithubClient) PullRequests() PullRequestServiceInterface {
   643  	return mgc.GithubPullRequests
   644  }
   645  
   646  type MockPullRequestService struct {
   647  	Title     string
   648  	Labels    []string
   649  	Assignees []*github.User
   650  	Body      string
   651  	Comments  int
   652  	ID        int
   653  	// grs *github.RepositoriesService
   654  }
   655  
   656  // PRRequest represents a request to create/edit a pull request.
   657  type PRRequest struct {
   658  	Title     *string   `json:"title,omitempty"`
   659  	Body      *string   `json:"body,omitempty"`
   660  	Labels    *[]string `json:"labels,omitempty"`
   661  	Assignee  *string   `json:"assignee,omitempty"`
   662  	State     *string   `json:"state,omitempty"`
   663  	Milestone *int      `json:"milestone,omitempty"`
   664  	Assignees *[]string `json:"assignees,omitempty"`
   665  }
   666  
   667  // nolint:revive
   668  func (mrs *MockPullRequestService) AddLabelsToPR(ctx context.Context, owner string, repo string, number int, labels []string) ([]*github.Label, *github.Response, error) {
   669  	fmt.Print("adding")
   670  	fmt.Printf("%+v", mrs)
   671  	mrs.Labels = append(mrs.Labels, labels...)
   672  	fmt.Printf("%+v", mrs.Labels)
   673  	var l []*github.Label
   674  
   675  	l = append(l, &github.Label{Name: github.String(labels[0])})
   676  	return l, nil, nil
   677  }
   678  
   679  // nolint:revive
   680  func (mrs *MockPullRequestService) RemoveLabelForPR(ctx context.Context, owner string, repo string, number int, label string) (*github.Response, error) {
   681  	fmt.Print("removing")
   682  	fmt.Printf("labels: %+v", mrs.Labels)
   683  
   684  	newitems := []string{}
   685  	for _, i := range mrs.Labels {
   686  		if i != label {
   687  			newitems = append(newitems, i)
   688  		}
   689  	}
   690  	// return newitems
   691  
   692  	mrs.Labels = newitems
   693  	fmt.Printf("remaining labels: %+v", mrs.Labels)
   694  
   695  	return nil, nil
   696  }
   697  
   698  // nolint:revive
   699  func (mrs *MockPullRequestService) Edit(ctx context.Context, owner string, repo string, number int, pr *github.IssueRequest) (*github.PullRequest, *github.Response, error) {
   700  	fmt.Print("editing")
   701  	if pr.Body != nil {
   702  		mrs.Body = *pr.Body
   703  	}
   704  	if pr.Labels != nil {
   705  		mrs.Labels = *pr.Labels
   706  	}
   707  	if pr.Title != nil {
   708  		mrs.Title = *pr.Title
   709  	}
   710  	return nil, nil, nil
   711  }
   712  
   713  // nolint:revive
   714  func (mrs *MockPullRequestService) Get(ctx context.Context, owner string, repo string, number int) (*github.PullRequest, *github.Response, error) {
   715  	fmt.Print("Getting PR")
   716  	var pr *github.PullRequest
   717  	var labels []*github.Label
   718  	pr.Assignees = mrs.Assignees
   719  	for i := range mrs.Labels {
   720  		labels = append(labels, &github.Label{Name: &mrs.Labels[i]})
   721  	}
   722  	pr.Labels = labels
   723  	return pr, nil, nil
   724  }
   725  
   726  // nolint:revive
   727  func (mrs *MockPullRequestService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *github.PullRequestComment) (*github.PullRequestComment, *github.Response, error) {
   728  	fmt.Print("creating comment")
   729  	mrs.Comments++
   730  	return nil, nil, nil
   731  }
   732  
   733  // nolint:revive
   734  func (mrs *MockPullRequestService) AddAssignees(ctx context.Context, owner string, repo string, number int, assignees []string) (*github.PullRequest, *github.Response, error) {
   735  	fmt.Print("adding assignee")
   736  	fmt.Printf("%+v", mrs)
   737  	users := []*github.User{}
   738  	for i := range assignees {
   739  		users = append(users, &github.User{Login: &assignees[i]})
   740  	}
   741  	mrs.Assignees = users
   742  	return nil, nil, nil
   743  }
   744  
   745  // nolint:revive
   746  func (mrs *MockPullRequestService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *github.PullRequestComment) (*github.PullRequestComment, *github.Response, error) {
   747  	fmt.Print("EditComment")
   748  	return nil, nil, nil
   749  }
   750  
   751  // nolint:revive
   752  func (mrs *MockPullRequestService) ListComments(ctx context.Context, owner string, repo string, number int, opts *github.PullRequestListCommentsOptions) ([]*github.PullRequestComment, *github.Response, error) {
   753  	fmt.Print("ListComments")
   754  	return nil, nil, nil
   755  }
   756  
   757  // nolint:revive
   758  func (mrs *MockPullRequestService) List(ctx context.Context, owner string, repo string, opts *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) {
   759  	fmt.Print("List")
   760  	return nil, nil, nil
   761  }
   762  
   763  //
   764  // v4 mocks
   765  //
   766  //
   767  
   768  type MockGithubV4Client struct {
   769  	ProjectNodeID     string
   770  	ProjectNextItemID string
   771  }
   772  
   773  // nolint:revive
   774  func (cv4 *MockGithubV4Client) Query(ctx context.Context, q interface{}, variables map[string]interface{}) error {
   775  	fmt.Println("mock query...")
   776  	cv4.ProjectNodeID = "pnid"
   777  	return nil
   778  }
   779  
   780  // nolint:revive
   781  func (cv4 *MockGithubV4Client) Mutate(ctx context.Context, m interface{}, input Input, variables map[string]interface{}) error {
   782  	fmt.Println("mock mutate...")
   783  	cv4.ProjectNextItemID = "next"
   784  	return nil
   785  }
   786  

View as plain text