<commit_before>class PublishToDiscourse

  def initialize
    @client = DiscourseApi::Client.new(Settings.ui.community_url)
    @client.api_key = ENV["DISCOURSE_API"]
    @client.api_username = ENV["DISCOURSE_USER"]
  end

  def publish(title, raw, category, topic_id = nil)
    unless topic_id.nil?
      topic = @client.topic(topic_id)
      if topic.nil?
        topic_id = nil
      else
        update(title, raw, category, topic_id)
      end
    end

    topic_id = create(title, raw, category) if topic_id.nil?
    topic_id
  end

  def create(title, raw, category)
    @client.create_topic(
      category: category,
      title: title,
      raw: raw
    )["topic_id"]
  end

  def update(title, raw, category, topic_id)
    topic = @client.topic(topic_id)
    return if topic.nil?

    post = topic["post_stream"]["posts"][0]
    return if post["username"] != @client.api_username

    begin
      @client.edit_post(post["id"], raw)
    rescue DiscourseApi::UnauthenticatedError
      recover(topic_id)
      @client.edit_post(post["id"], raw)
    end

    @client.rename_topic(topic_id, title)

    category = @client.category(sanitize_slug(category))
    unless category.nil?
      @client.recategorize_topic(topic_id, category["id"])
    end<commit_msg>PublishToDiscourse properly check if object not found<commit_after>class PublishToDiscourse

  def initialize
    @client = DiscourseApi::Client.new(Settings.ui.community_url)
    @client.api_key = ENV["DISCOURSE_API"]
    @client.api_username = ENV["DISCOURSE_USER"]
  end

  def publish(title, raw, category, topic_id = nil)
    unless topic_id.nil?
      topic = @client.topic(topic_id)
      if topic.has_key?("errors")
        topic_id = nil
      else
        update(title, raw, category, topic_id)
      end
    end

    topic_id = create(title, raw, category) if topic_id.nil?
    topic_id
  end

  def create(title, raw, category)
    @client.create_topic(
      category: category,
      title: title,
      raw: raw
    )["topic_id"]
  end

  def update(title, raw, category, topic_id)
    topic = @client.topic(topic_id)
    return if topic.has_key?("errors")

    post = topic["post_stream"]["posts"][0]
    return if post["username"] != @client.api_username

    begin
      @client.edit_post(post["id"], raw)
    rescue DiscourseApi::UnauthenticatedError
      recover(topic_id)
      @client.edit_post(post["id"], raw)
    end

    @client.rename_topic(topic_id, title)

    category = @client.category(sanitize_slug(category))
    unless category.has_key?("errors")
      @client.recategorize_topic(topic_id, category["id"])
    end
{'fraction_non_alphanumeric': 0.11697247706422019, 'fraction_numerical': 0.0007645259938837921, 'mean_word_length': 3.297208538587849, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2238253', 'n_tokens_mistral': 874, 'n_tokens_neox': 869, 'n_words': 179}