github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/clients/gitlab/git.go (about)

     1  package gitlab
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"time"
     7  
     8  	. "github.com/onsi/gomega"
     9  	gitlab "github.com/xanzy/go-gitlab"
    10  )
    11  
    12  // CreateBranch creates a new branch in a GitLab project with the given projectID and newBranchName
    13  func (gc *GitlabClient) CreateBranch(projectID, newBranchName, defaultBranch string) error {
    14  	// Prepare the branch creation request
    15  	branchOpts := &gitlab.CreateBranchOptions{
    16  		Branch: gitlab.Ptr(newBranchName),
    17  		Ref:    gitlab.Ptr(defaultBranch),
    18  	}
    19  
    20  	// Perform the branch creation
    21  	_, _, err := gc.client.Branches.CreateBranch(projectID, branchOpts)
    22  	if err != nil {
    23  		return fmt.Errorf("failed to create branch %s in project %s: %w", newBranchName, projectID, err)
    24  	}
    25  
    26  	// Wait for the branch to actually exist
    27  	Eventually(func(gomega Gomega) {
    28  		exist, err := gc.ExistsBranch(projectID, newBranchName)
    29  		gomega.Expect(err).NotTo(HaveOccurred())
    30  		gomega.Expect(exist).To(BeTrue())
    31  
    32  	}, 2*time.Minute, 2*time.Second).Should(Succeed())
    33  
    34  	return nil
    35  }
    36  
    37  // ExistsBranch checks if a branch exists in a specified GitLab repository.
    38  func (gc *GitlabClient) ExistsBranch(projectID, branchName string) (bool, error) {
    39  
    40  	fmt.Println("ExistRdf dddd")
    41  	_, _, err := gc.client.Branches.GetBranch(projectID, branchName)
    42  	if err == nil {
    43  		return true, nil
    44  	}
    45  	if err, ok := err.(*gitlab.ErrorResponse); ok && err.Response.StatusCode == 404 {
    46  		return false, nil
    47  	}
    48  	return false, err
    49  }
    50  
    51  // DeleteBranch deletes a branch by its name and project ID
    52  func (gc *GitlabClient) DeleteBranch(projectID, branchName string) error {
    53  
    54  	_, err := gc.client.Branches.DeleteBranch(projectID, branchName)
    55  	if err != nil {
    56  		return fmt.Errorf("failed to delete branch %s: %v", branchName, err)
    57  	}
    58  
    59  	fmt.Printf("Deleted branch: %s", branchName)
    60  
    61  	return nil
    62  }
    63  
    64  // CreateGitlabNewBranch creates a new branch
    65  func (gc *GitlabClient) CreateGitlabNewBranch(projectID, branchName, sha, baseBranch string) error {
    66  
    67  	// If sha is not provided, get the latest commit from the base branch
    68  	if sha == "" {
    69  		commit, _, err := gc.client.Commits.GetCommit(projectID, baseBranch)
    70  		if err != nil {
    71  			return fmt.Errorf("failed to get latest commit from base branch: %v", err)
    72  		}
    73  		sha = commit.ID
    74  	}
    75  
    76  	opt := &gitlab.CreateBranchOptions{
    77  		Branch: &branchName,
    78  		Ref:    &sha,
    79  	}
    80  	_, resp, err := gc.client.Branches.CreateBranch(projectID, opt)
    81  	if err != nil {
    82  		// Check if the error is due to the branch already existing
    83  		if resp != nil && resp.StatusCode == http.StatusConflict {
    84  			return fmt.Errorf("branch '%s' already exists", branchName)
    85  		}
    86  		return fmt.Errorf("failed to create branch '%s': %v", branchName, err)
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  // GetMergeRequests returns a list of all MergeRequests in a given project ID and repository name
    93  func (gc *GitlabClient) GetMergeRequests() ([]*gitlab.MergeRequest, error) {
    94  
    95  	// Get merge requests using Gitlab client
    96  	mergeRequests, _, err := gc.client.MergeRequests.ListMergeRequests(&gitlab.ListMergeRequestsOptions{})
    97  	if err != nil {
    98  		// Handle error
    99  		return nil, err
   100  	}
   101  
   102  	return mergeRequests, nil
   103  }
   104  
   105  // CloseMergeRequest closes merge request in Gitlab repo by given MR IID
   106  func (gc *GitlabClient) CloseMergeRequest(projectID string, mergeRequestIID int) error {
   107  
   108  	// Get merge requests using Gitlab client
   109  	_, _, err := gc.client.MergeRequests.GetMergeRequest(projectID, mergeRequestIID, nil)
   110  	if err != nil {
   111  		return fmt.Errorf("failed to get MR of IID %d in projectID %s, %v", mergeRequestIID, projectID, err)
   112  	}
   113  
   114  	_, _, err = gc.client.MergeRequests.UpdateMergeRequest(projectID, mergeRequestIID, &gitlab.UpdateMergeRequestOptions{
   115  		StateEvent: gitlab.Ptr("close"),
   116  	})
   117  	if err != nil {
   118  		return fmt.Errorf("failed to close MR of IID %d in projectID %s, %v", mergeRequestIID, projectID, err)
   119  	}
   120  
   121  	return nil
   122  }