Skip to content

Commit

Permalink
accept pain
Browse files Browse the repository at this point in the history
  • Loading branch information
nayandas69 authored Jan 7, 2025
1 parent 52fdb1b commit 0c706a2
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 198 deletions.
179 changes: 116 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,112 +1,165 @@
# GitHub Repository Insights
# GitHub Repository Insights

GitHub Repository Insights is a Ruby-based CLI tool for tracking and analyzing repositories of any GitHub user. The application fetches repository data using the GitHub API, displays it in a readable table, and provides options for filtering, sorting, and exporting the data.
**GitHub Repository Insights** is your go-to CLI tool for exploring and analyzing GitHub repos like a pro – no browser needed! 💀 It’s sleek, it’s fast, and it’s built with Ruby. Whether you're stalking your favorite dev’s projects or managing your own, this app has got you covered.

---

## **Features**
- Fetches repositories using the GitHub API.
- Displays repository details such as:
- Name, stars, forks, open issues, language, size, and yearly commit count.
- Filter repositories by criteria (stars, forks, issues, archived status).
- Sort repositories by stars, forks, or issues.
- Export repository data to a CSV file.
- Handles paginated API responses.
- Uses advanced commit activity insights.
## **Why You'll Love It 🌟**
- **Fetch Repos Like a Boss**: Get a complete list of public repos from any GitHub user.
- **Deets Galore**: Check out stats like stars, forks, issues, language, size, and commit counts.
- **Filter & Sort FTW**: Flex those sorting skills—stars, forks, issues, you name it.
- **Export Data**: Save repo info to CSV or JSON (because spreadsheets rule).
- **Zero Clutter**: Simple, intuitive CLI vibes.
- **Error-Handled Like a Pro**: No crashing, no burning. 🛸

---

## **Prerequisites**
- Ruby (version >= 2.7 recommended)
- GitHub Personal Access Token (PAT) with `public_repo` access.
## **Before You Start ⚡**

