Skip to content

Commit

Permalink
Merge 21bd0c4 into 8825341
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed Sep 24, 2021
2 parents 8825341 + 21bd0c4 commit 5e04441
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 39 deletions.
81 changes: 42 additions & 39 deletions src/CosyComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,6 @@ public function run()
if (!$this->chdir($this->compserJsonDir)) {
throw new ChdirException('Problem with changing dir to the clone dir.');
}
$this->runAuthExport($hostname);
$local_adapter = new Local($this->compserJsonDir);
if (!empty($_SERVER['config_branch'])) {
$config_branch = $_SERVER['config_branch'];
Expand All @@ -628,6 +627,48 @@ public function run()
if (false == $cdata) {
throw new \InvalidArgumentException('Invalid composer.json file');
}
$config = Config::createFromComposerData($cdata);
$this->client = $this->getClient($this->slug);
$this->privateClient = $this->getClient($this->slug);
$this->privateClient->authenticate($this->userToken, null);
try {
$this->isPrivate = $this->privateClient->repoIsPrivate($this->slug);
// Get the default branch of the repo.
$default_branch = $this->privateClient->getDefaultBranch($this->slug);
} catch (\Throwable $e) {
// Could be a personal access token.
if (!method_exists($this->privateClient, 'authenticatePersonalAccessToken')) {
throw $e;
}
try {
$this->privateClient->authenticatePersonalAccessToken($this->userToken, null);
$this->isPrivate = $this->privateClient->repoIsPrivate($this->slug);
// Get the default branch of the repo.
$default_branch = $this->privateClient->getDefaultBranch($this->slug);
} catch (\Throwable $other_exception) {
// Throw the first exception, probably.
throw $e;
}
}
// 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
// way, if a person configures this wrong, several parts will fail spectacularly anyway.
$default_branch = $config->getDefaultBranch();
}
// Now make sure we are actually on that branch.
if ($this->execCommand('git remote set-branches origin "*"')) {
throw new \Exception('There was an error trying to configure default branch');
}
if ($this->execCommand('git fetch origin ' . $default_branch)) {
throw new \Exception('There was an error trying to fetch default branch');
}
if ($this->execCommand('git checkout ' . $default_branch)) {
throw new \Exception('There was an error trying to switch to default branch');
}
// Re-read the composer.json file, since it can be different on the default branch,
$cdata = $this->composerGetter->getComposerJsonData();
$this->runAuthExport($hostname);
$this->handleDrupalContribSa($cdata);
$config = Config::createFromComposerData($cdata);
$this->handleTimeIntervalSetting($cdata);
Expand Down Expand Up @@ -804,44 +845,6 @@ public function run()
$this->log($updates_string, Message::UPDATE, [
'packages' => $data,
]);
$this->client = $this->getClient($this->slug);
$this->privateClient = $this->getClient($this->slug);
$this->privateClient->authenticate($this->userToken, null);
try {
$this->isPrivate = $this->privateClient->repoIsPrivate($this->slug);
// Get the default branch of the repo.
$default_branch = $this->privateClient->getDefaultBranch($this->slug);
} catch (\Throwable $e) {
// Could be a personal access token.
if (!method_exists($this->privateClient, 'authenticatePersonalAccessToken')) {
throw $e;
}
try {
$this->privateClient->authenticatePersonalAccessToken($this->userToken, null);
$this->isPrivate = $this->privateClient->repoIsPrivate($this->slug);
// Get the default branch of the repo.
$default_branch = $this->privateClient->getDefaultBranch($this->slug);
} catch (\Throwable $other_exception) {
// Throw the first exception, probably.
throw $e;
}
}
// 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
// way, if a person configures this wrong, several parts will fail spectacularly anyway.
$default_branch = $config->getDefaultBranch();
}
// Now make sure we are actually on that branch.
if ($this->execCommand('git remote set-branches origin "*"')) {
throw new \Exception('There was an error trying to configure default branch');
}
if ($this->execCommand('git fetch origin ' . $default_branch)) {
throw new \Exception('There was an error trying to fetch default branch');
}
if ($this->execCommand('git checkout ' . $default_branch)) {
throw new \Exception('There was an error trying to switch to default branch');
}
// Try to see if we have already dealt with this (i.e already have a branch for all the updates.
$branch_user = $this->forkUser;
if ($this->isPrivate) {
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/composer-develop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extra": {
"violinist": {
"default_branch": "develop"
}
},
"require": {
"psr/log": "^1.0.0"
}
}
61 changes: 61 additions & 0 deletions test/fixtures/composer-develop.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "12419b18b7f2a646b445f66a758bab4a",
"packages": [
{
"name": "psr/log",
"version": "1.0.0",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "fe0936ee26643249e916849d48e3a51d5f5e278b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/fe0936ee26643249e916849d48e3a51d5f5e278b",
"reference": "fe0936ee26643249e916849d48e3a51d5f5e278b",
"shasum": ""
},
"type": "library",
"autoload": {
"psr-0": {
"Psr\\Log\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
"keywords": [
"log",
"psr",
"psr-3"
],
"support": {
"issues": "https://github.com/php-fig/log/issues",
"source": "https://github.com/php-fig/log/tree/1.0.0"
},
"time": "2012-12-21T11:40:51+00:00"
}
],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.1.0"
}
7 changes: 7 additions & 0 deletions test/fixtures/composer-master.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extra": {
"violinist": {
"default_branch": "develop"
}
}
}
18 changes: 18 additions & 0 deletions test/fixtures/composer-master.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "f9f1f63b110b9e511d7f794f53c7816b",
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.1.0"
}
42 changes: 42 additions & 0 deletions test/integration/ConfigBranchOrderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace eiriksm\CosyComposerTest\integration;

/**
* Test for a default commit message.
*/
class ConfigBranchOrderTest extends ComposerUpdateIntegrationBase
{
protected $packageForUpdateOutput = 'psr/log';
protected $packageVersionForFromUpdateOutput = '1.0.0';
protected $packageVersionForToUpdateOutput = '1.0.2';

public function setUp()
{
parent::setUp();
$this->createComposerFileFromFixtures($this->dir, 'composer-master.json');
}

public function testUpdateInCorrectBranch()
{
$this->runtestExpectedOutput();
// Since we don't place the updated composer.lock file, we fully expect the package to not be updated.
// This might also of course be the case for the actual bug we are testing here, but since we already know that
// the lock file in master contains no packages, then this demonstrates that this bug prevents the config branch
// files from being read. Totally.
$this->assertOutputContainsMessage('psr/log was not updated running composer update', $this->cosy);
}

protected function handleExecutorReturnCallback($cmd, &$return)
{
if ($cmd === 'git checkout develop') {
$this->createComposerFileFromFixtures($this->dir, 'composer-develop.json');
$this->placeComposerLockContentsFromFixture('composer-develop.lock', $this->dir);
}
}

protected function placeInitialComposerLock()
{
$this->placeComposerLockContentsFromFixture('composer-master.lock', $this->dir);
}
}

0 comments on commit 5e04441

Please sign in to comment.