Skip to content

Commit

Permalink
Merge pull request #1 from MarkCorazon/feature/add-branch-support
Browse files Browse the repository at this point in the history
Added support for different branches
  • Loading branch information
mydnic authored Jan 6, 2025
2 parents e56b3c5 + e710b85 commit a07980b
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 40 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ return [
'github_repositories' => [
'mydnic/changelog-commit-for-laravel', // change me
// other repositories if you want to fetch the commit history from multiple repositories
// To specify a branch, make it an array like this one below:
// [ 'mydnic/changelog-commit-for-laravel', 'main' ]
],
];
```

When you are not specifying a branch, it will use the default `main` branch.

## Usage

Once the package is installed on your project, you should add the `changelod:fetch` command to your deployment process.
Once the package is installed on your project, you should add the `changelog:fetch` command to your deployment process.

Now, every time your application is deployed, the changelog will be updated with the latest commit messages.

Expand Down Expand Up @@ -212,7 +216,7 @@ class ChangelogController
public function __invoke()
{
$changelog = DB::table(config('changelog-commit-for-laravel.table_name'))
->select('message', 'date')
->select('message', 'date', 'branch')
->orderBy('date', 'desc')
->paginate(10);

Expand Down
2 changes: 2 additions & 0 deletions config/changelog-commit-for-laravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@
'github_repositories' => [
'mydnic/changelog-commit-for-laravel', // change me
// other repositories if you want to fetch the commit history from multiple repositories
// To specify a branch, make it an array like this one below:
// [ 'mydnic/changelog-commit-for-laravel', 'main' ]
],
];
1 change: 1 addition & 0 deletions database/factories/ChangelogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function definition()
{
return [
'commit_url' => $this->faker->url(),
'branch' => $this->faker->word(),
'message' => $this->faker->sentence(),
'date' => $this->faker->date(),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return new class extends Migration
Schema::create('changelog', function (Blueprint $table) {
$table->id();
$table->string('commit_url');
$table->string('branch');
$table->text('message');
$table->date('date');
});
Expand Down
7 changes: 4 additions & 3 deletions src/ChangelogCommitForLaravel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ChangelogCommitForLaravel
public static function prepareCommits(Collection $commits): Collection
{
return $commits->map(function ($commit) {
$commitMessage = $commit['commit']['message'] ?? '';
$commitMessage = $commit['github']['commit']['message'] ?? '';

// Check if the commit message contains a double newline
if (($pos = strpos($commitMessage, "\n\n")) !== false) {
Expand All @@ -27,9 +27,10 @@ public static function prepareCommits(Collection $commits): Collection

if ($detailedMessages) {
return [
'commit_url' => $commit['html_url'],
'commit_url' => $commit['github']['html_url'],
'branch' => $commit['branch'],
'message' => $detailedMessages,
'date' => Carbon::parse($commit['commit']['author']['date'])->format('Y-m-d'),
'date' => Carbon::parse($commit['github']['commit']['author']['date'])->format('Y-m-d'),
];
}
}
Expand Down
33 changes: 26 additions & 7 deletions src/Commands/FetchGithubCommitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function handle(): void
if (
DB::table(config('changelog-commit-for-laravel.table_name'))
->where('commit_url', $commitMessage['commit_url'])
->where('branch', $commitMessage['branch'])
->exists()
) {
continue;
Expand All @@ -44,21 +45,39 @@ public function fetchCommits(): Collection
$commits = collect();

foreach (config('changelog-commit-for-laravel.github_repositories') as $repository) {
$branch = [
'sha' => '',
'name' => ''
];

if(is_array($repository)){
$branch['sha'] = '?sha='.$repository[1];
$branch['name'] = $repository[1];
$repository = $repository[0];
} else {
$branch['name'] = 'main';
}

$repoCommits = Http::withHeaders([
'Accept' => 'application/vnd.github+json',
'Authorization' => 'Bearer '.config('changelog-commit-for-laravel.github_access_token'),
'X-GitHub-Api-Version' => '2022-11-28',
])
->get('https://api.github.com/repos/'.$repository.'/commits')
->json();
->get('https://api.github.com/repos/'.$repository.'/commits'.$branch['sha']);

if(!$repoCommits->successful()){
$this->error("Error while getting commit messages: " . $repoCommits->json()['message']);
return collect();
}

foreach ($repoCommits as $commit) {
$commits->push(
$commit
);
foreach ($repoCommits->json() as $commit) {
$commits->push([
'branch' => $branch['name'],
'github' => $commit
]);
}
}

return $commits;
}
}
68 changes: 40 additions & 28 deletions tests/CommitMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ function normalizeLineEndings($string)

it('correctly fetches the message from a commit', function () {
$commitMessage = [
'html_url' => 'https://github.com/mydnic/changelog-commit-for-laravel/commit/123',
'commit' => [
'author' => [
'date' => '2021-01-01T00:00:00Z',
],
'message' => normalizeLineEndings(<<<'EOF'
'branch' => 'main',
'github' => [
'html_url' => 'https://github.com/mydnic/changelog-commit-for-laravel/commit/123',
'commit' => [
'author' => [
'date' => '2021-01-01T00:00:00Z',
],
'message' => normalizeLineEndings(<<<'EOF'
feat: fix issue with authentication
> You can now login without any issue
> Enjoy!
EOF
),
),
],
],
];

Expand All @@ -31,17 +34,20 @@ function normalizeLineEndings($string)

it('ignores messages without a correctly formatted commit message', function () {
$commitMessage = [
'html_url' => 'https://github.com/mydnic/changelog-commit-for-laravel/commit/123',
'commit' => [
'author' => [
'date' => '2021-01-01T00:00:00Z',
],
'message' => normalizeLineEndings(<<<'EOF'
'branch' => 'main',
'github' => [
'html_url' => 'https://github.com/mydnic/changelog-commit-for-laravel/commit/123',
'commit' => [
'author' => [
'date' => '2021-01-01T00:00:00Z',
],
'message' => normalizeLineEndings(<<<'EOF'
feat: fix issue with authentication
Refactor the code
EOF
),
),
],
],
];

Expand All @@ -52,33 +58,39 @@ function normalizeLineEndings($string)

it('ignores messages without a correctly formatted commit message but keep good ones', function () {
$badcommitMessage = [
'html_url' => 'https://github.com/mydnic/changelog-commit-for-laravel/commit/123',
'commit' => [
'author' => [
'date' => '2021-01-01T00:00:00Z',
],
'message' => normalizeLineEndings(<<<'EOF'
'branch' => 'main',
'github' => [
'html_url' => 'https://github.com/mydnic/changelog-commit-for-laravel/commit/123',
'commit' => [
'author' => [
'date' => '2021-01-01T00:00:00Z',
],
'message' => normalizeLineEndings(<<<'EOF'
feat: fix issue with authentication
Refactor the code
EOF
),
),
],
],
];

$commitMessage2 = [
'html_url' => 'https://github.com/mydnic/changelog-commit-for-laravel/commit/123',
'commit' => [
'author' => [
'date' => '2021-01-01T00:00:00Z',
],
'message' => normalizeLineEndings(<<<'EOF'
'branch' => 'main',
'github' => [
'html_url' => 'https://github.com/mydnic/changelog-commit-for-laravel/commit/123',
'commit' => [
'author' => [
'date' => '2021-01-01T00:00:00Z',
],
'message' => normalizeLineEndings(<<<'EOF'
feat: fix issue with authentication
> You can now login without any issue
> Enjoy!
EOF
),
),
],
],
];

Expand Down

0 comments on commit a07980b

Please sign in to comment.