Skip to content

Commit c005c2f

Browse files
committed
Create "import since" feature
When your Gatlab repo is old and hudge, may be you should not import all your repo. Commits messages may respect the correct layout and do not refer any mantis ticket number. This feature can also avoid erors during very large imports. When your report is very hudge, reimport all commits could be very timeconsuming, and the result is so huge that php engine can raise a memory error. fix "deprecated dynamic parrameter" creation.
1 parent 7528e98 commit c005c2f

File tree

8 files changed

+117
-49
lines changed

8 files changed

+117
-49
lines changed

Source/MantisSourceGitBasePlugin.class.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ abstract class MantisSourceGitBasePlugin extends MantisSourcePlugin
4646
* Error constants
4747
*/
4848
const ERROR_INVALID_BRANCH = 'invalid_branch';
49+
const ERROR_INVALID_DATE = 'invalid_date';
4950

5051
/**
5152
* Define plugin's Error strings
@@ -54,6 +55,7 @@ abstract class MantisSourceGitBasePlugin extends MantisSourcePlugin
5455
public function errors() {
5556
$t_errors_list = array(
5657
self::ERROR_INVALID_BRANCH,
58+
self::ERROR_INVALID_DATE,
5759
);
5860

5961
foreach( $t_errors_list as $t_error ) {
@@ -132,6 +134,21 @@ protected function get_default_primary_branches() {
132134
return $t_value;
133135
}
134136

137+
/**
138+
* Validates a date
139+
* Triggers an ERROR_INVALID_DATE if date is not valid
140+
* @return void
141+
*/
142+
protected function validate_date($p_date) {
143+
if (empty($p_date)) {
144+
return;
145+
}
146+
if (! (bool)strtotime($p_date)) {
147+
error_parameters( $p_date );
148+
plugin_error( self::ERROR_INVALID_DATE );
149+
}
150+
}
151+
135152
/**
136153
* Output form elements for configuration options.
137154
*/

Source/lang/strings_english.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ $s_plugin_Source_changeset_removed = 'Changeset removed';
138138

139139
$s_plugin_Source_ensure_delete = 'Do you really want to delete the "%s" repository and all of its history?';
140140
$s_plugin_Source_ensure_import_full = 'This will import to a new copy of your repository, and then destroy the old repository data, and may require use of shell access. Do you wish to continue?';
141+
$s_plugin_Source_ensure_import__full_since = 'This will import to a new copy of your repository since "%s", and then destroy the old repository data, and may require use of shell access. Do you wish to continue?';
141142
$s_plugin_Source_ensure_import_latest = 'This will attempt to import recent data for your repository, and may require use of shell access. Do you wish to continue?';
143+
$s_plugin_Source_ensure_import_latest_since = 'This will attempt to import since "%s" for yout repository, and may require use of shell access. Do you wish to continue?';
142144
$s_plugin_Source_import_results = 'Import Results';
143145
$s_plugin_Source_import_stats = 'Imported %s changesets, %s files, and %s bug references.';
144146
$s_plugin_Source_import_repo_error = 'Import process produced an error.';
@@ -150,6 +152,7 @@ $s_plugin_Source_invalid_changeset = 'Changeset information could not be loaded'
150152

151153
$s_plugin_Source_import_latest_failed = 'Repository latest data importing failed.';
152154
$s_plugin_Source_import_full_failed = 'Full repository data importing failed.';
155+
$s_plugin_Source_import_since_failed = 'Repository "since date" importing failed.';
153156

154157
$s_plugin_Source_changeset_column_title = 'C';
155158

Source/lang/strings_french.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ $s_plugin_Source_back_repo = 'Retour au dépôt';
7070
$s_plugin_Source_back_changeset = 'Retour au jeu de changements';
7171
$s_plugin_Source_import_full = 'Tout importer';
7272
$s_plugin_Source_import_latest = 'Importer les dernières données';
73+
$s_plugin_Source_import_since = 'Importer depuis le...';
7374
$s_plugin_Source_related_changesets = 'Jeux de changements liés';
7475
$s_plugin_Source_affected_issues = 'Demandes affectées';
7576
$s_plugin_Source_attach_to_issue = 'Attacher des demandes :';
@@ -134,7 +135,9 @@ $s_plugin_Source_changeset_removed = 'Jeu de changement détaché';
134135

