Skip to content

Commit 8eb4c61

Browse files
authored
Merge pull request #240 from aik099/download-attachment-test
Fixed the "Api:downloadAttachment" method + added tests
2 parents 52b5bd6 + faa1474 commit 8eb4c61

File tree

7 files changed

+83
-9
lines changed

7 files changed

+83
-9
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ This project adheres to [Semantic Versioning](https://semver.org/).
77
...
88

99
### Changed
10-
...
10+
- The `Api::downloadAttachment` method now throws an exception, when attempting to download from a non-Jira website by [@aik099] (#240).
1111

1212
### Removed
1313
...
1414

1515
### Fixed
1616
- The `CurlClient` in combination with cURL version < 7.33.0 was getting `426 Upgrade Required` error on any request to the Atlassian Cloud servers by [@aik099] (#239).
1717
- The `PHPClient` on PHP < 8.0 was getting `426 Upgrade Required` error on any request to the Atlassian Cloud servers by [@aik099] (#239).
18+
- The `Api::downloadAttachment` method wasn't working with `CurlClient` for Atlassian Cloud servers by [@aik099] (#240).
1819

1920
## [2.0.0-B1] - 2025-01-04
2021
### Added

src/Jira/Api.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use chobie\Jira\Api\Authentication\AuthenticationInterface;
3030
use chobie\Jira\Api\Client\ClientInterface;
3131
use chobie\Jira\Api\Client\CurlClient;
32+
use chobie\Jira\Api\Exception;
3233
use chobie\Jira\Api\Result;
3334

3435
class Api
@@ -802,23 +803,29 @@ public function api(
802803
/**
803804
* Downloads attachment.
804805
*
805-
* @param string $url URL.
806+
* @param string $url URL in "https://your-jira-project.net/rest/api/2/attachment/content/{id}" format.
806807
*
807808
* @return array|string
809+
* @throws Exception When a download URL is coming from a different Jira instance.
810+
* @link https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issue-attachments/#api-rest-api-2-attachment-content-id-get
808811
*/
809812
public function downloadAttachment($url)
810813
{
811-
$result = $this->client->sendRequest(
814+
$relative_url = preg_replace('/^' . preg_quote($this->getEndpoint(), '/') . '/', '', $url);
815+
816+
if ( $relative_url === $url ) {
817+
throw new Exception('The download url is coming from the different Jira instance.');
818+
}
819+
820+
return $this->client->sendRequest(
812821
self::REQUEST_GET,
813-
$url,
822+
$relative_url,
814823
array(),
815-
null,
824+
$this->getEndpoint(),
816825
$this->authentication,
817826
true,
818-
false
827+
false // Needed for test suite to work (probably Prophecy glitch).
819828
);
820-
821-
return $result;
822829
}
823830

824831
/**

src/Jira/Api/Client/CurlClient.php

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public function sendRequest(
9797
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
9898
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
9999
curl_setopt($curl, CURLOPT_VERBOSE, $debug);
100+
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
100101

101102
if ( $is_file ) {
102103
if ( defined('CURLOPT_SAFE_UPLOAD') && PHP_VERSION_ID < 70000 ) {

src/Jira/Api/Client/PHPClient.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ public function sendRequest(
122122
'http' => array(
123123
'method' => $method,
124124
'header' => implode("\r\n", $header),
125-
'ignore_errors' => true,
125+
'ignore_errors' => true, // Not needed since PHP 5.1.0.
126126
'protocol_version' => 1.1, // Not needed since PHP 8.0.
127+
'follow_location' => 1, // Not needed since PHP 5.1.0.
127128
),
128129
);
129130

tests/Jira/Api/Client/AbstractClientTestCase.php

+33
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,39 @@ public static function fileUploadDataProvider()
144144
);
145145
}
146146

147+
public function testFileDownloadWithFollowLocation()
148+
{
149+
$result = $this->client->sendRequest(
150+
Api::REQUEST_GET,
151+
'/tests/download_attachment.php?download_url=' . rtrim($_SERVER['REPO_URL'], '/') . '/LICENSE.md',
152+
array(),
153+
rtrim($_SERVER['REPO_URL'], '/'),
154+
new Basic('user1', 'pass1'),
155+
true
156+
);
157+
158+
$this->assertStringEqualsFile(
159+
$this->getProjectRoot() . '/LICENSE.md',
160+
$result
161+
);
162+
}
163+
164+
/**
165+
* Returns project root.
166+
*
167+
* @return string
168+
*/
169+
protected function getProjectRoot()
170+
{
171+
$dir = __DIR__;
172+
173+
while ( !file_exists($dir . '/LICENSE.md') ) {
174+
$dir = dirname($dir);
175+
}
176+
177+
return $dir;
178+
}
179+
147180
public function testUnsupportedCredentialGiven()
148181
{
149182
$client_class_parts = explode('\\', get_class($this->client));

tests/Jira/ApiTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use chobie\Jira\Api;
77
use chobie\Jira\Api\Authentication\AuthenticationInterface;
8+
use chobie\Jira\Api\Exception;
89
use chobie\Jira\Api\Result;
910
use chobie\Jira\IssueType;
1011
use Prophecy\Prophecy\ObjectProphecy;
@@ -152,6 +153,33 @@ public function testReleaseVersionParameterMerging()
152153
$this->assertFalse($this->api->releaseVersion(111000, $release_date, array('test' => 'extra')));
153154
}
154155

156+
public function testDownloadAttachmentSuccessful()
157+
{
158+
$expected = 'file content';
159+
160+
$this->expectClientCall(
161+
Api::REQUEST_GET,
162+
'/rest/api/2/attachment/content/12345',
163+
array(),
164+
$expected,
165+
true
166+
);
167+
168+
$actual = $this->api->downloadAttachment(self::ENDPOINT . '/rest/api/2/attachment/content/12345');
169+
170+
if ( $actual !== null ) {
171+
$this->assertEquals($expected, $actual);
172+
}
173+
}
174+
175+
public function testDownloadAttachmentWithException()
176+
{
177+
$this->expectException(Exception::class);
178+
$this->expectExceptionMessage('The download url is coming from the different Jira instance.');
179+
180+
$this->api->downloadAttachment('https://other.jira-instance.com/rest/api/2/attachment/content/12345');
181+
}
182+
155183
/**
156184
* @dataProvider createRemoteLinkDataProvider
157185
*/

tests/download_attachment.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
header('Location: ' . $_GET['download_url'], true, 303);

0 commit comments

Comments
 (0)