<commit_before># frozen_string_literal: true

require_relative 'config_loader'

require 'cache_method'
require 'asana'

# Pull tasks from asana.com
#
# Convention:
#  _raw: Returns tasks objects, including section-tasks
#  _tasks: Returns task objects
#  _task_names: Returns an array of strings, no sections included
#  _by_section: Returns a hash from section name to array
module Checkoff
  # Work with projects in Asana
  class Projects
    MINUTE = 60
    HOUR = MINUTE * 60
    DAY = 24 * HOUR
    REALLY_LONG_CACHE_TIME = HOUR * 1
    LONG_CACHE_TIME = MINUTE * 15
    SHORT_CACHE_TIME = MINUTE * 5

    # XXX: Move low-level functions private

    def initialize(config: Checkoff::ConfigLoader.load(:asana),
                   asana_client: Asana::Client,
                   workspaces: Checkoff::Workspaces.new(config: config))
      @config = config
      @asana_client = asana_client
      @workspaces = workspaces
    end

    def client
      @workspaces.client
    end

    def projects
      client.projects
    end
    cache_method :projects, LONG_CACHE_TIME

    def projects_by_workspace_name(workspace_name)
      workspace = @workspaces.workspace_by_name(workspace_name)
      raise "Could not find workspace named #{workspace_name}" unless workspace

      projects.find_by_workspace(workspace: workspace.gid)
    end

    def my_tasks(workspace_name)
      my_tasks = @config.fetch(:my_tasks)
      gid = my_tasks[workspace_name] unless my_tasks.nil?
      raise "Please define [:my_tasks][#{workspace_name}] in config file" if my_tasks.nil? || gid.nil?

      projects.find_by_id(gid)
    end

    def project(workspace_name, project_name)
      if project_name.is_a?(Symbol) && project_name.to_s.start_with?('my_tasks')
        my_tasks(workspace_name)
      else
        projects = projects_by_workspace_name(workspace_name)
        projects.find do |project|
          project.name == project_name
        end
      end
    end
    cache_method :project, LONG_CACHE_TIME

    def active_tasks(tasks)
      tasks.select { |task| task.completed_at.nil? }
    end

    def task_options
      {
        per_page: 100,
        options: {
          fields: %w[name completed_at due_at due_on assignee_status tags
                     memberships.project.gid memberships.section.name dependencies],
        },
      }
    end
<commit_msg>Use new user task list API to drop some required config<commit_after># frozen_string_literal: true

require_relative 'config_loader'

require 'cache_method'
require 'asana'

# Pull tasks from asana.com
#
# Convention:
#  _raw: Returns tasks objects, including section-tasks
#  _tasks: Returns task objects
#  _task_names: Returns an array of strings, no sections included
#  _by_section: Returns a hash from section name to array
module Checkoff
  # Work with projects in Asana
  class Projects
    MINUTE = 60
    HOUR = MINUTE * 60
    DAY = 24 * HOUR
    REALLY_LONG_CACHE_TIME = HOUR * 1
    LONG_CACHE_TIME = MINUTE * 15
    SHORT_CACHE_TIME = MINUTE * 5

    # XXX: Move low-level functions private

    def initialize(config: Checkoff::ConfigLoader.load(:asana),
                   asana_client: Asana::Client,
                   workspaces: Checkoff::Workspaces.new(config: config))
      @config = config
      @asana_client = asana_client
      @workspaces = workspaces
    end

    def client
      @workspaces.client
    end

    def projects
      client.projects
    end
    cache_method :projects, LONG_CACHE_TIME

    def projects_by_workspace_name(workspace_name)
      workspace = @workspaces.workspace_by_name(workspace_name)
      raise "Could not find workspace named #{workspace_name}" unless workspace

      projects.find_by_workspace(workspace: workspace.gid)
    end

    def my_tasks(workspace_name)
      workspace = @workspaces.workspace_by_name(workspace_name)
      result = client.user_task_lists.get_user_task_list_for_user(user_gid: 'me',
                                                                  workspace: workspace.gid)
      gid = result.gid
      projects.find_by_id(gid)
    end

    def project(workspace_name, project_name)
      if project_name.is_a?(Symbol) && project_name.to_s.start_with?('my_tasks')
        my_tasks(workspace_name)
      else
        projects = projects_by_workspace_name(workspace_name)
        projects.find do |project|
          project.name == project_name
        end
      end
    end
    cache_method :project, LONG_CACHE_TIME

    def active_tasks(tasks)
      tasks.select { |task| task.completed_at.nil? }
    end

    def task_options
      {
        per_page: 100,
        options: {
          fields: %w[name completed_at due_at due_on assignee_status tags
                     memberships.project.gid memberships.section.name dependencies],
        },
      }
    end

{'fraction_non_alphanumeric': 0.07251194681072097, 'fraction_numerical': 0.0054020361520880945, 'mean_word_length': 2.7550702028081124, 'pattern_counts': {'":': 0, '<': 3, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1171619', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1423, 'n_words': 359}