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

     1  package github
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/google/go-github/v44/github"
     9  )
    10  
    11  func (g *Github) GetPullRequest(repository string, id int) (*github.PullRequest, error) {
    12  	pr, _, err := g.client.PullRequests.Get(context.Background(), g.organization, repository, id)
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  	return pr, nil
    17  }
    18  
    19  func (g *Github) CreatePullRequest(repository, title, body, head, base string) (*github.PullRequest, error) {
    20  	newPR := &github.NewPullRequest{
    21  		Title: &title,
    22  		Body:  &body,
    23  		Head:  &head,
    24  		Base:  &base,
    25  	}
    26  	pr, _, err := g.client.PullRequests.Create(context.Background(), g.organization, repository, newPR)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return pr, nil
    31  }
    32  
    33  func (g *Github) ListPullRequests(repository string) ([]*github.PullRequest, error) {
    34  	prs, _, err := g.client.PullRequests.List(context.Background(), g.organization, repository, &github.PullRequestListOptions{})
    35  	if err != nil {
    36  		return nil, fmt.Errorf("error when listing pull requests for the repo %s: %v", repository, err)
    37  	}
    38  
    39  	return prs, nil
    40  }
    41  
    42  func (g *Github) ListPullRequestCommentsSince(repository string, prNumber int, since time.Time) ([]*github.IssueComment, error) {
    43  	comments, _, err := g.client.Issues.ListComments(context.Background(), g.organization, repository, prNumber, &github.IssueListCommentsOptions{
    44  		Since:     &since,
    45  		Sort:      github.String("created"),
    46  		Direction: github.String("asc"),
    47  	})
    48  	if err != nil {
    49  		return nil, fmt.Errorf("error when listing pull requests comments for the repo %s: %v", repository, err)
    50  	}
    51  
    52  	return comments, nil
    53  }
    54  
    55  func (g *Github) MergePullRequest(repository string, prNumber int) (*github.PullRequestMergeResult, error) {
    56  	mergeResult, _, err := g.client.PullRequests.Merge(context.Background(), g.organization, repository, prNumber, "", &github.PullRequestOptions{})
    57  	if err != nil {
    58  		return nil, fmt.Errorf("error when merging pull request number %d for the repo %s: %v", prNumber, repository, err)
    59  	}
    60  
    61  	return mergeResult, nil
    62  }
    63  
    64  func (g *Github) ListCheckRuns(repository string, ref string) ([]*github.CheckRun, error) {
    65  	checkRunResults, _, err := g.client.Checks.ListCheckRunsForRef(context.Background(), g.organization, repository, ref, &github.ListCheckRunsOptions{})
    66  	if err != nil {
    67  		return nil, fmt.Errorf("error when listing check runs for the repo %s and ref %s: %v", repository, ref, err)
    68  	}
    69  	return checkRunResults.CheckRuns, nil
    70  }
    71  
    72  func (g *Github) GetCheckRun(repository string, id int64) (*github.CheckRun, error) {
    73  	checkRun, _, err := g.client.Checks.GetCheckRun(context.Background(), g.organization, repository, id)
    74  	if err != nil {
    75  		return nil, fmt.Errorf("error when getting check run with id %d for the repo %s: %v", id, repository, err)
    76  	}
    77  	return checkRun, nil
    78  }
    79  
    80  func (g *Github) GetPRDetails(ghRepo string, prID int) (string, string, error) {
    81  	pullRequest, err := g.GetPullRequest(ghRepo, prID)
    82  	if err != nil {
    83  		return "", "", err
    84  	}
    85  	return *pullRequest.Head.Repo.CloneURL, *pullRequest.Head.Ref, nil
    86  }