I have a company repository that locks users out f...
# community-support
s
I have a company repository that locks users out for several hours if they try to access it several times in a row with bad credentials. With Gradle this problem is easy to hit once a user's token expires, so I was thinking of adding some kind of check to Gradle that would do a HEAD request before downloading anything from a repository and fail the build if the request shows that the user is unauthorized. I'm not sure Gradle has an appropriate listener for that though. DependencyResolutionListener looks like almost what I want but it will also get called for previously downloaded dependencies, won't it? Does anybody know of anything in Gradle that could help me?
v
Iirc it should actually do a
HEAD
request already. And if it fails for anything but 404 I think the build should already fail.
s
Yes but it does several of those (for different dependencies or different repositories) before it fails the build and by then it’s too late.
v
Ah, you mean a fail-fast as soon as the first dependency is failing. I guess this is not so easy. 1. a normal user wants to right away know all that fail 2. dependency resolution is done in parallel (unless you use configuration cache, unless you enabled the parallel configuration cache storage) So even if it would fail for the first of this cases, it would maybe already have done multiple requests and be blocked. Maybe you could add a
withDependencies
action to all configurations. This action is done for each configuration right before it is first part of dependency resolution. In that action you could maybe - guarded by a count down latch - once check whether accessing the repository works and otherwise fail the build with a meaningful error right away.
s
That would be similar to adding a DependencyResolutionListener to the build, wouldn’t it?
a
maybe you could use an ArtifactView to fetch the failures? I have no idea whether the failures would contain the relevant info, but if they do, then you could throw a more specific error. Quick demo code - just make other tasks depend on checkErrors.
Copy code
val checkErrors by tasks.registering {
    val failures = provider {
        configurations.create("foo") {
            isCanBeConsumed = false
            isCanBeResolved = true
            isCanBeDeclared = false
            extendsFrom(configurations.implementation)
        }
            .incoming
            .artifactView { }
            .artifacts
            .failures
    }

    doLast {
        failures.orNull.orEmpty().forEach {
            if (it.message?.contains("credential error...") == true) {
                error("Refresh your credentials!")
            }
        }
    }
}
s
no, I want to stop after the first failure and not try anything. Or do a quick HEAD request before proceeding with dependency resolution and abort everything if the request comes back with 401 or 403
v
That would be similar to adding a DependencyResolutionListener to the build, wouldn’t it?
Probably 😄
And probably cleaner