codecalender /test_scripts/bitbucket.rb

Language Ruby Lines 77
MD5 Hash d945b5769fb330b73ea95502914435c9 Estimated Cost $1,334 (why?)
Repository https://bitbucket.org/seanhealy/codecalender View Raw File View Project SPDX
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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)
Back to Top