Skip to content

Commit

Permalink
feat(BitbucketCommits): get all commits methods (#13)
Browse files Browse the repository at this point in the history
* feat(BitbucketCommits): allWithParams

* feat(BitbucketCommits): allInBranch:

* feat(BitbucketPharoApi): allSince:Until:
  • Loading branch information
knowbased authored Feb 5, 2025
1 parent 0bde510 commit a25fc08
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
142 changes: 142 additions & 0 deletions src/BitbucketPharoAPI-Tests/BitbucketCommitsTest.class.st
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
]
5 changes: 5 additions & 0 deletions src/BitbucketPharoAPI/BitbucketApi.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ BitbucketApi >> client: anObject [
client := anObject
]

{ #category : 'ressources' }
BitbucketApi >> commits [
^BitbucketCommits new bitbucketApi: self.
]

{ #category : 'accessing' }
BitbucketApi >> host [

Expand Down
66 changes: 66 additions & 0 deletions src/BitbucketPharoAPI/BitbucketCommits.class.st
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.
]

0 comments on commit a25fc08

Please sign in to comment.