@@ -34,8 +34,57 @@ type FileChange struct {
3434 NewContent string
3535}
3636
37+ // initializeGitClone initializes git's configuration, clones a given repository, and sets up the given branch.
38+ // Returns the repository path, a cleanup function which should be deferred, and an error, if any occurred.
39+ func initializeGit (t * testing.T , opts * Opts ) (string , func (), error ) {
40+ tmpdir := fs .NewDir (t , t .Name ())
41+ fixPwd := env .ChangeWorkingDir (t , tmpdir .Path ())
42+ cleanupFunc := func () {
43+ fixPwd ()
44+ if os .Getenv ("TEST_NOCLEANUP" ) == "" {
45+ tmpdir .Remove ()
46+ }
47+ }
48+ path := tmpdir .Path ()
49+
50+ var err error
51+
52+ if _ , err = git .RunGit (path , "init" ); err != nil {
53+ return "" , func () {}, err
54+ }
55+ if _ , err = git .RunGit (path , "config" , "user.name" , "OpenShift Pipelines E2E test" ); err != nil {
56+ return "" , func () {}, err
57+ }
58+ if _ , err = git .RunGit (path , "config" , "user.email" , "e2e-pipeline@redhat.com" ); err != nil {
59+ return "" , func () {}, err
60+ }
61+
62+ if _ , err = git .RunGit (path , "remote" , "add" , "-f" , "origin" , opts .GitURL ); err != nil {
63+ return "" , func () {}, err
64+ }
65+
66+ if _ , err = git .RunGit (path , "fetch" , "-a" , "origin" ); err != nil {
67+ return "" , func () {}, err
68+ }
69+
70+ if strings .HasPrefix (opts .TargetRefName , "refs/tags" ) {
71+ _ , err = git .RunGit (path , "reset" , "--hard" , "origin/" + opts .BaseRefName )
72+ } else {
73+ if opts .NoCheckOutFromBase {
74+ // Create a new branch without the base reference,
75+ // which can be helpful for testing when you only want to add specific requested files
76+ _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName )
77+ } else {
78+ // checkout new branch from base branch
79+ _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName , "origin/" + opts .BaseRefName )
80+ }
81+ }
82+ return path , cleanupFunc , err
83+ }
84+
3785// gitPushPullRetry tries to push the files to the repo, if it fails it will try to rebase and push again.
38- func gitPushPullRetry (t * testing.T , opts * Opts , path string ) {
86+ // Returns the sha of the commit pushed.
87+ func gitPushPullRetry (t * testing.T , opts * Opts , path string ) string {
3988 // use a loop to try multiple times in case of error
4089 var err error
4190 count := 0
@@ -48,7 +97,12 @@ func gitPushPullRetry(t *testing.T, opts *Opts, path string) {
4897 opts .Log .Infof ("Pushed files to repo %s branch %s" , opts .WebURL , opts .TargetRefName )
4998 // trying to avoid the multiple events at the time of creation we have a sync
5099 time .Sleep (5 * time .Second )
51- return
100+
101+ // get sha
102+ sha , err := git .RunGit (path , "rev-parse" , "HEAD" )
103+ assert .NilError (t , err )
104+
105+ return sha
52106 }
53107 if strings .Contains (err .Error (), "non-fast-forward" ) {
54108 _ , err = git .RunGit (path , "fetch" , "-a" , "origin" )
@@ -69,40 +123,8 @@ func gitPushPullRetry(t *testing.T, opts *Opts, path string) {
69123}
70124
71125func PushFilesToRefGit (t * testing.T , opts * Opts , entries map [string ]string ) string {
72- tmpdir := fs .NewDir (t , t .Name ())
73- defer (func () {
74- if os .Getenv ("TEST_NOCLEANUP" ) == "" {
75- tmpdir .Remove ()
76- }
77- })()
78- defer env .ChangeWorkingDir (t , tmpdir .Path ())()
79- path := tmpdir .Path ()
80- _ , err := git .RunGit (path , "init" )
81- assert .NilError (t , err )
82-
83- _ , err = git .RunGit (path , "config" , "user.name" , "OpenShift Pipelines E2E test" )
84- assert .NilError (t , err )
85- _ , err = git .RunGit (path , "config" , "user.email" , "e2e-pipeline@redhat.com" )
86- assert .NilError (t , err )
87-
88- _ , err = git .RunGit (path , "remote" , "add" , "-f" , "origin" , opts .GitURL )
89- assert .NilError (t , err )
90-
91- _ , err = git .RunGit (path , "fetch" , "-a" , "origin" )
92- assert .NilError (t , err )
93-
94- if strings .HasPrefix (opts .TargetRefName , "refs/tags" ) {
95- _ , err = git .RunGit (path , "reset" , "--hard" , "origin/" + opts .BaseRefName )
96- } else {
97- if opts .NoCheckOutFromBase {
98- // Create a new branch without the base reference,
99- // which can be helpful for testing when you only want to add specific requested files
100- _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName )
101- } else {
102- // checkout new branch from base branch
103- _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName , "origin/" + opts .BaseRefName )
104- }
105- }
126+ path , cleanupFunc , err := initializeGit (t , opts )
127+ defer cleanupFunc ()
106128 assert .NilError (t , err )
107129
108130 for filename , content := range entries {
@@ -125,42 +147,12 @@ func PushFilesToRefGit(t *testing.T, opts *Opts, entries map[string]string) stri
125147 assert .NilError (t , err )
126148 }
127149
128- // get sha
129- sha , err := git .RunGit (path , "rev-parse" , "HEAD" )
130- assert .NilError (t , err )
131-
132- gitPushPullRetry (t , opts , path )
133- return sha
150+ return gitPushPullRetry (t , opts , path )
134151}
135152
136153func ChangeFilesRefGit (t * testing.T , opts * Opts , fileChanges []FileChange ) {
137- tmpdir := fs .NewDir (t , t .Name ())
138- defer (func () {
139- if os .Getenv ("TEST_NOCLEANUP" ) == "" {
140- tmpdir .Remove ()
141- }
142- })()
143- defer env .ChangeWorkingDir (t , tmpdir .Path ())()
144- path := tmpdir .Path ()
145- _ , err := git .RunGit (path , "init" )
146- assert .NilError (t , err )
147-
148- _ , err = git .RunGit (path , "config" , "user.name" , "OpenShift Pipelines E2E test" )
149- assert .NilError (t , err )
150- _ , err = git .RunGit (path , "config" , "user.email" , "e2e-pipeline@redhat.com" )
151- assert .NilError (t , err )
152-
153- _ , err = git .RunGit (path , "remote" , "add" , "-f" , "origin" , opts .GitURL )
154- assert .NilError (t , err )
155-
156- _ , err = git .RunGit (path , "fetch" , "-a" , "origin" )
157- assert .NilError (t , err )
158-
159- if strings .HasPrefix (opts .TargetRefName , "refs/tags" ) {
160- _ , err = git .RunGit (path , "reset" , "--hard" , "origin/" + opts .BaseRefName )
161- } else {
162- _ , err = git .RunGit (path , "checkout" , "-B" , opts .TargetRefName , "origin/" + opts .BaseRefName )
163- }
154+ path , cleanupFunc , err := initializeGit (t , opts )
155+ defer cleanupFunc ()
164156 assert .NilError (t , err )
165157
166158 for _ , fileChange := range fileChanges {
0 commit comments