{"action":"created","comment":{"url":"https://api.github.com/repos/allegro/ralph_pricing/pulls/comments/157753399","pull_request_review_id":84446436,"id":157753399,"diff_hunk":"@@ -0,0 +1,176 @@\n+# -*- coding: utf-8 -*-\n+import logging\n+\n+from pyhermes.decorators import subscriber\n+\n+from ralph_scrooge.models import (\n+    ProfitCenter,\n+    Environment,\n+    Service,\n+    ServiceEnvironment,\n+    OwnershipType,\n+    ScroogeUser,\n+    ServiceOwnership\n+)\n+\n+logger = logging.getLogger(__name__)\n+\n+\n+@subscriber(topic='createService')\n+@subscriber(topic='updateService')\n+def service_environment(event_data):\n+    logger.info(\n+        u'Start service environment processing for service with name: '\n+        u'\"{}\" and uid: {}'.format(\n+            event_data.get('name'), event_data.get('uid')\n+        )\n+    )\n+    logger.debug(u'Event data: {}'.format(event_data))\n+    errors = _validate_service_data(event_data)\n+    if errors:\n+        msg = (\n+            'Error(s) detected in event data: {}. Ignoring received '\n+            'createService (service name: \"{}\", uid: {}).'\n+        )\n+        logger.error(msg.format(\n+            '; '.join(errors),\n+            event_data.get('name'),\n+            event_data.get('uid')\n+        ))\n+        return\n+\n+    environments = _add_new_environments(event_data.get('environments', []))\n+    service = _update_service(event_data)\n+    _update_environments(service, environments)\n+    _update_owners(service, event_data)\n+    logger.info(\n+        u'Finished service environment processing for service with name: '\n+        u'\"{}\" and uid: {}'.format(event_data['name'], event_data['uid'])\n+    )\n+\n+\n+def _validate_service_data(event_data):\n+    errors = []\n+\n+    if not event_data.get('uid'):\n+        errors.append('missing uid')\n+    if not event_data.get('name'):\n+        errors.append('missing name')\n+    return errors\n+\n+\n+def _add_new_environments(envs):\n+    environments = []\n+    created_environments = []\n+    for env_name in envs:\n+        obj, created = Environment.objects.get_or_create(name=env_name)\n+        environments.append(obj)\n+        if created:\n+            created_environments.append(obj)\n+    if created_environments:\n+        logger.info(u'Added new environments: {}'.format(\n+            [env.name for env in created_environments]\n+        ))\n+    return environments\n+\n+\n+def _update_service(event_data):\n+    default_profit_center = ProfitCenter.objects.get(pk=1)  # from fixtures\n+\n+    service, created = Service.objects.get_or_create(ci_uid=event_data['uid'])","path":"src/ralph_scrooge/plugins/subscribers/service_environment.py","position":80,"original_position":80,"commit_id":"a760beea61dff1e6db1159c84d2c7c8843f7ffb4","original_commit_id":"a760beea61dff1e6db1159c84d2c7c8843f7ffb4","user":{"login":"mkurek","id":107437,"avatar_url":"https://avatars1.githubusercontent.com/u/107437?v=4","gravatar_id":"","url":"https://api.github.com/users/mkurek","html_url":"https://github.com/mkurek","followers_url":"https://api.github.com/users/mkurek/followers","following_url":"https://api.github.com/users/mkurek/following{/other_user}","gists_url":"https://api.github.com/users/mkurek/gists{/gist_id}","starred_url":"https://api.github.com/users/mkurek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mkurek/subscriptions","organizations_url":"https://api.github.com/users/mkurek/orgs","repos_url":"https://api.github.com/users/mkurek/repos","events_url":"https://api.github.com/users/mkurek/events{/privacy}","received_events_url":"https://api.github.com/users/mkurek/received_events","type":"User","site_admin":false},"body":"why not `update_or_create`?","created_at":"2017-12-19T13:29:28Z","updated_at":"2017-12-19T13:43:38Z","html_url":"https://github.com/allegro/ralph_pricing/pull/623#discussion_r157753399","pull_request_url":"https://api.github.com/repos/allegro/ralph_pricing/pulls/623","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/allegro/ralph_pricing/pulls/comments/157753399"},"html":{"href":"https://github.com/allegro/ralph_pricing/pull/623#discussion_r157753399"},"pull_request":{"href":"https://api.github.com/repos/allegro/ralph_pricing/pulls/623"}}},"pull_request":{"url":"https://api.github.com/repos/allegro/ralph_pricing/pulls/623","id":158852161,"html_url":"https://github.com/allegro/ralph_pricing/pull/623","diff_url":"https://github.com/allegro/ralph_pricing/pull/623.diff","patch_url":"https://github.com/allegro/ralph_pricing/pull/623.patch","issue_url":"https://api.github.com/repos/allegro/ralph_pricing/issues/623","number":623,"state":"open","locked":false,"title":"Feature/real time service update","user":{"login":"MarekBleschke","id":3616870,"avatar_url":"https://avatars2.githubusercontent.com/u/3616870?v=4","gravatar_id":"","url":"https://api.github.com/users/MarekBleschke","html_url":"https://github.com/MarekBleschke","followers_url":"https://api.github.com/users/MarekBleschke/followers","following_url":"https://api.github.com/users/MarekBleschke/following{/other_user}","gists_url":"https://api.github.com/users/MarekBleschke/gists{/gist_id}","starred_url":"https://api.github.com/users/MarekBleschke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MarekBleschke/subscriptions","organizations_url":"https://api.github.com/users/MarekBleschke/orgs","repos_url":"https://api.github.com/users/MarekBleschke/repos","events_url":"https://api.github.com/users/MarekBleschke/events{/privacy}","received_events_url":"https://api.github.com/users/MarekBleschke/received_events","type":"User","site_admin":false},"body":"","created_at":"2017-12-18T08:13:27Z","updated_at":"2017-12-19T13:43:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9bc52461c9d7e32f0641c17aeb488795f4d880b5","assignee":null,"assignees":[],"requested_reviewers":[],"milestone":null,"commits_url":"https://api.github.com/repos/allegro/ralph_pricing/pulls/623/commits","review_comments_url":"https://api.github.com/repos/allegro/ralph_pricing/pulls/623/comments","review_comment_url":"https://api.github.com/repos/allegro/ralph_pricing/pulls/comments{/number}","comments_url":"https://api.github.com/repos/allegro/ralph_pricing/issues/623/comments","statuses_url":"https://api.github.com/repos/allegro/ralph_pricing/statuses/a760beea61dff1e6db1159c84d2c7c8843f7ffb4","head":{"label":"MarekBleschke:feature/real-time-service-update","ref":"feature/real-time-service-update","sha":"a760beea61dff1e6db1159c84d2c7c8843f7ffb4","user":{"login":"MarekBleschke","id":3616870,"avatar_url":"https://avatars2.githubusercontent.com/u/3616870?v=4","gravatar_id":"","url":"https://api.github.com/users/MarekBleschke","html_url":"https://github.com/MarekBleschke","followers_url":"https://api.github.com/users/MarekBleschke/followers","following_url":"https://api.github.com/users/MarekBleschke/following{/other_user}","gists_url":"https://api.github.com/users/MarekBleschke/gists{/gist_id}","starred_url":"https://api.github.com/users/MarekBleschke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MarekBleschke/subscriptions","organizations_url":"https://api.github.com/users/MarekBleschke/orgs","repos_url":"https://api.github.com/users/MarekBleschke/repos","events_url":"https://api.github.com/users/MarekBleschke/events{/privacy}","received_events_url":"https://api.github.com/users/MarekBleschke/received_events","type":"User","site_admin":false},"repo":{"id":113187916,"name":"ralph_pricing","full_name":"MarekBleschke/ralph_pricing","owner":{"login":"MarekBleschke","id":3616870,"avatar_url":"https://avatars2.githubusercontent.com/u/3616870?v=4","gravatar_id":"","url":"https://api.github.com/users/MarekBleschke","html_url":"https://github.com/MarekBleschke","followers_url":"https://api.github.com/users/MarekBleschke/followers","following_url":"https://api.github.com/users/MarekBleschke/following{/other_user}","gists_url":"https://api.github.com/users/MarekBleschke/gists{/gist_id}","starred_url":"https://api.github.com/users/MarekBleschke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MarekBleschke/subscriptions","organizations_url":"https://api.github.com/users/MarekBleschke/orgs","repos_url":"https://api.github.com/users/MarekBleschke/repos","events_url":"https://api.github.com/users/MarekBleschke/events{/privacy}","received_events_url":"https://api.github.com/users/MarekBleschke/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/MarekBleschke/ralph_pricing","description":"A pricing module for Ralph","fork":true,"url":"https://api.github.com/repos/MarekBleschke/ralph_pricing","forks_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/forks","keys_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/keys{/key_id}","collaborators_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/teams","hooks_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/hooks","issue_events_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/issues/events{/number}","events_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/events","assignees_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/assignees{/user}","branches_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/branches{/branch}","tags_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/tags","blobs_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/git/refs{/sha}","trees_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/git/trees{/sha}","statuses_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/statuses/{sha}","languages_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/languages","stargazers_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/stargazers","contributors_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/contributors","subscribers_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/subscribers","subscription_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/subscription","commits_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/commits{/sha}","git_commits_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/git/commits{/sha}","comments_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/comments{/number}","issue_comment_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/issues/comments{/number}","contents_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/contents/{+path}","compare_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/compare/{base}...{head}","merges_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/merges","archive_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/downloads","issues_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/issues{/number}","pulls_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/pulls{/number}","milestones_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/milestones{/number}","notifications_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/labels{/name}","releases_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/releases{/id}","deployments_url":"https://api.github.com/repos/MarekBleschke/ralph_pricing/deployments","created_at":"2017-12-05T13:48:23Z","updated_at":"2017-12-05T13:48:26Z","pushed_at":"2017-12-18T09:59:45Z","git_url":"git://github.com/MarekBleschke/ralph_pricing.git","ssh_url":"git@github.com:MarekBleschke/ralph_pricing.git","clone_url":"https://github.com/MarekBleschke/ralph_pricing.git","svn_url":"https://github.com/MarekBleschke/ralph_pricing","homepage":null,"size":5649,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":null,"forks":0,"open_issues":0,"watchers":0,"default_branch":"develop"}},"base":{"label":"allegro:develop","ref":"develop","sha":"c20764cd6681cca52cc4ece05d5c1aa589036a16","user":{"login":"allegro","id":562236,"avatar_url":"https://avatars3.githubusercontent.com/u/562236?v=4","gravatar_id":"","url":"https://api.github.com/users/allegro","html_url":"https://github.com/allegro","followers_url":"https://api.github.com/users/allegro/followers","following_url":"https://api.github.com/users/allegro/following{/other_user}","gists_url":"https://api.github.com/users/allegro/gists{/gist_id}","starred_url":"https://api.github.com/users/allegro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allegro/subscriptions","organizations_url":"https://api.github.com/users/allegro/orgs","repos_url":"https://api.github.com/users/allegro/repos","events_url":"https://api.github.com/users/allegro/events{/privacy}","received_events_url":"https://api.github.com/users/allegro/received_events","type":"Organization","site_admin":false},"repo":{"id":9495095,"name":"ralph_pricing","full_name":"allegro/ralph_pricing","owner":{"login":"allegro","id":562236,"avatar_url":"https://avatars3.githubusercontent.com/u/562236?v=4","gravatar_id":"","url":"https://api.github.com/users/allegro","html_url":"https://github.com/allegro","followers_url":"https://api.github.com/users/allegro/followers","following_url":"https://api.github.com/users/allegro/following{/other_user}","gists_url":"https://api.github.com/users/allegro/gists{/gist_id}","starred_url":"https://api.github.com/users/allegro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/allegro/subscriptions","organizations_url":"https://api.github.com/users/allegro/orgs","repos_url":"https://api.github.com/users/allegro/repos","events_url":"https://api.github.com/users/allegro/events{/privacy}","received_events_url":"https://api.github.com/users/allegro/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/allegro/ralph_pricing","description":"A pricing module for Ralph","fork":false,"url":"https://api.github.com/repos/allegro/ralph_pricing","forks_url":"https://api.github.com/repos/allegro/ralph_pricing/forks","keys_url":"https://api.github.com/repos/allegro/ralph_pricing/keys{/key_id}","collaborators_url":"https://api.github.com/repos/allegro/ralph_pricing/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/allegro/ralph_pricing/teams","hooks_url":"https://api.github.com/repos/allegro/ralph_pricing/hooks","issue_events_url":"https://api.github.com/repos/allegro/ralph_pricing/issues/events{/number}","events_url":"https://api.github.com/repos/allegro/ralph_pricing/events","assignees_url":"https://api.github.com/repos/allegro/ralph_pricing/assignees{/user}","branches_url":"https://api.github.com/repos/allegro/ralph_pricing/branches{/branch}","tags_url":"https://api.github.com/repos/allegro/ralph_pricing/tags","blobs_url":"https://api.github.com/repos/allegro/ralph_pricing/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/allegro/ralph_pricing/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/allegro/ralph_pricing/git/refs{/sha}","trees_url":"https://api.github.com/repos/allegro/ralph_pricing/git/trees{/sha}","statuses_url":"https://api.github.com/repos/allegro/ralph_pricing/statuses/{sha}","languages_url":"https://api.github.com/repos/allegro/ralph_pricing/languages","stargazers_url":"https://api.github.com/repos/allegro/ralph_pricing/stargazers","contributors_url":"https://api.github.com/repos/allegro/ralph_pricing/contributors","subscribers_url":"https://api.github.com/repos/allegro/ralph_pricing/subscribers","subscription_url":"https://api.github.com/repos/allegro/ralph_pricing/subscription","commits_url":"https://api.github.com/repos/allegro/ralph_pricing/commits{/sha}","git_commits_url":"https://api.github.com/repos/allegro/ralph_pricing/git/commits{/sha}","comments_url":"https://api.github.com/repos/allegro/ralph_pricing/comments{/number}","issue_comment_url":"https://api.github.com/repos/allegro/ralph_pricing/issues/comments{/number}","contents_url":"https://api.github.com/repos/allegro/ralph_pricing/contents/{+path}","compare_url":"https://api.github.com/repos/allegro/ralph_pricing/compare/{base}...{head}","merges_url":"https://api.github.com/repos/allegro/ralph_pricing/merges","archive_url":"https://api.github.com/repos/allegro/ralph_pricing/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/allegro/ralph_pricing/downloads","issues_url":"https://api.github.com/repos/allegro/ralph_pricing/issues{/number}","pulls_url":"https://api.github.com/repos/allegro/ralph_pricing/pulls{/number}","milestones_url":"https://api.github.com/repos/allegro/ralph_pricing/milestones{/number}","notifications_url":"https://api.github.com/repos/allegro/ralph_pricing/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/allegro/ralph_pricing/labels{/name}","releases_url":"https://api.github.com/repos/allegro/ralph_pricing/releases{/id}","deployments_url":"https://api.github.com/repos/allegro/ralph_pricing/deployments","created_at":"2013-04-17T10:30:06Z","updated_at":"2017-12-05T13:40:57Z","pushed_at":"2017-12-18T09:59:46Z","git_url":"git://github.com/allegro/ralph_pricing.git","ssh_url":"git@github.com:allegro/ralph_pricing.git","clone_url":"https://github.com/allegro/ralph_pricing.git","svn_url":"https://github.com/allegro/ralph_pricing","homepage":null,"size":5641,"stargazers_count":9,"watchers_count":9,"language":"Python","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":18,"mirror_url":null,"archived":false,"open_issues_count":7,"license":{"key":"other","name":"Other","spdx_id":null,"url":null},"forks":18,"open_issues":7,"watchers":9,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/allegro/ralph_pricing/pulls/623"},"html":{"href":"https://github.com/allegro/ralph_pricing/pull/623"},"issue":{"href":"https://api.github.com/repos/allegro/ralph_pricing/issues/623"},"comments":{"href":"https://api.github.com/repos/allegro/ralph_pricing/issues/623/comments"},"review_comments":{"href":"https://api.github.com/repos/allegro/ralph_pricing/pulls/623/comments"},"review_comment":{"href":"https://api.github.com/repos/allegro/ralph_pricing/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/allegro/ralph_pricing/pulls/623/commits"},"statuses":{"href":"https://api.github.com/repos/allegro/ralph_pricing/statuses/a760beea61dff1e6db1159c84d2c7c8843f7ffb4"}},"author_association":"NONE"}}
{"actor":{"display_login":"mkurek"}}