From 7e757c4a3d649b5632f85b249eb830065e27536c Mon Sep 17 00:00:00 2001 From: Eoin McAfee <83226740+eoinmcafee00@users.noreply.github.com> Date: Mon, 10 Jan 2022 10:49:37 +0000 Subject: [PATCH] (feat) ignore archive repos on sync (#3178) * (feat) ignore archive repos on sync --- core/repo.go | 1 + go.mod | 2 +- go.sum | 2 ++ service/repo/util.go | 1 + service/syncer/syncer.go | 7 +++++ service/syncer/syncer_test.go | 49 +++++++++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/core/repo.go b/core/repo.go index f88b0083..2220213a 100644 --- a/core/repo.go +++ b/core/repo.go @@ -65,6 +65,7 @@ type ( Secret string `json:"-"` Build *Build `json:"build,omitempty"` Perms *Perm `json:"permissions,omitempty"` + Archived bool `json:"archived"` } RepoBuildStage struct { diff --git a/go.mod b/go.mod index 1104d7a6..428e5145 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/drone/funcmap v0.0.0-20210823160631-9e9dec149056 github.com/drone/go-license v1.0.2 github.com/drone/go-login v1.1.0 - github.com/drone/go-scm v1.16.3 + github.com/drone/go-scm v1.17.0 github.com/drone/signal v1.0.0 github.com/dustin/go-humanize v1.0.0 github.com/go-chi/chi v3.3.3+incompatible diff --git a/go.sum b/go.sum index 6345651e..c1e8a00d 100644 --- a/go.sum +++ b/go.sum @@ -100,6 +100,8 @@ github.com/drone/go-scm v1.16.2 h1:IoxgyIACUArg3d7US8OR1Vwsph3U/x7imNTU5QK47C4= github.com/drone/go-scm v1.16.2/go.mod h1:DFIJJjhMj0TSXPz+0ni4nyZ9gtTtC40Vh/TGRugtyWw= github.com/drone/go-scm v1.16.3 h1:jm8xSGv5vVfaVhPY7Dswjq2EdNm0HTDVCQ+/Lqe7KPM= github.com/drone/go-scm v1.16.3/go.mod h1:DFIJJjhMj0TSXPz+0ni4nyZ9gtTtC40Vh/TGRugtyWw= +github.com/drone/go-scm v1.17.0 h1:Yg0IxA8XAIgAohcQy7S1P9D3Sd6sJq3dNMI7uajwXhQ= +github.com/drone/go-scm v1.17.0/go.mod h1:DFIJJjhMj0TSXPz+0ni4nyZ9gtTtC40Vh/TGRugtyWw= github.com/drone/signal v1.0.0 h1:NrnM2M/4yAuU/tXs6RP1a1ZfxnaHwYkd0kJurA1p6uI= github.com/drone/signal v1.0.0/go.mod h1:S8t92eFT0g4WUgEc/LxG+LCuiskpMNsG0ajAMGnyZpc= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= diff --git a/service/repo/util.go b/service/repo/util.go index ef917325..ecb62ebc 100644 --- a/service/repo/util.go +++ b/service/repo/util.go @@ -35,6 +35,7 @@ func convertRepository(src *scm.Repository, visibility string, trusted bool) *co Visibility: convertVisibility(src, visibility), Branch: src.Branch, Trusted: trusted, + Archived: src.Archived, } } diff --git a/service/syncer/syncer.go b/service/syncer/syncer.go index 83308c5c..af148fc3 100644 --- a/service/syncer/syncer.go +++ b/service/syncer/syncer.go @@ -110,6 +110,13 @@ func (s *Synchronizer) Sync(ctx context.Context, user *core.User) (*core.Batch, WithField("uid", repo.UID). Traceln("syncer: skipping subrepositories") } + } else if repo.Archived { + if logrus.GetLevel() == logrus.TraceLevel { + logger.WithField("namespace", repo.Namespace). + WithField("name", repo.Name). + WithField("uid", repo.UID). + Traceln("syncer: skipping archived repositories") + } } else if s.match(repo) { remote[repo.UID] = repo if logrus.GetLevel() == logrus.TraceLevel { diff --git a/service/syncer/syncer_test.go b/service/syncer/syncer_test.go index 4b8640e6..0d7ae7aa 100644 --- a/service/syncer/syncer_test.go +++ b/service/syncer/syncer_test.go @@ -434,3 +434,52 @@ func TestSync_SkipSubrepo(t *testing.T) { t.Errorf(diff) } } + +func TestSyncArchive(t *testing.T) { + controller := gomock.NewController(t) + defer controller.Finish() + + user := &core.User{ID: 1} + + userStore := mock.NewMockUserStore(controller) + userStore.EXPECT().Update(gomock.Any(), user).Return(nil) + userStore.EXPECT().Update(gomock.Any(), user).Return(nil) + + batcher := mock.NewMockBatcher(controller) + batcher.EXPECT().Batch(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + + repoStore := mock.NewMockRepositoryStore(controller) + repoStore.EXPECT().List(gomock.Any(), gomock.Any()).Return([]*core.Repository{}, nil) + + repoService := mock.NewMockRepositoryService(controller) + repoService.EXPECT().List(gomock.Any(), user).Return([]*core.Repository{ + { + UID: "1", + Slug: "octocat/hello-world", + Namespace: "octocat", + Name: "hello-world", + Private: false, + Visibility: core.VisibilityPublic, + Archived: true, + }, + }, nil) + + s := New( + repoService, + repoStore, + userStore, + batcher, + ) + got, err := s.Sync(context.Background(), user) + if err != nil { + t.Error(err) + } + + want := &core.Batch{} + + ignore := cmpopts.IgnoreFields(core.Repository{}, + "Synced", "Created", "Updated") + if diff := cmp.Diff(got, want, ignore); len(diff) != 0 { + t.Errorf(diff) + } +}