### Prerequisites
1. **Ruby**: Make sure you have Ruby installed (v2.7 or later is 🌈).
- To install Ruby: [Check this out](https://www.ruby-lang.org/en/documentation/installation/)
2. **GitHub PAT (Personal Access Token)**: Create one with `public_repo` access. [Here’s how](https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token).
3. **Required Gems**: Install dependencies by running:
```bash
gem install terminal-table dotenv
```

---

## **Setup Instructions**
1. Clone the repository:
## **Setup 👌**

1. Clone this bad boy:
```bash
git clone https://github.com/NML-LETRAS/github_repository_insights.git
cd github_repository_insights
```
2. Install dependencies:
```bash
bundle install
```
3. Create a `.env` file in the root directory and add your GitHub API key:
2. Create a `.env` file and paste your GitHub PAT:
```env
GITHUB_API_KEY=your_personal_access_token_here
```
4. Run the application:
3. Run the app:
```bash
ruby main.rb
```

---

## **Usage**
1. Enter the GitHub username when prompted.
2. View the fetched repository data in a formatted table.
3. Apply filters and sorting to refine the displayed data.
4. Export the repository data to a CSV file if needed.
## **How It Works 🚀**

---

## **Example**
```bash
Enter GitHub username:
your-user-name
### Main Menu 🔁
When you fire up the app, you’ll see:
```plaintext
--- GitHub Repository Insights ---
1. View Repositories
2. Search Repository by Name
3. View Detailed Repository Information
4. Export Repository Data
5. Help
6. Exit
```
Output:

### Features Explained 🎯

#### **1. View Repositories**
Get all public repos for a GitHub user. See them laid out in a sick table:
```plaintext
+-------------------------+-------+-------+-------------+----------+-----------+---------+
| Name | Stars | Forks | Open Issues | Language | Size (MB) | Commits |
+-------------------------+-------+-------+-------------+----------+-----------+---------+
| repo-1 | 150 | 25 | 3 | Ruby | 0.20 | 500 |
+-------------------------+-------+-------+-------------+----------+-----------+---------+
```
+----------------+-------+-------+-------------+----------+---------+---------+
| Name | Stars | Forks | Open Issues | Language | Size KB | Commits |
+----------------+-------+-------+-------------+----------+---------+---------+
| my-repo | 150 | 25 | 3 | Ruby | 200 | 500 |
| another-repo | 75 | 10 | 1 | Python | 300 | 200 |
+----------------+-------+-------+-------------+----------+---------+---------+

#### **2. Search Repository by Name**
Looking for a specific repo? No problem. Search by keywords and get instant results.

#### **3. View Detailed Repository Info**
Want the nitty-gritty on a repo? Dive deep:
```plaintext
--- Repository Details ---
Name: repo-1
Description: CLI tool.
Stars: 150
Forks: 25
Open Issues: 3
Language: Ruby
Size: 0.20 MB
Default Branch: main
Created At: 2025-01-01
Last Updated: 2025-01-03
```

#### **4. Export Repository Data**
Save your results for later:
- CSV: Perfect for Excel nerds.
- JSON: Because who doesn’t love APIs?

#### **5. Help**
Get a quick rundown of all features. 🌐

#### **6. Exit**
Peace out! 🙋

---

## **Advanced Features**
- Filter repositories by minimum stars, forks, or open issues.
- Sort repositories based on metrics like stars or forks.
- Export the displayed data to `repositories.csv`.
## **Example Usage 🤩**

1. Enter the GitHub username:
```plaintext
Enter GitHub username: octocat
```
2. View repo data in style:
```plaintext
+-----------+-------+-------+-------------+----------+-----------+---------+
| Name | Stars | Forks | Open Issues | Language | Size (MB) | Commits |
+-----------+-------+-------+-------------+----------+-----------+---------+
| repo-1 | 150 | 25 | 3 | Ruby | 0.20 | 500 |
+-----------+-------+-------+-------------+----------+-----------+---------+
```
3. Export the data:
```plaintext
Export data as: 1) CSV 2) JSON: 1
Data exported to repositories.csv
```

---

## **Development**
### **Project Structure**
## **Project Structure 🔢**
```plaintext
github_repository_insights/
├── lib/
│ ├── api_client.rb # Handles API requests and authentication.
│ ├── repository.rb # Defines the Repository model.
│ ├── display.rb # Handles displaying repository data.
│ └── utils.rb # Utility methods for sorting, filtering, and exporting.
├── spec/ # Contains RSpec tests.
├── .env # Stores the GitHub API key.
├── .gitignore # Ignores sensitive files like .env and logs.
├── Gemfile # Lists project dependencies.
├── main.rb # Entry point for the application.
│ ├── api_client.rb # GitHub API requests.
│ ├── repository.rb # Repo model.
│ ├── display.rb # CLI output.
│ └── utils.rb # Filtering, sorting, exporting.
├── main.rb # App entry point.
├── .env # Your API key lives here.
├── Gemfile # Dependencies.
└── README.md # This file! 😉
```

---

## **Testing**
Run the tests using RSpec:
## **Testing 🐝**
To make sure it’s all working, run:
```bash
rspec
```
This will check your code and ensure no bugs are lurking. 💡

---

## **Future Plans 🌍**
- Add analytics for contributors and pull requests.
- Cache API responses for faster loading.
- Build a web app version (because CLI isn’t for everyone 😂).

---

## **Future Enhancements**
- Add support for analyzing contributors and pull requests.
- Implement caching to minimize API calls.
- Add a web interface for better user interaction.
## **License 🔒**
MIT License—because sharing is caring. 😎 See the `LICENSE` file for details.

---

## **License**
This project is licensed under the MIT License. See the `LICENSE` file for details.
### **Made with ❤ by nmlletras**
58 changes: 26 additions & 32 deletions lib/api_client.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
# lib/api_client.rb
require 'httparty'
require 'dotenv/load'
require 'net/http'
require 'json'

class ApiClient
BASE_URL = "https://api.github.com"

def initialize
@headers = {
"Authorization" => "token #{ENV['GITHUB_API_KEY']}",
"User-Agent" => "GitHub Repository Insights"
}
end

def fetch_repositories(username)
all_repos = []
page = 1

loop do
response = HTTParty.get("#{BASE_URL}/users/#{username}/repos?per_page=100&page=#{page}", headers: @headers)
repos = handle_response(response)
break if repos.empty?

all_repos.concat(repos)
page += 1
end

all_repos
url = URI("#{BASE_URL}/users/#{username}/repos")
response = make_request(url)
JSON.parse(response, symbolize_names: true)
end

def fetch_commit_activity(username, repo_name)
response = HTTParty.get("#{BASE_URL}/repos/#{username}/#{repo_name}/stats/commit_activity", headers: @headers)
handle_response(response).sum { |week| week['total'] }
url = URI("#{BASE_URL}/repos/#{username}/#{repo_name}/commits")
response = make_request(url)
commits = JSON.parse(response, symbolize_names: true)
commits.size
rescue
nil
"N/A"
end

def fetch_repository_details(username, repo_name)
url = URI("#{BASE_URL}/repos/#{username}/#{repo_name}")
response = make_request(url)
JSON.parse(response, symbolize_names: true)
end

private

def handle_response(response)
if response.code == 200
JSON.parse(response.body)
else
raise "Error: #{response.message} (#{response.code})"
def make_request(url)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Get.new(url)
request['User-Agent'] = 'Ruby'
response = http.request(request)

unless response.is_a?(Net::HTTPSuccess)
raise "Error: #{response.message}"
end

response.body
end
end

29 changes: 21 additions & 8 deletions lib/display.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
# lib/display.rb
require 'tty-table'
require 'terminal-table'

class Display
module Display
def self.show_repositories(repositories)
rows = repositories.map do |repo|
[repo.name, repo.stars, repo.forks, repo.open_issues, repo.language, repo.size, repo.commits || 'N/A']
[repo.name, repo.stars, repo.forks, repo.open_issues, repo.language, repo.size.round(2), repo.commits]
end

table = TTY::Table.new(
['Name', 'Stars', 'Forks', 'Open Issues', 'Language', 'Size (KB)', 'Commits'],
rows
table = Terminal::Table.new(
headings: ['Name', 'Stars', 'Forks', 'Open Issues', 'Language', 'Size (MB)', 'Commits'],
rows: rows
)
puts table.render(:ascii)
puts table
end

def self.show_repository_details(data)
puts "\n--- Repository Details ---"
puts "Name: #{data[:name]}"
puts "Description: #{data[:description] || 'N/A'}"
puts "Stars: #{data[:stargazers_count]}"
puts "Forks: #{data[:forks_count]}"
puts "Open Issues: #{data[:open_issues_count]}"
puts "Language: #{data[:language] || 'N/A'}"
puts "Size: #{data[:size] / 1024.0} MB"
puts "Default Branch: #{data[:default_branch]}"
puts "Created At: #{data[:created_at]}"
puts "Last Updated: #{data[:updated_at]}"
end
end
Loading

0 comments on commit 0c706a2

Please sign in to comment.