{"action":"created","comment":{"url":"https://api.github.com/repos/cycloidio/approval-resource/pulls/comments/90448701","pull_request_review_id":10966489,"id":90448701,"diff_hunk":"@@ -0,0 +1,344 @@\n+#!/usr/bin/env python3\n+\n+from datetime import (datetime, timezone)\n+from flywheel import Model, Field, Engine\n+from decimal import Decimal\n+import simplejson as json\n+import logging as log\n+import os\n+import sys\n+import tempfile\n+import time\n+import uuid\n+\n+\n+class Approval(Model):\n+    \"\"\"\n+        We define the dynamodb model of the Approval locks\n+    \"\"\"\n+\n+    __metadata__ = {\n+        '_name': 'concourse-approval',\n+        'throughput': {\n+            'read': 1,\n+            'write': 1,\n+        }\n+    }\n+    id = Field(type=str, range_key=True)\n+    lockname = Field()\n+    pool = Field(hash_key=True)\n+    timestamp = Field(type=datetime, index='ts-index')\n+    claimed = Field(type=bool)\n+    need_approval = Field(type=bool, default=False)\n+    approved = Field(type=bool, nullable=True)\n+    team = Field()\n+    pipeline = Field()\n+    description = Field(type=str, nullable=True)\n+\n+\n+class ApprovalResource:\n+    \"\"\"\n+        Approval resource implementation.\n+        This python script is the target of symbolic links and is used for check, in and out.\n+        These three commands are defined as methods on this class and common parameters live in the constructor.\n+        To enable the debug output, the resource source configuration must have the debug parameter.\n+\n+    \"\"\"\n+\n+    def __init__(self, command_name, json_data, command_argument):\n+        self.command_name = command_name\n+        self.command_argument = command_argument\n+        # Namespace the approval lock\n+        self.data = json.loads(json_data)\n+        self.wait_lock = 10\n+        self.pool = ''\n+        self.engine = Engine()\n+\n+        # allow debug logging to console for tests\n+        if os.getenv('RESOURCE_DEBUG', False) or self.data.get('source', {}).get('debug', False):\n+            log.basicConfig(level=log.DEBUG)\n+        else:\n+            logfile = tempfile.NamedTemporaryFile(delete=False, prefix='log')\n+            log.basicConfig(level=log.DEBUG, filename=logfile.name)\n+        stderr = log.StreamHandler()\n+        stderr.setLevel(log.INFO)\n+        log.getLogger().addHandler(stderr)\n+\n+        log.debug('command: %s', command_name)\n+        log.debug('input: %s', self.data)\n+        log.debug('args: %s', command_argument)\n+        log.debug('environment: %s', os.environ)\n+\n+    def check_cmd(self, source, version):\n+        \"\"\"\n+        Check for new version(s)\n+        This function will look on Dynamodb if there's a lock within the pool\n+        and will return the last timestamps associated.\n+        :param source: is an arbitrary JSON object which specifies the location of the resource,\n+        including any credentials. This is passed verbatim from the pipeline configuration.\n+        :param version: is a JSON object with string fields, used to uniquely identify an instance of the resource.\n+        :return: a dict with the version fetched\n+        \"\"\"\n+\n+        log.debug('version: %s', version)\n+\n+        if not version:\n+            version = {\"timestamp\": '0'}\n+        log.debug('source: %s', source)\n+        log.debug('version: %s', version)\n+        approval_locks = self.engine.query(Approval)\\\n+            .filter(\n+                Approval.timestamp >= datetime.fromtimestamp(Decimal(version.get('timestamp'))),\n+                pool=self.pool) \\\n+            .index('ts-index') \\\n+            .all()\n+        versions_list = []\n+        for lock in approval_locks:\n+            versions_list.append({\"timestamp\": \"{timestamp}\".format(timestamp=Decimal(lock.timestamp.timestamp()))})\n+        if not approval_locks:\n+            versions_list.append(version)\n+        log.debug(versions_list)\n+        return versions_list\n+\n+    def in_cmd(self, target_dir, source, version, params):\n+        \"\"\"\n+        This function will fetch a lock in dynamodb an write it in the target directory.\n+        If parameters lock_name and need_approval are passed, then the function will wait for a change\n+        on the dynamodb lock item.\n+        :param target_dir: a temporary directory which will be exposed as an output\n+        :param source: is the same value as passed to check\n+        :param version: is the same type of value passed to check, and specifies the version to fetch.\n+        :param params: is an arbitrary JSON object passed along verbatim from params on a get.\n+        :return: a dict with the version fetched and the metadata of the lock\n+        \"\"\"\n+        log.debug('source: %s', source)\n+        log.debug('version: %s', version)\n+\n+        if not version:\n+            version = {\"timestamp\": 0}\n+\n+        if params:\n+            need_approval = params.get('need_approval', False)\n+\n+        # Does the get should wait for an approval or not ?\n+        if 'lock_name' in params and 'need_approval' in params:\n+            approval_lock = self.engine.query(Approval) \\\n+                .filter(\n+                    pool=self.pool,\n+                    lockname=params.get('lock_name')) \\\n+                .index('ts-index') \\\n+                .first(desc=True)\n+\n+            # We want to wait until the approve is done\n+            if approval_lock.need_approval:\n+                while approval_lock.approved is None:","path":"assets/approval.py","position":134,"original_position":134,"commit_id":"13d53e09dcf9a845e5ba0075f198a90fea236eec","original_commit_id":"13d53e09dcf9a845e5ba0075f198a90fea236eec","user":{"login":"Seraf","id":393324,"avatar_url":"https://avatars.githubusercontent.com/u/393324?v=3","gravatar_id":"","url":"https://api.github.com/users/Seraf","html_url":"https://github.com/Seraf","followers_url":"https://api.github.com/users/Seraf/followers","following_url":"https://api.github.com/users/Seraf/following{/other_user}","gists_url":"https://api.github.com/users/Seraf/gists{/gist_id}","starred_url":"https://api.github.com/users/Seraf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Seraf/subscriptions","organizations_url":"https://api.github.com/users/Seraf/orgs","repos_url":"https://api.github.com/users/Seraf/repos","events_url":"https://api.github.com/users/Seraf/events{/privacy}","received_events_url":"https://api.github.com/users/Seraf/received_events","type":"User","site_admin":false},"body":":+1: ","created_at":"2016-12-01T13:46:15Z","updated_at":"2016-12-01T13:46:15Z","html_url":"https://github.com/cycloidio/approval-resource/pull/1#discussion_r90448701","pull_request_url":"https://api.github.com/repos/cycloidio/approval-resource/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/cycloidio/approval-resource/pulls/comments/90448701"},"html":{"href":"https://github.com/cycloidio/approval-resource/pull/1#discussion_r90448701"},"pull_request":{"href":"https://api.github.com/repos/cycloidio/approval-resource/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/cycloidio/approval-resource/pulls/1","id":95851285,"html_url":"https://github.com/cycloidio/approval-resource/pull/1","diff_url":"https://github.com/cycloidio/approval-resource/pull/1.diff","patch_url":"https://github.com/cycloidio/approval-resource/pull/1.patch","issue_url":"https://api.github.com/repos/cycloidio/approval-resource/issues/1","number":1,"state":"open","locked":false,"title":"Initial commit of the approval resource","user":{"login":"Seraf","id":393324,"avatar_url":"https://avatars.githubusercontent.com/u/393324?v=3","gravatar_id":"","url":"https://api.github.com/users/Seraf","html_url":"https://github.com/Seraf","followers_url":"https://api.github.com/users/Seraf/followers","following_url":"https://api.github.com/users/Seraf/following{/other_user}","gists_url":"https://api.github.com/users/Seraf/gists{/gist_id}","starred_url":"https://api.github.com/users/Seraf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Seraf/subscriptions","organizations_url":"https://api.github.com/users/Seraf/orgs","repos_url":"https://api.github.com/users/Seraf/repos","events_url":"https://api.github.com/users/Seraf/events{/privacy}","received_events_url":"https://api.github.com/users/Seraf/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-11-30T09:01:55Z","updated_at":"2016-12-01T13:46:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"db24ff6644c773870733c03ed0bda18af0d93a33","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/cycloidio/approval-resource/pulls/1/commits","review_comments_url":"https://api.github.com/repos/cycloidio/approval-resource/pulls/1/comments","review_comment_url":"https://api.github.com/repos/cycloidio/approval-resource/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cycloidio/approval-resource/issues/1/comments","statuses_url":"https://api.github.com/repos/cycloidio/approval-resource/statuses/13d53e09dcf9a845e5ba0075f198a90fea236eec","head":{"label":"cycloidio:develop","ref":"develop","sha":"13d53e09dcf9a845e5ba0075f198a90fea236eec","user":{"login":"cycloidio","id":14579210,"avatar_url":"https://avatars.githubusercontent.com/u/14579210?v=3","gravatar_id":"","url":"https://api.github.com/users/cycloidio","html_url":"https://github.com/cycloidio","followers_url":"https://api.github.com/users/cycloidio/followers","following_url":"https://api.github.com/users/cycloidio/following{/other_user}","gists_url":"https://api.github.com/users/cycloidio/gists{/gist_id}","starred_url":"https://api.github.com/users/cycloidio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cycloidio/subscriptions","organizations_url":"https://api.github.com/users/cycloidio/orgs","repos_url":"https://api.github.com/users/cycloidio/repos","events_url":"https://api.github.com/users/cycloidio/events{/privacy}","received_events_url":"https://api.github.com/users/cycloidio/received_events","type":"Organization","site_admin":false},"repo":{"id":74557210,"name":"approval-resource","full_name":"cycloidio/approval-resource","owner":{"login":"cycloidio","id":14579210,"avatar_url":"https://avatars.githubusercontent.com/u/14579210?v=3","gravatar_id":"","url":"https://api.github.com/users/cycloidio","html_url":"https://github.com/cycloidio","followers_url":"https://api.github.com/users/cycloidio/followers","following_url":"https://api.github.com/users/cycloidio/following{/other_user}","gists_url":"https://api.github.com/users/cycloidio/gists{/gist_id}","starred_url":"https://api.github.com/users/cycloidio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cycloidio/subscriptions","organizations_url":"https://api.github.com/users/cycloidio/orgs","repos_url":"https://api.github.com/users/cycloidio/repos","events_url":"https://api.github.com/users/cycloidio/events{/privacy}","received_events_url":"https://api.github.com/users/cycloidio/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cycloidio/approval-resource","description":null,"fork":false,"url":"https://api.github.com/repos/cycloidio/approval-resource","forks_url":"https://api.github.com/repos/cycloidio/approval-resource/forks","keys_url":"https://api.github.com/repos/cycloidio/approval-resource/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cycloidio/approval-resource/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cycloidio/approval-resource/teams","hooks_url":"https://api.github.com/repos/cycloidio/approval-resource/hooks","issue_events_url":"https://api.github.com/repos/cycloidio/approval-resource/issues/events{/number}","events_url":"https://api.github.com/repos/cycloidio/approval-resource/events","assignees_url":"https://api.github.com/repos/cycloidio/approval-resource/assignees{/user}","branches_url":"https://api.github.com/repos/cycloidio/approval-resource/branches{/branch}","tags_url":"https://api.github.com/repos/cycloidio/approval-resource/tags","blobs_url":"https://api.github.com/repos/cycloidio/approval-resource/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cycloidio/approval-resource/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cycloidio/approval-resource/git/refs{/sha}","trees_url":"https://api.github.com/repos/cycloidio/approval-resource/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cycloidio/approval-resource/statuses/{sha}","languages_url":"https://api.github.com/repos/cycloidio/approval-resource/languages","stargazers_url":"https://api.github.com/repos/cycloidio/approval-resource/stargazers","contributors_url":"https://api.github.com/repos/cycloidio/approval-resource/contributors","subscribers_url":"https://api.github.com/repos/cycloidio/approval-resource/subscribers","subscription_url":"https://api.github.com/repos/cycloidio/approval-resource/subscription","commits_url":"https://api.github.com/repos/cycloidio/approval-resource/commits{/sha}","git_commits_url":"https://api.github.com/repos/cycloidio/approval-resource/git/commits{/sha}","comments_url":"https://api.github.com/repos/cycloidio/approval-resource/comments{/number}","issue_comment_url":"https://api.github.com/repos/cycloidio/approval-resource/issues/comments{/number}","contents_url":"https://api.github.com/repos/cycloidio/approval-resource/contents/{+path}","compare_url":"https://api.github.com/repos/cycloidio/approval-resource/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cycloidio/approval-resource/merges","archive_url":"https://api.github.com/repos/cycloidio/approval-resource/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cycloidio/approval-resource/downloads","issues_url":"https://api.github.com/repos/cycloidio/approval-resource/issues{/number}","pulls_url":"https://api.github.com/repos/cycloidio/approval-resource/pulls{/number}","milestones_url":"https://api.github.com/repos/cycloidio/approval-resource/milestones{/number}","notifications_url":"https://api.github.com/repos/cycloidio/approval-resource/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cycloidio/approval-resource/labels{/name}","releases_url":"https://api.github.com/repos/cycloidio/approval-resource/releases{/id}","deployments_url":"https://api.github.com/repos/cycloidio/approval-resource/deployments","created_at":"2016-11-23T08:38:56Z","updated_at":"2016-11-30T15:15:01Z","pushed_at":"2016-11-30T15:23:56Z","git_url":"git://github.com/cycloidio/approval-resource.git","ssh_url":"git@github.com:cycloidio/approval-resource.git","clone_url":"https://github.com/cycloidio/approval-resource.git","svn_url":"https://github.com/cycloidio/approval-resource","homepage":null,"size":18,"stargazers_count":1,"watchers_count":1,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"base":{"label":"cycloidio:master","ref":"master","sha":"c45617edd8eb86b54b95d601c48df3c7a80a9233","user":{"login":"cycloidio","id":14579210,"avatar_url":"https://avatars.githubusercontent.com/u/14579210?v=3","gravatar_id":"","url":"https://api.github.com/users/cycloidio","html_url":"https://github.com/cycloidio","followers_url":"https://api.github.com/users/cycloidio/followers","following_url":"https://api.github.com/users/cycloidio/following{/other_user}","gists_url":"https://api.github.com/users/cycloidio/gists{/gist_id}","starred_url":"https://api.github.com/users/cycloidio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cycloidio/subscriptions","organizations_url":"https://api.github.com/users/cycloidio/orgs","repos_url":"https://api.github.com/users/cycloidio/repos","events_url":"https://api.github.com/users/cycloidio/events{/privacy}","received_events_url":"https://api.github.com/users/cycloidio/received_events","type":"Organization","site_admin":false},"repo":{"id":74557210,"name":"approval-resource","full_name":"cycloidio/approval-resource","owner":{"login":"cycloidio","id":14579210,"avatar_url":"https://avatars.githubusercontent.com/u/14579210?v=3","gravatar_id":"","url":"https://api.github.com/users/cycloidio","html_url":"https://github.com/cycloidio","followers_url":"https://api.github.com/users/cycloidio/followers","following_url":"https://api.github.com/users/cycloidio/following{/other_user}","gists_url":"https://api.github.com/users/cycloidio/gists{/gist_id}","starred_url":"https://api.github.com/users/cycloidio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cycloidio/subscriptions","organizations_url":"https://api.github.com/users/cycloidio/orgs","repos_url":"https://api.github.com/users/cycloidio/repos","events_url":"https://api.github.com/users/cycloidio/events{/privacy}","received_events_url":"https://api.github.com/users/cycloidio/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cycloidio/approval-resource","description":null,"fork":false,"url":"https://api.github.com/repos/cycloidio/approval-resource","forks_url":"https://api.github.com/repos/cycloidio/approval-resource/forks","keys_url":"https://api.github.com/repos/cycloidio/approval-resource/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cycloidio/approval-resource/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cycloidio/approval-resource/teams","hooks_url":"https://api.github.com/repos/cycloidio/approval-resource/hooks","issue_events_url":"https://api.github.com/repos/cycloidio/approval-resource/issues/events{/number}","events_url":"https://api.github.com/repos/cycloidio/approval-resource/events","assignees_url":"https://api.github.com/repos/cycloidio/approval-resource/assignees{/user}","branches_url":"https://api.github.com/repos/cycloidio/approval-resource/branches{/branch}","tags_url":"https://api.github.com/repos/cycloidio/approval-resource/tags","blobs_url":"https://api.github.com/repos/cycloidio/approval-resource/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cycloidio/approval-resource/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cycloidio/approval-resource/git/refs{/sha}","trees_url":"https://api.github.com/repos/cycloidio/approval-resource/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cycloidio/approval-resource/statuses/{sha}","languages_url":"https://api.github.com/repos/cycloidio/approval-resource/languages","stargazers_url":"https://api.github.com/repos/cycloidio/approval-resource/stargazers","contributors_url":"https://api.github.com/repos/cycloidio/approval-resource/contributors","subscribers_url":"https://api.github.com/repos/cycloidio/approval-resource/subscribers","subscription_url":"https://api.github.com/repos/cycloidio/approval-resource/subscription","commits_url":"https://api.github.com/repos/cycloidio/approval-resource/commits{/sha}","git_commits_url":"https://api.github.com/repos/cycloidio/approval-resource/git/commits{/sha}","comments_url":"https://api.github.com/repos/cycloidio/approval-resource/comments{/number}","issue_comment_url":"https://api.github.com/repos/cycloidio/approval-resource/issues/comments{/number}","contents_url":"https://api.github.com/repos/cycloidio/approval-resource/contents/{+path}","compare_url":"https://api.github.com/repos/cycloidio/approval-resource/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cycloidio/approval-resource/merges","archive_url":"https://api.github.com/repos/cycloidio/approval-resource/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cycloidio/approval-resource/downloads","issues_url":"https://api.github.com/repos/cycloidio/approval-resource/issues{/number}","pulls_url":"https://api.github.com/repos/cycloidio/approval-resource/pulls{/number}","milestones_url":"https://api.github.com/repos/cycloidio/approval-resource/milestones{/number}","notifications_url":"https://api.github.com/repos/cycloidio/approval-resource/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cycloidio/approval-resource/labels{/name}","releases_url":"https://api.github.com/repos/cycloidio/approval-resource/releases{/id}","deployments_url":"https://api.github.com/repos/cycloidio/approval-resource/deployments","created_at":"2016-11-23T08:38:56Z","updated_at":"2016-11-30T15:15:01Z","pushed_at":"2016-11-30T15:23:56Z","git_url":"git://github.com/cycloidio/approval-resource.git","ssh_url":"git@github.com:cycloidio/approval-resource.git","clone_url":"https://github.com/cycloidio/approval-resource.git","svn_url":"https://github.com/cycloidio/approval-resource","homepage":null,"size":18,"stargazers_count":1,"watchers_count":1,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cycloidio/approval-resource/pulls/1"},"html":{"href":"https://github.com/cycloidio/approval-resource/pull/1"},"issue":{"href":"https://api.github.com/repos/cycloidio/approval-resource/issues/1"},"comments":{"href":"https://api.github.com/repos/cycloidio/approval-resource/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/cycloidio/approval-resource/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/cycloidio/approval-resource/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cycloidio/approval-resource/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/cycloidio/approval-resource/statuses/13d53e09dcf9a845e5ba0075f198a90fea236eec"}}}}
{"actor":{"display_login":"Seraf"}}