Skip to content

Commit 2c4c88f

Browse files
committed
Default primary branch configurable for git-based repos
Fixes #308
2 parents 122d2de + 2ac49f3 commit 2c4c88f

File tree

14 files changed

+117
-21
lines changed

14 files changed

+117
-21
lines changed

Source/MantisSourceGitBasePlugin.class.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ abstract class MantisSourceGitBasePlugin extends MantisSourcePlugin
2727
*/
2828
private $valid_branch_regex = '%^(?!/|.*([/.]\.|//|@\{|\\\\)|@$)[^\000-\037\177 ~^:?*[]+(?<!\.lock|[/.])$%';
2929

30+
/**
31+
* @var bool Parent class includes global configuratino
32+
*/
33+
public $configuration = true;
34+
35+
/**
36+
* @var bool Prevent more than one Git-based plugin from processing form
37+
*/
38+
private static $config_form_handled = false;
39+
40+
/**
41+
*
42+
*/
43+
const CFG_DEFAULT_PRIMARY_BRANCH = 'git_default_primary_branch';
44+
3045
/**
3146
* Error constants
3247
*/
@@ -87,4 +102,63 @@ protected function validate_branch_list( $p_list )
87102
$this->ensure_branch_valid( trim( $t_branch ) );
88103
}
89104
}
105+
106+
/**
107+
* Retrieves the default primary branches from Source plugin's config
108+
* @return string
109+
*/
110+
protected function get_default_primary_branches() {
111+
plugin_push_current( 'Source' );
112+
$t_value = plugin_config_get( self::CFG_DEFAULT_PRIMARY_BRANCH, 'master' );
113+
plugin_pop_current();
114+
return $t_value;
115+
}
116+
117+
/**
118+
* Output form elements for configuration options.
119+
*/
120+
public function update_config_form() {
121+
# Prevent more than one Git-based class from outputting form elements.
122+
if( !MantisSourceGitBasePlugin::$config_form_handled ) {
123+
plugin_push_current( 'Source' );
124+
MantisSourceGitBasePlugin::$config_form_handled = true;
125+
?>
126+
<tr class="spacer"></tr>
127+
<tr>
128+
<td colspan="2"><h4><?php echo plugin_lang_get( 'git_title' ) ?></h4></td>
129+
</tr>
130+
<tr>
131+
<td class="category"><?php echo plugin_lang_get( self::CFG_DEFAULT_PRIMARY_BRANCH ) ?></td>
132+
<td>
133+
<input name="<?php echo self::CFG_DEFAULT_PRIMARY_BRANCH ?>"
134+
type="text" class="input-sm" size="50"
135+
value="<?php echo string_attribute( plugin_config_get( self::CFG_DEFAULT_PRIMARY_BRANCH, 'master' ) ) ?>"
136+
/>
137+
<br>
138+
<span class="small"><?php echo plugin_lang_get( 'git_default_primary_branch_info' ) ?></span>
139+
</td>
140+
</tr>
141+
<tr></tr>
142+
<?php
143+
plugin_pop_current();
144+
}
145+
}
146+
147+
/**
148+
* Process form elements for configuration options.
149+
*/
150+
public function update_config() {
151+
# Prevent more than one SVN class from handling form elements.
152+
if( !MantisSourceGitBasePlugin::$config_form_handled ) {
153+
MantisSourceGitBasePlugin::$config_form_handled = true;
154+
155+
plugin_push_current( 'Source' );
156+
$f_default_branch = trim( gpc_get_string( self::CFG_DEFAULT_PRIMARY_BRANCH ) ) ?: 'master';
157+
$t_default_branch = plugin_config_get( self::CFG_DEFAULT_PRIMARY_BRANCH, 'master' );
158+
if ( $f_default_branch != $t_default_branch ) {
159+
plugin_config_set( self::CFG_DEFAULT_PRIMARY_BRANCH, $f_default_branch );
160+
}
161+
plugin_pop_current();
162+
}
163+
}
90164
}

