Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: obtain projects from github #35

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions db/migrations/20171124104157_add_owner_to_project.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Hanami::Model.migration do
up do
alter_table(:projects) do
add_column :owner, String
end
end

down do
alter_table(:projects) do
drop_column :owner
end
end
end
23 changes: 4 additions & 19 deletions db/seed.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
require_relative '../config/boot'

PROJECTS = %w[
hanami.github.io
hanami
view
utils
validations
model
controller
router
assets
community
helpers
mailer
docs
ecosystem
contributors
cli
]
GITHUB_ORGANIZATION = "hamani".freeze

repo = ProjectRepository.new
PROJECTS.each { |name| repo.create(name: name) }
AllProjects.new(GITHUB_ORGANIZATION).call.each do |project|
repo.create(project)
end
AddNewContributors.new.call
AddNewCommits.new.call
6 changes: 6 additions & 0 deletions lib/contributors/entities/project.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
class Project < Hanami::Entity
attributes do
attribute :id, Types::Int

attribute :name, Types::String
attribute :owner, Types::String
end
end
1 change: 1 addition & 0 deletions lib/contributors/graphql/types/project.rb
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ module Types
name "Project"
field :id, types.ID
field :name, types.String
field :owner, types.String
end
end
end
3 changes: 2 additions & 1 deletion lib/contributors/services/all_commits.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative '../../http_request'

class AllCommits
API_URL = 'https://api.github.com/repos/hanami/%{project}/commits?client_id=%{github_id}&client_secret=%{github_key}&page=%{page}&count=100&author=%{author}'.freeze
API_URL = 'https://api.github.com/repos/%{owner}/%{project}/commits?client_id=%{github_id}&client_secret=%{github_key}&page=%{page}&count=100&author=%{author}'.freeze

COMMITS_COUNT_ON_PAGE = 100

@@ -35,6 +35,7 @@ def commit_information(contributor, project, data)

def get_response(project, contributors, page)
params = {
owner: project.owner,
project: project.name,
author: contributors.github,
page: page,
5 changes: 3 additions & 2 deletions lib/contributors/services/all_contributors.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative '../../http_request'

class AllContributors
API_URL = 'https://api.github.com/repos/hanami/%{project}/commits?client_id=%{github_id}&client_secret=%{github_key}&page=%{page}&count=100'.freeze
API_URL = 'https://api.github.com/repos/%{owner}/%{project}/commits?client_id=%{github_id}&client_secret=%{github_key}&page=%{page}&count=100'.freeze

def call
projects = ProjectRepository.new.all
@@ -24,7 +24,7 @@ def call
private

def contributor_data(data)
{
{
github: data['author']['login'],
avatar_url: data['author']['avatar_url']
}
@@ -33,6 +33,7 @@ def contributor_data(data)
def get_response(project, page)
params = {
page: page,
owner: project.owner,
project: project.name,
github_id: ENV['GITHUB_API_ID'],
github_key: ENV['GITHUB_API_KEY']
47 changes: 47 additions & 0 deletions lib/contributors/services/all_projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_relative '../../http_request'

class AllProjects
API_URL = 'https://api.github.com/orgs/%{owner}/repos?client_id=%{github_id}&client_secret=%{github_key}&page=%{page}&count=100'.freeze

def initialize(owner)
@owner = owner
end

def call
projects = []
page = 1

while (repositories = get_response(page)) && !repositories.empty?
page += 1
repositories.each do |data|
projects.push project_data(data)
end
break
end

projects
end

private

def project_data(data)
{
name: data.fetch('name'),
owner: data.fetch('owner').fetch('login')
}
end

def get_response(page)
params = {
page: page,
owner: @owner,
github_id: ENV['GITHUB_API_ID'],
github_key: ENV['GITHUB_API_KEY']
}

response = HttpRequest.new(API_URL % params).get
return [] unless response.is_a?(Net::HTTPSuccess)

JSON.parse(response.body)
end
end
317 changes: 317 additions & 0 deletions spec/cassettes/repositories.yml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions spec/contributors/services/add_new_commits_spec.rb
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
let(:commit_repo) { CommitRepository.new }
let(:project_repo) { ProjectRepository.new }

let!(:contributor) { contributor_repo.create(github: 'artofhuman') }
let!(:project) { project_repo.create(name: 'hanami') }
let!(:contributor) { contributor_repo.create(github: 'artofhuman', owner: 'hanami') }
let!(:project) { project_repo.create(name: 'hanami', owner: 'hanami') }

after do
commit_repo.clear
2 changes: 1 addition & 1 deletion spec/contributors/services/add_new_contributors_spec.rb
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
let(:project_repo) { ProjectRepository.new}

before do
project_repo.create(name: 'contributors')
project_repo.create(name: 'contributors', owner: 'hanami')
end

after do
2 changes: 1 addition & 1 deletion spec/contributors/services/all_commits_spec.rb
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
let(:repo) { ProjectRepository.new }
let(:contributor) { Contributor.new(id: 1, github: 'artofhuman') }

let!(:project) { repo.create(name: 'hanami') }
let!(:project) { repo.create(name: 'hanami', owner: 'hanami') }

after do
repo.clear
4 changes: 2 additions & 2 deletions spec/contributors/services/all_contributors_spec.rb
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@
let(:repo) { ProjectRepository.new }

before do
repo.create(name: 'contributors')
repo.create(name: 'utils')
repo.create(name: 'contributors', owner: 'hanami')
repo.create(name: 'utils', owner: 'hanami')
end

after do
13 changes: 13 additions & 0 deletions spec/contributors/services/all_projects_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
RSpec.describe AllProjects do
it 'returns array of projects' do
VCR.use_cassette("repositories") do
projects = described_class.new("hanami").call

expect(projects.size).to eq(21)
projects.each do |project_data|
expect(project_data[:name]).to be
expect(project_data[:owner]).to eq "hanami"
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'spec/cassettes'
c.default_cassette_options = {record: :new_episodes}
c.default_cassette_options = {record: :none}
end

# This file was generated by the `rspec --init` command. Conventionally, all