Skip to content
This repository was archived by the owner on May 11, 2019. It is now read-only.

Commit 7ee0329

Browse files
committed
Sorting and version ordering works!
1 parent 129b320 commit 7ee0329

File tree

5 files changed

+119
-13
lines changed

5 files changed

+119
-13
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ eg: for https://github.com/SU-SWS/stanford_person
3434
erus[account] = SU-SWS
3535
erus[project] = stanford_person
3636

37+
## Plugins
38+
[GitHub](plugins/README.md)
39+
40+
A plugin to connect with GitHub's release functionalty. See [plugin documentation](plugins/README.md)
41+
for more information.
42+
3743
## API
3844

3945
see [erus.api.php](erus.api.php)
@@ -48,7 +54,7 @@ see [erus.api.php](erus.api.php)
4854
* Alter existing plugin information
4955
*/
5056
hook_erus_plugin_info_alter()
51-
57+
5258

5359
## Example Plugin
5460

erus.info

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ name = External Repository Update Status
22
description = "Provides update information to the update status module from external repository hosts such as github"
33
package = Administration
44
core = 7.x
5+
version = 7.x-1.0-alpha1
56
configure = admin/config/administration/erus
67
project = erus
78
project status url = https://github.com/SU-SWS/erus

includes/erusPlugin.inc

+13
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,17 @@ abstract class erusPlugin implements erusPluginInterface {
248248

249249
}
250250

251+
/**
252+
* [parse_latest_minor description]
253+
* @param [type] $tag_name [description]
254+
* @return [type] [description]
255+
*/
256+
protected function parse_latest_minor($tag_name) {
257+
258+
$big_bits = explode("-",$tag_name);
259+
$little_bits = explode(".", $big_bits[1]);
260+
return $little_bits[1];
261+
262+
}
263+
251264
}

plugins/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ Todo: update this...
1313
1. Create tag with Drupal version naming convention
1414
2. Create release using the release UI.
1515
a. Explain how releases work. Pre-release vs. Real Release.
16+
17+
18+
When creating a release you may place some key text into the body of the release
19+
notes that will be picked up and parsed by the plugin. Please be advised that
20+
the parser is case sensitive.
21+
22+
For release types, have one line of comma separated values.
23+
eg:
24+
Release type: Bug fixes, New features, Security update
25+

plugins/erusGithub.inc

