Skip to content

Commit 1594efc

Browse files
authored
Merge pull request #1474 from TuongNM/delete-candidate-after-publish
Delete matching candidate after publish
2 parents 6ce7c5d + 428985b commit 1594efc

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

src/Distribution/Server/Features/PackageCandidates.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,16 @@ initPackageCandidatesFeature :: ServerEnv
139139
initPackageCandidatesFeature env@ServerEnv{serverStateDir} = do
140140
candidatesState <- candidatesStateComponent False serverStateDir
141141

142-
return $ \user core upload tarIndexCache -> do
142+
return $ \user core upload@UploadFeature{..} tarIndexCache -> do
143143
-- one-off migration
144144
CandidatePackages{candidateMigratedPkgTarball = migratedPkgTarball} <-
145145
queryState candidatesState GetCandidatePackages
146146
unless migratedPkgTarball $ do
147147
migrateCandidatePkgTarball_v1_to_v2 env candidatesState
148148
updateState candidatesState SetMigratedPkgTarball
149149

150+
registerHook packageUploaded $ updateState candidatesState . DeleteCandidate
151+
150152
let feature = candidatesFeature env
151153
user core upload tarIndexCache
152154
candidatesState

src/Distribution/Server/Features/Upload.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ data UploadFeature = UploadFeature {
4949
-- For new pacakges lifecycle, this should be removed
5050
uploadPackage :: ServerPartE UploadResult,
5151

52+
-- | Notification that a new package was uploaded.
53+
packageUploaded :: Hook PackageId (),
54+
5255
--TODO: consider moving the trustee and/or per-package maintainer groups
5356
-- lower down in the feature hierarchy; many other features want to
5457
-- use the trustee group purely for auth decisions
@@ -110,6 +113,8 @@ initUploadFeature env@ServerEnv{serverStateDir} = do
110113
uploadersState <- uploadersStateComponent serverStateDir
111114
maintainersState <- maintainersStateComponent serverStateDir
112115

116+
packageUploaded <- newHook
117+
113118
return $ \user@UserFeature{..} core@CoreFeature{..} -> do
114119

115120
-- Recusively tie the knot: the feature contains new user group resources
@@ -122,6 +127,7 @@ initUploadFeature env@ServerEnv{serverStateDir} = do
122127
trusteesState trusteesGroup trusteesGroupResource
123128
uploadersState uploadersGroup uploadersGroupResource
124129
maintainersState maintainersGroup maintainersGroupResource
130+
packageUploaded
125131

126132
(trusteesGroup, trusteesGroupResource) <-
127133
groupResourceAt "/packages/trustees" trusteesGroupDescription
@@ -184,6 +190,7 @@ uploadFeature :: ServerEnv
184190
-> StateComponent AcidState HackageTrustees -> UserGroup -> GroupResource
185191
-> StateComponent AcidState HackageUploaders -> UserGroup -> GroupResource
186192
-> StateComponent AcidState PackageMaintainers -> (PackageName -> UserGroup) -> GroupResource
193+
-> Hook PackageId ()
187194
-> (UploadFeature,
188195
UserGroup,
189196
UserGroup,
@@ -198,6 +205,7 @@ uploadFeature ServerEnv{serverBlobStore = store}
198205
trusteesState trusteesGroup trusteesGroupResource
199206
uploadersState uploadersGroup uploadersGroupResource
200207
maintainersState maintainersGroup maintainersGroupResource
208+
packageUploaded
201209
= ( UploadFeature {..}
202210
, trusteesGroupDescription, uploadersGroupDescription, maintainersGroupDescription)
203211
where
@@ -314,6 +322,7 @@ uploadFeature ServerEnv{serverBlobStore = store}
314322
liftIO $ addUserToGroup group uid
315323
runHook_ groupChangedHook (groupDesc group, True,uid,uid,"initial upload")
316324

325+
runHook_ packageUploaded pkgid
317326
return uresult
318327
-- this is already checked in processUpload, and race conditions are highly unlikely but imaginable
319328
else errForbidden "Upload failed" [MText "Package already exists."]

tests/HackageClientUtils.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,14 @@ getJSONStrings :: RelativeURL -> IO [String]
351351
getJSONStrings url = getUrl NoAuth url >>= decodeJSON
352352

353353
checkIsForbidden :: Authorization -> RelativeURL -> IO ()
354-
checkIsForbidden auth url = void $
355-
Http.execRequest' auth (mkGetReq url) isForbidden
354+
checkIsForbidden = checkIsExpectedCode isForbidden
356355

357356
checkIsUnauthorized :: Authorization -> RelativeURL -> IO ()
358-
checkIsUnauthorized auth url = void $
359-
Http.execRequest' auth (mkGetReq url) isUnauthorized
357+
checkIsUnauthorized = checkIsExpectedCode isUnauthorized
358+
359+
checkIsExpectedCode :: ExpectedCode -> Authorization -> RelativeURL -> IO ()
360+
checkIsExpectedCode expectedCode auth url = void $
361+
Http.execRequest' auth (mkGetReq url) expectedCode
360362

361363
delete :: ExpectedCode -> Authorization -> RelativeURL -> IO ()
362364
delete expectedCode auth url = void $

tests/HighLevelTest.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import Util
2828
import HttpUtils ( isOk
2929
, isNoContent
3030
, isForbidden
31+
, isSeeOther
32+
, isNotFound
3133
, Authorization(..)
3234
)
3335
import HackageClientUtils
@@ -158,6 +160,15 @@ runPackageUploadTests = do
158160
post (Auth "admin" "admin") "/packages/uploaders/" [
159161
("user", "HackageTestUser1")
160162
]
163+
do info "Uploading testpackage candidate"
164+
postFile isSeeOther
165+
(Auth "HackageTestUser1" "testpass1")
166+
"/packages/candidates" "package"
167+
(testpackageTarFilename, testpackageTarFileContent)
168+
do info "Checking Package Candidate Exists"
169+
xs <- validate NoAuth "/package/testpackage-1.0.0.0/candidate"
170+
unless (">testpackage: <small>test package testpackage</small></h1>" `isInfixOf` xs) $
171+
die ("Bad package candidate info: " ++ show xs)
161172
do info "Uploading testpackage"
162173
postFile isOk
163174
(Auth "HackageTestUser1" "testpass1")
@@ -171,6 +182,8 @@ runPackageUploadTests = do
171182
xs <- validate NoAuth "/package/testpackage-1.0.0.0"
172183
unless (">testpackage</a>: <small>test package testpackage</small></h1>" `isInfixOf` xs) $
173184
die ("Bad package info: " ++ show xs)
185+
do info "Checking Package Candidate no longer exists after package upload"
186+
checkIsExpectedCode isNotFound NoAuth "/package/testpackage-1.0.0.0/candidate"
174187
do info "Setting upload time"
175188
putText (Auth "HackageTestUser1" "testpass1")
176189
"/package/testpackage-1.0.0.0/upload-time"

tests/HttpUtils.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module HttpUtils (
1111
, isNotModified
1212
, isUnauthorized
1313
, isForbidden
14+
, isNotFound
1415
, parseQuery
1516
-- * Stateful functions
1617
, Authorization(..)
@@ -51,13 +52,15 @@ type ExpectedCode = (Int, Int, Int) -> Bool
5152

5253
isOk, isAccepted, isNoContent, isSeeOther :: ExpectedCode
5354
isNotModified, isUnauthorized, isForbidden :: ExpectedCode
55+
isNotFound :: ExpectedCode
5456
isOk = (== (2, 0, 0))
5557
isAccepted = (== (2, 0, 2))
5658
isNoContent = (== (2, 0, 4))
5759
isSeeOther = (== (3, 0, 3))
5860
isNotModified = (== (3, 0, 4))
5961
isUnauthorized = (== (4, 0, 1))
6062
isForbidden = (== (4, 0, 3))
63+
isNotFound = (== (4, 0, 4))
6164

6265
parseQuery :: String -> [(String, String)]
6366
parseQuery = map parseAssignment . explode '&'

0 commit comments

Comments
 (0)