This is the official SDK for the SensioLabsInsight API.
To install the SDK, run the command below and you will get the latest version:
composer require sensiolabs/insight
The easiest way to use the SensioLabsInsight API is via the built-in command line tool.
A phar version of the command line tool exists to avoid installation of this project. Download it, then use it like the command line tool:
$ curl -o insight.phar -s http://get.insight.sensiolabs.com/insight.phar
# or
$ wget http://get.insight.sensiolabs.com/insight.phar
# Then
$ php insight.phar
List all the projects in your account:
$ php insight.phar projects
The first time, you will be prompted for your SensioLabsInsight API key and
user UUID (which can be found under the "Account" section on the website).
These information are then stored locally, but can still be overridden via the
--api-token
and --user-uuid
options.
To run an analysis:
$ php insight.phar analyze UUID
where UUID
is the UUID of the project you want to analyze (the UUIDs are
listed by the projects
command).
To export an analysis report:
$ php insight.phar analysis UUID --format="xml" # or --format="json" or --format="pmd"
use SensioLabs\Insight\Sdk\Api;
$api = new Api(array(
'api_token' => 'your api token',
'user_uuid' => 'your user uuid',
));
If you want, you can give a Guzzle\Http\Client
and a
Psr\Log\LoggerInterface
to this library:
use Guzzle\Http\Client;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$config = array(
'api_token' => 'your api token',
'user_uuid' => 'your user uuid',
)
$client = new Client();
$logger = new Logger('insight-sdk');
$logger->pushHandler(new StreamHandler(__DIR__.'/insight-sdk.log', Logger::DEBUG));
$api = new Api($config, $client, null, $logger);
You can also give a cache
folder. The SDK will only cache metadatas for
serialization. And you can also give a debug
flag:
$api = new Api(array(
'api_token' => 'your api token',
'user_uuid' => 'your user uuid',
'cache' => __DIR__.'/cache/insight',
'debug' => true,
));
$api->getProjects();
$api->getProjects(2); // For the second page
$project = $api->getProject('project uuid');
$api->updateProject($project);
Note: If something went wrong, see Error management section
use SensioLabs\Insight\Sdk\Model\Project;
$project = new Project();
$project
->setName('Foo')
->setDescription('Foo')
->setType(TYPE_WEBSITE::TYPE_WEBSITE)
;
$api->createProject($project)
Note: If something went wrong, see Error management section
// on the default branch
$api->analyze('project uuid', 'master');
// for a specific branch or reference
$api->analyze('project uuid', '1.0');
$api->getAnalyses('project uuid');
$api->getAnalysis('project uuid', 'analysis id');
$api->getAnalysisStatus('project uuid', 'analysis id');
If something went wrong, an
SensioLabs\Insight\Sdk\Exception\ExceptionInterface
will be throw:
ApiClientException
If you did something wrong. This exception contains the previous exception throw by guzzle. You can easily check if it is a:- 403: In this case, check your credentials
- 404: In this case, check your request
- 400: In this case, check the data sent. In this case, the Exception will
contains a
SensioLabs\Insight\Sdk\Model\Error
object. Which will contains all form errors.
ApiServerException
If something went wrong with the API.
Thanks to Jenkins PMD Plugin and SensioLabsInsight SDK PMD output you can easily embed SensioLabsInsight reports into your build workflow, following these steps:
It is assumed you already have your project up and building in Jenkins and SensioLabsInsight SDK installed
-
Retrieve your
SensioLabsInsight API Token
,User UUID
andProject UUID
on your account page -
Install the Jenkins
PMD plugin
: How to install a jenkins plugin -
Optionally you can also install the
EnvInject Plugin
-
Edit your project configuration
-
If you have EnvInject Plugin installed, enabled
Set environment variables
then add and adapt the following lines to variables name:INSIGHT_API_TOKEN="Your API Token" INSIGHT_USER_UUID="Your user UUID" INSIGHT_PROJECT_UUID="Your project UUID"
-
Add a
Execute shell
build step -
In the new shell step add and adapt the following command (if you don't have EnvInject plugin, replace variables by plain values):
/path/to/insight-sdk/bin/insight analysis \ --user-uuid $INSIGHT_USER_UUID \ --api-token $INSIGHT_API_TOKEN \ $INSIGHT_PROJECT_UUID --format=pmd > insight-pmd.xml
-
Enable
Publish PMD analysis results
usinginsight-pmd.xml
as PMD result filename -
Optionally, you can add the
insight-pmd.xml
file to artifacts to archive -
Save and build!
This library is licensed under the MIT license.