Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix gitlab import issue
Fix1: Regex are nos supprted as `Allowed Branches`.
Supported input are either a 'comma-separated list', or a 'regex' or '*'

Fix2: Because Gitlab REST v4 API returns are paginated, we have
to retrieve only interested branches
  • Loading branch information
Zic0-91 committed Oct 1, 2025
commit 7528e98ccd560d152083f2cbaad83a67377d758b
22 changes: 20 additions & 2 deletions Source/MantisSourceGitBasePlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ protected function is_branch_valid( $p_branch )
return (bool)preg_match( $this->valid_branch_regex, $p_branch );
}

/**
* Determines if given string name is a valid regex.
* @param string $p_regex regex to validate
* @return bool True if valid
*/
protected function is_regex_branch_valid( $p_regex )
{
# Trick to compile the regex
# see ReturnValues of the official doc https://www.php.net/manual/en/function.preg-match.php
return (0 === preg_match( $p_regex, "" ));
}


/**
* Triggers an error if the branch is invalid
* @param string $p_branch Branch name to validate
Expand All @@ -89,15 +102,20 @@ protected function ensure_branch_valid( $p_branch )
/**
* Validates a comma-delimited list of git branches.
* Triggers an ERROR_INVALID_BRANCH if one of the branches is invalid
* @param string $p_list Comma-delimited list of branch names (or '*')
* @param string $p_list Comma-delimited list of branch names, or a regex, or '*'
* @return void
*/
protected function validate_branch_list( $p_list )
{
#Case '*'
if( $p_list == '*' ) {
return;
}

#Case regex
if( preg_match( "/^\/.+\/[a-z]*$/i", $p_list ) ) {
return;
}
#Case list of validi git branches
foreach( explode( ',', $p_list ) as $t_branch ) {
$this->ensure_branch_valid( trim( $t_branch ) );
}
Expand Down
85 changes: 60 additions & 25 deletions SourceGitlab/SourceGitlab.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,33 @@ public function commit( $p_repo, $p_data ) {
return $this->import_commits( $p_repo, $t_commits, $t_branch );
}

protected function build_gitlab_apis($p_repo, $t_branch) {

$t_repoid = $p_repo->info['hub_repoid'];

/*
* Because Gitlab will return a pageg result (only the 20 first branches)
* The request for '*' should be reworked
*/
if ( $t_branch === "*" ) {
return array( "projects/$t_repoid/repository/branches/");
}

if ( preg_match( "/^\/.+\/[a-z]*$/i", $t_branch )) /* is a regex ? */
{
return array("projects/$t_repoid/repository/branches/?regex=$t_branch");
}

$gitlab_url_by_name = function( $branch ) use ($t_repoid) {
$branch_name = urlencode($branch);
return "projects/$t_repoid/repository/branches/$branch_name/";
};

$t_branches = array_map( 'trim', explode( ',', $t_branch ) );
return array_map( $gitlab_url_by_name, $t_branches);
}


public function import_full( $p_repo ) {
echo '<pre>';

Expand All @@ -308,41 +335,49 @@ public function import_full( $p_repo ) {
$t_branch = $this->get_default_primary_branches();
}

# if we're not allowed everything, populate an array of what we are allowed
if( $t_branch != '*' ) {
$t_branches_allowed = array_map( 'trim', explode( ',', $t_branch ) );
# Always pull back only interested branches
$t_api_names = $this->build_gitlab_apis($p_repo, $t_branch);

$t_uris = array();
foreach( $t_api_names as $t_api_name)
{
array_push($t_uris, $this->api_uri( $p_repo, $t_api_name));
}

# Always pull back full list of repos
$t_repoid = $p_repo->info['hub_repoid'];
$t_uri = $this->api_uri( $p_repo, "projects/$t_repoid/repository/branches" );

$t_member = null;
$t_json = json_url( $t_uri, $t_member );
if( $t_json === null ) {
echo "Could not retrieve data from GitLab at '$t_uri'. Make sure your ";
print_link(
plugin_page( 'repo_update_page', null, 'Source' )
. "&id=$p_repo->id",
'repository settings'
);
echo " are correct.";
echo '</pre>';
return array();
}
$t_json = array();
try {

$t_branches = array();
foreach( $t_json as $t_branch ) {
if( empty( $t_branches_allowed ) || in_array( $t_branch->name, $t_branches_allowed ) ) {
$t_branches[] = $t_branch;
foreach ($t_uris as $t_uri)
{
$t_member = null;
#print_r($t_uri);
$t_json_tmp = json_url( $t_uri, $t_member );
#print_r($t_json_tmp);
if( $t_json_tmp === null ) {
echo "Could not retrieve data from GitLab at '$t_uri'. Make sure your ";
print_link(
plugin_page( 'repo_update_page', null, 'Source' )
. "&id=$p_repo->id",
'repository settings'
);
echo " are correct.";
echo '</pre>';
return array();
}
array_push( $t_json, $t_json_tmp);
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
echo '</pre>';
return array();
}
#print_r($t_json);

$t_changesets = array();

$t_changeset_table = plugin_table( 'changeset', 'Source' );

foreach( $t_branches as $t_branch ) {
foreach( $t_json as $t_branch ) {
$t_query = "SELECT parent FROM $t_changeset_table
WHERE repo_id=" . db_param() . ' AND branch=' . db_param() .
' ORDER BY timestamp';
Expand Down