fun updateHooks()

in src/main/kotlin/org/jetbrains/teamcity/github/action/ActionContext.kt [33:79]


    fun updateHooks(server: String, repo: RepositoryId, filtered: List<RepositoryHook>): Map<RepositoryHook, WebHookInfo> {
        // TODO: Report more than one active webhook in storage as misconfiguration
        if (filtered.isEmpty()) {
            // Mark old hooks as removed
            storage.update(server, repo) {
                it.status = Status.MISSING
            }
            return emptyMap()
        }
        val result = HashMap<RepositoryHook, WebHookInfo>()
        val hooks = storage.getHooks(server, repo).toMutableList()

        val missing = hooks.any { hi ->
            !filtered.any { rh -> hi.isSame(rh) }
        }
        if (missing) {
            storage.update(server, repo) { hi ->
                val rh = filtered.firstOrNull { rh -> hi.isSame(rh) }
                if (rh == null) {
                    hi.status = Status.MISSING
                } else if (!rh.isActive) {
                    // TODO: Should check that status is OK?
                    hi.status = Status.DISABLED
                } else {
                    // TODO: Should update status?
                    if (hi.status in listOf(Status.MISSING, Status.DISABLED)) {
                        hi.status = Status.WAITING_FOR_SERVER_RESPONSE
                    }
                }
            }
        }

        for (hook in filtered) {
            if (hooks.isEmpty()) {
                result[hook] = addHook(hook)!!
            } else {
                val same = hooks.firstOrNull { it.isSame(hook) }
                if (same != null) {
                    result[hook] = same
                    continue
                } else {
                    result[hook] = addHook(hook)!!
                }
            }
        }
        return result
    }