Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
renovate
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
Renovate Bot
renovate
Commits
976a5a1a
Unverified
Commit
976a5a1a
authored
1 year ago
by
Adam Setch
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
feat(bitbucket): add paginate http option (#22135)
parent
2aae5118
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/util/http/bitbucket.spec.ts
+30
-0
30 additions, 0 deletions
lib/util/http/bitbucket.spec.ts
lib/util/http/bitbucket.ts
+73
-7
73 additions, 7 deletions
lib/util/http/bitbucket.ts
with
103 additions
and
7 deletions
lib/util/http/bitbucket.spec.ts
+
30
−
0
View file @
976a5a1a
...
@@ -55,4 +55,34 @@ describe('util/http/bitbucket', () => {
...
@@ -55,4 +55,34 @@ describe('util/http/bitbucket', () => {
statusCode
:
200
,
statusCode
:
200
,
});
});
});
});
it
(
'
paginates
'
,
async
()
=>
{
httpMock
.
scope
(
baseUrl
)
.
get
(
'
/some-url
'
)
.
reply
(
200
,
{
values
:
[
'
a
'
],
page
:
'
1
'
,
next
:
`
${
baseUrl
}
/some-url?page=2`
,
})
.
get
(
'
/some-url?page=2
'
)
.
reply
(
200
,
{
values
:
[
'
b
'
,
'
c
'
],
page
:
'
2
'
,
next
:
`
${
baseUrl
}
/some-url?page=3`
,
})
.
get
(
'
/some-url?page=3
'
)
.
reply
(
200
,
{
values
:
[
'
d
'
],
page
:
'
3
'
,
});
const
res
=
await
api
.
getJson
(
'
some-url
'
,
{
paginate
:
true
});
expect
(
res
.
body
).
toEqual
({
page
:
'
1
'
,
pagelen
:
4
,
size
:
4
,
values
:
[
'
a
'
,
'
b
'
,
'
c
'
,
'
d
'
],
next
:
undefined
,
});
});
});
});
This diff is collapsed.
Click to expand it.
lib/util/http/bitbucket.ts
+
73
−
7
View file @
976a5a1a
import
type
{
HttpOptions
,
HttpResponse
,
InternalHttpOptions
}
from
'
./types
'
;
import
is
from
'
@sindresorhus/is
'
;
import
type
{
PagedResult
}
from
'
../../modules/platform/bitbucket/types
'
;
import
{
parseUrl
,
resolveBaseUrl
}
from
'
../url
'
;
import
type
{
HttpOptions
,
HttpResponse
}
from
'
./types
'
;
import
{
Http
}
from
'
.
'
;
import
{
Http
}
from
'
.
'
;
let
baseUrl
=
'
https://api.bitbucket.org/
'
;
let
baseUrl
=
'
https://api.bitbucket.org/
'
;
...
@@ -7,16 +10,79 @@ export const setBaseUrl = (url: string): void => {
...
@@ -7,16 +10,79 @@ export const setBaseUrl = (url: string): void => {
baseUrl
=
url
;
baseUrl
=
url
;
};
};
export
class
BitbucketHttp
extends
Http
{
export
interface
BitbucketHttpOptions
extends
HttpOptions
{
constructor
(
type
=
'
bitbucket
'
,
options
?:
HttpOptions
)
{
paginate
?:
boolean
;
}
export
class
BitbucketHttp
extends
Http
<
BitbucketHttpOptions
>
{
constructor
(
type
=
'
bitbucket
'
,
options
?:
BitbucketHttpOptions
)
{
super
(
type
,
options
);
super
(
type
,
options
);
}
}
protected
override
request
<
T
>
(
protected
override
async
request
<
T
>
(
url
:
string
|
URL
,
path
:
string
,
options
?:
Internal
HttpOptions
options
?:
Bitbucket
HttpOptions
):
Promise
<
HttpResponse
<
T
>>
{
):
Promise
<
HttpResponse
<
T
>>
{
const
opts
=
{
baseUrl
,
...
options
};
const
opts
=
{
baseUrl
,
...
options
};
return
super
.
request
<
T
>
(
url
,
opts
);
const
result
=
await
super
.
request
<
T
>
(
path
,
opts
);
if
(
opts
.
paginate
&&
isPagedResult
(
result
.
body
))
{
const
resultBody
=
result
.
body
as
PagedResult
<
T
>
;
let
nextPage
=
getPageFromURL
(
resultBody
.
next
);
while
(
is
.
nonEmptyString
(
nextPage
))
{
const
nextPath
=
getNextPagePath
(
path
,
nextPage
);
// istanbul ignore if
if
(
is
.
nullOrUndefined
(
nextPath
))
{
break
;
}
const
nextResult
=
await
super
.
request
<
PagedResult
<
T
>>
(
nextPath
,
options
);
resultBody
.
values
.
push
(...
nextResult
.
body
.
values
);
nextPage
=
getPageFromURL
(
nextResult
.
body
?.
next
);
}
// Override other page-related attributes
resultBody
.
pagelen
=
resultBody
.
values
.
length
;
resultBody
.
size
=
resultBody
.
values
.
length
;
resultBody
.
next
=
undefined
;
}
return
result
;
}
}
}
}
function
getPageFromURL
(
url
:
string
|
undefined
):
string
|
null
{
const
resolvedURL
=
parseUrl
(
url
);
if
(
is
.
nullOrUndefined
(
resolvedURL
))
{
return
null
;
}
return
resolvedURL
.
searchParams
.
get
(
'
page
'
);
}
function
getNextPagePath
(
path
:
string
,
nextPage
:
string
):
string
|
null
{
const
resolvedURL
=
parseUrl
(
resolveBaseUrl
(
baseUrl
,
path
));
// istanbul ignore if
if
(
is
.
nullOrUndefined
(
resolvedURL
))
{
return
null
;
}
resolvedURL
.
searchParams
.
set
(
'
page
'
,
nextPage
);
return
resolvedURL
.
toString
();
}
function
isPagedResult
(
obj
:
any
):
obj
is
PagedResult
{
return
is
.
nonEmptyObject
(
obj
)
&&
Array
.
isArray
(
obj
.
values
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment