<commit_before>#!/usr/bin/env bash
#
# Move all repositories from one organization to another and replicate
# the teams.
#
# Usage:
#   org-transfer-all-repos.sh [OPTIONS] <source org> <target org>
#
# Options:
#   --i-know-what-im-doing Suppress any warning (useful for scripting!)
#   -n                     Dry-run; only show what would be done.
#   -h, --help             Display this message.
#
# Example:
#   org-transfer-all-repos.sh org1 org2
#

BASE_DIR=$(cd "${0%/*}/.." && pwd)
. "$BASE_DIR/lib-ghe.sh"

while [ $# -gt 0 ]; do
    case $1 in
        (--i-know-what-im-doing) NO_WARNING=1; shift;;
        (-n) DRY_RUN=1; shift;;
        (-h|--help) usage 2>&1;;
        (--) shift; break;;
        (-*) usage "$1: unknown option";;
        (*) break;;
    esac
done

SOURCE_ORG=$1
if [ -z "$SOURCE_ORG" ]; then
    usage "Source org is a required parameter!"
fi

TARGET_ORG=$2
if [ -z "$TARGET_ORG" ]; then
    usage "Target org is a required parameter!"
fi

[ -n "$NO_WARNING" ] || warning "This script moves all repos from '$SOURCE_ORG' to '$TARGET_ORG' on $GHE_HOST!"

execute << EOF
    github-env bin/runner -e production "'
        staff_user = User.find_by_login(\"$GHE_USER\");
        target_org = User.find_by_login(\"$TARGET_ORG\");
        source_org = User.find_by_login(\"$SOURCE_ORG\");
        if source_org and target_org and staff_user;
            source_org.repositories.each {|repo|
                repo.teams.each {|source_team|
                    if target_org.teams.find {|t| t.name == source_team.name };
                        raise \"Error: Team #{source_team.name} already exists in #{target_org.login}!\"
                    end;
                }
            }
            GitHub.context.push(actor_id: staff_user.id);
            source_org.repositories.each {|repo|
                teams = repo.teams.map { |t| {
                    \"name\" => t.name,
                    \"description\" => t.description,
                    \"permission\" => t.permission_for(repo),
                    \"privacy\" => t.privacy,
                    \"members\" => t.members.map { |m| m.login }
                } };
                puts \"Transferring repo: #{repo.name}\";
                repo.async_transfer_ownership_to(target_org, actor: staff_user, target_teams: []);
                target_repo = nil;
                loop do
                    target_repo = Repository.find_by_name_with_owner(\"#{target_org.login}/#{repo.name}\");
                    break if target_repo;
                    sleep(1);
                end;
                teams.each {|source_team|
                    target_org.reload();
                    target_team = target_org.teams.find {|t| t.name == source_team[\"name\"] };
                    if !target_team;
                        puts \"Creating team: #{source_team[\"name\"]}\";
                        target_team = target_org.create_team(source_team[\"name\"], creator: staff_user);
                        target_team.description = source_team[\"description\"];
                        target_team.privacy = source_team[\"privacy\"];
                        target_team.save!;
                        source_team[\"members\"].each {|m|
                            puts \"Adding member to team _#{target_team.name}_: #{m}\";
                            target_team.add_member(User.find_by_login(m));
                        };
                    end
                    puts \"Adding repo to team _#{target_team.name}_: #{target_repo.name} (#{source_team[\"permission\"]})\";
                    target_team.add_repository(target_repo, source_team[\"permission\"]);
                }
            }
        else
            puts \"ERROR: Source org, target org, or staff user does not exist.\";
        end;
    '"
EOF<commit_msg>org-transfer: fix `create_team` call for GHE 2.10<commit_after>#!/usr/bin/env bash
#
# Move all repositories from one organization to another and replicate
# the teams.
#
# Usage:
#   org-transfer-all-repos.sh [OPTIONS] <source org> <target org>
#
# Options:
#   --i-know-what-im-doing Suppress any warning (useful for scripting!)
#   -n                     Dry-run; only show what would be done.
#   -h, --help             Display this message.
#
# Example:
#   org-transfer-all-repos.sh org1 org2
#

BASE_DIR=$(cd "${0%/*}/.." && pwd)
. "$BASE_DIR/lib-ghe.sh"

while [ $# -gt 0 ]; do
    case $1 in
        (--i-know-what-im-doing) NO_WARNING=1; shift;;
        (-n) DRY_RUN=1; shift;;
        (-h|--help) usage 2>&1;;
        (--) shift; break;;
        (-*) usage "$1: unknown option";;
        (*) break;;
    esac
done

SOURCE_ORG=$1
if [ -z "$SOURCE_ORG" ]; then
    usage "Source org is a required parameter!"
fi

TARGET_ORG=$2
if [ -z "$TARGET_ORG" ]; then
    usage "Target org is a required parameter!"
fi

[ -n "$NO_WARNING" ] || warning "This script moves all repos from '$SOURCE_ORG' to '$TARGET_ORG' on $GHE_HOST!"

execute << EOF
    github-env bin/runner -e production "'
        staff_user = User.find_by_login(\"$GHE_USER\");
        target_org = User.find_by_login(\"$TARGET_ORG\");
        source_org = User.find_by_login(\"$SOURCE_ORG\");
        if source_org and target_org and staff_user;
            source_org.repositories.each {|repo|
                repo.teams.each {|source_team|
                    if target_org.teams.find {|t| t.name == source_team.name };
                        raise \"Error: Team #{source_team.name} already exists in #{target_org.login}!\"
                    end;
                }
            }
            GitHub.context.push(actor_id: staff_user.id);
            source_org.repositories.each {|repo|
                teams = repo.teams.map { |t| {
                    \"name\" => t.name,
                    \"description\" => t.description,
                    \"permission\" => t.permission_for(repo),
                    \"privacy\" => t.privacy,
                    \"members\" => t.members.map { |m| m.login }
                } };
                puts \"Transferring repo: #{repo.name}\";
                repo.async_transfer_ownership_to(target_org, actor: staff_user, target_teams: []);
                target_repo = nil;
                loop do
                    target_repo = Repository.find_by_name_with_owner(\"#{target_org.login}/#{repo.name}\");
                    break if target_repo;
                    sleep(1);
                end;
                teams.each {|source_team|
                    target_org.reload();
                    target_team = target_org.teams.find {|t| t.name == source_team[\"name\"] };
                    if !target_team;
                        puts \"Creating team: #{source_team[\"name\"]}\";
                        target_team = target_org.create_team(creator: staff_user, attrs: { :name => source_team[\"name\"] });
                        target_team.description = source_team[\"description\"];
                        target_team.privacy = source_team[\"privacy\"];
                        target_team.save!;
                        source_team[\"members\"].each {|m|
                            puts \"Adding member to team _#{target_team.name}_: #{m}\";
                            target_team.add_member(User.find_by_login(m));
                        };
                    end
                    puts \"Adding repo to team _#{target_team.name}_: #{target_repo.name} (#{source_team[\"permission\"]})\";
                    target_team.add_repository(target_repo, source_team[\"permission\"]);
                }
            }
        else
            puts \"ERROR: Source org, target org, or staff user does not exist.\";
        end;
    '"
EOF
{'fraction_non_alphanumeric': 0.1399321857068336, 'fraction_numerical': 0.0037819509650495564, 'mean_word_length': 1.6426602343211578, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6791339', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2261, 'n_words': 503}