Passed
Pull Request — master (#65)
by Romain
04:03
created

UpdateChangelog::getLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
rs 9.4285
1
<?php
2
3
/*
4
 * Copyright (C) 2018
5
 * Nathan Boiron <[email protected]>
6
 * Romain Canon <[email protected]>
7
 *
8
 * This file is part of the TYPO3 NotiZ project.
9
 * It is free software; you can redistribute it and/or modify it
10
 * under the terms of the GNU General Public License, either
11
 * version 3 of the License, or any later version.
12
 *
13
 * For the full copyright and license information, see:
14
 * http://www.gnu.org/licenses/gpl-3.0.html
15
 */
16
17
/**
18
 * Run the following command in shell:
19
 *
20
 * $ php Build/update-changelog.php x.y.z
21
 *
22
 * Then update the `CHANGELOG.md` file, do modifications if needed, then commit.
23
 */
24
class UpdateChangelog
25
{
26
    const CHANGELOG_FILE = 'CHANGELOG.md';
27
28
    const LOG_REVISION = '[%h](https://github.com/CuyZ/NotiZ/commit/%H)';
29
    const LOG_FORMAT_FULL = '%n - **%s**%n%n   >*' . self::LOG_REVISION . ' by [%an](mailto:%ae) – %ad*%n%n%w(72, 3, 3)%b';
30
    const LOG_FORMAT_TINY = '%n - [' . self::LOG_REVISION . '] **%s** – *by [%an](mailto:%ae) – %ad*';
31
32
    protected $version;
33
    protected $currentDate;
34
    protected $lastGitTag;
35
36
    /**
37
     * Must have the new version number as parameter.
38
     *
39
     * @param string $version
40
     */
41
    public function __construct($version)
42
    {
43
        $this->version = $version;
44
45
        // Format "02 Feb 2018"
46
        $this->currentDate = date('d M Y');
47
48
        // Fetches last tag that was added in git
49
        $this->lastGitTag = trim(shell_exec('git describe --tags --abbrev=0'));
50
    }
51
52
    /**
53
     * Will update the changelog file with the latest commits, ordered as
54
     * follow:
55
     *
56
     * - New features
57
     * - Bugs fixed
58
     * - Important/breaking changes
59
     * - Other less important commits
60
     */
61
    public function run()
62
    {
63
        $features = $this->getLog('FEATURE');
64
        $bugfix = $this->getLog('BUGFIX');
65
        $important = $this->getLog('!!!');
66
        $others = $this->getLogTiny();
67
68
        $currentChangelog = file_get_contents(self::CHANGELOG_FILE);
69
70
        $changelog = $this->getChangelog($features, $bugfix, $important, $others);
71
        $changelog = preg_replace('/\n/', "\n$changelog", $currentChangelog, 1);
72
73
        file_put_contents(self::CHANGELOG_FILE, $changelog);
74
    }
75
76
    /**
77
     * @param string $features
78
     * @param string $bugfix
79
     * @param string $important
80
     * @param string $others
81
     * @return string
82
     */
83
    protected function getChangelog($features, $bugfix, $important, $others)
84
    {
85
        $changelog = "
86
v$this->version - $this->currentDate
87
====================";
88
89
        if ($features) {
90
            $changelog .= "
91
92
New features
93
------------
94
$features";
95
        }
96
97
        if ($bugfix) {
98
            $changelog .= "
99
Bugs fixed
100
----------
101
$bugfix";
102
        }
103
104
        if ($important) {
105
            $changelog .= "
106
Important
107
---------
108
109
**⚠ Please pay attention to the changes below as they might break your TYPO3 installation:** 
110
$important";
111
        }
112
113
        if ($others) {
114
            $changelog .= "
115
Others
116
------
117
$others";
118
        }
119
120
        $changelog .= "
121
----
122
";
123
124
        return $changelog;
125
    }
126
127
    /**
128
     * @param string $type
129
     * @return string
130
     */
131
    protected function getLog($type)
132
    {
133
        $script = "git log HEAD...$this->lastGitTag" .
134
            ' --grep="^\[' . $type . '\]"' .
135
            ' --date=format:"%d %b %Y"' .
136
            ' --pretty=tformat:"' . self::LOG_FORMAT_FULL . '"';
137
138
        return $this->sanitizeLog(shell_exec($script));
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    protected function getLogTiny()
145
    {
146
        $script = "git log HEAD...$this->lastGitTag" .
147
            ' --grep="^\[BUGFIX\\]" --grep="^\[FEATURE\\]" --grep="^\[!!!\\]"' .
148
            ' --invert-grep' .
149
            ' --date=format:"%d %b %Y"' .
150
            ' --pretty=tformat:"' . self::LOG_FORMAT_TINY . '"';
151
152
        return $this->sanitizeLog(shell_exec($script));
153
    }
154
155
    /**
156
     * @param string $text
157
     * @return string
158
     */
159
    protected function sanitizeLog($text)
160
    {
161
        // Add a link to all detected GitHub issues number.
162
        $text = preg_replace('/#([0-9]+)/', '[#$1](https:\/\/github.com\/CuyZ\/NotiZ\/issues\/$1)', $text);
163
164
        // Replace redundant line breaks.
165
        $text = preg_replace('/\n\n\n+/', "\n\n", $text);
166
167
        return $text;
168
    }
169
}
170
171
unset($argv[0]);
172
(new UpdateChangelog(...$argv))->run();
0 ignored issues
show
Bug introduced by
$argv is expanded, but the parameter $version of UpdateChangelog::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
(new UpdateChangelog(/** @scrutinizer ignore-type */ ...$argv))->run();
Loading history...
173