Skip to content

Commit b4c4a26

Browse files
authored
Merge pull request #1036 from aryan9600/absolute-ref
Advertise absolute reference in Artifact for GitRepository name ref
2 parents 5b2321f + 4cbacd0 commit b4c4a26

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

controllers/gitrepository_controller.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func (r *GitRepositoryReconciler) notify(ctx context.Context, oldObj, newObj *so
342342
if git.IsConcreteCommit(commit) {
343343
message = fmt.Sprintf("stored artifact for commit '%s'", commit.ShortMessage())
344344
} else {
345-
message = fmt.Sprintf("stored artifact for commit '%s'", commit.String())
345+
message = fmt.Sprintf("stored artifact for commit '%s'", commitReference(newObj, &commit))
346346
}
347347

348348
// Notify on new artifact and failure recovery.
@@ -558,7 +558,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
558558
if !gitContentConfigChanged(obj, includes) {
559559
ge := serror.NewGeneric(
560560
fmt.Errorf("no changes since last reconcilation: observed revision '%s'",
561-
commit.String()), sourcev1.GitOperationSucceedReason,
561+
commitReference(obj, commit)), sourcev1.GitOperationSucceedReason,
562562
)
563563
ge.Notification = false
564564
ge.Ignore = true
@@ -570,7 +570,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
570570
// reconciliation reconcileArtifact() ensures that it's set at the
571571
// very end.
572572
conditions.MarkTrue(obj, sourcev1.ArtifactInStorageCondition, meta.SucceededReason,
573-
"stored artifact for revision '%s'", commit.String())
573+
"stored artifact for revision '%s'", commitReference(obj, commit))
574574
// TODO: Find out if such condition setting is needed when commit
575575
// signature verification is enabled.
576576
return sreconcile.ResultEmpty, ge
@@ -584,7 +584,7 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
584584
}
585585
*commit = *c
586586
}
587-
ctrl.LoggerFrom(ctx).V(logger.DebugLevel).Info("git repository checked out", "url", obj.Spec.URL, "revision", commit.String())
587+
ctrl.LoggerFrom(ctx).V(logger.DebugLevel).Info("git repository checked out", "url", obj.Spec.URL, "revision", commitReference(obj, commit))
588588
conditions.Delete(obj, sourcev1.FetchFailedCondition)
589589

590590
// Verify commit signature
@@ -593,8 +593,8 @@ func (r *GitRepositoryReconciler) reconcileSource(ctx context.Context, sp *patch
593593
}
594594

