Passed
Pull Request — master (#65)
by Romain
07:28 queued 02:53
created

UpdateChangelog::replaceIssues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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 = ' - **%s**%n%n   >*' . self::LOG_REVISION . ' by [%an](mailto:%ae) – %ad*%n%n%w(72, 3, 3)%b';
30
    const LOG_FORMAT_TINY = ' - [' . self::LOG_REVISION . '] **%s** – *by [%an](mailto:%ae) – %ad*%n';
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
95
$features";
96
        }
97
98
        if ($bugfix) {
99
            $changelog .= "
100
Bugs fixed
101
----------
102
103
$bugfix";
104
        }
105
106
        if ($important) {
107
            $changelog .= "
108
Important
109
---------
110
111
**⚠ Please pay attention to the changes below as they might break your TYPO3 installation:** 
112
113
$important";
114
        }
115
116
        if ($others) {
117
            $changelog .= "
118
Others
119
------
120
121
$others";
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->replaceIssues(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->replaceIssues(shell_exec($script));
153
    }
154
155
    /**
156
     * Adds a link to all detected GitHub issues number.
157
     *
158
     * @param string $text
159
     * @return string
160
     */
161
    protected function replaceIssues($text)
162
    {
163
        return preg_replace('/#([0-9]+)/', '[#$1](https:\/\/github.com\/CuyZ\/NotiZ\/issues\/$1)', $text);
164
    }
165
}
166
167
unset($argv[0]);
168
(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

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