<commit_before>module Github
  class AddCollaboratorToProductRepoWorker < Github::Worker
    def perform(repo_url, github_username)
      return unless org

      repo = Repo::Github.new(repo_url)
      username = github_username

      team = find_or_create_repo_team(repo)
      add_user_to_team(team['id'], username)
      add_team_to_repo(team['id'], repo)
    end

    def find_or_create_repo_team(repo)
      find_repo_team(repo) || create_repo_team(repo)
    end

    def find_repo_team(repo)
      get("/repos/#{repo.full_name}/teams").find{|team| team['name'] == repo.name }
    end

    def create_repo_team(repo)
      post "/orgs/#{org}/teams",
                name: repo.name,
                repo_names: [repo.name],
                permission: 'push'
    end

    def add_user_to_team(team_id, github_username)
      put "/teams/#{team_id}/memberships/#{github_username}"
    end

    def add_team_to_repo(team_id, repo)
      put "/teams/#{team_id}/repos/#{org}/#{repo.name}"
    end

    def org
      ENV['GITHUB_PRODUCTS_ORG']
    end
  end<commit_msg>handle not found team message from github<commit_after>module Github
  class AddCollaboratorToProductRepoWorker < Github::Worker
    def perform(repo_url, github_username)
      return unless org

      repo = Repo::Github.new(repo_url)
      username = github_username

      team = find_or_create_repo_team(repo)
      add_user_to_team(team['id'], username)
      add_team_to_repo(team['id'], repo)
    end

    def find_or_create_repo_team(repo)
      find_repo_team(repo) || create_repo_team(repo)
    end

    def find_repo_team(repo)
      get("/repos/#{repo.full_name}/teams").find{|team| team['name'] == repo.name rescue false }
    end

    def create_repo_team(repo)
      post "/orgs/#{org}/teams",
                name: repo.name,
                repo_names: [repo.name],
                permission: 'push'
    end

    def add_user_to_team(team_id, github_username)
      put "/teams/#{team_id}/memberships/#{github_username}"
    end

    def add_team_to_repo(team_id, repo)
      put "/teams/#{team_id}/repos/#{org}/#{repo.name}"
    end

    def org
      ENV['GITHUB_PRODUCTS_ORG']
    end
  end
{'fraction_non_alphanumeric': 0.11635364177737059, 'fraction_numerical': 0.0, 'mean_word_length': 2.8792184724689167, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5147084', 'n_tokens_mistral': 770, 'n_tokens_neox': 734, 'n_words': 129}