+88-12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class erusGithub extends erusPlugin {
4040
*/
4141
public function process($data) {
4242

43+
// Go old or go home.
44+
if (!isset($data['info']['datestamp'])) {
45+
$data['info']['datestamp'] = 0;
46+
}
47+
4348
// Store data for later
4449
$this->set_update_data($data);
4550
$this->authenticate_with_github();
@@ -64,6 +69,7 @@ class erusGithub extends erusPlugin {
6469
$available = $this->map_data($releases);
6570
// Sanitize the status of the data from the original call
6671
$this->sanitize_info_data($data);
72+
6773
// Allow update module to do its magic.
6874
update_calculate_project_update_status($this->get_project_name(), $data, $available);
6975

@@ -95,8 +101,11 @@ class erusGithub extends erusPlugin {
95101
protected function map_data($releases) {
96102
$update_data = $this->get_update_data();
97103
$data = $this->get_default_update_status_available_data_array();
104+
98105
$latest_release = $this->get_latest_release($releases);
99-
$latest_prod_tag = !empty($latest_release['tag_name']) ? $latest_release['tag_name'] : '';
106+
$dev_release = $this->get_latest_dev_release($releases);
107+
108+
$latest_prod_tag = !empty($latest_release['tag_name']) ? $latest_release['tag_name'] : $dev_release["tag_name"];
100109

101110
$data['title'] = $update_data['info']['name'];
102111
$data['short_name'] = $update_data['name'];
@@ -107,10 +116,9 @@ class erusGithub extends erusPlugin {
107116
$data['recommended_major'] = $this->parse_latest_major($latest_prod_tag);
108117
$data['supported_majors'] = implode(",", range(1,99));
109118
$data['default_major'] = $this->parse_latest_major($latest_prod_tag);
119+
// $data['latest_version'] = $dev_release['tag_name'];
120+
// $data['dev_version'] = $this->parse_latest_major($latest_prod_tag);
110121

111-
// ASSETS
112-
// https://github.com/SU-SWS/stanford_person/releases/download/7.x-3.0-alpha8/stanford_person-7.x-3.0-alpha8.tar.gz
113-
// https://github.com/SU-SWS/stanford_person/archive/7.x-3.0-alpha8.tar.gz
114122

115123
foreach ($releases as $index => $release) {
116124
$tag_name = $release['tag_name'];
@@ -129,38 +137,106 @@ class erusGithub extends erusPlugin {
129137
'version' => $tag_name,
130138
'tag' => $tag_name,
131139
'version_major' => $this->parse_latest_major($tag_name),
132-
'version_patch' => 0,
133-
'version_extra' => ($release['prerelease']) ? 'dev' : @$tag_parts[2],
140+
'version_patch' => $this->parse_latest_minor($tag_name),
141+
'version_extra' => @$tag_parts[2],
134142
'status' => 'published',
135143
'release_link' => $release['html_url'],
136-
// 'download_link' => $release['tarball_url'],
137144
'download_link' => $download_link,
138-
//'date' => strtotime($release['published_at']), // when the release was published.
139-
'date' => strtotime($release['created_at']), // when the tag was created
145+
'date' => strtotime($release['published_at']), // when the release was published.
146+
// 'date' => strtotime($release['created_at']), // when the tag was created
140147
'mdhash' => md5($release['url']), // just making this stuff up.
141148
'filesize' => isset($release['assets'][0]) ? $release['assets'][0]['size'] : 0,
142149
'files' => "\n \n \n ",
143-
'terms' => array('Release type' => array('Bug fixes')),
150+
'terms' => $this->parse_release_terms($release['body']),
144151
);
145152

146153
}
147154

155+
// sort by name so that the list shows up as:
156+
// 7.x-1.1-alpha1
157+
// 7.x-1.0
158+
// 7.x-1.0-beta11
159+
// 7.x-1.0-beta10
160+
// 7.x-1.0-beta9
161+
162+
$keys = array_keys($data['releases']);
163+
164+
uasort($keys, function($a, $b) {
165+
if ($a == $b) {
166+
return 0;
167+
}
168+
$a = str_replace('7.x-', '', $a);
169+
$b = str_replace('7.x-', '', $b);
170+
171+
return version_compare($a, $b, '>') ? -1 : 1;
172+
}
173+
);
174+
$keys = array_flip($keys);
175+
$data['releases'] = array_merge($keys, $data['releases']);
148176
return $data;
149177
}
150178

179+
/**
180+
* Look for "Release type:" in the body of a release for tag information.
181+
* Tags should be a comma separated list
182+
* eg: Release type: Bug fixes, New features, Security update
183+
* @param string $string [description]
184+
* @return [type] [description]
185+
*/
186+
protected function parse_release_terms($string = '') {
187+
$terms = array('Release type' => array());
188+
189+
preg_match_all('/Release type:.*\n/', $string, $matches);
190+
$found = array_pop($matches);
191+
192+
if (!empty($found)) {
193+
$found = str_replace("Release type:", "", $found[0]);
194+
$found = str_replace(", ", ",", $found);
195+
$found = str_replace(" ,", ",", $found);
196+
$found = trim($found);
197+
$terms['Release type'] = explode(",", $found);
198+
}
199+
else {
200+
$terms['Release type'] = array('Bug fixes');
201+
}
202+
203+
204+
return $terms;
205+
}
206+
207+
151208
/**
152209
* Goes through the releases looking for a release that isnt marked as
153210
* a "pre-lease" in the github ui.
154211
* @param [array] $releases [all of the releases from GH]
155-
* @return [array] [the latest mared as non prerelease]
212+
* @return [array] [the latest marked as non prerelease]
156213
*/
157214
protected function get_latest_release($releases) {
215+
$potentials = array();
216+
158217
foreach ($releases as $i => $release) {
159218
if ($release['draft'] == TRUE || $release['prerelease'] == TRUE) {
160219
continue;
161220
}
162-
return $release;
221+
$potentials[$release['tag_name']] = $release;
163222
}
223+
224+
if (empty($potentials)) {
225+
return FALSE;
226+
}
227+
228+
// Silly ksort will put 7.x-1.0-alpha1 in front of 7.x-1.0
229+
krsort($potentials);
230+
$key = array_shift($potentials);
231+
232+
$parts = explode("-", $key["tag_name"]);
233+
if (isset($potentials[$parts[0] . "-" . $parts[1]])) {
234+
return $potentials[$parts[0] . "-" . $parts[1]];
235+
}
236+
else {
237+
return $key;
238+
}
239+
164240
}
165241

166242
/**

0 commit comments

Comments
 (0)