Skip to content

Commit

Permalink
refactor: use generic client in gitlab build tests
Browse files Browse the repository at this point in the history
- Use generic, provider-agnostic, Git client
functions leading to a nicer,
more readable and maintainable code
  • Loading branch information
tnevrlka committed Oct 17, 2024
1 parent d9c1df1 commit 0e3bb66
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 176 deletions.
1 change: 1 addition & 0 deletions pkg/clients/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ type Client interface {
GetFileContent(repository, pathToFile, branchName string) (string, error)
CreatePullRequest(repository, title, body, head, base string) (*PullRequest, error)
MergePullRequest(repository string, prNumber int) (*PullRequest, error)
DeleteBranchAndClosePullRequest(repository string, prNumber int) error
CleanupWebhooks(repository, clusterAppDomain string) error
}
12 changes: 12 additions & 0 deletions pkg/clients/git/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,15 @@ func (g *GitHubClient) CleanupWebhooks(repository, clusterAppDomain string) erro
}
return nil
}

func (g *GitHubClient) DeleteBranchAndClosePullRequest(repository string, prNumber int) error {
pr, err := g.Github.GetPullRequest(repository, prNumber)
if err != nil {
return err
}
err = g.DeleteBranch(repository, pr.Head.GetRef())
if err != nil && strings.Contains(err.Error(), "Reference does not exist") {
return nil
}
return err
}
12 changes: 12 additions & 0 deletions pkg/clients/git/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,15 @@ func (g *GitLabClient) CreatePullRequest(repository, title, body, head, base str
func (g *GitLabClient) CleanupWebhooks(repository, clusterAppDomain string) error {
return g.DeleteWebhooks(repository, clusterAppDomain)
}

func (g *GitLabClient) DeleteBranchAndClosePullRequest(repository string, prNumber int) error {
mr, _, err := g.GitlabClient.GetClient().MergeRequests.GetMergeRequest(repository, prNumber, nil)
if err != nil {
return err
}
err = g.DeleteBranch(repository, mr.SourceBranch)
if err != nil {
return err
}
return g.CloseMergeRequest(repository, prNumber)
}
Loading

0 comments on commit 0e3bb66

Please sign in to comment.