135136
$s_plugin_Source_ensure_delete = 'Voulez-vous vraiment supprimer le dépôt "%s" et tout son historique ?';
136137
$s_plugin_Source_ensure_import_full = 'Ceci va importer une nouvelle copie de votre dépôt, puis détruira les vieilles données de dépôt et pourrait avoir besoin d\'un accès au shell. Voulez-vous continuer ?';
138+
$s_plugin_Source_ensure_import_full_since = 'Ceci va importer une nouvelle copie de votre dépôt depuis le "%s", puis détruira les vieilles données de dépôt et pourrait avoir besoin d\'un accès au shell. Voulez-vous continuer ?';
137139
$s_plugin_Source_ensure_import_latest = 'Ceci va tenter d\'importer les données récentes dans votre dépôt et pourrait avoir besoin d\'un accès au shell. Voulez-vous continuer ?';
140+
$s_plugin_Source_ensure_import_latest_since = 'Cevi va tenter d\'importer les données depuis le "%s" dans votre dépôt et pourrait avoir besoin d\'un accès au shell. Voulez-vous continuer ?';
138141
$s_plugin_Source_import_results = 'Résultats de l\'import';
139142
$s_plugin_Source_import_stats = '%s jeux de changements importés, %s fichiers, et %s références de demande.';
140143
$s_plugin_Source_import_repo_error = 'Le processus d\'import a rencontré une erreur.';

Source/pages/repo_import_full.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66
form_security_validate( 'plugin_Source_repo_import_full' );
77
access_ensure_global_level( plugin_config_get( 'manage_threshold' ) );
88

9-
$f_repo_id = gpc_get_string( 'id' );
9+
$f_repo_id = gpc_get_int( 'id' );
1010

1111
$t_repo = SourceRepo::load( $f_repo_id );
1212
$t_vcs = SourceVCS::repo( $t_repo );
1313

14-
helper_ensure_confirmed( plugin_lang_get( 'ensure_import_full' ), plugin_lang_get( 'import_full' ) );
14+
if (empty( $t_repo->info['hub_oldest_commit_date'])){
15+
helper_ensure_confirmed( plugin_lang_get( 'ensure_import_full' ), plugin_lang_get( 'import_full' ) );
16+
}else{
17+
$t_date_since = $t_repo->info['hub_oldest_commit_date'];
18+
helper_ensure_confirmed( sprintf( plugin_lang_get( 'ensure_import_full_since' ), $t_date_since), plugin_lang_get( 'import_full' ) );
19+
}
20+
1521
helper_begin_long_process();
1622

1723
layout_page_header( plugin_lang_get( 'title' ) );

Source/pages/repo_import_latest.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@
77

88
form_security_validate( 'plugin_Source_repo_import_latest' );
99
access_ensure_global_level( plugin_config_get( 'manage_threshold' ) );
10-
helper_ensure_confirmed( plugin_lang_get( 'ensure_import_latest' ), plugin_lang_get( 'import_latest' ) );
1110

12-
$f_repo_id = strtolower( gpc_get_string( 'id' ) );
11+
$f_repo_id = gpc_get_int( 'id' );
1312

1413
$t_repo_id = (int) $f_repo_id;
1514
$t_repos = array( SourceRepo::load( $t_repo_id ) );
16-
1715
$t_repo = array_shift( $t_repos );
1816
$t_vcs = SourceVCS::repo( $t_repo );
1917

20-
$t_repo->pre_stats = $t_repo->stats();
18+
$t_pre_stats = $t_repo->stats();
19+
20+
if ( empty( $t_repo->info['hub_oldest_commit_date'] ) ) {
21+
helper_ensure_confirmed( plugin_lang_get( 'ensure_import_latest' ), plugin_lang_get( 'import_latest' ) );
22+
}else{
23+
$t_date_since = $t_repo->info['hub_oldest_commit_date'];
24+
helper_ensure_confirmed( sprintf(plugin_lang_get( 'ensure_import_latest_since' ), $t_date_since), plugin_lang_get( 'import_latest' ) );
25+
}
26+
2127

2228
layout_page_header( plugin_lang_get( 'title' ) );
2329
layout_page_begin();
@@ -39,15 +45,15 @@
3945

4046
<?php
4147
# keep checking for more changesets to import
42-
$t_repo->import_error = false;
48+
$t_import_error = false;
4349
while( true ) {
4450

4551
# import the next batch of changesets
4652
$t_changesets = $t_vcs->import_latest( $t_repo );
4753

4854
# check for errors
4955
if ( !is_array( $t_changesets ) ) {
50-
$t_repo->import_error = true;
56+
$t_import_error = true;
5157
break;
5258
}
5359

@@ -59,21 +65,21 @@
5965
Source_Process_Changesets( $t_changesets );
6066
}
6167

62-
$t_repo->post_stats = $t_repo->stats();
68+
$t_post_stats = $t_repo->stats();
6369
?>
6470

6571
<tr>
6672
<td class="category"><?php echo string_display_line( $t_repo->name ) ?></td>
6773
<td>
6874
<?php
69-
if ( $t_repo->import_error ) {
75+
if ( $t_import_error) {
7076
echo plugin_lang_get( 'import_latest_failed' ), '<br/>';
7177
}
7278

73-
$t_stats = $t_repo->post_stats;
74-
$t_stats['changesets'] -= $t_repo->pre_stats['changesets'];
75-
$t_stats['files'] -= $t_repo->pre_stats['files'];
76-
$t_stats['bugs'] -= $t_repo->pre_stats['bugs'];
79+
$t_stats = $t_post_stats;
80+
$t_stats['changesets'] -= $t_pre_stats['changesets'];
81+
$t_stats['files'] -= $t_pre_stats['files'];
82+
$t_stats['bugs'] -= $t_pre_stats['bugs'];
7783

7884
echo sprintf( plugin_lang_get( 'import_stats' ), $t_stats['changesets'], $t_stats['files'], $t_stats['bugs'] );
7985
?>

Source/pages/repo_manage_page.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,12 @@ function convert_to_key_value( $p_array ) {
177177
<form action="<?php echo plugin_page( 'repo_import_latest' ) . '&amp;id=' . $t_repo->id ?>" method="post" class="pull-right">
178178
<?php echo form_security_field( 'plugin_Source_repo_import_latest' ) ?>
179179
<input type="submit" class="btn btn-primary btn-white btn-sm btn-round " value="<?php echo plugin_lang_get( 'import_latest' ) ?>"/>
180-
</form>
180+
</form>
181181
</div>
182182
</div>
183183
</div>
184184
</div>
185-
185+
186186
<div class="space-10"></div>
187187

188188

SourceGitlab/SourceGitlab.php

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public function update_repo_form( $p_repo ) {
9494
$t_hub_repoid = null;
9595
$t_hub_reponame = null;
9696
$t_hub_app_secret = null;
97+
$t_hub_oldest_commit_date = null;
9798

9899
if ( isset( $p_repo->info['hub_root'] ) ) {
99100
$t_hub_root = $p_repo->info['hub_root'];
@@ -112,6 +113,9 @@ public function update_repo_form( $p_repo ) {
112113
} else {
113114
$t_master_branch = $this->get_default_primary_branches();
114115
}
116+
if ( isset( $p_repo->info['hub_oldest_commit_date'] ) ) {
117+
$t_hub_oldest_commit_date = $p_repo->info['hub_oldest_commit_date'];
118+
}
115119
?>
116120
<tr>
117121
<th class="category">
@@ -182,6 +186,18 @@ public function update_repo_form( $p_repo ) {
182186
/>
183187
</td>
184188
</tr>
189+
<tr>
190+
<th class="category">
191+
<label for="hub_oldest_commit_date">
192+
<?php echo plugin_lang_get( 'hub_oldest_commit_date' ) ?>
193+
</label>
194+
</th>
195+
<td>
196+
<input type="date" id="hub_oldest_commit_date" name="hub_oldest_commit_date"
197+
value="<?php echo string_attribute( $t_hub_oldest_commit_date ) ?>"
198+
/>
199+
</td>
200+
</tr>
185201
<?php
186202
}
187203

@@ -240,6 +256,13 @@ public function update_repo( $p_repo ) {
240256
$p_repo->info['hub_repoid'] = $f_hub_repoid;
241257
$p_repo->info['master_branch'] = $f_master_branch;
242258

259+
260+
# Check date format
261+
$t_hub_oldest_commit_date = gpc_get_string( 'hub_oldest_commit_date' );
262+
$this->validate_date($t_hub_oldest_commit_date);
263+
264+
$p_repo->info['hub_oldest_commit_date'] = $t_hub_oldest_commit_date;
265+
243266
return $p_repo;
244267
}
245268

@@ -327,8 +350,10 @@ protected function build_gitlab_apis($p_repo, $t_branch) {
327350
}
328351

329352

330-
public function import_full( $p_repo ) {
331-
echo '<pre>';
353+
public function import_full( $p_repo) {
354+
static $counter=0;
355+
$counter = $counter + 1;
356+
echo "<pre>Pass #$counter: \n";
332357

333358
$t_branch = $p_repo->info['master_branch'];
334359
if ( is_blank( $t_branch ) ) {
@@ -382,17 +407,18 @@ public function import_full( $p_repo ) {
382407
WHERE repo_id=" . db_param() . ' AND branch=' . db_param() .
383408
' ORDER BY timestamp';
384409
$t_result = db_query( $t_query, array( $p_repo->id, $t_branch->name ), 1 );
385-
410+
echo "Process branch '$t_branch->name':\n";
386411
$t_commits = array( $t_branch->commit->id );
387412
if ( db_num_rows( $t_result ) > 0 ) {
388413
$t_parent = db_result( $t_result );
389414
echo "Oldest '$t_branch->name' branch parent: '$t_parent'\n";
390415

391416
if ( !empty( $t_parent ) ) {
392417
$t_commits[] = $t_parent;
393-
echo "Parents not empty";
418+
echo "Parents not empty - ";
419+
} else {
420+
echo "Parents empty";
394421
}
395-
echo "Parents empty";
396422
}
397423

398424
$t_changesets = array_merge( $t_changesets, $this->import_commits( $p_repo, $t_commits, $t_branch->name ) );
@@ -449,37 +475,43 @@ public function import_commits( $p_repo, $p_commit_ids, $p_branch='' ) {
449475

450476
private function json_commit_changeset( $p_repo, $p_json, $p_branch='' ) {
451477
echo "processing $p_json->id ... ";
452-
if ( !SourceChangeset::exists( $p_repo->id, $p_json->id ) ) {
453-
# Message will be replaced by title in gitlab version earlier than 7.2
454-
$t_message = ( !property_exists( $p_json, 'message' ) )
455-
? $p_json->title
456-
: $p_json->message;
457-
$t_changeset = new SourceChangeset(
458-
$p_repo->id,
459-
$p_json->id,
460-
$p_branch,
461-
$p_json->created_at,
462-
$p_json->author_name,
463-
$t_message
464-
);
465-
466-
$t_parents = array();
467-
foreach( $p_json->parent_ids as $t_parent ) {
468-
$t_parents[] = $t_parent;
469-
}
470-
if( $t_parents ) {
471-
$t_changeset->parent = $t_parents[0];
472-
}
473-
474-
$t_changeset->author_email = $p_json->author_email;
475-
$t_changeset->save();
476-
477-
echo "saved.\n";
478-
return array( $t_changeset, $t_parents );
479-
} else {
478+
if ( SourceChangeset::exists( $p_repo->id, $p_json->id ) ) {
480479
echo "already exists.\n";
481480
return array( null, array() );
482481
}
482+
483+
$t_oldest_commit_date = $p_repo->info['hub_oldest_commit_date'];
484+
if ( !empty( $t_oldest_commit_date ) && new DateTime( $p_json->created_at ) < new DateTime( $t_oldest_commit_date ) ) {
485+
echo "commit before the oldest_commit_date.\n";
486+
return array( null, array() );
487+
}
488+
489+
# Message will be replaced by title in gitlab version earlier than 7.2
490+
$t_message = ( !property_exists( $p_json, 'message' ) )
491+
? $p_json->title
492+
: $p_json->message;
493+
$t_changeset = new SourceChangeset(
494+
$p_repo->id,
495+
$p_json->id,
496+
$p_branch,
497+
$p_json->created_at,
498+
$p_json->author_name,
499+
$t_message
500+
);
501+
502+
$t_parents = array();
503+
foreach( $p_json->parent_ids as $t_parent ) {
504+
$t_parents[] = $t_parent;
505+
}
506+
if( $t_parents ) {
507+
$t_changeset->parent = $t_parents[0];
508+
}
509+
510+
$t_changeset->author_email = $p_json->author_email;
511+
$t_changeset->save();
512+
513+
echo "saved.\n";
514+
return array( $t_changeset, $t_parents );
483515
}
484516

485517
public static function url_post( $p_url, $p_post_data ) {

SourceGitlab/lang/strings_english.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $s_plugin_SourceGitlab_hub_repoid = 'GitLab Repository ID<br /><span class="smal
1414
$s_plugin_SourceGitlab_hub_reponame = 'GitLab Repository Name<br /><span class="small">Name &amp; Namespace with no leading or trailing slashes (namespace/project)</span>';
1515
$s_plugin_SourceGitlab_hub_app_secret = 'GitLab API Key<br /><span class="small">api key for mantis user (continuous user)</span>';
1616
$s_plugin_SourceGitlab_master_branch = 'Allowed Branches<br/><span class="small">(comma-separated list, regular expression with /.../ or *)</span>';
17+
$s_plugin_SourceGitlab_hub_oldest_commit_date = 'Import since (optional)<br/><span class="small">Oldest commit will be ignored, leave empty to import all commits</span>';
1718
$s_plugin_SourceGitlab_import_limit = 'Import Limit<br/><span class="small">at most this many commits per import action (0: unlimited)</span>';
1819

1920
$s_plugin_SourceGitlab_error_api = 'GitLab API error (%1$s)';

0 commit comments

Comments
 (0)