Skip to content

Commit

Permalink
[Chore] Simplify regexp usages
Browse files Browse the repository at this point in the history
  • Loading branch information
aeqz committed Dec 30, 2022
1 parent e84dc9c commit a7caf56
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ There are several ways to fix this:
- to: "https?://forbidden.com.*"
on: 307
outcome: invalid
- from: "^http://.*"
to: "^https://.*"
- from: "http://.*"
to: "https://.*"
outcome: follow
```

Expand All @@ -172,7 +172,7 @@ There are several ways to fix this:
* The number of redirects allowed in a single redirect chain is limited and can be configured with the
`maxRedirectFollows` parameter, also within `networking`. A number smaller than 0 disables the limit.

2. How does `xrefcheck` handle localhost links?
1. How does `xrefcheck` handle localhost links?
* By default, `xrefcheck` will ignore links to localhost.
* This behavior can be disabled by removing the corresponding entry from the `ignoreExternalRefsTo` list in the config file.

Expand Down
9 changes: 3 additions & 6 deletions src/Xrefcheck/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ overrideConfig config

defScanners = cScanners $ defConfig flavor
defExclusions = cExclusions $ defConfig flavor
defNetworking = cNetworking $ defConfig flavor
defRedirectConfig = []
defNetworking = cNetworking (defConfig flavor)
& ncExternalRefRedirectsL .~ []

overrideExclusions exclusionConfig
= ExclusionConfig
Expand All @@ -150,15 +150,12 @@ overrideConfig config
, ncMaxRetries = overrideField ncMaxRetries
, ncMaxTimeoutRetries = overrideField ncMaxTimeoutRetries
, ncMaxRedirectFollows = overrideField ncMaxRedirectFollows
, ncExternalRefRedirects = externalRefRedirects
, ncExternalRefRedirects = overrideField ncExternalRefRedirects
}
where
overrideField :: (forall f. NetworkingConfig' f -> Field f a) -> a
overrideField field = fromMaybe (field defNetworking) $ field networkingConfig

externalRefRedirects :: RedirectConfig
externalRefRedirects = fromMaybe defRedirectConfig $ ncExternalRefRedirects networkingConfig

-----------------------------------------------------------
-- Yaml instances
-----------------------------------------------------------
Expand Down
36 changes: 22 additions & 14 deletions src/Xrefcheck/Config/Default.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Universum

import Text.Interpolation.Nyan

import Fmt (Builder)
import Xrefcheck.Core
import Xrefcheck.Util

Expand Down Expand Up @@ -36,7 +37,7 @@ exclusions:
# List of POSIX extended regular expressions.
ignoreExternalRefsTo:
# Ignore localhost links by default
- ^(https?|ftps?)://(localhost|127\\.0\\.0\\.1).*
- (https?|ftps?)://(localhost|127\\.0\\.0\\.1).*

# Networking parameters.
networking:
Expand Down Expand Up @@ -101,18 +102,8 @@ networking:
#
# The first one that matches is applied, and the link is considered
# as valid if none of them does match.
#
# If a value is provided for 'networking' but not for 'externalRefRedirects',
# then it will default to an empty list of rules and every redirect will pass.
externalRefRedirects:
- from: .*
to: .*
on: permanent
outcome: invalid
- from: .*
to: .*
on: temporary
outcome: valid
#{interpolateIndentF 4 externalRefRedirects}

# Parameters of scanners for various file types.
scanners:
Expand All @@ -128,7 +119,7 @@ scanners:
|]
where
ignoreLocalRefsFrom :: NonEmpty Text
ignoreLocalRefsFrom = fromList $ case flavor of
ignoreLocalRefsFrom = fromList $ case flavor of
GitHub ->
[ ".github/pull_request_template.md"
, ".github/issue_template.md"
Expand All @@ -141,7 +132,7 @@ scanners:
]

ignoreLocalRefsTo :: NonEmpty Text
ignoreLocalRefsTo = fromList $ case flavor of
ignoreLocalRefsTo = fromList $ case flavor of
GitHub ->
[ "../../../issues"
, "../../../issues/*"
Expand All @@ -154,3 +145,20 @@ scanners:
, "../../merge_requests"
, "../../merge_requests/*"
]

externalRefRedirects :: Builder
externalRefRedirects = case flavor of
GitHub ->
[int||
- on: permanent
outcome: invalid|]
GitLab ->
[int||
- on: permanent
outcome: invalid
# GitLab redirects non-existing files to the repository's main page
# with a 302 code instead of answering with a 404 response.
- from: https?://gitlab.com/.*/-/blob/.*
to: https?://gitlab.com/.*
on: 302
outcome: invalid|]
14 changes: 7 additions & 7 deletions tests/Test/Xrefcheck/RedirectConfigSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test_redirectRequests = testGroup "Redirect config tests"
[ testCase "Do match" $ do
setRef <- newIORef mempty
checkLinkAndProgressWithServer
(configMod [RedirectRule Nothing (regex "^.*/ok$") Nothing RROValid] [])
(configMod [RedirectRule Nothing (regex ".*/ok") Nothing RROValid] [])
setRef
mockRedirect
(link "/permanent-redirect")
Expand All @@ -58,7 +58,7 @@ test_redirectRequests = testGroup "Redirect config tests"
, testCase "Do not match" $ do
setRef <- newIORef mempty
checkLinkAndProgressWithServer
(configMod [RedirectRule Nothing (regex "^.*/no-ok$") (Just RROPermanent) RROValid] [])
(configMod [RedirectRule Nothing (regex ".*/no-ok") (Just RROPermanent) RROValid] [])
setRef
mockRedirect
(link "/permanent-redirect")
Expand All @@ -69,7 +69,7 @@ test_redirectRequests = testGroup "Redirect config tests"
[ testCase "Do match" $ do
setRef <- newIORef mempty
checkLinkAndProgressWithServer
(configMod [RedirectRule (regex "^.*/permanent-.*$") Nothing Nothing RROValid] [])
(configMod [RedirectRule (regex ".*/permanent-.*") Nothing Nothing RROValid] [])
setRef
mockRedirect
(link "/permanent-redirect")
Expand All @@ -78,7 +78,7 @@ test_redirectRequests = testGroup "Redirect config tests"
, testCase "Do not match" $ do
setRef <- newIORef mempty
checkLinkAndProgressWithServer
(configMod [RedirectRule (regex "^.*/temporary-.*$") Nothing (Just RROPermanent) RROValid] [])
(configMod [RedirectRule (regex ".*/temporary-.*") Nothing (Just RROPermanent) RROValid] [])
setRef
mockRedirect
(link "/permanent-redirect")
Expand All @@ -89,7 +89,7 @@ test_redirectRequests = testGroup "Redirect config tests"
[ testCase "Do match" $ do
setRef <- newIORef mempty
checkLinkAndProgressWithServer
(configMod [RedirectRule (regex "^.*/follow[0-9]$") (regex "^.*/ok$") (Just (RROCode 307)) RROInvalid] [])
(configMod [RedirectRule (regex ".*/follow[0-9]") (regex "^.*/ok$") (Just (RROCode 307)) RROInvalid] [])
setRef
mockRedirect
(link "/follow3")
Expand All @@ -98,7 +98,7 @@ test_redirectRequests = testGroup "Redirect config tests"
, testCase "Do not match" $ do
setRef <- newIORef mempty
checkLinkAndProgressWithServer
(configMod [RedirectRule (regex "^.*/follow[0-9]$") (regex "^.*/ok$") (Just (RROCode 307)) RROInvalid] [])
(configMod [RedirectRule (regex ".*/follow[0-9]") (regex "^.*/ok$") (Just (RROCode 307)) RROInvalid] [])
setRef
mockRedirect
(link "/follow2")
Expand Down Expand Up @@ -137,7 +137,7 @@ test_redirectRequests = testGroup "Redirect config tests"
, testCase "Mixed with ignore" $ do
setRef <- newIORef mempty
checkLinkAndProgressWithServer
(configMod [RedirectRule Nothing Nothing (Just (RROCode 307)) RROInvalid, RedirectRule Nothing Nothing Nothing RROFollow] (maybeToList (regex "^.*/follow3$")))
(configMod [RedirectRule Nothing Nothing (Just (RROCode 307)) RROInvalid, RedirectRule Nothing Nothing Nothing RROFollow] (maybeToList (regex ".*/follow3")))
setRef
mockRedirect
(link "/follow1")
Expand Down
13 changes: 2 additions & 11 deletions tests/configs/github-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exclusions:
# List of POSIX extended regular expressions.
ignoreExternalRefsTo:
# Ignore localhost links by default
- ^(https?|ftps?)://(localhost|127\.0\.0\.1).*
- (https?|ftps?)://(localhost|127\.0\.0\.1).*

# Networking parameters.
networking:
Expand Down Expand Up @@ -90,18 +90,9 @@ networking:
#
# The first one that matches is applied, and the link is considered
# as valid if none of them does match.
#
# If a value is provided for 'networking' but not for 'externalRefRedirects',
# then it will default to an empty list of rules and every redirect will pass.
externalRefRedirects:
- from: .*
to: .*
on: permanent
- on: permanent
outcome: invalid
- from: .*
to: .*
on: temporary
outcome: valid

# Parameters of scanners for various file types.
scanners:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

exclusions:
ignoreExternalRefsTo:
- ^(https?|ftps?)://(localhost|127\.0\.0\.1).*
- (https?|ftps?)://(localhost|127\.0\.0\.1).*

scanners:
markdown:
Expand Down
1 change: 0 additions & 1 deletion tests/golden/check-redirect-parse/bad-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ scanners:
networking:
externalRefRedirects:
- outcome: valid
to: ^.*$
on: 404
1 change: 0 additions & 1 deletion tests/golden/check-redirect-parse/bad-on.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ scanners:
networking:
externalRefRedirects:
- outcome: valid
to: ^.*$
on: premanent
2 changes: 1 addition & 1 deletion tests/golden/check-redirect-parse/full-rule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ scanners:
networking:
externalRefRedirects:
- outcome: valid
to: ^https://.*$
to: https://.*
on: permanent
2 changes: 1 addition & 1 deletion tests/golden/check-redirect-parse/no-outcome.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ networking:
externalRefRedirects:
- outcome: valid
on: temporary
- to: ^https://.*$
- to: https://.*
on: temporary
2 changes: 1 addition & 1 deletion tests/golden/check-redirect-parse/only-outcome-to.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ scanners:
networking:
externalRefRedirects:
- outcome: valid
to: ^https://.*$
to: https://.*

0 comments on commit a7caf56

Please sign in to comment.