Skip to content

Commit 95a51cb

Browse files
willie-yaosaschagrunert
authored andcommitted
Fix links for deps with tag that contains one slash
1 parent 5076f46 commit 95a51cb

File tree

2 files changed

+105
-4
lines changed

2 files changed

+105
-4
lines changed

pkg/modiff/modiff.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package modiff
22

33
import (
44
"bufio"
5+
"context"
56
"errors"
67
"fmt"
8+
"net/http"
79
"os"
810
"os/exec"
911
"slices"
@@ -216,7 +218,14 @@ func getModules(workDir, from, to string) (modules, error) {
216218
linkPrefix := name
217219
// Remove the module name from the link
218220
if splitLink := strings.Split(linkPrefix, "/"); len(splitLink) == 4 {
219-
linkPrefix = strings.Join(splitLink[:3], "/")
221+
// Check if the last part of string is part of the tag.
222+
linkPrefixTree := strings.Join(slices.Insert(splitLink, 3, "tree"), "/")
223+
url := fmt.Sprintf("https://%s%s%s", linkPrefixTree, "/", strings.TrimSpace(split[1]))
224+
// If the url is valid, then we keep the linkPrefix as is
225+
client := http.Client{}
226+
if valid, err := CheckURLValid(client, url); !valid && err == nil {
227+
linkPrefix = strings.Join(splitLink[:3], "/")
228+
}
220229
}
221230
version := strings.TrimSpace(split[1])
222231

@@ -251,6 +260,25 @@ func getModules(workDir, from, to string) (modules, error) {
251260
return res, nil
252261
}
253262

263+
func CheckURLValid(client http.Client, url string) (bool, error) {
264+
ctx := context.Background()
265+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody)
266+
if err != nil {
267+
return false, fmt.Errorf("error while creating request: %w", err)
268+
}
269+
resp, err := client.Do(req)
270+
if err != nil {
271+
return false, fmt.Errorf("error while sending request: %w", err)
272+
} else if resp.StatusCode == http.StatusNotFound {
273+
resp.Body.Close()
274+
275+
return false, nil
276+
}
277+
resp.Body.Close()
278+
279+
return true, nil
280+
}
281+
254282
func retrieveModules(rev, workDir string) (string, error) {
255283
logrus.Infof("Retrieving modules of %s", rev)
256284
if err := runGit(

pkg/modiff/modiff_test.go

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ package modiff_test
22

33
//nolint:revive // test file
44
import (
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
510
. "github.com/onsi/ginkgo/v2"
611
. "github.com/onsi/gomega"
712
"github.com/saschagrunert/go-modiff/pkg/modiff"
@@ -42,9 +47,10 @@ _Nothing has changed._
4247
`
4348

4449
const (
45-
repo = "github.com/saschagrunert/go-modiff"
46-
from = "v0.10.0"
47-
to = "v0.11.0"
50+
repo = "github.com/saschagrunert/go-modiff"
51+
from = "v0.10.0"
52+
to = "v0.11.0"
53+
badRepo = "github.com/saschagrunert/go-modiff-invalid"
4854
)
4955

5056
BeforeEach(func() {
@@ -120,4 +126,71 @@ _Nothing has changed._
120126
Expect(err).To(HaveOccurred())
121127
Expect(res).To(BeEmpty())
122128
})
129+
130+
It("should fail if the repository url is invalid", func() {
131+
// Given
132+
config := modiff.NewConfig(badRepo, from, to, true, 1)
133+
134+
// When
135+
res, err := modiff.Run(config)
136+
137+
// Then
138+
Expect(err).To(HaveOccurred())
139+
Expect(res).To(BeEmpty())
140+
})
123141
})
142+
143+
func TestCheckURLValid(t *testing.T) {
144+
t.Parallel()
145+
146+
tests := []struct {
147+
name string
148+
url string
149+
expected bool
150+
err error
151+
client *http.Client
152+
}{
153+
{
154+
name: "Valid URL",
155+
url: "https://github.com/hashicorp/consul/compare/api/v1.18.0...api/v1.20.0",
156+
expected: true,
157+
err: nil,
158+
},
159+
{
160+
name: "Invalid URL",
161+
url: "https://github.com/hashicorp/consul/compare/v1.18.0...v1.20.0",
162+
expected: false,
163+
err: nil,
164+
},
165+
{
166+
name: "Request Sending Error",
167+
url: "invalid-url",
168+
expected: false,
169+
err: fmt.Errorf("error while sending request: "),
170+
},
171+
}
172+
173+
for _, tt := range tests {
174+
tt := tt
175+
t.Run(tt.name, func(t *testing.T) {
176+
t.Parallel()
177+
g := NewGomegaWithT(t)
178+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
179+
w.WriteHeader(http.StatusNotFound)
180+
}))
181+
defer server.Close()
182+
183+
if tt.client == nil {
184+
tt.client = &http.Client{}
185+
}
186+
valid, err := modiff.CheckURLValid(*tt.client, tt.url)
187+
g.Expect(valid).To(Equal(tt.expected))
188+
if tt.err != nil {
189+
g.Expect(err).To(HaveOccurred())
190+
g.Expect(err.Error()).To(ContainSubstring(tt.err.Error()))
191+
} else {
192+
g.Expect(err).ToNot(HaveOccurred())
193+
}
194+
})
195+
}
196+
}

0 commit comments

Comments
 (0)