Skip to content

Commit

Permalink
Merge pull request #31 from mohamed-abdul-fattah/issue-23
Browse files Browse the repository at this point in the history
Issue-23
  • Loading branch information
mohamed-abdul-fattah authored Mar 7, 2020
2 parents dddfc0c + 71bf03c commit d37d6b1
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 25 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [0.2.0] - 2020-03-08
## Added
- Check for updates on logs sync

## [0.1.0] - 2020-03-07
### Added
- Setup command
- Connect command
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Jira Logger
>A command line tool to automate Jira logging
![Release](https://img.shields.io/badge/release-1.0.0-blue.svg)
![Release](https://img.shields.io/badge/release-0.2.0-blue.svg)
![PHP](https://img.shields.io/badge/php-^7.0-green)
![License](https://img.shields.io/badge/license-MIT-yellowgreen.svg)
## Table of Content
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
}
],
"require": {
"php": ">=7.0",
"php": "^7.0",
"symfony/console": "^5.0",
"psr/http-message": "^1.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.5",
"ext-pdo": "*",
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^0.12.14"
"ext-pdo": "*"
},
"autoload": {
"psr-4": {
Expand All @@ -27,6 +25,8 @@
"files": ["src/helpers.php"]
},
"require-dev": {
"phpunit/phpunit": "^8.5"
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^0.12.14"
}
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
build:
context: .
dockerfile: Dockerfile
image: jira-logger-cli:1.2.0
image: jira-logger-cli:1.0.0
container_name: jiralogger-cli
tty: true
stdin_open: true
Expand Down
2 changes: 1 addition & 1 deletion jiralogger
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require __DIR__ . '/vendor/autoload.php';
*
* @see https://semver.org/
*/
define('APP_VERSION', '1.0.0');
define('APP_VERSION', '0.2.0');

/**
* Application name to be displayed on listing commands
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
</testsuites>
<php>
<env name="ENV" value="testing" />
<const name="APP_VERSION" value="0.2.0" />
</php>
</phpunit>
18 changes: 18 additions & 0 deletions src/Commands/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (empty($tasks)) {
$output->writeln('<info>Logs are up to date.</info>');
$this->checkUpdates($output);
return self::EXIT_SUCCESS;
}

Expand Down Expand Up @@ -97,6 +98,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("\n<info>Logs synced successfully</info>");
$table->render();

$this->checkUpdates($output);
return self::EXIT_SUCCESS;
}

/**
* Check whether there is a new version released or not
*
* @param OutputInterface $output
*/
private function checkUpdates(OutputInterface $output): void
{
$output->writeln("\n<comment>Checking for updates...</comment>");
$release = $this->connect->checkUpdates();
if ($release === APP_VERSION) {
$output->writeln('<info>All is up to date</info>');
} else {
$output->writeln("New version has been released <info>{$release}</info>");
}
}
}
4 changes: 2 additions & 2 deletions src/Http/IRequestDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ interface IRequestDispatcher
/**
* Set base URI for dispatcher
*
* @param string $baseUri
* @param string|null $baseUri
* @return $this
*/
public function setBaseUri(string $baseUri);
public function setBaseUri($baseUri);

/**
* Set saved session ID
Expand Down
12 changes: 11 additions & 1 deletion src/Http/MockResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
*/
class MockResponse implements ResponseInterface
{
/**
* @var string
*/
private $message;

public function __construct(string $responseMessage)
{
$this->message = $responseMessage;
}

public function getStatusCode()
{
// Placeholder
Expand Down Expand Up @@ -75,7 +85,7 @@ public function withHeader($name, $value)

public function getBody()
{
return '{"session": {"value": "sessionId"}}';
return $this->message;
}

public function withBody(StreamInterface $body)
Expand Down
21 changes: 13 additions & 8 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public function __construct()
/**
* Set dispatcher base URI
*
* @param string $baseUri
* @param string|null $baseUri
* @return $this
*/
public function setBaseUri(string $baseUri)
public function setBaseUri($baseUri)
{
$this->baseUri = $baseUri;

Expand Down Expand Up @@ -123,7 +123,7 @@ public function json(
}

$jar = null;
if (! empty($this->sessionId)) {
if (! empty($this->sessionId) && ! empty($this->baseUri)) {
$jar = new CookieJar;
$jar = $jar->fromArray(['JSESSIONID' => $this->sessionId], $this->domain());
}
Expand All @@ -140,12 +140,17 @@ public function json(
);
} catch (Exception $e) {
if ($e->getCode() === 0) {
throw new ConnectionException('Could not resolve host!. Please check your internet connection.', $e->getCode());
throw new ConnectionException(
'Could not resolve host!. Please check your internet connection.',
$e->getCode()
);
} elseif ($e->getCode() === IResponse::HTTP_NOT_FOUND) {
throw new ConnectionException('404 Not Found!. Please, re-run `setup` command with proper platform URI', $e->getCode());
} elseif (
$e->getCode() === IResponse::HTTP_UNAUTHENTICATED ||
$e->getCode() === IResponse::HTTP_UNAUTHORIZED
throw new ConnectionException(
'404 Not Found!. The requested URL is not valid.',
$e->getCode()
);
} elseif ($e->getCode() === IResponse::HTTP_UNAUTHENTICATED
|| $e->getCode() === IResponse::HTTP_UNAUTHORIZED
) {
throw new ConnectionException('Unauthorized!. Wrong username or password.', $e->getCode());
} else {
Expand Down
9 changes: 7 additions & 2 deletions src/Http/TestRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function postJson(string $uri, array $params = [], array $headers = [])
return $this->json(self::HTTP_POST, $uri, $params, $headers);
}

public function setBaseUri(string $baseUri)
public function setBaseUri($baseUri)
{
return $this;
}
Expand All @@ -39,7 +39,12 @@ public function json(
array $params = [],
array $headers = []
) {
$response = new MockResponse();
// Workaround until DI is introduced
if (empty($this->baseUri)) {
$response = new MockResponse('[{"name": "0.2.0"}]');
} else {
$response = new MockResponse('{"session": {"value": "sessionId"}}');
}

return new Response($response);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Services/Connect/IConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ public function syncLog(Task $task): array;
* @throws ConnectionException
*/
public function checkPlatformConnection(): void;

/**
* Check whether there are a new version released or not
*
* @return string
*/
public function checkUpdates(): string;
}
36 changes: 33 additions & 3 deletions src/Services/Connect/JiraConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
*/
class JiraConnect implements IConnect
{
/**
* Github releases/tags URL
*/
const TAGS_URL = 'https://api.github.com/repos/mohamed-abdul-fattah/jira-logger/tags';
const GITHUB_VERSION = 'application/vnd.github.v3+json';

/**
* @var Jira
*/
Expand Down Expand Up @@ -160,9 +166,8 @@ public function checkPlatformConnection(): void
throw new ConnectionException(
'Cannot connect to Jira server. Please, re-run `setup` with proper platform URI'
);
} elseif (
$e->getCode() === IResponse::HTTP_UNAUTHENTICATED ||
$e->getCode() === IResponse::HTTP_UNAUTHORIZED
} elseif ($e->getCode() === IResponse::HTTP_UNAUTHENTICATED
|| $e->getCode() === IResponse::HTTP_UNAUTHORIZED
) {
throw new ConnectionException(
'Invalid credentials. Please, run `connect` command to login to Jira'
Expand All @@ -173,6 +178,31 @@ public function checkPlatformConnection(): void
}
}

/**
* Check whether there are a new version released or not
*
* @return string
*/
public function checkUpdates(): string
{
// Unset base URI to request an external URI
$this->dispatcher->setBaseUri(null);
/** @var IResponse $response */
$response = $this->dispatcher->getJson(
self::TAGS_URL,
[],
['Accept' => self::GITHUB_VERSION]
);

$versions = array_column($response->body(), 'name');
$release = end($versions);
if (version_compare($release, APP_VERSION, '>')) {
return $release;
}

return APP_VERSION;
}

/**
* @param int $errorCode
* @return string
Expand Down

0 comments on commit d37d6b1

Please sign in to comment.