-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BitbucketCommits): get all commits methods (#13)
* feat(BitbucketCommits): allWithParams * feat(BitbucketCommits): allInBranch: * feat(BitbucketPharoApi): allSince:Until:
- Loading branch information
Showing
3 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/BitbucketPharoAPI-Tests/BitbucketCommitsTest.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
" | ||
A BitbucketCommitsTest is a test class for testing the behavior of BitbucketCommits | ||
" | ||
Class { | ||
#name : 'BitbucketCommitsTest', | ||
#superclass : 'TestCase', | ||
#category : 'BitbucketPharoAPI-Tests', | ||
#package : 'BitbucketPharoAPI-Tests' | ||
} | ||
|
||
{ #category : 'tests' } | ||
BitbucketCommitsTest >> testAllInBranchInRepositoryOfProject [ | ||
|
||
| hostUrl client bitbucketApi projectKey repositorySlug endpoint response result params bitbucketCommits branch | | ||
"Given" | ||
hostUrl := 'www.url.com'. | ||
client := Mock new. | ||
|
||
bitbucketApi := BitbucketApi new | ||
bearerToken: 'token'; | ||
host: hostUrl; | ||
client: client. | ||
|
||
bitbucketCommits := BitbucketCommits new bitbucketApi: bitbucketApi. | ||
|
||
projectKey := 'OOO'. | ||
repositorySlug := 'my project'. | ||
branch := 'dev'. | ||
|
||
params := { (#until -> branch) } asDictionary. | ||
|
||
endpoint := '/projects/' , projectKey , '/repos/' , repositorySlug | ||
, '/commits'. | ||
|
||
response := { (#commitId -> '1') } asDictionary. | ||
|
||
(bitbucketCommits stub getAll: endpoint withParams: params) | ||
willReturn: response. | ||
|
||
"When" | ||
result := bitbucketCommits | ||
allInBranch: branch | ||
inRepository: repositorySlug | ||
ofProject: projectKey. | ||
|
||
"Then" | ||
self assert: result equals: response | ||
] | ||
|
||
{ #category : 'tests' } | ||
BitbucketCommitsTest >> testAllSinceUntilInRepositoryOfProject [ | ||
|
||
| hostUrl client endpoint bitbucketApi result response bitbucketCommits projectKey repositorySlug since until commit1 commit2 sinceAsTimestamp untilPlusOneDayAsTimestamp | | ||
"Given" | ||
hostUrl := 'https://www.url.com'. | ||
client := ZnClient new. | ||
|
||
bitbucketApi := BitbucketApi new | ||
bearerToken: 'token'; | ||
host: hostUrl; | ||
client: client. | ||
|
||
bitbucketCommits := BitbucketCommits new bitbucketApi: bitbucketApi. | ||
|
||
projectKey := 'OOO'. | ||
repositorySlug := 'my project'. | ||
since := '02-05-2025'. | ||
until := '02-06-2025'. | ||
|
||
sinceAsTimestamp := (since asDate asDateAndTime) asUnixTime * 1000. | ||
untilPlusOneDayAsTimestamp := (until asDate asDateAndTime + 1day) asUnixTime * 1000. | ||
|
||
endpoint := '/projects/' , projectKey , '/repos/' , repositorySlug | ||
, '/commits'. | ||
|
||
commit1 := { #authorTimestamp | ||
-> sinceAsTimestamp } | ||
asDictionary. | ||
commit2 := { #authorTimestamp | ||
-> untilPlusOneDayAsTimestamp } | ||
asDictionary. | ||
|
||
|
||
response := { | ||
(#values -> { | ||
commit1. | ||
commit2 }). | ||
(#isLastPage -> true) } asDictionary. | ||
|
||
client stub get willReturn: (NeoJSONWriter toString: response). | ||
|
||
|
||
"When" | ||
result := bitbucketCommits | ||
allSince: since | ||
until: until | ||
inRepository: repositorySlug | ||
ofProject: projectKey. | ||
|
||
"Then" | ||
self assert: result size equals: 1. | ||
self assert: result first equals: commit1 | ||
] | ||
|
||
{ #category : 'tests' } | ||
BitbucketCommitsTest >> testAllWithParamsInRepositoryOfProject [ | ||
|
||
"Given" | ||
|
||
| hostUrl client bitbucketApi projectKey repositorySlug endpoint response result params bitbucketCommits | | ||
hostUrl := 'www.url.com'. | ||
client := Mock new. | ||
|
||
bitbucketApi := BitbucketApi new | ||
bearerToken: 'token'; | ||
host: hostUrl; | ||
client: client. | ||
|
||
bitbucketCommits := BitbucketCommits new bitbucketApi: bitbucketApi. | ||
|
||
projectKey := 'OOO'. | ||
repositorySlug := 'my project'. | ||
|
||
params := { (#until -> 'dev') } asDictionary. | ||
|
||
endpoint := '/projects/' , projectKey , '/repos/' , repositorySlug | ||
, '/commits'. | ||
|
||
response := { (#commitId -> '1') } asDictionary. | ||
|
||
(bitbucketCommits stub getAll: endpoint withParams: params) | ||
willReturn: response. | ||
|
||
"When" | ||
result := bitbucketCommits | ||
allWithParams: params | ||
inRepository: repositorySlug | ||
ofProject: projectKey. | ||
|
||
"Then" | ||
self assert: result equals: response | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Class { | ||
#name : 'BitbucketCommits', | ||
#superclass : 'BitbucketRessource', | ||
#category : 'BitbucketPharoAPI', | ||
#package : 'BitbucketPharoAPI' | ||
} | ||
|
||
{ #category : 'api - get' } | ||
BitbucketCommits >> allInBranch: branchRef inRepository: repositorySlug ofProject: projectKey [ | ||
"GET /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits" | ||
|
||
| endpoint params | | ||
endpoint := '/projects/' , projectKey , '/repos/' , repositorySlug | ||
, '/commits'. | ||
|
||
params := { | ||
#until -> branchRef | ||
} asDictionary. | ||
|
||
^ self getAll: endpoint withParams: params | ||
] | ||
|
||
{ #category : 'api - get' } | ||
BitbucketCommits >> allSince: since until: until inRepository: repositorySlug ofProject: projectKey [ | ||
"GET /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits" | ||
"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 lastCommitterTimestamp commits endpoint | | ||
bitbucketApi prepareClient. | ||
endpoint := '/projects/' , projectKey , '/repos/' | ||
, repositorySlug , '/commits'. | ||
bitbucketApi client path: bitbucketApi basePath , endpoint. | ||
|
||
commits := OrderedCollection new. | ||
|
||
[ | ||
results := bitbucketApi client get. | ||
results := (NeoJSONReader on: results readStream) next. | ||
commits addAll: (results at: #values). | ||
results | ||
at: #nextPageStart | ||
ifPresent: [ | ||
bitbucketApi client queryAt: #start put: (results at: #nextPageStart) ]. | ||
|
||
(results at: #isLastPage) | ||
ifTrue: [ false ] | ||
ifFalse: [ | ||
lastCommitterTimestamp := commits last at: #authorTimestamp. | ||
lastDate := DateAndTime fromUnixTime: lastCommitterTimestamp / 1000. | ||
since asDate <= lastDate ] ] whileTrue. | ||
|
||
^ commits select: [ :commit | | ||
| commitDate | | ||
commitDate := DateAndTime fromUnixTime: | ||
(commit at: #authorTimestamp) / 1000. | ||
commitDate >= since asDate asDateAndTime and: | ||
commitDate <= until asDate asDateAndTime ] | ||
] | ||
|
||
{ #category : 'api - get' } | ||
BitbucketCommits >> allWithParams: paramsDictionary inRepository: repositorySlug ofProject: projectKey [ | ||
"GET /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/commits" | ||
| endpoint | | ||
endpoint := '/projects/', projectKey, '/repos/', repositorySlug, '/commits'. | ||
^self getAll: endpoint withParams: paramsDictionary. | ||
] |