-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathgenerate-project-data.rb
More file actions
143 lines (118 loc) · 3.87 KB
/
Copy pathgenerate-project-data.rb
File metadata and controls
143 lines (118 loc) · 3.87 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Preprocessing script
# Run before `jekyll build` to go through _config.yml and use octokit to fill out metadata
#
# Example:
# - repo: trvrb/coaltrace
# - owner: trvrb
# - title: coaltrace
# - description: Simulating genealogies using charged particles
# - url: /projects/coaltrace/
# - date: 2013-10-19 04:12:17 UTC
# - contributors:
# - login: trvrb
# - avatar: https://2.gravatar.com/avatar/ab7fe2db559c7924316c4391ba00b3f0
# - url: https://github.com/trvrb
# - commits:
# - date: 2013-10-19T04:12:06Z
# - message: Update readme.
# - url: https://github.com/trvrb/coaltrace/commit/ebb95806f989d8fd6ecbf6aa8308647298dd21ad
require 'octokit'
require 'yaml'
module Projects
def self.generate_data(config_file)
project_data = {}
config = YAML.load_file(config_file)
projects_array = config["projects"] + config["readmes"]
readmes_array = config["readmes"]
puts "Generating project data"
# create octokit client
client = Octokit::Client.new(:netrc => true, :access_token => ENV['GITHUB_TOKEN'])
project_data = Array.new
if projects_array.length > 0
projects_array.each do |repo|
puts "\t#{repo}"
# load repo metadata
octokit_repo = client.repository(repo)
project_title = octokit_repo.name
project_owner = octokit_repo.owner.login
project_description = octokit_repo.description
project_url = "/projects/#{project_title}/"
project_date = octokit_repo.updated_at
project_homepage = octokit_repo.homepage
project_homepage = nil if project_homepage.nil? || project_homepage.strip.empty?
# load contributor metadata
octokit_contributors = client.contributors(repo)
project_contributors = Array.new
for i in 0 ... [octokit_contributors.size, 10].min
contributor = octokit_contributors[i]
contributor_login = contributor.login
contributor_avatar = contributor.rels[:avatar].href + "&s=50"
contributor_url = contributor.rels[:html].href
project_contributors = project_contributors.push(
"login" => contributor_login,
"avatar" => contributor_avatar,
"url" => contributor_url
)
end
# load commit metadata
octokit_commits = client.commits(repo)
project_commits = Array.new
counter = 0
for i in 0 ... octokit_commits.size
commit = octokit_commits[i]
commit_date = commit.commit.author.date
commit_message = commit.commit.message
commit_url = commit.rels[:html].href
if commit.author != nil
commit_author_login = commit.author.login
if commit.author.rels[:html] != nil
commit_author_url = commit.author.rels[:html].href
else
commit_author_url = ""
end
else
commit_author_login = ""
commit_author_url = ""
end
project_commits = project_commits.push(
"date" => commit_date,
"message" => commit_message,
"url" => commit_url,
"author_login" => commit_author_login,
"author_url" => commit_author_url
)
counter += 1
if counter == 5
break
end
end
if readmes_array.include? repo then
readme_only = true
else
readme_only = false
end
# assemble metadata
project_data = project_data.push(
"repo" => repo,
"title" => project_title,
"owner" => project_owner,
"description" => project_description,
"url" => project_url,
"homepage" => project_homepage,
"contributors" => project_contributors,
"commits" => project_commits,
"readme_only" => readme_only
)
# sort by date
project_data.sort! { |x, y| y["commits"].first["date"] <=> x["commits"].first["date"] }
end
end
return project_data
end
def self.write_data(project_data, data_file)
puts "Writing project data"
File.write(data_file, project_data.to_yaml)
end
end
project_data = Projects.generate_data("_config.yml")
Projects.write_data(project_data, "_data/projects.yml")