def lambda_handler()

in src/trigger-changed-files/trigger-changed-files.py [0:0]


def lambda_handler(event, context):
    # Initialize needed variables
    file_extension_allowed = [".txt"]
    file_names_allowed = ["requirements"]
    commit_hash = event['Records'][0]['codecommit']['references'][0]['commit']

    repo_name = event['Records'][0]['eventSourceARN'].split(':')[-1]
    branch_name = os.path.basename(
        str(event['Records'][0]['codecommit']['references'][0]['ref']))

    # Get commit ID for fetching the commit log
    if (commit_hash == None) or (commit_hash == '0000000000000000000000000000000000000000'):
        commit_hash = get_last_commit_id(repo_name, branch_name)

    last_commit = get_last_commit_log(repo_name, commit_hash)

    previous_commit_id = None
    if len(last_commit['parents']) > 0:
        previous_commit_id = last_commit['parents'][0]

    differences = get_file_differences(repo_name, commit_hash, previous_commit_id)
    for diff in differences:
        root, extension = os.path.splitext(str(diff['afterBlob']['path']))
        file_name = os.path.basename(str(diff['afterBlob']['path']))
        if ((extension in file_extension_allowed) or (file_name in file_names_allowed)):
            # Extract the actual changes
            after_blob = diff['afterBlob']['blobId']
            after_changes = set(codecommit.get_blob(repositoryName=repo_name, blobId=after_blob)['content'].decode().split())

            before_blob = diff['beforeBlob']['blobId']
            before_changes = set(codecommit.get_blob(repositoryName=repo_name, blobId=before_blob)['content'].decode().split())

            added_modified_packages = list(after_changes - before_changes)

    return {
        'changed_packages': added_modified_packages,
        'commit_id': commit_hash
        }