@@ -13,7 +13,6 @@ import (
1313 "context"
1414 _ "embed"
1515 "encoding/json"
16- "errors"
1716 "fmt"
1817 "os"
1918 "os/exec"
@@ -24,13 +23,11 @@ import (
2423 "github.com/argoproj-labs/gitops-promoter/internal/types/constants"
2524 "github.com/relvacode/iso8601"
2625 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27- "sigs.k8s.io/controller-runtime/pkg/client"
2826 "sigs.k8s.io/controller-runtime/pkg/log"
2927
3028 "github.com/argoproj-labs/gitops-promoter/api/v1alpha1"
3129 "github.com/argoproj-labs/gitops-promoter/internal/metrics"
3230 "github.com/argoproj-labs/gitops-promoter/internal/scms"
33- "github.com/argoproj-labs/gitops-promoter/internal/utils"
3431 "github.com/argoproj-labs/gitops-promoter/internal/utils/gitpaths"
3532)
3633
@@ -64,19 +61,12 @@ type HydratorMetadata struct {
6461// NewEnvironmentOperations creates a new EnvironmentOperations instance. The activeBranch parameter is used to differentiate
6562// between different environments that might use the same GitRepository and avoid conflicts between concurrent
6663// operations.
67- func NewEnvironmentOperations (ctx context.Context , k8sClient client.Client , gap scms.GitOperationsProvider , repoRef v1alpha1.ObjectReference , obj v1.Object , activeBranch string ) (* EnvironmentOperations , error ) {
68- gitRepo , err := utils .GetGitRepositoryFromObjectKey (ctx , k8sClient , client.ObjectKey {Namespace : obj .GetNamespace (), Name : repoRef .Name })
69- if err != nil {
70- return nil , fmt .Errorf ("failed to get GitRepository: %w" , err )
71- }
72-
73- gitOperations := EnvironmentOperations {
64+ func NewEnvironmentOperations (gitRepo * v1alpha1.GitRepository , gap scms.GitOperationsProvider , activeBranch string ) * EnvironmentOperations {
65+ return & EnvironmentOperations {
7466 gap : gap ,
7567 gitRepo : gitRepo ,
7668 activeBranch : activeBranch ,
7769 }
78-
79- return & gitOperations , nil
8070}
8171
8272// CloneRepo clones the gitRepo to a temporary directory if needed. Does nothing if the repo is already cloned.
@@ -143,46 +133,42 @@ func (g *EnvironmentOperations) GetBranchShas(ctx context.Context, branch string
143133 }
144134
145135 logger .V (4 ).Info ("git path" , "path" , gitPath )
146- _ , stderr , err := g .runCmd (ctx , gitPath , "checkout" , "--progress" , "-B" , branch , "origin/" + branch )
147- if err != nil {
148- logger .Error (err , "could not git checkout" , "gitError" , stderr )
149- return BranchShas {}, err
150- }
151- logger .V (4 ).Info ("Checked out branch" , "branch" , branch )
152136
137+ // Fetch the branch to ensure we have the latest remote ref
153138 start := time .Now ()
154- _ , stderr , err = g .runCmd (ctx , gitPath , "pull " , "--progress" )
155- metrics .RecordGitOperation (g .gitRepo , metrics .GitOperationPull , metrics .GitOperationResultFromError (err ), time .Since (start ))
139+ _ , stderr , err : = g .runCmd (ctx , gitPath , "fetch " , "origin" , branch )
140+ metrics .RecordGitOperation (g .gitRepo , metrics .GitOperationFetch , metrics .GitOperationResultFromError (err ), time .Since (start ))
156141 if err != nil {
157- logger .Error (err , "could not git pull " , "gitError" , stderr )
158- return BranchShas {}, err
142+ logger .Error (err , "could not fetch branch " , "gitError" , stderr )
143+ return BranchShas {}, fmt . Errorf ( "failed to fetch branch %q: %w" , branch , err )
159144 }
160- logger .V (4 ).Info ("Pulled branch" , "branch" , branch )
145+ logger .V (4 ).Info ("Fetched branch" , "branch" , branch )
161146
162- stdout , stderr , err := g .runCmd (ctx , gitPath , "rev-parse" , branch )
147+ // Get the SHA of the remote branch
148+ stdout , stderr , err := g .runCmd (ctx , gitPath , "rev-parse" , "origin/" + branch )
163149 if err != nil {
164- logger .Error (err , "could not get branch shas " , "gitError" , stderr )
165- return BranchShas {}, err
150+ logger .Error (err , "could not get branch sha " , "gitError" , stderr )
151+ return BranchShas {}, fmt . Errorf ( "failed to get SHA for branch %q: %w" , branch , err )
166152 }
167153
168154 shas := BranchShas {}
169155 shas .Hydrated = strings .TrimSpace (stdout )
170156 logger .V (4 ).Info ("Got hydrated branch sha" , "branch" , branch , "sha" , shas .Hydrated )
171157
172- // TODO: safe path join
173- metadataFile := gitPath + "/hydrator.metadata"
174- jsonFile , err := os .Open (metadataFile )
158+ // Get the metadata file contents directly from the remote branch
159+ metadataFileStdout , stderr , err := g .runCmd (ctx , gitPath , "show" , "origin/" + branch + ":hydrator.metadata" )
175160 if err != nil {
176- if errors . Is ( err , os . ErrNotExist ) {
177- logger .Info ("hydrator.metadata file not found" , "file " , metadataFile )
161+ if strings . Contains ( stderr , "does not exist" ) || strings . Contains ( stderr , "Path not in" ) {
162+ logger .Info ("hydrator.metadata file not found" , "branch " , branch )
178163 return shas , nil
179164 }
180- return BranchShas {}, fmt .Errorf ("could not open metadata file: %w" , err )
165+ logger .Error (err , "could not get metadata file" , "gitError" , stderr )
166+ return BranchShas {}, fmt .Errorf ("failed to read hydrator.metadata from branch %q: %w" , branch , err )
181167 }
168+ logger .V (4 ).Info ("Got metadata file" , "branch" , branch )
182169
183170 var hydratorFile HydratorMetadata
184- decoder := json .NewDecoder (jsonFile )
185- err = decoder .Decode (& hydratorFile )
171+ err = json .Unmarshal ([]byte (metadataFileStdout ), & hydratorFile )
186172 if err != nil {
187173 return BranchShas {}, fmt .Errorf ("could not unmarshal metadata file: %w" , err )
188174 }
0 commit comments