Exemplo n.º 1
0
def handler(event: dict, context: dict) -> None:
    """
        Entrypoint for Lambda function that syncs the mapping of Github handles
        to Asana user ids with the dynamodb table USERS_TABLE. This happens
        through an Asana project with custom fields - one task per user with
        custom fields defined in SgtmUser (GITHUB_HANDLE_CUSTOM_FIELD_NAME,
        USER_ID_CUSTOM_FIELD_NAME)

        `event` and `context` are passed into the Lambda function, but we don't
        really care what they are for this function, and they are ignored
    """
    logger.info("Starting sync from Asana project to Dynamodb {} table".format(
        USERS_TABLE))

    users_in_dynamodb = set([
        SgtmUser.from_dynamodb_item(item)
        for item in dynamodb_client.get_all_user_items()
    ])
    logger.info("Found {} users in dynamodb".format(len(users_in_dynamodb)))

    asana_user_tasks_from_asana_project = asana_client.find_all_tasks_for_project(
        ASANA_USERS_PROJECT_ID, opt_fields=["custom_fields"])
    users_in_asana = [
        u for u in [
            SgtmUser.from_custom_fields_list(task["custom_fields"])
            for task in asana_user_tasks_from_asana_project
        ] if u is not None
    ]
    logger.info("Found {} users in Asana".format(len(users_in_asana)))

    users_to_add = [
        user for user in users_in_asana if user not in users_in_dynamodb
    ]
    logger.info("{} users to add to DynamoDb".format(len(users_to_add)))

    # Batch write the users
    if len(users_to_add) > 0:
        dynamodb_client.bulk_insert_github_handle_to_asana_user_id_mapping([
            (u.github_handle, u.domain_user_id) for u in users_to_add
        ])
        logger.info("Done writing user mappings to DynamoDb")
Exemplo n.º 2
0
 def test_from_custom_fields_list__missing_custom_fields_returns_None(self):
     custom_fields = [
         {
             "name": "some-unknown_custom-field",
             "type": "text",
             "text_value": "foo",
         },
         {
             "name": SgtmUser.USER_ID_CUSTOM_FIELD_NAME,
             "type": "number",
             "number_value": 123,
         },
     ]
     user = SgtmUser.from_custom_fields_list(custom_fields)
     self.assertEqual(user, None)
Exemplo n.º 3
0
 def test_from_custom_fields_list__empty_value_returns_None(self):
     custom_fields = [
         {
             "name": SgtmUser.GITHUB_HANDLE_CUSTOM_FIELD_NAME,
             "type": "text",
             "text_value": "",  # Empty string
         },
         {
             "name": SgtmUser.USER_ID_CUSTOM_FIELD_NAME,
             "type": "number",
             "number_value": 123,
         },
     ]
     user = SgtmUser.from_custom_fields_list(custom_fields)
     self.assertEqual(user, None)
Exemplo n.º 4
0
 def test_from_custom_fields_list__None_custom_field_value_returns_None(
         self):
     custom_fields = [
         {
             "name": SgtmUser.GITHUB_HANDLE_CUSTOM_FIELD_NAME,
             "type": "text",
             "text_value": "elainebenes",
         },
         {
             "name": SgtmUser.USER_ID_CUSTOM_FIELD_NAME,
             "type": "number",
             "number_value": None,
         },
     ]
     user = SgtmUser.from_custom_fields_list(custom_fields)
     self.assertEqual(user, None)
Exemplo n.º 5
0
 def test_from_custom_fields_list__creates_user(self):
     custom_fields = [
         {
             "name": SgtmUser.GITHUB_HANDLE_CUSTOM_FIELD_NAME,
             "type": "text",
             "text_value": "elainebenes",
         },
         {
             "name": SgtmUser.USER_ID_CUSTOM_FIELD_NAME,
             "type": "number",
             "number_value": 123,
         },
     ]
     user = SgtmUser.from_custom_fields_list(custom_fields)
     self.assertEqual(user.github_handle, "elainebenes")
     self.assertEqual(user.domain_user_id, "123")
Exemplo n.º 6
0
 def test_equality(self):
     user1 = SgtmUser("Foo", "123")
     user2 = SgtmUser("fOO", "123")
     self.assertEqual(user1, user2)
Exemplo n.º 7
0
 def test_constructor_lower_cases_github_handle(self):
     user = SgtmUser("JerrySeinfeld", "123")
     self.assertEqual(user.github_handle, "jerryseinfeld")
Exemplo n.º 8
0
 def test_constructor_handles_None(self):
     user = SgtmUser(None, None)
     self.assertEqual(user.github_handle, None)
     self.assertEqual(user.domain_user_id, None)
Exemplo n.º 9
0
 def test_constructor_removes_whitespace_from_github_handle(self):
     user = SgtmUser(" JerrySeinfeld\n ", "123")
     self.assertEqual(user.github_handle, "jerryseinfeld")