Source/MantisSourcePlugin.class.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,12 @@ public function update_repo( $p_repo ) {}
109109

110110
/**
111111
* Output form elements for configuration options.
112-
* The first div should have class 'spacer'
112+
*
113+
* They are displayed at the bottom of the plugin's config page
114+
* (see manage_config_page.php). The first row should have class 'spacer',
115+
* and the function should output an even number of rows (including the
116+
* spacer row), to ensure that the VCS-specific section always start on an
117+
* even row (i.e. with white background). Add an empty row if needed.
113118
*/
114119
public function update_config_form() {}
115120

Source/Source.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ function config() {
8383
'bugfix_message' => 'Fix committed to $1 branch.',
8484
'bugfix_message_view_status' => VS_PUBLIC,
8585

86+
'default_primary_branch' => 'master',
87+
8688
'remote_checkin' => OFF,
8789
'checkin_urls' => serialize( array( 'localhost' ) ),
8890

Source/lang/strings_english.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ $s_plugin_Source_enable_message = 'Bug Fixed Message';
114114
$s_plugin_Source_enable_porting = 'Porting Status';
115115
$s_plugin_Source_enable_product_matrix = 'Product Matrix Integration';
116116

117+
$s_plugin_Source_git_title = 'Git-based Plugins Integration';
118+
$s_plugin_Source_git_default_primary_branch = 'Default primary branches';
119+
$s_plugin_Source_git_default_primary_branch_info = 'Default Branches to use when creating a repository (comma-separated list or *).';
120+
117121
$s_plugin_Source_branch_mapping = 'Branch Mappings';
118122
$s_plugin_Source_mapping_update = 'Update Mappings';
119123
$s_plugin_Source_mapping_strategy = 'Strategy';

Source/lang/strings_french.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ $s_plugin_Source_enable_resolving = 'Résoudre les demandes résolues';
110110
$s_plugin_Source_enable_message = 'Message de résolution';
111111
$s_plugin_Source_enable_porting = 'Etat de portage';
112112

113+
$s_plugin_Source_git_title = 'Intégration de greffons basés sur Git';
114+
$s_plugin_Source_git_default_primary_branch = 'Branche par défaut';
115+
$s_plugin_Source_git_default_primary_branch_info = 'Branches renseignées par défaut lors de la création d\'un dépôt (liste avec séparateur virgule ou *).';
116+
113117
$s_plugin_Source_branch_mapping = 'Associations de branche';
114118
$s_plugin_Source_mapping_update = 'Mettre à jour les associations';
115119
$s_plugin_Source_mapping_strategy = 'Stratégie';

Source/pages/manage_config_page.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
</td>
139139
</tr>
140140

141-
<tr class="spacer" />
141+
<tr class="spacer"></tr>
142142
<tr>
143143
<td class="category"><?php echo plugin_lang_get( 'buglink_regex_1' ) ?></td>
144144
<td>
@@ -159,7 +159,7 @@
159159
</td>
160160
</tr>
161161

162-
<tr class="spacer" />
162+
<tr class="spacer"></tr>
163163
<tr>
164164
<td class="category"><span><?php echo plugin_lang_get( 'bugfix_regex_1' ) ?></td>
165165
<td>
@@ -258,7 +258,7 @@
258258
</td>
259259
</tr>
260260

261-
<tr class="spacer" />
261+
<tr class="spacer"></tr>
262262
<tr>
263263
<td class="category"><?php echo plugin_lang_get( 'api_key' ) ?></td>
264264
<td>
@@ -268,7 +268,7 @@
268268
</td>
269269
</tr>
270270

271-
<tr class="spacer" />
271+
<tr class="spacer"></tr>
272272
<tr>
273273
<td class="category"><?php echo plugin_lang_get( 'allow_remote_checkin' ) ?></td>
274274
<td>
@@ -288,7 +288,7 @@
288288
</td>
289289
</tr>
290290

291-
<tr class="spacer" />
291+
<tr class="spacer"></tr>
292292
<tr>
293293
<td class="category"><?php echo plugin_lang_get( 'allow_remote_import' ) ?></td>
294294
<td>
@@ -308,6 +308,11 @@
308308
</td>
309309
</tr>
310310

311+
<?php
312+
# Add an empty row `<tr></tr>` here as needed, to ensure that any VCS-specific
313+
# config starts on an even row (i.e. with white background) for better display.
314+
?>
315+
<tr></tr>
311316
<?php
312317
foreach( SourceVCS::all() as $t_type => $t_vcs ) {
313318
if ( $t_vcs->configuration ) {

SourceBitBucket/SourceBitBucket.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function update_repo_form( $p_repo ) {
111111
if( isset($p_repo->info['master_branch']) ) {
112112
$t_master_branch = $p_repo->info['master_branch'];
113113
} else {
114-
$t_master_branch = 'master';
114+
$t_master_branch = $this->get_default_primary_branches();
115115
}
116116
?>
117117
<tr>
@@ -223,7 +223,7 @@ public function import_full( $p_repo, $p_use_cache = true ) {
223223
$t_branch = $p_repo->info['master_branch'];
224224

225225
if( is_blank( $t_branch ) ) {
226-
$t_branch = 'master';
226+
$t_branch = $this->get_default_primary_branches();
227227
}
228228

229229
$t_username = $p_repo->info['bit_username'];

SourceCgit/SourceCgit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function update_repo_form( $p_repo ) {
9393
if ( isset( $p_repo->info['master_branch'] ) ) {
9494
$t_master_branch = $p_repo->info['master_branch'];
9595
} else {
96-
$t_master_branch = 'master';
96+
$t_master_branch = $this->get_default_primary_branches();
9797
}
9898
?>
9999
<tr>
@@ -157,7 +157,7 @@ public function import_full( $p_repo ) {
157157

158158
$t_branch = $p_repo->info['master_branch'];
159159
if ( is_blank( $t_branch ) ) {
160-
$t_branch = 'master';
160+
$t_branch = $this->get_default_primary_branches();
161161
}
162162

163163
$t_branches = array_map( 'trim', explode( ',', $t_branch ) );

SourceGithub/SourceGithub.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public function update_repo_form( $p_repo ) {
288288
if ( isset( $p_repo->info['master_branch'] ) ) {
289289
$t_master_branch = $p_repo->info['master_branch'];
290290
} else {
291-
$t_master_branch = 'master';
291+
$t_master_branch = $this->get_default_primary_branches();
292292
}
293293
?>
294294

@@ -572,7 +572,7 @@ public function import_full( $p_repo ) {
572572

573573
$t_branch = $p_repo->info['master_branch'];
574574
if ( is_blank( $t_branch ) ) {
575-
$t_branch = 'master';
575+
$t_branch = $this->get_default_primary_branches();
576576
}
577577

578578
if ($t_branch != '*')
@@ -804,5 +804,4 @@ public static function url_post( $p_url, $p_post_data ) {
804804
);
805805
return (string)$t_response->getBody();
806806
}
807-
808807
}

SourceGitlab/SourceGitlab.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function update_repo_form( $p_repo ) {
115115
if ( isset( $p_repo->info['master_branch'] ) ) {
116116
$t_master_branch = $p_repo->info['master_branch'];
117117
} else {
118-
$t_master_branch = 'master';
118+
$t_master_branch = $this->get_default_primary_branches();
119119
}
120120
?>
121121
<tr>
@@ -249,7 +249,7 @@ public function import_full( $p_repo ) {
249249

250250
$t_branch = $p_repo->info['master_branch'];
251251
if ( is_blank( $t_branch ) ) {
252-
$t_branch = 'master';
252+
$t_branch = $this->get_default_primary_branches();
253253
}
254254

255255
# if we're not allowed everything, populate an array of what we are allowed

0 commit comments

Comments
 (0)