Esempio n. 1
0
def update_lang_version(branch_name, lang_version):
    dist_repo = github.get_repo(constants.BALLERINA_ORG_NAME + '/ballerina-distribution', ref=branch_name)
    properties_content = dist_repo.get_contents(constants.GRADLE_PROPERTIES_FILE)
    properties_content = properties_content.decoded_content.decode(constants.ENCODING)

    synced = False
    updated_properties_file = ''
    module = {'name': 'ballerina-distribution', 'auto_merge': True}
    for line in properties_content.splitlines():
        if line.startswith('ballerinaLangVersion'):
            if line.split('=')[1] == lang_version:
                print("[Info] lang version is already synced in " + branch_name + " branch")
                Synced = True
                break
            else:
                updated_properties_file += 'ballerinaLangVersion' + '=' + lang_version + '\n'
        else:
            updated_properties_file += line + '\n'

    if not synced:
        committed = commit_file(dist_repo, constants.GRADLE_PROPERTIES_FILE, updated_properties_file,
                   branch_name, COMMIT_MESSAGE_PREFIX + lang_version)

        print("[Info] Update lang version in " + branch_name + " branch")
        if committed:
            pr = create_pull_request(dist_repo, branch_name)
            utils.approve_pr(module, 'TRUE', pr.number)
            ref = dist_repo.get_git_ref('heads/' + branch_name + "_temp")
            pending = True
                wait_cycles = 0
                while pending:
                    if wait_cycles < MAX_WAIT_CYCLES:
                        time.sleep(SLEEP_INTERVAL)
                        pending, passing, failing_pr_checks = check_pending_pr_checks(dist_repo, pr)
                        if not pending:
                            if len(failing_pr_checks) > 0:
                                passing = all(check.startswith('codecov') for check in failing_pr_checks)
                            if passing:
                                if(pr.mergeable_state != 'dirty'):
                                    try:
                                        pr.merge()
                                        ref.delete()
                                        log_message = "[Info] Automated lang version update PR merged. PR: " + pr.html_url
                                        print(log_message)
                                    except Exception as e:
                                        print("[Error] Error occurred while merging version update PR " , e)
                                else:
                                    notify_chat.send_message("[Info] Automated ballerina-distribution version update PR is unmerged due to conflicts." + "\n" +\
                                            "Please visit <" + pr.html_url + "|the build page> for more information")
                            else:
                                notify_chat.send_message("[Info] Automated ballerina-distribution version update PR has failed checks." + "\n" +\
                                 "Please visit <" + pr.html_url + "|the build page> for more information")
                                pr.edit(state = 'closed')
                                ref.delete()
                        else:
                            wait_cycles += 1
                    else:
                        notify_chat.send_message("[Info] Automated ballerina-distribution version update PR is unmerged due to pr checks timeout." + "\n" +\
                         "Please visit <" + pr.html_url + "|the build page> for more information")
                        break
def create_pull_request(idx: int, repo):
    module = current_level_modules[idx]
    pulls = repo.get_pulls(state='open')
    pr_exists = False
    created_pr = ''

    sha_of_lang = lang_version.split('-')[-1]

    for pull in pulls:
        if pull.head.ref == constants.DEPENDENCY_UPDATE_BRANCH:
            pr_exists = True
            created_pr = pull
            pull.edit(title=pull.title.rsplit('-', 1)[0] + '-' + sha_of_lang +
                      ')',
                      body=pull.body.rsplit('-', 1)[0] + '-' + sha_of_lang +
                      '` and relevant extensions.')
            print("[Info] Automated version bump PR found for module '" +
                  module['name'] + "'. PR: " + pull.html_url)
            break

    if not pr_exists:
        try:
            pull_request_title = PULL_REQUEST_TITLE
            if (auto_merge_pull_requests.lower()
                    == 'true') & module['auto_merge']:
                pull_request_title = AUTO_MERGE_PULL_REQUEST_TITLE
            pull_request_title = pull_request_title + lang_version + ')'

            created_pr = repo.create_pull(
                title=pull_request_title,
                body=PULL_REQUEST_BODY_PREFIX + lang_version +
                '` and relevant extensions.',
                head=constants.DEPENDENCY_UPDATE_BRANCH,
                base=repo.default_branch)
            log_message = "[Info] Automated version bump PR created for module '" + module['name'] \
                          + "'. PR: " + created_pr.html_url
            print(log_message)
        except Exception as e:
            print(
                "[Error] Error occurred while creating pull request for module '"
                + module['name'] + "'.", e)
            sys.exit(1)

        try:
            utils.approve_pr(module, auto_merge_pull_requests,
                             created_pr.number)
        except Exception as e:
            print(
                "[Error] Error occurred while approving dependency PR for module '"
                + module['name'] + "'", e)

    current_level_modules[idx][MODULE_CREATED_PR] = created_pr
    current_level_modules[idx][MODULE_STATUS] = MODULE_STATUS_IN_PROGRESS
    current_level_modules[idx][
        MODULE_CONCLUSION] = MODULE_CONCLUSION_PR_PENDING
def create_pull_request(idx: int, repo):
    global connectors

    connector = connectors[idx]
    pulls = repo.get_pulls(state='open')
    pr_exists = False
    created_pr = ''

    sha_of_lang = ballerina_version.split('-')[-1]

    for pull in pulls:
        if pull.head.ref == constants.DEPENDENCY_UPDATE_BRANCH:
            pr_exists = True
            created_pr = pull
            pull.edit(
                title=pull.title.rsplit('-', 1)[0] + '-' + sha_of_lang + ')',
                body=pull.body.rsplit('-', 1)[0] + '-' + sha_of_lang + '`.')
            print("[Info] Automated version bump PR found for connector '" +
                  connector['name'] + "'. PR: " + pull.html_url)
            break

    if not pr_exists:
        try:
            pull_request_title = PULL_REQUEST_TITLE
            if (auto_merge_pull_requests.lower()
                    == 'true') & connector['auto_merge']:
                pull_request_title = AUTO_MERGE_PULL_REQUEST_TITLE
            pull_request_title = pull_request_title + ballerina_version + ')'

            created_pr = repo.create_pull(
                title=pull_request_title,
                body=PULL_REQUEST_BODY_PREFIX + ballerina_version + '`.',
                head=constants.DEPENDENCY_UPDATE_BRANCH,
                base=repo.default_branch)
            log_message = "[Info] Automated version bump PR created for connector '" + connector['name'] \
                          + "'. PR: " + created_pr.html_url
            print(log_message)
        except Exception as e:
            print(
                "[Error] Error occurred while creating pull request for connector '"
                + connector['name'] + "'.", e)
            sys.exit(1)

        try:
            utils.approve_pr(connector, auto_merge_pull_requests,
                             created_pr.number)
        except Exception as e:
            print(
                "[Error] Error occurred while approving dependency PR for connector '"
                + connector['name'] + "'", e)
    return created_pr