/test_scripts/bitbucket.rb
Ruby | 76 lines | 52 code | 17 blank | 7 comment | 13 complexity | d945b5769fb330b73ea95502914435c9 MD5 | raw file
- #!/usr/bin/ruby
- require 'date'
- require 'uri'
- require 'net/https'
- require 'rubygems'
- require 'json'
- require 'pp'
- HOST = 'https://api.bitbucket.org'
- OTHER_HOST = 'https://www.bitbucket.org'
- USER_PATH = '/1.0/users/'
- USER = 'gamblore'
- $now_is_the_time = Time.now
- def fetch_json_from_string(url_string)
- print "Fetching #{url_string}\n\n"
- url = URI.parse(url_string)
- http_session = Net::HTTP.new(url.host, url.port)
- req = Net::HTTP::Get.new(url.path)
- http_session.use_ssl = true if url.port == 443
- res = http_session.start { |http|
- http.request(req)
- }
- results = JSON.parse( res.body )
- return results
- end
- def repository_active_today(repo)
- results = fetch_json_from_string(HOST + repo["resource_uri"].sub(/\/api/, '') + '/changesets/')
-
- latest = results["changesets"].each { |cs|
- if ( cs["author"] == USER ) then
- =begin
- print "Our users changeset\n==========\n"
- pp cs
- print "==========\n"
- =end
- time_of_changeset = Date.parse(cs["timestamp"])
- if (time_of_changeset.day == $now_is_the_time.day and
- time_of_changeset.month == $now_is_the_time.month and
- time_of_changeset.year == $now_is_the_time.year) then
- return true
- end
- if (time_of_changeset.day < $now_is_the_time.day and
- time_of_changeset.month == $now_is_the_time.month and
- time_of_changeset.year == $now_is_the_time.year) then
- return nil
- end
- end
- }
- return nil
- end
- def pull_user(user)
- results = fetch_json_from_string(HOST + USER_PATH + user)
- #pp results
- results["repositories"].each { |repo|
- print "Checking if #{repo["name"]} is active for user #{USER}\n"
- if (repository_active_today(repo) ) then
- print "-> #{repo["name"]} is active for user #{USER}."
- end
- }
- end
- pull_user(USER)