<commit_before># encoding: utf-8
require 'json'
require 'octokit'
require 'open-uri'
require 'yaml'

buildpacks_ci_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require "#{buildpacks_ci_dir}/lib/slack-client"
require "#{buildpacks_ci_dir}/lib/tracker-client"
require "#{buildpacks_ci_dir}/lib/buildpack-dependency"

class NewReleasesDetector
  attr_reader :new_releases_dir
  attr_reader :dependency_tags

  def initialize(new_releases_dir)
    @new_releases_dir = new_releases_dir
    @dependency_tags = generate_dependency_tags(new_releases_dir)
  end

  def post_to_slack
    slack_client = SlackClient.new(
      ENV['SLACK_WEBHOOK'],
      ENV['SLACK_CHANNEL'],
      'dependency-notifier'
    )
    dependency_tags.each do |dependency, versions|
      versions.each do |version|
        new_dependency_version_output = "There is a new update to the *#{dependency}* dependency: version *#{version}*\n"
        slack_client.post_to_slack new_dependency_version_output
      end
    end
  end

  def post_to_tracker
    tracker_client = TrackerClient.new(
      ENV['TRACKER_API_TOKEN'],
      ENV['TRACKER_PROJECT_ID'],
      ENV['TRACKER_REQUESTER_ID'].to_i
    )
    dependency_tags.each do |dependency, versions|
      tracker_story_title = "Build and/or Include new releases: #{dependency} #{versions.join(', ')}"
      tracker_story_description = "We have #{versions.count} new releases for **#{dependency}**:\n**version #{versions.join(', ')}**\n See the google doc https://sites.google.com/a/pivotal.io/cloudfoundry-buildpacks/check-lists/adding-a-new-dependency-release-to-a-buildpack for info on building a new release binary and adding it to the buildpack manifest file."
      tracker_story_tasks = BuildpackDependency.for(dependency).map do |buildpack|
        "Update #{dependency} in #{buildpack}-buildpack"
      end

      tracker_client.post_to_tracker(tracker_story_title, tracker_story_description, tracker_story_tasks, 1)
    end
  end

  private

  def configure_octokit
    Octokit.auto_paginate = true
    Octokit.configure do |c|
      c.login    = ENV.fetch('GITHUB_USERNAME')
      c.password = ENV.fetch('<PASSWORD>')
    end
  end

  def generate_dependency_tags(new_releases_dir)
    configure_octokit
    dependency_tags = {}

    tags.each do |current_dependency, get_tags|
      current_tags = get_tags.call

      filename = "#{new_releases_dir}/#{current_dependency}.yaml"
      previous_tags = if File.exist?(filename)
                        YAML.load_file(filename)
                      else
                        []
                      end

      diff_tags = current_tags - previous_tags

      if diff_tags.any?
        dependency_tags[current_dependency] = diff_tags
        File.write(filename, current_tags.to_yaml)
      else
        warn "There are no new updates to the *#{current_dependency}* dependency"
      end
    end
    dependency_tags
  end

  def tags
    @get_tags_functions = {
      bundler:   -> { Octokit.tags('bundler/bundler').map(&:name).grep(/^v/) },
      composer:  -> { Octokit.tags('composer/composer').map(&:name) },
      go:        -> { Octokit.tags('golang/go').map(&:name).grep(/^go/) },
      godep:     -> { Octokit.tags('tools/godep').map(&:name).grep(/^v/) },
      httpd:     -> { Octokit.tags('apache/httpd').map(&:name).grep(/^2\./) },
      jruby:     -> { Octokit.tags('jruby/jruby').map(&:name).grep(/^(1|9)\./) },
      maven:     -> { Octokit.tags('apache/maven').map(&:name).grep(/^maven/) },
      nginx:     -> { Octokit.tags('nginx/nginx').map(&:name).grep(/^release/) },
      nodejs:    -> { Octokit.tags('nodejs/node').map(&:name).grep(/^v/) },
      openjdk:   -> { YAML.load(open('https://download.run.pivotal.io/openjdk/trusty/x86_64/index.yml').read).keys },
      php:       -> { Octokit.tags('php/php-src').map(&:name).grep(/^php/) },
      python:    -> { JSON.parse(open('https://hg.python.org/cpython/json-tags').read)['tags'].map { |t| t['tag'] } },
      ruby:      -> { Octokit.tags('ruby/ruby').map(&:name).grep(/^v/) }
    }
  end<commit_msg>Start tracking new glide releases<commit_after># encoding: utf-8
require 'json'
require 'octokit'
require 'open-uri'
require 'yaml'