595595
// Mark observations about the revision on the object
596-
if !obj.GetArtifact().HasRevision(commit.String()) {
597-
message := fmt.Sprintf("new upstream revision '%s'", commit.String())
596+
if !obj.GetArtifact().HasRevision(commitReference(obj, commit)) {
597+
message := fmt.Sprintf("new upstream revision '%s'", commitReference(obj, commit))
598598
if obj.GetArtifact() != nil {
599599
conditions.MarkTrue(obj, sourcev1.ArtifactOutdatedCondition, "NewRevision", message)
600600
}
@@ -622,7 +622,7 @@ func (r *GitRepositoryReconciler) reconcileArtifact(ctx context.Context, sp *pat
622622
obj *sourcev1.GitRepository, commit *git.Commit, includes *artifactSet, dir string) (sreconcile.Result, error) {
623623

624624
// Create potential new artifact with current available metadata
625-
artifact := r.Storage.NewArtifactFor(obj.Kind, obj.GetObjectMeta(), commit.String(), fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
625+
artifact := r.Storage.NewArtifactFor(obj.Kind, obj.GetObjectMeta(), commitReference(obj, commit), fmt.Sprintf("%s.tar.gz", commit.Hash.String()))
626626

627627
// Set the ArtifactInStorageCondition if there's no drift.
628628
defer func() {
@@ -1048,3 +1048,10 @@ func gitRepositoryIncludeEqual(a, b sourcev1.GitRepositoryInclude) bool {
10481048
}
10491049
return true
10501050
}
1051+
1052+
func commitReference(obj *sourcev1.GitRepository, commit *git.Commit) string {
1053+
if obj.Spec.Reference != nil && obj.Spec.Reference.Name != "" {
1054+
return commit.AbsoluteReference()
1055+
}
1056+
return commit.String()
1057+
}

controllers/gitrepository_controller_test.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
600600

601601
branches := []string{"staging"}
602602
tags := []string{"non-semver-tag", "v0.1.0", "0.2.0", "v0.2.1", "v1.0.0-alpha", "v1.1.0", "v2.0.0"}
603+
refs := []string{"refs/pull/420/head"}
603604

604605
tests := []struct {
605606
name string
@@ -645,6 +646,24 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
645646
wantRevision: "staging@sha1:<commit>",
646647
wantReconciling: true,
647648
},
649+
{
650+
name: "Ref Name pointing to a branch",
651+
reference: &sourcev1.GitRepositoryRef{
652+
Name: "refs/heads/staging",
653+
},
654+
want: sreconcile.ResultSuccess,
655+
wantRevision: "refs/heads/staging@sha1:<commit>",
656+
wantReconciling: true,
657+
},
658+
{
659+
name: "Ref Name pointing to a PR",
660+
reference: &sourcev1.GitRepositoryRef{
661+
Name: "refs/pull/420/head",
662+
},
663+
want: sreconcile.ResultSuccess,
664+
wantRevision: "refs/pull/420/head@sha1:<commit>",
665+
wantReconciling: true,
666+
},
648667
{
649668
name: "SemVer",
650669
reference: &sourcev1.GitRepositoryRef{
@@ -801,6 +820,10 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
801820
g.Expect(remoteTagForHead(localRepo, headRef, tag)).To(Succeed())
802821
}
803822

823+
for _, ref := range refs {
824+
g.Expect(remoteRefForHead(localRepo, headRef, ref)).To(Succeed())
825+
}
826+
804827
r := &GitRepositoryReconciler{
805828
Client: fakeclient.NewClientBuilder().WithScheme(testEnv.GetScheme()).Build(),
806829
EventRecorder: record.NewFakeRecorder(32),
@@ -854,7 +877,7 @@ func TestGitRepositoryReconciler_reconcileSource_checkoutStrategy(t *testing.T)
854877
g.Expect(got).To(Equal(tt.want))
855878
if tt.wantRevision != "" && !tt.wantErr {
856879
revision := strings.ReplaceAll(tt.wantRevision, "<commit>", headRef.Hash().String())
857-
g.Expect(commit.String()).To(Equal(revision))
880+
g.Expect(commitReference(obj, &commit)).To(Equal(revision))
858881
g.Expect(conditions.IsTrue(obj, sourcev1.ArtifactOutdatedCondition)).To(Equal(tt.wantArtifactOutdated))
859882
g.Expect(conditions.IsTrue(obj, meta.ReconcilingCondition)).To(Equal(tt.wantReconciling))
860883
}
@@ -1888,6 +1911,20 @@ func remoteTagForHead(repo *gogit.Repository, head *plumbing.Reference, tag stri
18881911
})
18891912
}
18901913

1914+
func remoteRefForHead(repo *gogit.Repository, head *plumbing.Reference, reference string) error {
1915+
if err := repo.Storer.SetReference(plumbing.NewHashReference(plumbing.ReferenceName(reference), head.Hash())); err != nil {
1916+
return err
1917+
}
1918+
if err := repo.Push(&gogit.PushOptions{
1919+
RefSpecs: []config.RefSpec{
1920+
config.RefSpec("+" + reference + ":" + reference),
1921+
},
1922+
}); err != nil {
1923+
return err
1924+
}
1925+
return nil
1926+
}
1927+
18911928
func TestGitRepositoryReconciler_statusConditions(t *testing.T) {
18921929
tests := []struct {
18931930
name string

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ require (
2525
github.com/fluxcd/go-git/v5 v5.0.0-20221219190809-2e5c9d01cfc4
2626
github.com/fluxcd/pkg/apis/event v0.4.0
2727
github.com/fluxcd/pkg/apis/meta v0.19.0
28-
github.com/fluxcd/pkg/git v0.10.0
29-
github.com/fluxcd/pkg/git/gogit v0.7.1
28+
github.com/fluxcd/pkg/git v0.11.0
29+
github.com/fluxcd/pkg/git/gogit v0.8.0
3030
github.com/fluxcd/pkg/gittestserver v0.8.1
3131
github.com/fluxcd/pkg/helmtestserver v0.11.1
3232
github.com/fluxcd/pkg/lockedfile v0.1.0
@@ -45,7 +45,7 @@ require (
4545
github.com/google/go-containerregistry/pkg/authn/k8schain v0.0.0-20230217043738-4a0e0af4bf95
4646
github.com/google/uuid v1.3.0
4747
github.com/minio/minio-go/v7 v7.0.49
48-
github.com/onsi/gomega v1.27.1
48+
github.com/onsi/gomega v1.27.2
4949
github.com/opencontainers/go-digest v1.0.0
5050
github.com/opencontainers/go-digest/blake3 v0.0.0-20220411205349-bde1400a84be
5151
github.com/ory/dockertest/v3 v3.9.1
@@ -96,7 +96,7 @@ require (
9696
github.com/Masterminds/squirrel v1.5.3 // indirect
9797
github.com/Microsoft/go-winio v0.6.0 // indirect
9898
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
99-
github.com/ProtonMail/go-crypto v0.0.0-20230214155104-81033d7f4442 // indirect
99+
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
100100
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d // indirect
101101
github.com/ThalesIgnite/crypto11 v1.2.5 // indirect
102102
github.com/acomagu/bufpipe v1.0.3 // indirect

go.sum

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEV
172172
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
173173
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
174174
github.com/ProtonMail/go-crypto v0.0.0-20221026131551-cf6655e29de4/go.mod h1:UBYPn8k0D56RtnR8RFQMjmh4KrZzWJ5o7Z9SYjossQ8=
175-
github.com/ProtonMail/go-crypto v0.0.0-20230214155104-81033d7f4442 h1:OUJ54Fkd+AQXYmr9eOUxZfWNzpK3/e/KD40qa2rKHS4=
176-
github.com/ProtonMail/go-crypto v0.0.0-20230214155104-81033d7f4442/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
175+
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 h1:wPbRQzjjwFc0ih8puEVAOFGELsn1zoIIYdxvML7mDxA=
176+
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8/go.mod h1:I0gYDMZ6Z5GRU7l58bNFSkPTFN6Yl12dsUlAZ8xy98g=
177177
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
178178
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
179179
github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d h1:UrqY+r/OJnIp5u0s1SbQ8dVfLCZJsnvazdBP5hS4iRs=
@@ -537,10 +537,10 @@ github.com/fluxcd/pkg/apis/event v0.4.0 h1:UPCC269KjgKgkmtiCiBq/DNue/EpXy8Tq1zFx
537537
github.com/fluxcd/pkg/apis/event v0.4.0/go.mod h1:xYOOlf+9gCBSYcs93N2XAbJvSVwuVBDBUzqhR+cAo7M=
538538
github.com/fluxcd/pkg/apis/meta v0.19.0 h1:CX75e/eaRWZDTzNdMSWomY1InlssLKcS8GQDSg/aopI=
539539
github.com/fluxcd/pkg/apis/meta v0.19.0/go.mod h1:7b6prDPsViyAzoY7eRfSPS0/MbXpGGsOMvRq2QrTKa4=
540-
github.com/fluxcd/pkg/git v0.10.0 h1:tO04FyUV3kmyJOpAKjMFZWClqr1JNGxS8RxI7znq6is=
541-
github.com/fluxcd/pkg/git v0.10.0/go.mod h1:zn3pJ4mRItezf6J0okHZbZ+3YNAGsjnhrS+Kbo+56Jw=
542-
github.com/fluxcd/pkg/git/gogit v0.7.1 h1:9QQtx8olL9CE0RaDUIPGBvkuh1IYZ5i5iFLQbcSvcyU=
543-
github.com/fluxcd/pkg/git/gogit v0.7.1/go.mod h1:QrYVKE25QpLTvM83Toec6KtVJ3WCnvvGTybL+2Zabxs=
540+
github.com/fluxcd/pkg/git v0.11.0 h1:GvB+3QOB8xbF5WNjVrkskseOnsZBuqSOzW3VxfsHuX4=
541+
github.com/fluxcd/pkg/git v0.11.0/go.mod h1:VHRVlrZMHNoWBlaSAWxlGH6Vwlb9VRazUhPUykviHwY=
542+
github.com/fluxcd/pkg/git/gogit v0.8.0 h1:rSOiTnNOLCyJbVYu2P0uqXtYEg4oRwyQB1RPNG9/wts=
543+
github.com/fluxcd/pkg/git/gogit v0.8.0/go.mod h1:wN5GrntOSQDHNSjse/qf387x+dcQjmabqBHRgA0Qfr4=
544544
github.com/fluxcd/pkg/gittestserver v0.8.1 h1:FMqnZBuS/11+9NhtLv9UAg+wm/v0Nf+hHeUOi2wJR3Q=
545545
github.com/fluxcd/pkg/gittestserver v0.8.1/go.mod h1:Ar0epRFZ7ZKZZldSjytWkkMiCWfxgpZ4jZZvJEKhTE0=
546546
github.com/fluxcd/pkg/helmtestserver v0.11.1 h1:seotZ19JtzPfuzru5zHCEX/0Ff96PVPI41OLaHh4rC0=
@@ -681,6 +681,7 @@ github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfC
681681
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
682682
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
683683
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
684+
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
684685
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
685686
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
686687
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
@@ -852,6 +853,7 @@ github.com/google/pprof v0.0.0-20210226084205-cbba55b83ad5/go.mod h1:kpwsk12EmLe
852853
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
853854
github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
854855
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
856+
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
855857
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
856858
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
857859
github.com/google/rpmpack v0.0.0-20191226140753-aa36bfddb3a0/go.mod h1:RaTPr0KUf2K7fnZYLNDrr8rxAamWs3iNywJLtQ2AzBg=
@@ -1244,15 +1246,15 @@ github.com/onsi/ginkgo v1.16.4/go.mod h1:dX+/inL/fNMqNlz0e9LfyB9TswhZpCVdJM/Z6Vv
12441246
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
12451247
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
12461248
github.com/onsi/ginkgo/v2 v2.1.3/go.mod h1:vw5CSIxN1JObi/U8gcbwft7ZxR2dgaR70JSE3/PpL4c=
1247-
github.com/onsi/ginkgo/v2 v2.8.1 h1:xFTEVwOFa1D/Ty24Ws1npBWkDYEV9BqZrsDxVrVkrrU=
1249+
github.com/onsi/ginkgo/v2 v2.8.4 h1:gf5mIQ8cLFieruNLAdgijHF1PYfLphKm2dxxcUtcqK0=
12481250
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
12491251
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
12501252
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
12511253
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
12521254
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
12531255
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
1254-
github.com/onsi/gomega v1.27.1 h1:rfztXRbg6nv/5f+Raen9RcGoSecHIFgBBLQK3Wdj754=
1255-
github.com/onsi/gomega v1.27.1/go.mod h1:aHX5xOykVYzWOV4WqQy0sy8BQptgukenXpCXfadcIAw=
1256+
github.com/onsi/gomega v1.27.2 h1:SKU0CXeKE/WVgIV1T61kSa3+IRE8Ekrv9rdXDwwTqnY=
1257+
github.com/onsi/gomega v1.27.2/go.mod h1:5mR3phAHpkAVIDkHEUBY6HGVsU+cpcEscrGPB4oPlZI=
12561258
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
12571259
github.com/opencontainers/go-digest v1.0.1-0.20220411205349-bde1400a84be h1:f2PlhC9pm5sqpBZFvnAoKj+KzXRzbjFMA+TqXfJdgho=
12581260
github.com/opencontainers/go-digest v1.0.1-0.20220411205349-bde1400a84be/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=

0 commit comments

Comments
 (0)