-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dc7e1de
Showing
10 changed files
with
400 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor/ | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 minicli | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# commit-calendar | ||
|
||
Display a list of dates and commits from public GitHub repositories. | ||
You will display the list in the terminal, and you can choose to save it to a CSV file (";" separated) and display into a browser (thanks to [itty.bitty](https://itty.bitty.site)) | ||
|
||
Commit Calendar is a CLI utility based on [Minicli](https://github.com/minicli/minicli). | ||
|
||
## Installation | ||
|
||
Requirements: | ||
- `php-cli` >= 7.3 | ||
- Composer | ||
|
||
Installation: | ||
|
||
1. Clone this repository | ||
2. Run `composer install` | ||
|
||
## How to use | ||
|
||
In a terminal, in the project folder: | ||
|
||
```bash | ||
./commit-calendar display owner=OWNER name=NAME [number=NUMBER] | ||
``` | ||
|
||
- OWNER: is the repository owner | ||
- NAME: is the repository name | ||
- NUMBER: the number of commits you want to extract | ||
|
||
For example: from this url https://github.com/dottxado/pico-macro-pad the OWNER is "dottxado" and the name is "pico-macro-pad", resulting in | ||
```bash | ||
./commit-calendar display owner=dottxado name=pico-macro-pad | ||
``` | ||
|
||
While OWNER and NAME are required, and if not given they will be queried by the CLI, NUMBER has a default value of 30. | ||
|
||
## Help | ||
In a terminal, in the project folder: | ||
|
||
```bash | ||
./commit-calendar help | ||
``` | ||
will display the available commands, and | ||
|
||
```bash | ||
./commit-calendar help display | ||
``` | ||
will display the help for the display command. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
<?php | ||
|
||
namespace App\Command\Display; | ||
|
||
use App\Service\GithubService; | ||
use Minicli\App; | ||
use Minicli\Command\CommandController; | ||
use Minicli\Input; | ||
use Minicli\Output\Adapter\FilePrinterAdapter; | ||
use Minicli\Output\Filter\ColorOutputFilter; | ||
use Minicli\Output\Helper\TableHelper; | ||
use Minicli\Output\OutputHandler; | ||
|
||
class DefaultController extends CommandController | ||
{ | ||
private int $numberOfResults; | ||
private int $perPage; | ||
protected Input $userInput; | ||
|
||
public function boot(App $app) | ||
{ | ||
parent::boot($app); | ||
$this->numberOfResults = 30; | ||
$this->perPage = 30; | ||
$this->userInput = new Input('CommitCalendar$> '); | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$owner = $this->owner(); | ||
$name = $this->name(); | ||
$this->number(); | ||
|
||
$pages = ceil($this->numberOfResults / $this->perPage); | ||
|
||
$this->getPrinter()->info(sprintf('Your input -> OWNER %s and NAME %s', $owner, $name)); | ||
$this->getPrinter()->info(sprintf('Total number of results to get: %s', $this->numberOfResults)); | ||
|
||
$output = []; | ||
for ($i = 1; $i <= $pages; $i++) { | ||
$numberOfResults = ($this->perPage * $i) <= $this->numberOfResults | ||
? $this->perPage | ||
: $this->numberOfResults % $this->perPage; | ||
$this->getPrinter()->info(sprintf('Querying GitHub for %s results...', $numberOfResults)); | ||
/** | ||
* @var GithubService | ||
*/ | ||
$githubService = $this->getApp()->github; | ||
$output = array_merge($output, $githubService->getCommitList($owner, $name, $i, $numberOfResults) ?? []); | ||
} | ||
|
||
if (is_null($output)) { | ||
$this->getPrinter()->error('Ooooops, something has gone bad'); | ||
exit; | ||
} elseif (empty($output)) { | ||
$this->getPrinter()->error('Ooooops, the result is empty!'); | ||
exit; | ||
} | ||
|
||
$this->displayTable($output); | ||
$this->saveToFile($output); | ||
$this->displayInBrowser($output, $owner, $name); | ||
|
||
$this->getPrinter()->success('Bye!'); | ||
$this->getPrinter()->newline(); | ||
} | ||
|
||
private function owner(): string | ||
{ | ||
if ($this->hasParam('owner')) { | ||
$owner = $this->getParam('owner'); | ||
} else { | ||
$this->getPrinter()->info('Provide the owner of the repository:'); | ||
$owner = $this->userInput->read(); | ||
} | ||
return $owner; | ||
} | ||
|
||
private function name(): string | ||
{ | ||
if ($this->hasParam('name')) { | ||
$name = $this->getParam('name'); | ||
} else { | ||
$this->getPrinter()->info('Provide the name of the repository:'); | ||
$name = $this->userInput->read(); | ||
} | ||
return $name; | ||
} | ||
|
||
private function number() | ||
{ | ||
if ($this->hasParam('number') && ! is_null($this->getParam('number'))) { | ||
$this->numberOfResults = (int)$this->getParam('number'); | ||
} | ||
} | ||
|
||
private function displayTable(array $list): void | ||
{ | ||
$table = new TableHelper(); | ||
$table->addHeader(['Date', 'Message']); | ||
foreach ($list as $element) { | ||
$date = $element->commit->author->date ?? ''; | ||
$message = isset($element->commit->message) | ||
? str_replace("\n", ' ', trim($element->commit->message)) | ||
: ''; | ||
$table->addRow([$date, $message]); | ||
} | ||
$this->getPrinter()->newline(); | ||
$this->getPrinter()->rawOutput($table->getFormattedTable(new ColorOutputFilter())); | ||
$this->getPrinter()->newline(); | ||
} | ||
|
||
private function saveToFile(array $list): void | ||
{ | ||
$this->getPrinter()->display('Do you want it saved to a CSV file?[Y/n]', true); | ||
$logToFile = $this->userInput->read(); | ||
if (strtolower($logToFile) === 'y' || $logToFile === '') { | ||
$this->getPrinter()->display('Full path and filename?[./commitcalendar.csv]', true); | ||
$filePath = $this->userInput->read(); | ||
if ($filePath === '') { | ||
$filePath = 'commitcalendar.csv'; | ||
} | ||
$backupOutput = $this->getApp()->getPrinter(); | ||
$this->getApp()->setOutputHandler(new OutputHandler(new FilePrinterAdapter($filePath))); | ||
$content = $this->generateCsv($list); | ||
$this->getPrinter()->rawOutput($content); | ||
$this->getApp()->setOutputHandler($backupOutput); | ||
} | ||
} | ||
|
||
private function displayInBrowser(array $list, string $owner, string $name): void | ||
{ | ||
$this->getPrinter()->display('Do you want to see it in a browser? (It may not work if the data are > 2Kb)[Y/n]', true); | ||
$generateHtml = $this->userInput->read(); | ||
if (strtolower($generateHtml) === 'y' || $generateHtml === '') { | ||
$link = shell_exec( | ||
'echo -n "' | ||
.$this->generateHtml($list, $owner, $name) | ||
.'" | lzma -9 | base64 | xargs -0 printf "https://itty.bitty.site/#/%s\n"' | ||
); | ||
$this->getPrinter()->info('Copy this link to your browser'); | ||
$this->getPrinter()->rawOutput($link); | ||
} | ||
} | ||
|
||
private function generateHtml(array $list, string $owner, string $name): string | ||
{ | ||
$pageTemplate = '<html lang=""><head><title>Commit List</title><style>td{padding:10px;}</style></head><body><h1>%s</h1><table><tr><th>Date</th><th>Message</th></tr>%s</table></body></html>'; | ||
$rowTemplate = '<tr><td>%s</td><td>%s</td></tr>'; | ||
$rows = ''; | ||
foreach ($list as $element) { | ||
$date = $element->commit->author->date ?? ''; | ||
$message = isset($element->commit->message) | ||
? str_replace("\n", ' ', $element->commit->message) | ||
: ''; | ||
$message = $this->sanitizeMessage($message); | ||
$rows .= sprintf($rowTemplate, $date, $message); | ||
} | ||
|
||
return sprintf($pageTemplate, $owner . '/'.$name, $rows); | ||
} | ||
|
||
private function generateCsv(array $list, string $delimiter = ';'): string | ||
{ | ||
$result = 'DATE, MESSAGE'; | ||
foreach ($list as $element) { | ||
$date = $element->commit->author->date ?? ''; | ||
$message = isset($element->commit->message) | ||
? str_replace("\n", ' ', $element->commit->message) | ||
: ''; | ||
$result .= "\n" . $date . $delimiter . $message; | ||
} | ||
return $result; | ||
} | ||
|
||
private function sanitizeMessage(string $message): string | ||
{ | ||
$message = str_replace('`', "'", $message); | ||
$message = htmlentities($message); | ||
return $message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Command\Help; | ||
|
||
use Minicli\App; | ||
use Minicli\Command\CommandController; | ||
|
||
class DefaultController extends CommandController | ||
{ | ||
/** @var array */ | ||
protected $command_map = []; | ||
|
||
public function boot(App $app) | ||
{ | ||
parent::boot($app); | ||
$this->command_map = $app->command_registry->getCommandMap(); | ||
} | ||
|
||
public function handle() | ||
{ | ||
$this->getPrinter()->info('Available Commands'); | ||
|
||
foreach ($this->command_map as $command => $sub) { | ||
|
||
$this->getPrinter()->newline(); | ||
$this->getPrinter()->out($command, 'info_alt'); | ||
|
||
if (is_array($sub)) { | ||
foreach ($sub as $subcommand) { | ||
if ($subcommand !== 'default') { | ||
$this->getPrinter()->newline(); | ||
$this->getPrinter()->out(sprintf('%s%s','└──', $subcommand)); | ||
} | ||
} | ||
} | ||
$this->getPrinter()->newline(); | ||
} | ||
|
||
$this->getPrinter()->newline(); | ||
$this->getPrinter()->newline(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Command\Help; | ||
|
||
use Minicli\Command\CommandController; | ||
|
||
class DisplayController extends CommandController | ||
{ | ||
public function handle() | ||
{ | ||
$this->getPrinter()->info('Display the date and the commit messages of public repositories on GitHub', true); | ||
$this->getPrinter()->info('./commit-calendar display owner=OWNER name=NAME [number=NUMBER]'); | ||
$this->getPrinter()->display('OWNER is the repository owner. If not provided, the user will be queried.'); | ||
$this->getPrinter()->display('NAME is the repository name. If not provided, the user will be queried.'); | ||
$this->getPrinter()->display('NUMBER is the number of commits you want to extract. Default 30.'); | ||
|
||
$this->getPrinter()->info('You will display the commit calendar in the console, and you can choose to save it to a CSV file and display into a browser (thanks to itty.bitty)'); | ||
|
||
$this->getPrinter()->newline(); | ||
$this->getPrinter()->info('You can try...', true); | ||
$this->getPrinter()->info('./commit-calendar display owner=minicli name=minicli'); | ||
$this->getPrinter()->info('./commit-calendar display owner=wordpress name=wordpress number=10'); | ||
$this->getPrinter()->info('./commit-calendar display owner=quarkusio name=quarkus'); | ||
$this->getPrinter()->newline(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Service; | ||
|
||
use Minicli\App; | ||
use Minicli\ServiceInterface; | ||
|
||
class GithubService implements ServiceInterface | ||
{ | ||
private string $baseUrl; | ||
private array $headers; | ||
|
||
public function load(App $app) | ||
{ | ||
$this->baseUrl = 'https://api.github.com/repos/'; | ||
$this->headers[] = 'Accept: application/vnd.github.v3+json'; | ||
} | ||
|
||
public function getCommitList(string $ownerName, string $repoName, int $page = 1, int $perPage = 30): ?array | ||
{ | ||
$url = $this->baseUrl.$ownerName.'/'.$repoName.'/commits?page='.$page.'&per_page='.$perPage; | ||
$curlResource = curl_init(); | ||
curl_setopt($curlResource, CURLOPT_URL, $url); | ||
curl_setopt($curlResource, CURLOPT_HEADER, $this->headers); | ||
curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, 1); | ||
curl_setopt($curlResource, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'); | ||
$output = curl_exec($curlResource); | ||
$info = curl_getinfo($curlResource); | ||
curl_close($curlResource); | ||
if (false === $output || $info['http_code'] !== 200) { | ||
return null; | ||
} | ||
return json_decode(substr($output, $info["header_size"])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/php | ||
<?php | ||
|
||
if (php_sapi_name() !== 'cli') { | ||
exit; | ||
} | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
use App\Service\GithubService; | ||
use Minicli\App; | ||
|
||
$app = new App([ | ||
'app_path' => __DIR__ . '/app/Command', | ||
]); | ||
|
||
$app->setSignature(<<<EOL | ||
___ _ _ ___ _ _ | ||
/ __|___ _ __ _ __ (_) |_ / __|__ _| |___ _ _ __| |__ _ _ _ | ||
| (__/ _ \ ' \| ' \| | _| (__/ _` | / -_) ' \/ _` / _` | '_| | ||
\___\___/_|_|_|_|_|_|_|\__|\___\__,_|_\___|_||_\__,_\__,_|_| | ||
./commit-calendar help | ||
EOL); | ||
|
||
$app->addService('github', new GithubService()); | ||
|
||
$app->runCommand($argv); |
Oops, something went wrong.