buildpacks_ci_dir = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require "#{buildpacks_ci_dir}/lib/slack-client"
require "#{buildpacks_ci_dir}/lib/tracker-client"
require "#{buildpacks_ci_dir}/lib/buildpack-dependency"

class NewReleasesDetector
  attr_reader :new_releases_dir
  attr_reader :dependency_tags

  def initialize(new_releases_dir)
    @new_releases_dir = new_releases_dir
    @dependency_tags = generate_dependency_tags(new_releases_dir)
  end

  def post_to_slack
    slack_client = SlackClient.new(
      ENV['SLACK_WEBHOOK'],
      ENV['SLACK_CHANNEL'],
      'dependency-notifier'
    )
    dependency_tags.each do |dependency, versions|
      versions.each do |version|
        new_dependency_version_output = "There is a new update to the *#{dependency}* dependency: version *#{version}*\n"
        slack_client.post_to_slack new_dependency_version_output
      end
    end
  end

  def post_to_tracker
    tracker_client = TrackerClient.new(
      ENV['TRACKER_API_TOKEN'],
      ENV['TRACKER_PROJECT_ID'],
      ENV['TRACKER_REQUESTER_ID'].to_i
    )
    dependency_tags.each do |dependency, versions|
      tracker_story_title = "Build and/or Include new releases: #{dependency} #{versions.join(', ')}"
      tracker_story_description = "We have #{versions.count} new releases for **#{dependency}**:\n**version #{versions.join(', ')}**\n See the google doc https://sites.google.com/a/pivotal.io/cloudfoundry-buildpacks/check-lists/adding-a-new-dependency-release-to-a-buildpack for info on building a new release binary and adding it to the buildpack manifest file."
      tracker_story_tasks = BuildpackDependency.for(dependency).map do |buildpack|
        "Update #{dependency} in #{buildpack}-buildpack"
      end

      tracker_client.post_to_tracker(tracker_story_title, tracker_story_description, tracker_story_tasks, 1)
    end
  end

  private

  def configure_octokit
    Octokit.auto_paginate = true
    Octokit.configure do |c|
      c.login    = ENV.fetch('GITHUB_USERNAME')
      c.password = <PASSWORD>.fetch('GITHUB_PASSWORD')
    end
  end

  def generate_dependency_tags(new_releases_dir)
    configure_octokit
    dependency_tags = {}

    tags.each do |current_dependency, get_tags|
      current_tags = get_tags.call

      filename = "#{new_releases_dir}/#{current_dependency}.yaml"
      previous_tags = if File.exist?(filename)
                        YAML.load_file(filename)
                      else
                        []
                      end

      diff_tags = current_tags - previous_tags

      if diff_tags.any?
        dependency_tags[current_dependency] = diff_tags
        File.write(filename, current_tags.to_yaml)
      else
        warn "There are no new updates to the *#{current_dependency}* dependency"
      end
    end
    dependency_tags
  end

  def tags
    @get_tags_functions = {
      bundler:   -> { Octokit.tags('bundler/bundler').map(&:name).grep(/^v/) },
      composer:  -> { Octokit.tags('composer/composer').map(&:name) },
      glide:     -> { Octokit.tags('Masterminds/glide').map(&:name) },
      go:        -> { Octokit.tags('golang/go').map(&:name).grep(/^go/) },
      godep:     -> { Octokit.tags('tools/godep').map(&:name).grep(/^v/) },
      httpd:     -> { Octokit.tags('apache/httpd').map(&:name).grep(/^2\./) },
      jruby:     -> { Octokit.tags('jruby/jruby').map(&:name).grep(/^(1|9)\./) },
      maven:     -> { Octokit.tags('apache/maven').map(&:name).grep(/^maven/) },
      nginx:     -> { Octokit.tags('nginx/nginx').map(&:name).grep(/^release/) },
      nodejs:    -> { Octokit.tags('nodejs/node').map(&:name).grep(/^v/) },
      openjdk:   -> { YAML.load(open('https://download.run.pivotal.io/openjdk/trusty/x86_64/index.yml').read).keys },
      php:       -> { Octokit.tags('php/php-src').map(&:name).grep(/^php/) },
      python:    -> { JSON.parse(open('https://hg.python.org/cpython/json-tags').read)['tags'].map { |t| t['tag'] } },
      ruby:      -> { Octokit.tags('ruby/ruby').map(&:name).grep(/^v/) }
    }
  end
{'fraction_non_alphanumeric': 0.14940021810250817, 'fraction_numerical': 0.0021810250817884407, 'mean_word_length': 3.139418254764293, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, 'https://': 6, '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': '942032', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2760, 'n_words': 437}