Skip to content

Commit

Permalink
Merge 82d7809 into b32966c
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed May 25, 2020
2 parents b32966c + 82d7809 commit 2eec13b
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 80 deletions.
18 changes: 9 additions & 9 deletions src/CosyComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,9 @@ public function run()
$this->client = $this->getClient($this->slug);
$this->privateClient = $this->getClient($this->slug);
$this->privateClient->authenticate($this->userToken, null);
$this->isPrivate = $this->privateClient->repoIsPrivate($user_name, $user_repo);
$this->isPrivate = $this->privateClient->repoIsPrivate($this->slug);
// Get the default branch of the repo.
$default_branch = $this->privateClient->getDefaultBranch($user_name, $user_repo);
$default_branch = $this->privateClient->getDefaultBranch($this->slug);
// We also allow the project to override this for violinist.
if ($config->getDefaultBranch()) {
// @todo: Would be better to make sure this can actually be set, based on the branches available. Either
Expand All @@ -732,17 +732,17 @@ public function run()
$prs_named = [];
$default_base = null;
try {
if ($default_base_upstream = $this->privateClient->getDefaultBase($user_name, $user_repo, $default_branch)) {
if ($default_base_upstream = $this->privateClient->getDefaultBase($this->slug, $default_branch)) {
$default_base = $default_base_upstream;
}
$prs_named = $this->privateClient->getPrsNamed($user_name, $user_repo);
$prs_named = $this->privateClient->getPrsNamed($this->slug);
// These can fail if we have not yet created a fork, and the repo is public. That is why we have them at the
// end of this try/catch, so we can still know the default base for the original repo, and its pull
// requests.
if (!$default_base) {
$default_base = $this->getPrClient()->getDefaultBase($branch_user, $user_repo, $default_branch);
$default_base = $this->getPrClient()->getDefaultBase($this->slug, $default_branch);
}
$branches_flattened = $this->getPrClient()->getBranchesFlattened($branch_user, $user_repo);
$branches_flattened = $this->getPrClient()->getBranchesFlattened($this->slug);
} catch (RuntimeException $e) {
// Safe to ignore.
$this->log('Had a runtime exception with the fetching of branches and Prs: ' . $e->getMessage());
Expand Down Expand Up @@ -1103,7 +1103,7 @@ protected function handleIndividualUpdates($data, $lockdata, $cdata, $one_pr_per
'body' => $body,
'assignees' => $assignees,
];
$pullRequest = $this->getPrClient()->createPullRequest($user_name, $user_repo, $pr_params);
$pullRequest = $this->getPrClient()->createPullRequest($this->slug, $pr_params);
if (!empty($pullRequest['html_url'])) {
$this->log($pullRequest['html_url'], Message::PR_URL, [
'package' => $package_name,
Expand Down Expand Up @@ -1136,13 +1136,13 @@ protected function handleIndividualUpdates($data, $lockdata, $cdata, $one_pr_per
$this->log('Had a problem with creating the pull request: ' . $e->getMessage(), 'error');
if (isset($branch_name) && isset($pr_params) && !empty($prs_named[$branch_name]['title']) && $prs_named[$branch_name]['title'] != $pr_params['title']) {
$this->log('Will try to update the PR.');
$this->getPrClient()->updatePullRequest($user_name, $user_repo, $prs_named[$branch_name]['number'], $pr_params);
$this->getPrClient()->updatePullRequest($this->slug, $prs_named[$branch_name]['number'], $pr_params);
}
} catch (\Gitlab\Exception\RuntimeException $e) {
$this->log('Had a problem with creating the pull request: ' . $e->getMessage(), 'error');
if (isset($branch_name) && isset($pr_params) && !empty($prs_named[$branch_name]['title']) && $prs_named[$branch_name]['title'] != $pr_params['title']) {
$this->log('Will try to update the PR based on settings.');
$this->getPrClient()->updatePullRequest($user_name, $user_repo, $prs_named[$branch_name]['number'], $pr_params);
$this->getPrClient()->updatePullRequest($this->slug, $prs_named[$branch_name]['number'], $pr_params);
}
} catch (ComposerUpdateProcessFailedException $e) {
$this->log('Caught an exception: ' . $e->getMessage(), 'error');
Expand Down
21 changes: 10 additions & 11 deletions src/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@

namespace eiriksm\CosyComposer;

use Violinist\Slug\Slug;

interface ProviderInterface
{
public function authenticate($user, $token);

public function authenticatePrivate($user, $token);

public function repoIsPrivate($user, $repo);
public function repoIsPrivate(Slug $slug);

public function getDefaultBranch($user, $repo);
public function getDefaultBranch(Slug $slug);

public function getBranchesFlattened($user, $repo);
public function getBranchesFlattened(Slug $slug);

public function getPrsNamed($user, $repo);
public function getPrsNamed(Slug $slug);

public function getDefaultBase($user, $repo, $default_branch);
public function getDefaultBase(Slug $slug, $default_branch);

public function createFork($user, $repo, $fork_user);

/**
* @param string $user_name
* User name.
* @param string $user_repo
* User repo.
* @param Slug $slug
* @param array $params
* An array that consists of the following:
* - base (a base branch).
Expand All @@ -36,7 +35,7 @@ public function createFork($user, $repo, $fork_user);
*
* @return mixed
*/
public function createPullRequest($user_name, $user_repo, $params);
public function createPullRequest(Slug $slug, $params);

public function updatePullRequest($user_name, $user_repo, $id, $params);
public function updatePullRequest(Slug $slug, $id, $params);
}
31 changes: 23 additions & 8 deletions src/Providers/Bitbucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Bitbucket\Client;
use eiriksm\CosyComposer\ProviderInterface;
use Violinist\Slug\Slug;

class Bitbucket implements ProviderInterface
{
Expand All @@ -30,16 +31,20 @@ public function authenticatePrivate($user, $token)
$this->client->authenticate(Client::AUTH_OAUTH_TOKEN, $user);
}

public function repoIsPrivate($user, $repo)
public function repoIsPrivate(Slug $slug)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
if (!isset($this->cache['repo'])) {
$this->cache['repo'] = $this->client->repositories()->users($user)->show($repo);
}
return (bool) $this->cache["repo"]["is_private"];
}

public function getDefaultBranch($user, $repo)
public function getDefaultBranch(Slug $slug)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
if (!isset($this->cache['repo'])) {
$this->cache['repo'] = $this->client->repositories()->users($user)->show($repo);
}
Expand All @@ -59,8 +64,10 @@ protected function getBranches($user, $repo)
return $this->cache["branches"]["values"];
}

public function getBranchesFlattened($user, $repo)
public function getBranchesFlattened(Slug $slug)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
$branches = $this->getBranches($user, $repo);

$branches_flattened = [];
Expand All @@ -70,8 +77,10 @@ public function getBranchesFlattened($user, $repo)
return $branches_flattened;
}

public function getPrsNamed($user, $repo)
public function getPrsNamed(Slug $slug)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
$repo_users = $this->client->repositories()->users($user);
$repo_users->setPerPage(1000);
$prs = $repo_users->pullRequests($repo)->list();
Expand All @@ -91,16 +100,18 @@ public function getPrsNamed($user, $repo)
return $prs_named;
}

public function getDefaultBase($user, $repo, $default_branch)
public function getDefaultBase(Slug $slug, $default_branch)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
$branches = $this->getBranches($user, $repo);
$default_base = null;
foreach ($branches as $branch) {
if ($branch['name'] == $default_branch) {
$default_base = $branch["target"]["hash"];
}
}
// Since the braches only gives us 12 characters, we need to trim the default base to the same.
// Since the branches only gives us 12 characters, we need to trim the default base to the same.
return substr($default_base, 0, 12);
}

Expand All @@ -109,8 +120,10 @@ public function createFork($user, $repo, $fork_user)
throw new \Exception('Gitlab integration only support creating PRs as the authenticated user.');
}

public function createPullRequest($user_name, $user_repo, $params)
public function createPullRequest(Slug $slug, $params)
{
$user_name = $slug->getUserName();
$user_repo = $slug->getUserRepo();
$bitbucket_params = [
'title' => $params['title'],
'source' => [
Expand Down Expand Up @@ -140,8 +153,10 @@ public function createPullRequest($user_name, $user_repo, $params)
return $data;
}

public function updatePullRequest($user_name, $user_repo, $id, $params)
public function updatePullRequest(Slug $slug, $id, $params)
{
$user_name = $slug->getUserName();
$user_repo = $slug->getUserRepo();
return $this->client->repositories()->users($user_name)->pullRequests($user_repo)->update($id, $params);
}
}
29 changes: 22 additions & 7 deletions src/Providers/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Github\Api\PullRequest;
use Github\Client;
use Github\ResultPager;
use Violinist\Slug\Slug;

class Github implements ProviderInterface
{
Expand All @@ -30,16 +31,20 @@ public function authenticatePrivate($user, $token)
$this->client->authenticate($user, null, Client::AUTH_HTTP_TOKEN);
}

public function repoIsPrivate($user, $repo)
public function repoIsPrivate(Slug $slug)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
if (!isset($this->cache['repo'])) {
$this->cache['repo'] = $this->client->api('repo')->show($user, $repo);
}
return (bool) $this->cache['repo']['private'];
}

public function getDefaultBranch($user, $repo)
public function getDefaultBranch(Slug $slug)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
if (!isset($this->cache['repo'])) {
$this->cache['repo'] = $this->client->api('repo')->show($user, $repo);
}
Expand All @@ -57,8 +62,10 @@ protected function getBranches($user, $repo)
return $this->cache['branches'];
}

