• 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

67.12
/src/Helpers.php
1
<?php
2

3
namespace eiriksm\CosyComposer;
4

5
use Psr\Log\LoggerInterface;
6
use Violinist\Config\Config;
7
use Violinist\Slug\Slug;
8

9
class Helpers
10
{
11

12
    /**
13
     * Helper to create branch name.
14
     */
15
    public static function createBranchName($item, $one_per_package = false, $config = null)
16
    {
17
        if ($one_per_package) {
137✔
18
            // Add a prefix.
19
            $prefix = '';
6✔
20
            if ($config) {
6✔
21
                /** @var Config $config */
22
                $prefix = $config->getBranchPrefix();
6✔
23
            }
24
            return sprintf('%sviolinist%s', $prefix, self::createBranchNameFromVersions($item->name, '', ''));
6✔
25
        }
26
        return self::createBranchNameFromVersions($item->name, $item->version, $item->latest, $config);
137✔
27
    }
28

29
    public static function getCommitMessageSeparator()
30
    {
31
        // Workaround for not being able to define a constant inside a trait.
32
        return '------';
89✔
33
    }
34

35
    public static function createBranchNameFromVersions($package, $version_from, $version_to, $config = null)
36
    {
37
        $item_string = sprintf('%s%s%s', $package, $version_from, $version_to);
137✔
38
        // @todo: Fix this properly.
39
        $result = preg_replace('/[^a-zA-Z0-9]+/', '', $item_string);
137✔
40
        $prefix = '';
137✔
41
        if ($config) {
137✔
42
            /** @var Config $config */
43
            $prefix = $config->getBranchPrefix();
137✔
44
        }
45
        return $prefix.$result;
137✔
46
    }
47

48
    public static function getComposerJsonName($cdata, $name, $tmp_dir)
49
    {
50
        if (!empty($cdata->{'require-dev'}->{$name})) {
118✔
51
            return $name;
7✔
52
        }
53
        if (!empty($cdata->require->{$name})) {
112✔
54
            return $name;
105✔
55
        }
56
        // If we can not find it, we have to search through the names, and try to normalize them. They could be in the
57
        // wrong casing, for example.
58
        $possible_types = [
8✔
59
            'require',
8✔
60
            'require-dev',
8✔
61
        ];
8✔
62
        foreach ($possible_types as $type) {
8✔
63
            if (empty($cdata->{$type})) {
8✔
64
                continue;
2✔
65
            }
66
            foreach ($cdata->{$type} as $package => $version) {
8✔
67
                if (strtolower($package) == strtolower($name)) {
8✔
68
                    return $package;
6✔
69
                }
70
            }
71
        }
72
        if (!empty($cdata->extra->{"merge-plugin"})) {
2✔
73
            $keys = [
×
74
                'include',
×
75
                'require',
×
76
            ];
×
77
            foreach ($keys as $key) {
×
78
                if (isset($cdata->extra->{"merge-plugin"}->{$key})) {
×
79
                    foreach ($cdata->extra->{"merge-plugin"}->{$key} as $extra_json) {
×
80
                        $files = glob(sprintf('%s/%s', $tmp_dir, $extra_json));
×
81
                        if (!$files) {
×
82
                            continue;
×
83
                        }
84
                        foreach ($files as $file) {
×
85
                            $contents = @file_get_contents($file);
×
86
                            if (!$contents) {
×
87
                                continue;
×
88
                            }
89
                            $json = @json_decode($contents);
×
90
                            if (!$json) {
×
91
                                continue;
×
92
                            }
93
                            try {
94
                                return self::getComposerJsonName($json, $name, $tmp_dir);
×
95
                            } catch (\Exception $e) {
×
96
                              // Fine.
97
                            }
98
                        }
99
                    }
100
                }
101
            }
102
        }
103
        throw new \Exception('Could not find ' . $name . ' in composer.json.');
2✔
104
    }
105

106
    public static function shouldUpdatePr($branch_name, $pr_params, $prs_named)
107
    {
108
        if (empty($branch_name)) {
102✔
NEW
109
            return false;
×
110
        }
111
        if (empty($pr_params)) {
102✔
NEW
112
            return false;
×
113
        }
114
        if (!empty($prs_named[$branch_name]['title']) && $prs_named[$branch_name]['title'] != $pr_params['title']) {
102✔
115
            return true;
22✔
116
        }
117
        if (!empty($prs_named[$branch_name]['body']) && !empty($pr_params['body'])) {
80✔
118
            if (trim($prs_named[$branch_name]['body']) != trim($pr_params['body'])) {
7✔
119
                return true;
5✔
120
            }
121
        }
122
        return false;
75✔
123
    }
124

125
    public static function handleAutoMerge(ProviderInterface $client, LoggerInterface $logger, Slug $slug, Config $config, $pullRequest, $security_update = false)
126
    {
127
        if ($config->shouldAutoMerge($security_update)) {
95✔
128
            $logger->log('info', 'Config indicated automerge should be enabled, Trying to enable automerge');
10✔
129
            $result = $client->enableAutomerge($pullRequest, $slug, $config->getAutomergeMethod($security_update));
10✔
130
            if (!$result) {
10✔
NEW
131
                $logger->log('info', 'Enabling automerge failed.');
×
132
            }
133
        }
134
    }
135

136
    public static function handleLabels(ProviderInterface $client, LoggerInterface $logger, Slug $slug, Config $config, $pullRequest, $security_update = false)
137
    {
138
        $labels = $config->getLabels();
11✔
139
        if ($security_update) {
11✔
NEW
140
            $labels = array_merge($labels, $config->getLabelsSecurity());
×
141
        }
142
        if (empty($labels)) {
11✔
143
            return;
9✔
144
        }
145
        $logger->log('info', 'Trying to add labels to PR');
2✔
146
        $result = $client->addLabels($pullRequest, $slug, $labels);
2✔
147
        if (!$result) {
2✔
NEW
148
            $logger->log('info', 'Error adding labels');
×
149
        } else {
150
            $logger->log('info', 'Labels added successfully');
2✔
151
        }
152
    }
153
}
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