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 29, 2024
1 parent abc2ffe commit 063ae99
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 224 deletions.
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
github.com/moby/buildkit v0.12.5
github.com/onsi/ginkgo/v2 v2.17.3
github.com/onsi/gomega v1.33.1
github.com/opencontainers/image-spec v1.1.0
github.com/openshift-pipelines/pipelines-as-code v0.18.0
github.com/openshift/api v0.0.0-20230213134911-7ba313770556
github.com/openshift/client-go v0.0.0-20221019143426-16aed247da5c
Expand Down Expand Up @@ -171,7 +172,6 @@ require (
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/errors v0.21.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
Expand Down Expand Up @@ -241,7 +241,6 @@ require (
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/opencontainers/runc v1.1.14 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/operator-framework/operator-lib v0.13.0 // indirect
Expand All @@ -255,10 +254,7 @@ require (
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/statsd_exporter v0.23.1 // indirect
github.com/redhat-appstudio/application-api v0.0.0-20240106104232-18f545e48a03 // indirect
github.com/redhat-appstudio/image-controller v0.0.0-20231003082540-48893226ba8b // indirect
github.com/redhat-appstudio/integration-service v0.0.0-20231025084434-b3f521c408d1 // indirect
github.com/redhat-appstudio/operator-toolkit v0.0.0-20231201124606-2087182322ae // indirect
github.com/redis/go-redis/v9 v9.0.5 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1501,8 +1501,6 @@ github.com/konflux-ci/integration-service v0.0.0-20240513113947-6c76e6e0eb83 h1:
github.com/konflux-ci/integration-service v0.0.0-20240513113947-6c76e6e0eb83/go.mod h1:g/8KTH8JJLFw+F/GE5OXC7CnL3SLAY3w7tfBM4ZM/ng=
github.com/konflux-ci/operator-toolkit v0.0.0-20240402130556-ef6dcbeca69d h1:z7j3mglNoXvIrw5Vz/Ul+izoITRaqYURPIWrFoEyHgI=
github.com/konflux-ci/operator-toolkit v0.0.0-20240402130556-ef6dcbeca69d/go.mod h1:AcChx7FjpYSIkDvQgaUKyauuF0PXm3ivB5MqZSC9Eis=
github.com/konflux-ci/release-service v0.0.0-20240624130704-c6133cc0b908 h1:fVFlyCIB4eniA708isOgzCKYOeAXAsg8WFEZp1QQ3A4=
github.com/konflux-ci/release-service v0.0.0-20240624130704-c6133cc0b908/go.mod h1:p+6Ua4y2VUJRW2BrwSXtVoV7b4PwL8PwbjJb2LaPu44=
github.com/konflux-ci/release-service v0.0.0-20241016090118-bd730698b9c5 h1:eBQ2yFy/TRGhCGCbZC2KCM9rD/ADWorLxzpSmZ4JyKM=
github.com/konflux-ci/release-service v0.0.0-20241016090118-bd730698b9c5/go.mod h1:KLuLvi1boDfnFsQL0cXytBLc+TWsRo80YQFTygElRvI=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -1790,16 +1788,10 @@ github.com/prometheus/statsd_exporter v0.22.7/go.mod h1:N/TevpjkIh9ccs6nuzY3jQn9
github.com/prometheus/statsd_exporter v0.23.1 h1:TiNAE1XevlZZrpSbmf51l/Ryl2Eek9rYh//KlvcNvKw=
github.com/prometheus/statsd_exporter v0.23.1/go.mod h1:FFmnBRWf+HxX+PR+2fnc0ciBIONVAPJ6k4lqIbdqVxo=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/redhat-appstudio/application-api v0.0.0-20240106104232-18f545e48a03 h1:UUgrEyvQJhEnvghkEaY9YlxeAk+D4cop+Bd3+8jh0eQ=
github.com/redhat-appstudio/application-api v0.0.0-20240106104232-18f545e48a03/go.mod h1:YvckuKHe82eWloGk0/BpSw4YYG2owrGZAanztbOj3pQ=
github.com/redhat-appstudio/image-controller v0.0.0-20231003082540-48893226ba8b h1:kM+t/FZSMVUydXIbii+bduAJov7pcADCybP9u1WSBcw=
github.com/redhat-appstudio/image-controller v0.0.0-20231003082540-48893226ba8b/go.mod h1:fNP2oUEejlHZUkAz2f0ViKNl0D8w4cD8QVoGy1pri9c=
github.com/redhat-appstudio/integration-service v0.0.0-20231025084434-b3f521c408d1 h1:Qo5VBBhi8ihz3mPaCQq0lGZAiCRAqhbv1XZkzyBTfGA=
github.com/redhat-appstudio/integration-service v0.0.0-20231025084434-b3f521c408d1/go.mod h1:bh8uJ4whJ92ef5PXH6Rfm4F8tRXkDAlSYR7Pqp1LLT4=
github.com/redhat-appstudio/jvm-build-service v0.0.0-20240126122210-0e2ee7e2e5b0 h1:wqP4/99Lg68zRpzt9zSc5OGf3zigaBlvflDuDLmDLdM=
github.com/redhat-appstudio/jvm-build-service v0.0.0-20240126122210-0e2ee7e2e5b0/go.mod h1:awZP3qi/GpBhQ50N3rRjFPsGbFperwWYSTN+OVG1Gb8=
github.com/redhat-appstudio/operator-toolkit v0.0.0-20231201124606-2087182322ae h1:98thWrMrr9YUNCVNGqkZxKfZkaWUmMC5dmkgAI7p3uY=
github.com/redhat-appstudio/operator-toolkit v0.0.0-20231201124606-2087182322ae/go.mod h1:7cX2+4KGZLJ4Yoj+1v0iV5hkCGBzbSd9wkNJQjCdDJs=
github.com/redhat-cop/operator-utils v1.3.3-0.20220121120056-862ef22b8cdf/go.mod h1:FfTyeSCu+e2VLgeMh/1RFG8TSkVjKRPEyR6EmDt0RIw=
github.com/redis/go-redis/v9 v9.0.0-rc.4/go.mod h1:Vo3EsyWnicKnSKCA7HhgnvnyA74wOA69Cd2Meli5mmA=
github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o=
Expand Down
13 changes: 11 additions & 2 deletions pkg/clients/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@ type PullRequest struct {
HeadSHA string
}

// RepositoryFile represents a generic provider-agnostic file in a repository
type RepositoryFile struct {
// CommitSHA is the SHA of the commit in which the file was created
CommitSHA string
// Content is a string representation of the content of the file
Content string
}

type Client interface {
CreateBranch(repository, baseBranchName, revision, branchName string) error
DeleteBranch(repository, branchName string) error
BranchExists(repository, branchName string) (bool, error)
ListPullRequests(repository string) ([]*PullRequest, error)
CreateFile(repository, pathToFile, content, branchName string) error
GetFileContent(repository, pathToFile, branchName string) (string, error)
CreateFile(repository, pathToFile, content, branchName string) (*RepositoryFile, error)
GetFile(repository, pathToFile, branchName string) (*RepositoryFile, 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
}
36 changes: 29 additions & 7 deletions pkg/clients/git/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,31 @@ func (g *GitHubClient) ListPullRequests(repository string) ([]*PullRequest, erro
return pullRequests, nil
}

func (g *GitHubClient) CreateFile(repository, pathToFile, content, branchName string) error {
_, err := g.Github.CreateFile(repository, pathToFile, content, branchName)
return err
func (g *GitHubClient) CreateFile(repository, pathToFile, content, branchName string) (*RepositoryFile, error) {
file, err := g.Github.CreateFile(repository, pathToFile, content, branchName)
if err != nil {
return nil, err
}
resultFile := &RepositoryFile{
CommitSHA: file.GetSHA(),
}
return resultFile, nil
}

func (g *GitHubClient) GetFileContent(repository, pathToFile, branchName string) (string, error) {
func (g *GitHubClient) GetFile(repository, pathToFile, branchName string) (*RepositoryFile, error) {
contents, err := g.Github.GetFile(repository, pathToFile, branchName)
if err != nil {
return "", err
return nil, err
}
content, err := contents.GetContent()
if err != nil {
return "", err
return nil, err
}
resultFile := &RepositoryFile{
CommitSHA: contents.GetSHA(),
Content: content,
}
return content, nil
return resultFile, nil
}

func (g *GitHubClient) MergePullRequest(repository string, prNumber int) (*PullRequest, error) {
Expand Down Expand Up @@ -103,3 +113,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
}
41 changes: 35 additions & 6 deletions pkg/clients/git/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,39 @@ func (g *GitLabClient) ListPullRequests(string) ([]*PullRequest, error) {
return pullRequests, nil
}

func (g *GitLabClient) CreateFile(repository, pathToFile, content, branchName string) error {
func (g *GitLabClient) CreateFile(repository, pathToFile, content, branchName string) (*RepositoryFile, error) {
_, err := g.GitlabClient.CreateFile(repository, pathToFile, content, branchName)
return err
if err != nil {
return nil, err
}

opts := gitlab2.GetFileOptions{Ref: gitlab2.Ptr(branchName)}
file, _, err := g.GitlabClient.GetClient().RepositoryFiles.GetFile(repository, pathToFile, &opts)
if err != nil {
return nil, err
}

resultFile := &RepositoryFile{
CommitSHA: file.CommitID,
}
return resultFile, nil
}

func (g *GitLabClient) GetFileContent(repository, pathToFile, branchName string) (string, error) {
func (g *GitLabClient) GetFile(repository, pathToFile, branchName string) (*RepositoryFile, error) {
opts := gitlab2.GetFileOptions{Ref: gitlab2.Ptr(branchName)}
file, _, err := g.GitlabClient.GetClient().RepositoryFiles.GetFile(repository, pathToFile, &opts)
if err != nil {
return "", err
return nil, err
}
decoded, err := base64.StdEncoding.DecodeString(file.Content)
if err != nil {
return "", err
return nil, err
}
return string(decoded), nil
resultFile := &RepositoryFile{
CommitSHA: file.CommitID,
Content: string(decoded),
}
return resultFile, nil
}

func (g *GitLabClient) MergePullRequest(repository string, prNumber int) (*PullRequest, error) {
Expand Down Expand Up @@ -95,3 +112,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 063ae99

Please sign in to comment.