lilessam/git-repos

简化API,用于访问用户的Github & Bitbucket仓库。

dev-master 2023-10-03 15:24 UTC

This package is not auto-updated.

Last update: 2024-10-01 00:46:11 UTC


README

此包提供了简化API,用于访问用户的Github & Bitbucket公共和私有仓库,并将内容作为zip文件下载。

支持我

给我买杯咖啡

需求

  • PHP >= 7.1
  • illuminate/support ~5.0
  • bitbucket/client ^1.1
  • knplabs/github-api ^2.10

安装

1- composer require lilessam/git-repos dev-master

2- 将Lilessam\Git\GitServiceProvider::class添加到config/app.php

3- 运行php artisan vendor:publish --provider='Lilessam\Git\GitServiceProvider

该包假设您已经有机制来验证Bitbucket和Github用户的身份。

我强烈建议使用laravel/socialite包,因为它很简单,并且已经包含了Github和Bitbucket驱动程序。

请记住,当您使用Bitbucket验证用户时,需要存储刷新令牌。您将作为Laravel Socialite用户的属性接收它。

/**
 * Obtain the user information from Bitbucket.
 *
 * @return \Illuminate\Http\RedirectResponse
 */
public function handleProviderCallback()
{
    $socialiteUser = Socialite::driver('bitbucket')->user();

    $user = User::registerUsingBitbucket($socialiteUser);

    $user->setBitbucketTokens($socialiteUser->token, $socialiteUser->refreshToken);

    Auth::login($user);

    return redirect($this->redirectTo);
}

因此,您需要在某处编写四个静态函数,并在config/git.php中指定它们

1- github.get_token_closure:一个回调,将返回已验证用户的Github访问令牌。

/**
 * Get Github token for the needed session.
 *
 * @return string
 */
public static function getGithubToken()
{
    return Auth::user()->{static::GITHUB_TOKEN_COLUMN};
}

2- bitbucket.get_token_closure:一个回调,将返回已验证用户的Bitbucket访问令牌。

/**
 * Get Bitbucket token for the needed session.
 *
 * @return string
 */
public static function getBitbucketToken()
{
    return Auth::user()->{static::BITBUCKET_TOKEN_COLUMN};
}

3- bitbucket.refresh_token_closure:一个回调,返回已验证用户的Bitbucket刷新令牌。

/**
 * Get Bitbucket refresh token and return it for Git service provider.
 *
 * @return string
 */
public static function getBitbucketRefreshToken()
{
    return Auth::user()->{static::BITBUCKET_REFRESH_TOKEN_COLUMN};
}

4- bitbucket.update_token_closure:一个回调,将用于更新Bitbcuket访问令牌。它将接收一个参数。

/**
 * Update the Bitbucket access token in storage.
 *
 * @param string $token
 * @return void
 */
public static function updateBitbucketToken($token)
{
    Auth::user()->update([static::BITBUCKET_TOKEN_COLUMN => $token]);
}

用法

1- 获取用户Github或Bitbucket电子邮件。

// Default driver | Can be changed from config/git.php
git()->email();

// Github email
git()->driver('github')->email();

// Bitbucket email
git()->driver('bitbucket')->email();

2- 获取用户仓库。

// Default driver | Can be changed from config/git.php
git()->repos();

// Github email
git()->driver('github')->repos();

// Bitbucket email
git()->driver('bitbucket')->repos();

3- 获取特定仓库信息。

$repo = git()->repos()->first(function  ($repo)  {
    return  $repo->id  ==  'lilessam/git-repos';
});

echo $repo->id; // 'lilessam/git-repos'
echo $repo->name; // 'git-repos'
echo $repo->url; // 'https://github.com/lilessam/git-repos'

4- 下载仓库。

$repo = git()->repos()->first(function  ($repo)  {
    return  $repo->id  ==  'lilessam/git-repos';
});
$repo->download('zip');
// OR
$repo->download('tar');

我计划添加更多功能以获取每个仓库的详细信息,但主要功能是下载仓库内容。

请随时创建PR以添加更多功能。