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

eiriksm / cosy-composer / 18336101870

08 Oct 2025 06:39AM UTC coverage: 86.723% (+0.008%) from 86.715%
18336101870

push

github

web-flow
Make sure we dont force push to a branch that is up to date (#430)

11 of 12 new or added lines in 3 files covered. (91.67%)

2051 of 2365 relevant lines covered (86.72%)

46.38 hits per line

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

95.89
/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, $clean = true)
20
    {
21
        $this->log('Checking out new branch: ' . $branch_name);
137✔
22
        $result = $this->execCommand(['git', 'checkout', '-b', $branch_name], false);
137✔
23
        if ($result) {
137✔
24
            $this->log($this->getLastStdErr());
×
25
            throw new \Exception(sprintf('There was an error checking out branch %s. Exit code was %d', $branch_name, $result));
×
26
        }
27
        if ($clean) {
137✔
28
            // Make sure we do not have any uncommitted changes.
29
            $this->execCommand(['git', 'checkout', '.'], false);
137✔
30
        }
31
    }
32

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

39
    protected function commitFiles($msg, ?UpdateListItem $item = null, string $updateType = 'unknown')
40
    {
41
        $command = array_filter([
117✔
42
            'git', "commit",
117✔
43
            'composer.json',
117✔
44
            $this->getlockFileContents() ? 'composer.lock' : '',
117✔
45
            '-m',
117✔
46
            $msg,
117✔
47
        ]);
117✔
48
        $metadata = $this->getCommitMetadata($item, $updateType);
117✔
49
        if (!empty($metadata)) {
117✔
50
            $command[] = '-m';
117✔
51
            $command[] = sprintf("%s\n%s", Helpers::getCommitMessageSeparator(), Yaml::dump($metadata));
117✔
52
        }
53
        if ($this->execCommand($command, false, 120)) {
117✔
54
            $this->log($this->getLastStdOut());
1✔
55
            $this->log($this->getLastStdErr());
1✔
56
            throw new \Exception('Error committing the composer files. They are probably not changed.');
1✔
57
        }
58
        $this->commitMessage = $msg;
116✔
59
    }
60

61
    protected function getCommitMetadata(?UpdateListItem $item, string $updateType) : array
62
    {
63
        $metadata = [
117✔
64
            'violinist_metadata' => [
117✔
65
                'source' => 'violinist',
117✔
66
                'type' => $updateType,
117✔
67
            ],
117✔
68
        ];
117✔
69
        if ($item) {
117✔
70
            $metadata['update_data'] = [
94✔
71
                'package' => $item->getPackageName(),
94✔
72
                'from' => $item->getOldVersion(),
94✔
73
                'to' => $item->getNewVersion(),
94✔
74
            ];
94✔
75
        }
76
        return $metadata;
117✔
77
    }
78

79
    protected function getCommitCreator(Config $config) : Creator
80
    {
81
        $creator = new Creator();
117✔
82
        $type = Type::NONE;
117✔
83
        $creator->setType($type);
117✔
84
        try {
85
            $creator->setType($config->getCommitMessageConvention());
117✔
86
        } catch (\InvalidArgumentException $e) {
112✔
87
            // Fall back to using none.
88
        }
89
        return $creator;
117✔
90
    }
91

92
    protected function commitFilesForPackage(UpdateListItem $item, Config $config, $is_dev = false)
93
    {
94
        $this->cleanRepoForCommit();
94✔
95
        $creator = $this->getCommitCreator($config);
94✔
96
        $msg = $creator->generateMessage($item, $is_dev);
94✔
97
        $this->commitFiles($msg, $item, 'package');
94✔
98
    }
99

100
    protected function commitFilesForGroup(string $group_name, Config $config)
101
    {
102
        $this->cleanRepoForCommit();
4✔
103
        $creator = $this->getCommitCreator($config);
4✔
104
        $msg = $creator->generateMessageForGroup($group_name);
4✔
105
        $this->commitFiles($msg, null, 'group');
4✔
106
    }
107

108
    protected function pushCode($branch_name, $default_base, $lock_file_contents, string $default_branch)
109
    {
110
        if ($this->isPrivate) {
116✔
111
            $origin = 'origin';
115✔
112
            // Let's double check if we don't still have a PR that is actually
113
            // open, and is up to date.
114
            $main_sha = $this->getPrClient()->getDefaultBase($this->getSlug(), $default_branch);
115✔
115
            if ($main_sha !== $default_base) {
115✔
NEW
116
                throw new \Exception('The main branch has changed since we started. Aborting to be safe.');
×
117
            }
118
            // OK, now look at all the PRs we have, and see if we can find one
119
            // with the branch name we are about to push.
120
            $all_prs = $this->getPrClient()->getPrsNamed($this->getSlug());
115✔
121
            $all_of_them = $all_prs->getAllPrsNamed();
115✔
122
            if (!empty($all_of_them[$branch_name]["base"]["sha"])) {
115✔
123
                if ($all_of_them[$branch_name]["base"]["sha"] === $main_sha) {
31✔
124
                    $this->log('A pull request already exists for branch ' . $branch_name . ' and it is up to date. Not pushing any code.');
10✔
125
                    return;
10✔
126
                }
127
            }
128
            if ($this->execCommand(["git", 'push', $origin, $branch_name, '--force'])) {
105✔
129
                $this->log($this->getLastStdOut());
1✔
130
                $this->log($this->getLastStdErr());
1✔
131
                throw new GitPushException('Could not push to ' . $branch_name);
105✔
132
            }
133
        } else {
134
            $this->preparePrClient();
1✔
135
            /** @var \eiriksm\CosyComposer\Providers\PublicGithubWrapper $this_client */
136
            $this_client = $this->client;
1✔
137
            $this_client->forceUpdateBranch($branch_name, $default_base);
1✔
138
            $msg = $this->commitMessage;
1✔
139
            $this_client->commitNewFiles($this->tmpDir, $default_base, $branch_name, $msg, $lock_file_contents);
1✔
140
        }
141
    }
142

143
    protected function createPullrequest($pr_params)
144
    {
145
        $this->log('Creating pull request from ' . $pr_params['head']);
115✔
146
        return $this->getPrClient()->createPullRequest($this->slug, $pr_params);
115✔
147
    }
148
}
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