bf7a4889e548ff41e690a43660450028d8ea9537
[yaffs-website] / vendor / consolidation / robo / src / Task / Development / GitHubRelease.php
1 <?php
2 namespace Robo\Task\Development;
3
4 use Robo\Result;
5
6 /**
7  * Publishes new GitHub release.
8  *
9  * ``` php
10  * <?php
11  * $this->taskGitHubRelease('0.1.0')
12  *   ->uri('consolidation-org/Robo')
13  *   ->description('Add stuff people need.')
14  *   ->change('Fix #123')
15  *   ->change('Add frobulation method to all widgets')
16  *   ->run();
17  * ?>
18  * ```
19  */
20 class GitHubRelease extends GitHub
21 {
22     /**
23      * @var string
24      */
25     protected $tag;
26
27     /**
28      * @var string
29      */
30     protected $name;
31
32     /**
33      * @var string
34      */
35     protected $description = '';
36
37     /**
38      * @var string[]
39      */
40     protected $changes = [];
41
42     /**
43      * @var bool
44      */
45     protected $draft = false;
46
47     /**
48      * @var bool
49      */
50     protected $prerelease = false;
51
52     /**
53      * @var string
54      */
55     protected $comittish = 'master';
56
57     /**
58      * @param string $tag
59      */
60     public function __construct($tag)
61     {
62         $this->tag = $tag;
63     }
64
65     /**
66      * @param string $tag
67      *
68      * @return $this
69      */
70     public function tag($tag)
71     {
72         $this->tag = $tag;
73         return $this;
74     }
75
76     /**
77      * @param bool $draft
78      *
79      * @return $this
80      */
81     public function draft($draft)
82     {
83         $this->draft = $draft;
84         return $this;
85     }
86
87     /**
88      * @param string $name
89      *
90      * @return $this
91      */
92     public function name($name)
93     {
94         $this->name = $name;
95         return $this;
96     }
97
98     /**
99      * @param string $description
100      *
101      * @return $this
102      */
103     public function description($description)
104     {
105         $this->description = $description;
106         return $this;
107     }
108
109     /**
110      * @param bool $prerelease
111      *
112      * @return $this
113      */
114     public function prerelease($prerelease)
115     {
116         $this->prerelease = $prerelease;
117         return $this;
118     }
119
120     /**
121      * @param string $comittish
122      *
123      * @return $this
124      */
125     public function comittish($comittish)
126     {
127         $this->comittish = $comittish;
128         return $this;
129     }
130
131     /**
132      * @param string $description
133      *
134      * @return $this
135      */
136     public function appendDescription($description)
137     {
138         if (!empty($this->description)) {
139             $this->description .= "\n\n";
140         }
141         $this->description .= $description;
142         return $this;
143     }
144
145     public function changes(array $changes)
146     {
147         $this->changes = array_merge($this->changes, $changes);
148         return $this;
149     }
150
151     /**
152      * @param string $change
153      *
154      * @return $this
155      */
156     public function change($change)
157     {
158         $this->changes[] = $change;
159         return $this;
160     }
161
162     /**
163      * @return string
164      */
165     protected function getBody()
166     {
167         $body = $this->description;
168         if (!empty($this->changes)) {
169             $changes = array_map(
170                 function ($line) {
171                     return "* $line";
172                 },
173                 $this->changes
174             );
175             $changesText = implode("\n", $changes);
176             $body .= "### Changelog \n\n$changesText";
177         }
178         return $body;
179     }
180
181     /**
182      * {@inheritdoc}
183      */
184     public function run()
185     {
186         $this->printTaskInfo('Releasing {tag}', ['tag' => $this->tag]);
187         $this->startTimer();
188         list($code, $data) = $this->sendRequest(
189             'releases',
190             [
191                 "tag_name" => $this->tag,
192                 "target_commitish" => $this->comittish,
193                 "name" => $this->name,
194                 "body" => $this->getBody(),
195                 "draft" => $this->draft,
196                 "prerelease" => $this->prerelease
197             ]
198         );
199         $this->stopTimer();
200
201         return new Result(
202             $this,
203             in_array($code, [200, 201]) ? 0 : 1,
204             isset($data->message) ? $data->message : '',
205             ['response' => $data, 'time' => $this->getExecutionTime()]
206         );
207     }
208 }