#!/usr/bin/env python3.10

import jenkins, os 

def modify_jenkins_job(server_url = 'https://man-dan-03:9443', jenkins_username = 'jonnybravo', jenkins_user_token = '113c3449636622439627ab1d51454fc07c', jobs = list, old_config_part = str, new_config_part = str ):
        server = jenkins.Jenkins(server_url, username=jenkins_username, password=jenkins_user_token)
        for job in jobs:
          try:
            job_config = server.get_job_config(job)
            new = job_config.replace(old_config_part, new_config_part)
            server.reconfig_job(job, new)
          except:
               error_output = "Job " + job + " existiet nicht"
               raise Exception(error_output)

if __name__ == '__main__':
        #os.putenv("PYTHONHTTPSVERIFY", 0)
        os.environ["PYTHONHTTPSVERIFY"] = "0"

        old_config = """       <strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
          <properties class="empty-list"/>
        </strategy>"""
        new_config = """        <strategy class="jenkins.branch.DefaultBranchPropertyStrategy">
          <properties class="java.util.Arrays$ArrayList">
            <a class="jenkins.branch.BranchProperty-array">
              <jenkins.branch.NoTriggerBranchProperty>
                <triggeredBranchesRegex>(?!support*).*</triggeredBranchesRegex>
                <strategy>NONE</strategy>
              </jenkins.branch.NoTriggerBranchProperty>
            </a>
          </properties>
        </strategy>"""
        modify_jenkins_job(old_config_part=old_config, new_config_part=new_config, jobs=['multi_branch_test', 'multi_branch_test_2'])