github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/pkg/apis/github/repositories.go (about)

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/google/go-github/v44/github"
     8  	. "github.com/onsi/ginkgo/v2"
     9  )
    10  
    11  func (g *Github) CheckIfRepositoryExist(repository string) bool {
    12  	_, resp, err := g.client.Repositories.Get(context.Background(), g.organization, repository)
    13  	if err != nil {
    14  		GinkgoWriter.Printf("error when sending request to Github API: %v\n", err)
    15  		return false
    16  	}
    17  	GinkgoWriter.Printf("repository %s status request to github: %d\n", repository, resp.StatusCode)
    18  	return resp.StatusCode == 200
    19  }
    20  
    21  func (g *Github) CreateFile(repository, pathToFile, fileContent, branchName string) (*github.RepositoryContentResponse, error) {
    22  	opts := &github.RepositoryContentFileOptions{
    23  		Message: github.String("e2e test commit message"),
    24  		Content: []byte(fileContent),
    25  		Branch:  github.String(branchName),
    26  	}
    27  
    28  	file, _, err := g.client.Repositories.CreateFile(context.Background(), g.organization, repository, pathToFile, opts)
    29  	if err != nil {
    30  		return nil, fmt.Errorf("error when creating file contents: %v", err)
    31  	}
    32  
    33  	return file, nil
    34  }
    35  
    36  func (g *Github) GetFile(repository, pathToFile, branchName string) (*github.RepositoryContent, error) {
    37  	opts := &github.RepositoryContentGetOptions{}
    38  	if branchName != "" {
    39  		opts.Ref = fmt.Sprintf("heads/%s", branchName)
    40  	}
    41  	file, _, _, err := g.client.Repositories.GetContents(context.Background(), g.organization, repository, pathToFile, opts)
    42  	if err != nil {
    43  		return nil, fmt.Errorf("error when listing file contents: %v", err)
    44  	}
    45  
    46  	return file, nil
    47  }
    48  
    49  func (g *Github) UpdateFile(repository, pathToFile, newContent, branchName, fileSHA string) (*github.RepositoryContentResponse, error) {
    50  	opts := &github.RepositoryContentGetOptions{}
    51  	if branchName != "" {
    52  		opts.Ref = fmt.Sprintf("heads/%s", branchName)
    53  	}
    54  	newFileContent := &github.RepositoryContentFileOptions{
    55  		Message: github.String("e2e test commit message"),
    56  		SHA:     github.String(fileSHA),
    57  		Content: []byte(newContent),
    58  		Branch:  github.String(branchName),
    59  	}
    60  	updatedFile, _, err := g.client.Repositories.UpdateFile(context.Background(), g.organization, repository, pathToFile, newFileContent)
    61  	if err != nil {
    62  		return nil, fmt.Errorf("error when updating a file on github: %v", err)
    63  	}
    64  
    65  	return updatedFile, nil
    66  }
    67  
    68  func (g *Github) DeleteFile(repository, pathToFile, branchName string) error {
    69  	getOpts := &github.RepositoryContentGetOptions{}
    70  	deleteOpts := &github.RepositoryContentFileOptions{}
    71  
    72  	if branchName != "" {
    73  		getOpts.Ref = fmt.Sprintf("heads/%s", branchName)
    74  		deleteOpts.Branch = github.String(branchName)
    75  	}
    76  	file, _, _, err := g.client.Repositories.GetContents(context.Background(), g.organization, repository, pathToFile, getOpts)
    77  	if err != nil {
    78  		return fmt.Errorf("error when listing file contents on github: %v", err)
    79  	}
    80  
    81  	deleteOpts = &github.RepositoryContentFileOptions{
    82  		Message: github.String("delete test files"),
    83  		SHA:     github.String(file.GetSHA()),
    84  	}
    85  
    86  	_, _, err = g.client.Repositories.DeleteFile(context.Background(), g.organization, repository, pathToFile, deleteOpts)
    87  	if err != nil {
    88  		return fmt.Errorf("error when deleting file on github: %v", err)
    89  	}
    90  	return nil
    91  }
    92  
    93  func (g *Github) GetAllRepositories() ([]*github.Repository, error) {
    94  
    95  	opt := &github.RepositoryListByOrgOptions{
    96  		ListOptions: github.ListOptions{
    97  			PerPage: 100,
    98  		},
    99  	}
   100  	var allRepos []*github.Repository
   101  	for {
   102  		repos, resp, err := g.client.Repositories.ListByOrg(context.Background(), g.organization, opt)
   103  		if err != nil {
   104  			return nil, err
   105  		}
   106  		allRepos = append(allRepos, repos...)
   107  		if resp.NextPage == 0 {
   108  			break
   109  		}
   110  		opt.Page = resp.NextPage
   111  	}
   112  	return allRepos, nil
   113  }
   114  
   115  func (g *Github) DeleteRepository(repository *github.Repository) error {
   116  	GinkgoWriter.Printf("Deleting repository %s\n", *repository.Name)
   117  	_, err := g.client.Repositories.Delete(context.Background(), g.organization, *repository.Name)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	return nil
   122  }