def upsert_build(hook: Hook, external_id: str, data: dict = None) -> Response: provider_name = hook.get_provider().get_name(hook.config) lock_key = "hook:build:{repo_id}:{provider}:{build_xid}".format( repo_id=hook.repository_id, provider=provider_name, build_xid=external_id) with redis.lock(lock_key): json = data.copy() if data else {} json["external_id"] = external_id json["provider"] = provider_name json["hook_id"] = str(hook.id) build = Build.query.filter(Build.provider == provider_name, Build.external_id == external_id).first() if build: return client.put( "/repos/{}/builds/{}".format(hook.repository.get_full_name(), build.number), json=json, ) return client.post("/repos/{}/builds".format( hook.repository.get_full_name()), json=json)
def upsert_job(build: Build, hook: Hook, external_id: str, data: dict = None) -> Response: provider_name = hook.get_provider().get_name(hook.config) lock_key = "upsert:job:{build_id}:{provider}:{job_xid}".format( build_id=build.id, provider=provider_name, job_xid=external_id) with redis.lock(lock_key): json = data.copy() if data else {} json["external_id"] = external_id json["provider"] = provider_name json["hook_id"] = str(hook.id) job = Job.query.filter( Job.provider == provider_name, Job.external_id == external_id, Job.build_id == build.id, ).first() if job: return client.put( "/repos/{}/builds/{}/jobs/{}".format( build.repository.get_full_name(), job.build.number, job.number), json=json, ) return client.post( "/repos/{}/builds/{}/jobs".format(build.repository.get_full_name(), build.number), json=json, )
def upsert_build(hook: Hook, external_id: str, data: dict = None) -> Response: provider_name = hook.get_provider().get_name(hook.config) lock_key = "hook:build:{repo_id}:{provider}:{build_xid}".format( repo_id=hook.repository_id, provider=provider_name, build_xid=external_id ) # TODO (here and in other upsert_* functions): it's better to move all the locking # code to async tasks. with redis.lock(lock_key, timeout=BUILD_LOCK_TIMEOUT, expire=30): json = data.copy() if data else {} json["external_id"] = external_id json["provider"] = provider_name json["hook_id"] = str(hook.id) build = Build.query.filter( Build.provider == provider_name, Build.external_id == external_id ).first() if build: return client.put( "/repos/{}/builds/{}".format( hook.repository.get_full_name(), build.number ), json=json, ) return client.post( "/repos/{}/builds".format(hook.repository.get_full_name()), json=json )