fun getOrAdd()

in src/main/kotlin/org/jetbrains/teamcity/github/WebHooksStorage.kt [131:162]


    fun getOrAdd(created: RepositoryHook): WebHookInfo {
        val key = HookKey.fromHookUrl(created.url)
        val mapKey = key.toMapKey()

        val hooks = getHooks(mapKey)
        val hook = hooks.firstOrNull { it.isSame(created) }
        if (hook != null) {
            LOG.info("Already exist $hook")
            return hook
        }

        myDataLock.write {
            @Suppress("NAME_SHADOWING")
            var hooks = myData[mapKey]
            @Suppress("NAME_SHADOWING")
            val hook = hooks?.firstOrNull { it.isSame(created) }
            if (hook != null) return hook

            val toAdd = WebHookInfo(url = created.url, callbackUrl = created.callbackUrl!!, key = key, status = created.getStatus())

            if (hooks == null || hooks.isEmpty()) {
                hooks = mutableListOf(toAdd)
                myData[mapKey] = hooks
            } else {
                hooks.add(toAdd)
                myData[mapKey] = hooks
            }
            schedulePersist()
            LOG.info("Added $toAdd")
            return toAdd
        }
    }