public function getBranchesFlattened($user, $repo)
public function getBranchesFlattened(Slug $slug)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
$branches = $this->getBranches($user, $repo);

$branches_flattened = [];
Expand All @@ -68,8 +75,10 @@ public function getBranchesFlattened($user, $repo)
return $branches_flattened;
}

public function getPrsNamed($user, $repo)
public function getPrsNamed(Slug $slug)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
$pager = new ResultPager($this->client);
$api = $this->client->api('pr');
$method = 'all';
Expand All @@ -81,8 +90,10 @@ public function getPrsNamed($user, $repo)
return $prs_named;
}

public function getDefaultBase($user, $repo, $default_branch)
public function getDefaultBase(Slug $slug, $default_branch)
{
$user = $slug->getUserName();
$repo = $slug->getUserRepo();
$branches = $this->getBranches($user, $repo);
$default_base = null;
foreach ($branches as $branch) {
Expand All @@ -100,8 +111,10 @@ public function createFork($user, $repo, $fork_user)
]);
}

public function createPullRequest($user_name, $user_repo, $params)
public function createPullRequest(Slug $slug, $params)
{
$user_name = $slug->getUserName();
$user_repo = $slug->getUserRepo();
/** @var PullRequest $prs */
$prs = $this->client->api('pull_request');
$data = $prs->create($user_name, $user_repo, $params);
Expand All @@ -121,8 +134,10 @@ public function createPullRequest($user_name, $user_repo, $params)
return $data;
}

public function updatePullRequest($user_name, $user_repo, $id, $params)
public function updatePullRequest(Slug $slug, $id, $params)
{
$user_name = $slug->getUserName();
$user_repo = $slug->getUserRepo();
return $this->client->api('pull_request')->update($user_name, $user_repo, $id, $params);
}
}

0 comments on commit 2eec13b

Please sign in to comment.