Skip to content

Commit

Permalink
GitLab: improve error handling when setting repoid
Browse files Browse the repository at this point in the history
- Store specific message in repoid field in case of error
- Clear the repoid if the reponame has changed
- Display a warning icon when repoid is not numeric (i.e. the field is
  null or contains some error message resulting from GitLab API call)

Define new language strings for API errors.

Fixes #352
  • Loading branch information
dregad committed Jan 19, 2021
1 parent 0a98cc3 commit 2404c1a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
35 changes: 29 additions & 6 deletions SourceGitlab/SourceGitlab.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ public function update_repo_form( $p_repo ) {
<td class="category"><?php echo plugin_lang_get( 'hub_repoid' ) ?></td>
<td>
<input type="text" name="hub_repoid" maxlength="250" size="40" value="<?php echo string_attribute( $t_hub_repoid ) ?>"/>
<?php if( !is_numeric( $t_hub_repoid ) ) { ?>
<i class="fa fa-warning ace-icon fa-lg red"></i>
<?php } ?>
</td>
</tr>
<tr>
Expand All @@ -153,10 +156,16 @@ public function update_repo_form( $p_repo ) {

public function update_repo( $p_repo ) {
$f_hub_root = gpc_get_string( 'hub_root' );
$f_hub_repoid = gpc_get_string( 'hub_repoid' );
$f_hub_reponame = gpc_get_string( 'hub_reponame' );
$f_hub_app_secret = gpc_get_string( 'hub_app_secret' );

# Clear the repoid if reponame has changed
if( $p_repo->info['hub_reponame'] != $f_hub_reponame ) {
$f_hub_repoid = null;
} else {
$f_hub_repoid = gpc_get_string( 'hub_repoid' );
}

# Update info required for getting the repoid
$p_repo->info['hub_root'] = $f_hub_root;
$p_repo->info['hub_reponame'] = $f_hub_reponame;
Expand All @@ -166,14 +175,28 @@ public function update_repo( $p_repo ) {
if( !is_numeric( $f_hub_repoid ) && !empty( $f_hub_reponame ) ) {
$t_hub_reponame_enc = urlencode( $f_hub_reponame );
$t_uri = $this->api_uri( $p_repo, "projects/$t_hub_reponame_enc" );
$t_member = null;
$t_json = json_url( $t_uri, $t_member );
$t_json = json_url( $t_uri );

$f_hub_repoid = 'Repository Name is invalid';
if( !is_null( $t_json ) ) {
if ( property_exists( $t_json, 'id' ) ) {
# Error handling
$t_message = '';
if( $t_json ) {
if( property_exists( $t_json, 'id' ) ) {
$f_hub_repoid = (string)$t_json ->id;
} elseif( property_exists( $t_json, 'error_description' ) ) {
$t_message = $t_json ->error_description;
} elseif( property_exists( $t_json, 'message' ) ) {
$t_message = $t_json ->message;
}
}
# repoid was not retrieved - format error message
if( !is_numeric( $f_hub_repoid ) ) {
if( !$t_message ) {
$t_message = plugin_lang_get( 'error_api_generic' );
}
$f_hub_repoid = sprintf(
plugin_lang_get( 'error_api' ),
$t_message
);
}
}

Expand Down
3 changes: 3 additions & 0 deletions SourceGitlab/lang/strings_english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ $s_plugin_SourceGitlab_hub_reponame = 'GitLab Repository Name<br /><span class="
$s_plugin_SourceGitlab_hub_app_secret = 'GitLab API Key<br /><span class="small">api key for mantis user (continuous user)</span>';
$s_plugin_SourceGitlab_master_branch = 'Primary Branches<br/><span class="small">(comma-separated list or *)</span>';

$s_plugin_SourceGitlab_error_api = 'GitLab API error (%1$s)';
$s_plugin_SourceGitlab_error_api_generic = 'Unable to retrieve Repository ID';

$s_plugin_SourceGitlab_back_repo = 'Back to Repository';
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ specification.
### Changed

- Minimum MantisBT version increased to 2.21.0
- GitLab: improve error handling when setting repoid
[#352](https://github.com/mantisbt-plugins/source-integration/issues/352)
- Code cleanup

### Fixed
Expand Down

0 comments on commit 2404c1a

Please sign in to comment.