Skip to content

Commit

Permalink
feat(BitbucketPullRequests): all pull requests (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
knowbased authored Feb 6, 2025
1 parent 2f17686 commit e676227
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/BitbucketPharoAPI-Tests/BitbucketPullRequestsTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,102 @@ BitbucketPullRequestsTest >> testActivitiesOfInRepositoryOfProject [
self assert: result equals: pullRequestActivity
]

{ #category : 'tests' }
BitbucketPullRequestsTest >> testAllSinceUntilWithParamsInRepositoryOfProject [

| hostUrl client endpoint bitbucketApi result response projectKey repositorySlug since until sinceAsTimestamp untilPlusOneDayAsTimestamp bitbucketPullRequests pullRequest1 pullRequest2 params |
"Given"
hostUrl := 'https://www.url.com'.
client := ZnClient new.

bitbucketApi := BitbucketApi new
bearerToken: 'token';
host: hostUrl;
client: client.

bitbucketPullRequests := BitbucketPullRequests new bitbucketApi:
bitbucketApi.

projectKey := 'OOO'.
repositorySlug := 'my project'.
since := '02-05-2025'.
until := '02-06-2025'.
params := { (#state -> 'all') } asDictionary.

sinceAsTimestamp := since asDate asDateAndTime asUnixTime * 1000.
untilPlusOneDayAsTimestamp := (until asDate asDateAndTime + 1 day)
asUnixTime * 1000.

endpoint := '/projects/' , projectKey , '/repos/' , repositorySlug
, '/pull-requests'.

pullRequest1 := { (#createdDate -> sinceAsTimestamp) } asDictionary.
pullRequest2 := { (#createdDate -> untilPlusOneDayAsTimestamp) }
asDictionary.


response := {
(#values -> {
pullRequest1.
pullRequest2 }).
(#isLastPage -> true) } asDictionary.

client stub get willReturn: (NeoJSONWriter toString: response).


"When"
result := bitbucketPullRequests
allSince: since
until: until
withParams: params
inRepository: repositorySlug
ofProject: projectKey.

"Then"
self assert: result size equals: 1.
self assert: result first equals: pullRequest1.
params keysAndValuesDo: [ :key :value |
self assert: (client request url query at: key) equals: value ]
]

{ #category : 'tests' }
BitbucketPullRequestsTest >> testAllWithParamsInRepositoryOfProject [

| hostUrl gitlabApi result client path bitbucketPullRequests projectKey repoSlug params pullRequest |
"Given"
hostUrl := 'www.url.com'.
client := ZnClient new.

gitlabApi := BitbucketApi new
bearerToken: 'token';
host: hostUrl;
client: client.

bitbucketPullRequests := BitbucketPullRequests new bitbucketApi:
gitlabApi.

projectKey := 'AAA'.
repoSlug := 'project'.
params := { #state -> 'all' } asDictionary.

pullRequest := { (#pr -> 'id') } asDictionary.

path := '/projects/' , projectKey , '/repos/' , repoSlug
, '/pull-requests'.

(bitbucketPullRequests stub getAll: path withParams: params)
willReturn: pullRequest.

"When"
result := bitbucketPullRequests
allWithParams: params
inRepository: repoSlug
ofProject: projectKey.

"Then"
self assert: result equals: pullRequest
]

{ #category : 'tests' }
BitbucketPullRequestsTest >> testCommitsOfInRepositoryOfProject [

Expand Down
50 changes: 50 additions & 0 deletions src/BitbucketPharoAPI/BitbucketPullRequests.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,56 @@ BitbucketPullRequests >> activitiesOf: pullRequestId inRepository: repositorySlu
^ self getAll: endpoint withParams: Dictionary new.
]

{ #category : 'api - get' }
BitbucketPullRequests >> allSince: since until: until withParams: paramsDictionary inRepository: repositorySlug ofProject: projectKey [
"GET /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests?direction&at&state&order&withAttributes&withProperties"

"There is no parameter in the API that allows us to directly get all the commits between two dates, so we have to do it manually."
| results lastDate endpoint pullRequests createdDateTimestamp |
bitbucketApi prepareClient.
endpoint := '/projects/' , projectKey , '/repos/' , repositorySlug
, '/pull-requests'.
bitbucketApi client path: bitbucketApi basePath , endpoint.

paramsDictionary keysAndValuesDo: [ :key :value |
bitbucketApi client queryAt: key put: value ].

pullRequests := OrderedCollection new.

[
results := bitbucketApi client get.
results := (NeoJSONReader on: results readStream) next.
pullRequests addAll: (results at: #values).
results at: #nextPageStart ifPresent: [
bitbucketApi client
queryAt: #start
put: (results at: #nextPageStart) ].

(results at: #isLastPage)
ifTrue: [ false ]
ifFalse: [
createdDateTimestamp := pullRequests last at: #createdDate.
lastDate := DateAndTime fromUnixTime: createdDateTimestamp / 1000.
since asDate <= lastDate ] ] whileTrue.

^ pullRequests select: [ :pullRequest |
| createdDate |
createdDate := DateAndTime fromUnixTime:
(pullRequest at: #createdDate) / 1000.
createdDate >= since asDate asDateAndTime and:
createdDate <= until asDate asDateAndTime ]
]

{ #category : 'api - get' }
BitbucketPullRequests >> allWithParams: paramsDictionary inRepository: repositorySlug ofProject: projectKey [
"GET /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests?direction&at&state&order&withAttributes&withProperties"

| endpoint |
endpoint := '/projects/', projectKey, '/repos/', repositorySlug, '/pull-requests'.

^self getAll: endpoint withParams: paramsDictionary.
]

{ #category : 'api - get' }
BitbucketPullRequests >> commitsOf: pullRequestId inRepository: repositorySlug ofProject: projectKey [
"GET /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/pull-requests/{pullRequestId}/commits"
Expand Down

0 comments on commit e676227

Please sign in to comment.