diff --git a/Source/Source.API.php b/Source/Source.API.php index fb96ad1..d0eb541 100644 --- a/Source/Source.API.php +++ b/Source/Source.API.php @@ -1103,7 +1103,8 @@ trigger_error( ERROR_GENERIC, ERROR ); } - return array_shift( self::from_result( $t_result ) ); + $tmp = self::from_result( $t_result ); + return array_shift( $tmp ); } /** @@ -1123,7 +1124,8 @@ trigger_error( ERROR_GENERIC, ERROR ); } - return array_shift( self::from_result( $t_result ) ); + $tmp = self::from_result( $t_result ); + return array_shift( $tmp ); } /** diff --git a/SourceGitBucket/LICENSE b/SourceGitBucket/LICENSE new file mode 100644 index 0000000..9d4cbb9 --- /dev/null +++ b/SourceGitBucket/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) 2018 Mira Long + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + diff --git a/SourceGitBucket/SourceGitBucket.php b/SourceGitBucket/SourceGitBucket.php new file mode 100755 index 0000000..3256b0c --- /dev/null +++ b/SourceGitBucket/SourceGitBucket.php @@ -0,0 +1,476 @@ +name = plugin_lang_get( 'title' ); + $this->description = plugin_lang_get( 'description' ); + + $this->version = '0.1'; + $this->requires = array( + 'MantisCore' => '1.2.16', + 'Source' => '0.18', + ); + + $this->author = 'Mira Long'; + $this->contact = 'ms502040@gmail.com'; + $this->url = 'http://neyowo.com'; + } + + public function errors() { + $t_errors_list = array( + self::ERROR_INVALID_PRIMARY_BRANCH, + ); + foreach( $t_errors_list as $t_error ) { + $t_errors[$t_error] = plugin_lang_get( 'error_' . $t_error ); + } + return $t_errors; + } + + public $type = 'gb'; + + public function show_type() { + return plugin_lang_get( 'gitbucket' ); + } + + public function show_changeset( $p_repo, $p_changeset ) { + $t_ref = substr( $p_changeset->revision, 0, 8 ); + $t_branch = $p_changeset->branch; + + return "$t_branch $t_ref"; + } + + public function show_file( $p_repo, $p_changeset, $p_file ) { + return "$p_file->action - $p_file->filename"; + } + + public function url_repo( $p_repo, $p_changeset=null ) { + if( empty( $p_repo->info ) ) { + return ''; + } + $t_username = $p_repo->info['hub_username']; + $t_reponame = $p_repo->info['hub_reponame']; + $t_ref = ""; + + if ( !is_null( $p_changeset ) ) { + $t_ref = "/tree/$p_changeset->revision"; + } + + return $p_repo->info['hub_url'] . "/$t_username/$t_reponame$t_ref"; + } + + public function url_changeset( $p_repo, $p_changeset ) { + $t_username = $p_repo->info['hub_username']; + $t_reponame = $p_repo->info['hub_reponame']; + $t_ref = $p_changeset->revision; + + return $p_repo->info['hub_url'] . "/$t_username/$t_reponame/commit/$t_ref"; + } + + public function url_file( $p_repo, $p_changeset, $p_file ) { + $t_username = $p_repo->info['hub_username']; + $t_reponame = $p_repo->info['hub_reponame']; + $t_ref = $p_changeset->revision; + $t_filename = $p_file->filename; + + return $p_repo->info['hub_url'] . "/$t_username/$t_reponame/blob/$t_ref/$t_filename"; + } + + public function url_diff( $p_repo, $p_changeset, $p_file ) { + $t_username = $p_repo->info['hub_username']; + $t_reponame = $p_repo->info['hub_reponame']; + $t_ref = $p_changeset->revision; + $t_filename = $p_file->filename; + + return $p_repo->info['hub_url'] . "/$t_username/$t_reponame/commit/$t_ref"; + } + + public function update_repo_form( $p_repo ) + { + $t_hub_username = null; + $t_hub_reponame = null; + $t_hub_url = null; + $t_hub_pwd = null; + + + if (isset($p_repo->info['hub_username'])) { + $t_hub_username = $p_repo->info['hub_username']; + } + + if (isset($p_repo->info['hub_reponame'])) { + $t_hub_reponame = $p_repo->info['hub_reponame']; + } + + if (isset($p_repo->info['hub_pwd'])) { + $t_hub_pwd = $p_repo->info['hub_pwd']; + } + + if (isset($p_repo->info['hub_url'])) { + $t_hub_url = $p_repo->info['hub_url']; + } + + if (isset($p_repo->info['master_branch'])) { + $t_master_branch = $p_repo->info['master_branch']; + } else { + $t_master_branch = 'master'; + } + ?> + + > + + + + + > + + + + + + > + + + + + + > + + + + + + + + + + > + + + + info['hub_url'] = $f_hub_url; + + $p_repo->info['hub_username'] = $f_hub_username; + $p_repo->info['hub_pwd'] = $f_hub_pwd; + + $p_repo->info['hub_reponame'] = $f_hub_reponame; + + $p_repo->info['master_branch'] = $f_master_branch; + + return $p_repo; + } + + private function api_uri( $p_repo, $p_path ) { + $t_uri = $p_repo->info['hub_url'] . '/api/v3/' . $p_path; + + return $t_uri; + } + + private function api_json_url( $p_repo, $p_url ) { + $t_data = $this->url_get( $p_repo, $p_url ); + $t_json = json_decode( utf8_encode( $t_data ) ); + return $t_json; + } + + public function precommit() { + $f_payload = gpc_get_string( 'payload', null ); + if ( is_null( $f_payload ) ) { + return; + } +/* + if ( false === stripos( $f_payload, 'github.com' ) ) { + return; + } +*/ + $t_data = json_decode( $f_payload, true ); + $t_reponame = $t_data['repository']['name']; + + $t_repo_table = plugin_table( 'repository', 'Source' ); + + $t_query = "SELECT * FROM $t_repo_table WHERE info LIKE " . db_param(); + $t_result = db_query_bound( $t_query, array( '%' . $t_reponame . '%' ) ); + + if ( db_num_rows( $t_result ) < 1 ) { + return; + } + + while ( $t_row = db_fetch_array( $t_result ) ) { + $t_repo = new SourceRepo( $t_row['type'], $t_row['name'], $t_row['url'], $t_row['info'] ); + $t_repo->id = $t_row['id']; + + if ( $t_repo->info['hub_reponame'] == $t_reponame ) { + return array( 'repo' => $t_repo, 'data' => $t_data ); + } + } + + return; + } + + public function commit( $p_repo, $p_data ) { + $t_commits = array(); + + foreach( $p_data['commits'] as $t_commit ) { + $t_commits[] = $t_commit['id']; + } + + $t_refData = explode( '/',$p_data['ref'] ); + $t_branch = $t_refData[2]; + + return $this->import_commits( $p_repo, $t_commits, $t_branch ); + } + + public function import_full( $p_repo ) { + echo '
';
+
+		$t_branch = $p_repo->info['master_branch'];
+		if ( is_blank( $t_branch ) ) {
+			$t_branch = 'master';
+		}
+
+		if ($t_branch != '*')
+		{
+			$t_branches = array_map( 'trim', explode( ',', $t_branch ) );
+		}
+		else
+		{
+
+			$t_username = $p_repo->info['hub_username'];
+			$t_reponame = $p_repo->info['hub_reponame'];
+
+			$t_uri = $this->api_uri( $p_repo, "repos/$t_username/$t_reponame/branches" );
+			$t_json = $this->api_json_url( $p_repo, $t_uri );
+
+			$t_branches = array();
+			foreach ($t_json as $t_branch)
+			{
+				$t_branches[] = $t_branch->name;
+			}
+		}
+		$t_changesets = array();
+
+		$t_changeset_table = plugin_table( 'changeset', 'Source' );
+
+		foreach( $t_branches as $t_branch ) {
+			$t_query = "SELECT parent FROM $t_changeset_table
+				WHERE repo_id=" . db_param() . ' AND branch=' . db_param() .
+				'ORDER BY timestamp ASC';
+			$t_result = db_query_bound( $t_query, array( $p_repo->id, $t_branch ), 1 );
+
+			$t_commits = array( $t_branch );
+
+			if ( db_num_rows( $t_result ) > 0 ) {
+				$t_parent = db_result( $t_result );
+				echo "Oldest '$t_branch' branch parent: '$t_parent'\n";
+
+				if ( !empty( $t_parent ) ) {
+					$t_commits[] = $t_parent;
+				}
+			}
+
+			$t_changesets = array_merge( $t_changesets, $this->import_commits( $p_repo, $t_commits, $t_branch ) );
+		}
+
+		echo '
'; + + return $t_changesets; + } + + public function import_latest( $p_repo ) { + return $this->import_full( $p_repo ); + } + + public function import_commits( $p_repo, $p_commit_ids, $p_branch='' ) { + static $s_parents = array(); + static $s_counter = 0; + + $t_username = $p_repo->info['hub_username']; + $t_reponame = $p_repo->info['hub_reponame']; + + if ( is_array( $p_commit_ids ) ) { + $s_parents = array_merge( $s_parents, $p_commit_ids ); + } else { + $s_parents[] = $p_commit_ids; + } + + $t_changesets = array(); + + while( count( $s_parents ) > 0 && $s_counter < 200 ) { + $t_commit_id = array_shift( $s_parents ); + + echo "Retrieving $t_commit_id ... "; + $t_uri = $this->api_uri( $p_repo, "repos/$t_username/$t_reponame/commits/$t_commit_id" ); + $t_json = $this->api_json_url( $p_repo, $t_uri ); + + if ( false === $t_json || is_null( $t_json ) ) { + # Some error occured retrieving the commit + echo "failed.\n"; + continue; + } else if ( !property_exists( $t_json, 'sha' ) ) { + echo "failed ($t_json->message).\n"; + continue; + } + + list( $t_changeset, $t_commit_parents ) = $this->json_commit_changeset( $p_repo, $t_json, $p_branch ); + if ( $t_changeset ) { + $t_changesets[] = $t_changeset; + } + + $s_parents = array_merge( $s_parents, $t_commit_parents ); + } + + $s_counter = 0; + return $t_changesets; + } + + private function json_commit_changeset( $p_repo, $p_json, $p_branch='' ) { + + echo "processing $p_json->sha ... "; + if ( !SourceChangeset::exists( $p_repo->id, $p_json->sha ) ) { + $t_parents = array(); + foreach( $p_json->parents as $t_parent ) { + $t_parents[] = $t_parent->sha; + } + + $t_changeset = new SourceChangeset( + $p_repo->id, + $p_json->sha, + $p_branch, + date( 'Y-m-d H:i:s', strtotime( $p_json->commit->author->date ) ), + $p_json->commit->author->name, + $p_json->commit->message + ); + + if ( count( $p_json->parents ) > 0 ) { + $t_parent = $p_json->parents[0]; + $t_changeset->parent = $t_parent->sha; + } + + $t_changeset->author_email = $p_json->commit->author->email; + $t_changeset->committer = $p_json->commit->committer->name; + $t_changeset->committer_email = $p_json->commit->committer->email; + + if ( isset( $p_json->files ) ) { + foreach ( $p_json->files as $t_file ) { + switch ( $t_file->status ) { + case 'added': + $t_changeset->files[] = new SourceFile( 0, '', $t_file->filename, 'add' ); + break; + case 'modified': + $t_changeset->files[] = new SourceFile( 0, '', $t_file->filename, 'mod' ); + break; + case 'removed': + $t_changeset->files[] = new SourceFile( 0, '', $t_file->filename, 'rm' ); + break; + } + } + } + + $t_changeset->save(); + + echo "saved.\n"; + return array( $t_changeset, $t_parents ); + } else { + echo "already exists.\n"; + return array( null, array() ); + } + } + +/* + public static function url_post( $p_url, $p_post_data ) { + $t_post_data = http_build_query( $p_post_data ); + + # Use the PHP cURL extension + if( function_exists( 'curl_init' ) ) { + $t_curl = curl_init( $p_url ); + curl_setopt( $t_curl, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $t_curl, CURLOPT_POST, true ); + curl_setopt( $t_curl, CURLOPT_POSTFIELDS, $t_post_data ); + + $t_data = curl_exec( $t_curl ); + curl_close( $t_curl ); + + return $t_data; + } else { + # Last resort system call + $t_url = escapeshellarg( $p_url ); + $t_post_data = escapeshellarg( $t_post_data ); + return shell_exec( 'curl ' . $t_url . ' -d ' . $t_post_data ); + } + } +*/ +//https://github.z-web.eu/mira/z-web.eu/blob/b7d70a2c2bc8d74afb134a9aade416d37e29d63c/bin/update_worktime.php +//https://github.z-web.eu/mira/z-web.eu/tree/b7d70a2c2bc8d74afb134a9aade416d37e29d63c/bin/update_worktime.php + +//Fix committed to $1 branch.
+//Revission: revission
+//Time: $3
+//Changeset:
+//http://bt.z-web.eu/plugin.php?page=Source/view&id=$6
+//Commit message:
$4 + +// https://github.z-web.eu/mira/z-web.eu/commit/af800c027fdb9ade8abed48b01027f23f9f7ce40 +// https://github.z-web.eu/git/mira/z-web.eu/commit/b7d70a2c2bc8d74afb134a9aade416d37e29d63c + + + public function url_get( $p_repo, $p_url ) { + + $t_user_pass = $p_repo->info['hub_username'] . ':' . $p_repo->info['hub_pwd']; + # Use the PHP cURL extension + if( function_exists( 'curl_init' ) ) { + $t_curl = curl_init( $p_url ); + # cURL options + $t_curl_opt[CURLOPT_USERPWD] = $t_user_pass; + $t_curl_opt[CURLOPT_RETURNTRANSFER] = true; + + $t_vers = curl_version(); + $t_curl_opt[CURLOPT_USERAGENT] = + 'mantisbt/' . MANTIS_VERSION . ' php-curl/' . $t_vers['version']; + + # Set the options + curl_setopt_array( $t_curl, $t_curl_opt ); + + # Retrieve data + $t_data = curl_exec( $t_curl ); + curl_close( $t_curl ); + + if( $t_data !== false ) { + return $t_data; + } + } + # Last resort system call + $t_url = escapeshellarg( $p_url ); //use -u user:pass + return shell_exec( 'curl -u ' . $t_user_pass . ' ' . $t_url ); + } + + +} + + diff --git a/SourceGitBucket/lang/strings_english.txt b/SourceGitBucket/lang/strings_english.txt new file mode 100644 index 0000000..539e13a --- /dev/null +++ b/SourceGitBucket/lang/strings_english.txt @@ -0,0 +1,33 @@ +(no spaces; must match the name as received from the webservice\'s payload)'; +$s_plugin_SourceGitBucket_hub_url = 'GitBucket Repository URL'; +$s_plugin_SourceGitBucket_hub_pwd = 'GitBucket password'; + +$s_plugin_SourceGitBucket_hub_app_client_id = 'GitHub Application Client ID
This is required for private repositories and also allows to get around the Rate Limit when importing data.
Create a new GitHub Application if needed.
'; +$s_plugin_SourceGitBucket_hub_app_secret = 'GitHub Application Secret'; +$s_plugin_SourceGitBucket_hub_app_access_token = 'GitHub Application Access Token'; + +$s_plugin_SourceGitBucket_master_branch = 'Primary Branches
(comma-separated list)'; + + +$s_plugin_SourceGitBucket_hub_app_client_id_secret_missing = 'You must first enter the GitHub Application Client ID & Secret and update the repository before you can authorize.'; +$s_plugin_SourceGitBucket_hub_app_authorize = 'Click to Authorize'; +$s_plugin_SourceGitBucket_hub_app_authorized = 'Authorized'; +$s_plugin_SourceGitBucket_repo_authorized = '

MantisBT is now authorized to access this GitHub repository.

'; +$s_plugin_SourceGitBucket_repo_authorization_failed = '

Sorry, MantisBT could not be authorized to access this GitHub repository.

'; +$s_plugin_SourceGitBucket_oauth_authorization = 'GitHub OAuth Authorization'; + + + +$s_plugin_SourceGitBucket_back_repo = 'Back to Repository'; +$s_plugin_SourceGitBucket_error_invalid_branch = 'Invalid character in Primary Branch'; diff --git a/SourceGitBucket/pages/oauth_authorize.php b/SourceGitBucket/pages/oauth_authorize.php new file mode 100644 index 0000000..659e464 --- /dev/null +++ b/SourceGitBucket/pages/oauth_authorize.php @@ -0,0 +1,39 @@ + + + + + + + + + + + + + +
id, plugin_lang_get( 'back_repo' ) ) ?>
+ + \ No newline at end of file