Skip to content

Commit f433059

Browse files
github-actions[bot]CopilotJguer
authored
[repo-assist] refactor(srcinfo): deduplicate VCS updates (#2917)
* refactor(srcinfo): deduplicate VCS updates Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix broken test --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Jo Garnier <me@jguer.space>
1 parent caab26b commit f433059

3 files changed

Lines changed: 33 additions & 14 deletions

File tree

pkg/sync/srcinfo/service.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,31 @@ func (s *Service) CheckPGPKeys(ctx context.Context) error {
7575

7676
func (s *Service) UpdateVCSStore(ctx context.Context, targets []map[string]*dep.InstallInfo, ignore map[string]error,
7777
) error {
78+
targetPackages := make(map[string]struct{})
79+
for _, target := range targets {
80+
for pkgName := range target {
81+
targetPackages[pkgName] = struct{}{}
82+
}
83+
}
84+
7885
for _, srcinfo := range s.srcInfos {
7986
if srcinfo.Source == nil {
8087
continue
8188
}
8289

83-
// TODO: high complexity - refactor
8490
for i := range srcinfo.Packages {
85-
for j := range targets {
86-
if _, ok := targets[j][srcinfo.Packages[i].Pkgname]; !ok {
87-
s.log.Debugln("skipping VCS update for", srcinfo.Packages[i].Pkgname, "not in targets")
88-
continue
89-
}
90-
if _, ok := ignore[srcinfo.Packages[i].Pkgname]; ok {
91-
s.log.Debugln("skipping VCS update for", srcinfo.Packages[i].Pkgname, "due to install error")
92-
continue
93-
}
94-
95-
s.log.Debugln("checking VCS entry for", srcinfo.Packages[i].Pkgname, fmt.Sprintf("source: %v", srcinfo.Source))
96-
s.vcsStore.Update(ctx, srcinfo.Packages[i].Pkgname, srcinfo.Source)
91+
pkgName := srcinfo.Packages[i].Pkgname
92+
if _, ok := targetPackages[pkgName]; !ok {
93+
s.log.Debugln("skipping VCS update for", pkgName, "not in targets")
94+
continue
9795
}
96+
if _, ok := ignore[pkgName]; ok {
97+
s.log.Debugln("skipping VCS update for", pkgName, "due to install error")
98+
continue
99+
}
100+
101+
s.log.Debugln("checking VCS entry for", pkgName, fmt.Sprintf("source: %v", srcinfo.Source))
102+
s.vcsStore.Update(ctx, pkgName, srcinfo.Source)
98103
}
99104
}
100105

pkg/sync/srcinfo/service_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,42 @@ func TestService_CheckPGPKeys(t *testing.T) {
103103
}
104104

105105
func TestService_UpdateVCSStore(t *testing.T) {
106+
vcsStore := &vcs.Mock{}
106107
srv := &Service{
108+
log: newTestLogger(),
107109
srcInfos: map[string]*gosrc.Srcinfo{
108110
"pkg1": {
109111
Packages: []gosrc.Package{
110112
{Pkgname: "pkg1"},
111113
},
114+
PackageBase: gosrc.PackageBase{
115+
Source: []gosrc.ArchString{{Value: "git+https://example.com/pkg1.git"}},
116+
},
112117
},
113118
"pkg2": {
114119
Packages: []gosrc.Package{
115120
{Pkgname: "pkg2"},
116121
},
122+
PackageBase: gosrc.PackageBase{
123+
Source: []gosrc.ArchString{{Value: "git+https://example.com/pkg2.git"}},
124+
},
117125
},
118126
},
119-
vcsStore: &vcs.Mock{},
127+
vcsStore: vcsStore,
120128
}
121129

122130
targets := []map[string]*dep.InstallInfo{
123131
{
124132
"pkg1": {},
125133
"pkg2": {},
126134
},
135+
{
136+
"pkg1": {},
137+
},
127138
}
128139
ignore := map[string]error{}
129140

130141
err := srv.UpdateVCSStore(context.Background(), targets, ignore)
131142
assert.NoError(t, err)
143+
assert.ElementsMatch(t, []string{"pkg1", "pkg2"}, vcsStore.Updates)
132144
}

pkg/vcs/mock.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import (
1111
type Mock struct {
1212
OriginsByPackage map[string]OriginInfoByURL
1313
ToUpgradeReturn []string
14+
Updates []string
1415
}
1516

1617
func (m *Mock) ToUpgrade(ctx context.Context, pkgName string) bool {
1718
return slices.Contains(m.ToUpgradeReturn, pkgName)
1819
}
1920

2021
func (m *Mock) Update(ctx context.Context, pkgName string, sources []gosrc.ArchString) {
22+
m.Updates = append(m.Updates, pkgName)
2123
}
2224

2325
func (m *Mock) Save() error {

0 commit comments

Comments
 (0)