• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eiriksm / cosy-composer / 13715678822

07 Mar 2025 07:13AM UTC coverage: 86.43% (+0.2%) from 86.28%
13715678822

push

github

web-flow
Refactor individual update (#398)

* Refactor individual update

* Code style

* new.files

* Update GitCommandsTrait.php

* Update GitCommandsTrait.php

* Update PrParamsCreator.php

608 of 643 new or added lines in 14 files covered. (94.56%)

3 existing lines in 1 file now uncovered.

1777 of 2056 relevant lines covered (86.43%)

43.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.08
/src/GitCommandsTrait.php
1
<?php
2

3
namespace eiriksm\CosyComposer;
4

5
use eiriksm\CosyComposer\Exceptions\GitPushException;
6
use eiriksm\ViolinistMessages\UpdateListItem;
7
use Symfony\Component\Yaml\Yaml;
8
use Violinist\CommitMessageCreator\Constant\Type;
9
use Violinist\CommitMessageCreator\Creator;
10
use Violinist\Config\Config;
11

12
trait GitCommandsTrait
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $commitMessage;
18

19
    protected function switchBranch($branch_name)
20
    {
21
        $this->log('Checking out new branch: ' . $branch_name);
124✔
22
        $result = $this->execCommand(['git', 'checkout', '-b', $branch_name], false);
124✔
23
        if ($result) {
124✔
NEW
24
            $this->log($this->getLastStdErr());
×
NEW
25
            throw new \Exception(sprintf('There was an error checking out branch %s. Exit code was %d', $branch_name, $result));
×
26
        }
27
        // Make sure we do not have any uncommitted changes.
28
        $this->execCommand(['git', 'checkout', '.'], false);
124✔
29
    }
30

31
    protected function cleanRepoForCommit()
32
    {
33
        // Clean up the composer.lock file if it was not part of the repo.
34
        $this->execCommand(['git', 'clean', '-f', 'composer.*']);
108✔
35
    }
36

37
    protected function commitFiles($msg, ?UpdateListItem $item = null)
38
    {
39
        $command = array_filter([
108✔
40
            'git', "commit",
108✔
41
            'composer.json',
108✔
42
            $this->getlockFileContents() ? 'composer.lock' : '',
108✔
43
            '-m',
108✔
44
            $msg,
108✔
45
        ]);
108✔
46
        if ($item) {
108✔
47
            $command[] = '-m';
89✔
48
            $command[] = sprintf("%s\n%s", Helpers::getCommitMessageSeparator(), Yaml::dump([
89✔
49
                'update_data' => [
89✔
50
                    'package' => $item->getPackageName(),
89✔
51
                    'from' => $item->getOldVersion(),
89✔
52
                    'to' => $item->getNewVersion(),
89✔
53
                ],
89✔
54
            ]));
89✔
55
        }
56
        if ($this->execCommand($command, false, 120)) {
108✔
57
            $this->log($this->getLastStdOut());
1✔
58
            $this->log($this->getLastStdErr());
1✔
59
            throw new \Exception('Error committing the composer files. They are probably not changed.');
1✔
60
        }
61
        $this->commitMessage = $msg;
107✔
62
    }
63

64
    protected function getCommitCreator(Config $config) : Creator
65
    {
66
        $creator = new Creator();
108✔
67
        $type = Type::NONE;
108✔
68
        $creator->setType($type);
108✔
69
        try {
70
            $creator->setType($config->getCommitMessageConvention());
108✔
71
        } catch (\InvalidArgumentException $e) {
103✔
72
            // Fall back to using none.
73
        }
74
        return $creator;
108✔
75
    }
76

77
    protected function commitFilesForPackage(UpdateListItem $item, Config $config, $is_dev = false)
78
    {
79
        $this->cleanRepoForCommit();
89✔
80
        $creator = $this->getCommitCreator($config);
89✔
81
        $msg = $creator->generateMessage($item, $is_dev);
89✔
82
        $this->commitFiles($msg, $item);
89✔
83
    }
84

85
    protected function pushCode($branch_name, $default_base, $lock_file_contents)
86
    {
87
        if ($this->isPrivate) {
107✔
88
            $origin = 'origin';
106✔
89
            if ($this->execCommand(["git", 'push', $origin, $branch_name, '--force'])) {
106✔
90
                $this->log($this->getLastStdOut());
1✔
91
                $this->log($this->getLastStdErr());
1✔
92
                throw new GitPushException('Could not push to ' . $branch_name);
106✔
93
            }
94
        } else {
95
            $this->preparePrClient();
1✔
96
            /** @var \eiriksm\CosyComposer\Providers\PublicGithubWrapper $this_client */
97
            $this_client = $this->client;
1✔
98
            $this_client->forceUpdateBranch($branch_name, $default_base);
1✔
99
            $msg = $this->commitMessage;
1✔
100
            $this_client->commitNewFiles($this->tmpDir, $default_base, $branch_name, $msg, $lock_file_contents);
1✔
101
        }
102
    }
103

104
    protected function createPullrequest($pr_params)
105
    {
106
        $this->log('Creating pull request from ' . $pr_params['head']);
106✔
107
        return $this->getPrClient()->createPullRequest($this->slug, $pr_params);
106✔
108
    }
109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc