def main()

in tools/check-tf-plan.py [0:0]


def main(PR):

    TOKEN             = os.getenv('GITHUB_TOKEN')
    GITHUB_WORKSPACE  = os.getenv('GITHUB_WORKSPACE')
    GITHUB_REPOSITORY = os.getenv('GITHUB_REPOSITORY')


    # Get Added / Modified files in PR
    modified_files, modified_files_raw, removed_files = pr_files(GITHUB_REPOSITORY, PR)

    # Get Working directories to run TF Plan on
    working_directories = get_updated_modules(modified_files, removed_files)

    # Loop through all the identified working directories
    # Deleting added/modified & removed files
    try:
        for dir in working_directories:
            print("----------> RUN FOR: " + dir)


            try:
                # IF MODULE EXISTS: Copying main directory in temp folder
                shutil.copytree(GITHUB_WORKSPACE+'/'+dir, os.getcwd()+'/temp/'+dir)

                # Deleting added/modified & removed files
                for mfile in modified_files:
                    if os.path.exists(os.getcwd()+'/temp/'+mfile):
                        print("Deleting file: " + mfile)
                        os.remove(os.getcwd()+'/temp/'+mfile)

                for rfile in removed_files:
                    if os.path.exists(os.getcwd()+'/temp/'+rfile):
                        print("Deleting file: " + rfile)
                        os.remove(os.getcwd()+'/temp/'+rfile)
            except:
                # IF MODULE DONOT EXISTS: Creating temp module folder
                os.makedirs(os.getcwd()+'/temp/'+dir)

    except requests.exceptions.RequestException as e: 
        print('No working directory with TF configs in PR.')
        raise SystemExit(e)

    # Loop through all the identified working directories
    # Download added/modified files
    try:
      
        for dir in working_directories:
            print("Module: " + dir)

            # Download added/modified files
            for file in modified_files:
        
                if dir in file:
                    print("File: " + file)
                    for raw in modified_files_raw:
                        # print("Raw: " + raw)
                        # print("Raw Decoded: " + unquote(raw))
                              
                        if file in unquote(raw):
                            
                            print("Downloading file: " + unquote(raw))
                            downloadprfiles(unquote(raw), file, os.getcwd()+'/temp/'+os.path.dirname(file))
                            break

    except requests.exceptions.RequestException as e: 
        print('No working directory with TF configs in PR.')
        raise SystemExit(e)


    # Loop through all the identified working directories
    # Run Terraform Plan
    try:
        for dir in working_directories:

            # print('****************************')
            # print(glob.glob(os.getcwd() + '/temp/' + dir+'/*'))
            # print('****************************')
            # print(glob.glob(os.getcwd() + '/temp/' + dir+'/*/*'))

            # Running Terraform Init & Terraform Plan
            comment, status = tf(os.getcwd() + '/temp/' + dir)
            comment = comment + ' for: **' + dir + '** !'

            # Commenting on the PR
            commentpr(GITHUB_REPOSITORY, PR, comment, TOKEN)
            if(status == 'fail'):
                sys.exit('Terraform Init or Terraform Plan FAILED for: '+ dir)

    except requests.exceptions.RequestException as e: 
        print('No working directory with TF configs in PR.')
        raise SystemExit(e)