Skip to content

Commit

Permalink
feat(BitbucketRepositories): update a file (#11)
Browse files Browse the repository at this point in the history
* feat(BitbucketRepositories): update a file

* feat(BitbucketRepositories) : update file method
  • Loading branch information
knowbased authored Jan 23, 2025
1 parent 4188905 commit 190ff80
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/BitbucketPharoAPI-Tests/BitbucketRepositoriesTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,48 @@ BitbucketRepositoriesTest >> testRawFileWithParamsInRepositoryOfProject [
params keysAndValuesDo: [ :key :value |
self assert: (client request url query at: key) equals: value ]
]

{ #category : 'tests' }
BitbucketRepositoriesTest >> testUpdateFileWithParamsInRepositoryOfProject [

| hostUrl gitlabApi result client filePath response bitbucketRepositories projectKey repoSlug bitbucketUpdateFileParams endpoint |
"Given"
hostUrl := 'www.url.com'.
client := ZnClient new.

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

filePath := 'file.txt'.
projectKey := 'BBB'.
repoSlug := 'project'.

bitbucketUpdateFileParams := BitbucketUpdateFileParams new
newContent: 'new content';
commitMessage: 'message';
sourceCommitId: 'ez21rferg31';
branch: 'test'.

response := { (#message -> 'success') } asDictionary.

endpoint := '/projects/' , projectKey , '/repos/' , repoSlug
, '/browse/' , filePath.
(bitbucketRepositories stub
put: endpoint
withData: bitbucketUpdateFileParams asDictionary) willReturn:
response.

"When"
result := bitbucketRepositories
updateFile: filePath
withParams: bitbucketUpdateFileParams
inRepository: repoSlug
ofProject: projectKey.

"Then"
self assert: result equals: response.
]
35 changes: 35 additions & 0 deletions src/BitbucketPharoAPI-Tests/BitbucketRessourceTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,38 @@ BitbucketRessourceTest >> testPostJsonWithData [
assert: client request entity string
equals: (NeoJSONWriter toString: data)
]

{ #category : 'tests' }
BitbucketRessourceTest >> testPutWithData [

| hostUrl result client endpoint gitlabRessource data responseMessage response bitbucketApi |
"Given"
hostUrl := 'https://www.url.com'.
client := ZnClient new.

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

endpoint := '/endpoint'.
data := { ('param' -> 'value') } asDictionary.

responseMessage := 'success'.
response := '{ "message": "' , responseMessage , '" }'.

gitlabRessource := BitbucketRessource new bitbucketApi: bitbucketApi.

client stub put willReturn: response.

"When"
result := gitlabRessource put: endpoint withData: data.

"Then"
self assert: (result at: #message) equals: responseMessage.

client request entity parts doWithIndex: [ :part :index |
self assert: part fieldName equals: (data keys at: index).

self assert: part fieldValue equals: (data values at: index) ]
]
12 changes: 12 additions & 0 deletions src/BitbucketPharoAPI/BitbucketRepositories.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ BitbucketRepositories >> rawFile: filePath withParams: paramsDict inRepository:

^ result
]

{ #category : 'api - put' }
BitbucketRepositories >> updateFile: filePath withParams: bitbucketUpdateFileParams inRepository: repositorySlug ofProject: projectKey [
"PUT /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/browse/{path:.*}?at&type&blame&noContent"

| endpoint |
endpoint := '/projects/' , projectKey , '/repos/' , repositorySlug
, '/browse/' , filePath.


^ self put: endpoint withData: bitbucketUpdateFileParams asDictionary
]
16 changes: 16 additions & 0 deletions src/BitbucketPharoAPI/BitbucketRessource.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,19 @@ BitbucketRessource >> postJson: endpoint withData: data [

^ NeoJSONReader fromString: response
]

{ #category : 'api' }
BitbucketRessource >> put: endpoint withData: data [

| response |
bitbucketApi prepareClient.
bitbucketApi client path: bitbucketApi basePath , endpoint.

data keysAndValuesDo: [ :key :value |
bitbucketApi client addPart:
(ZnMimePart fieldName: key value: value) ].

response := bitbucketApi client put.

^ NeoJSONReader fromString: response
]
50 changes: 50 additions & 0 deletions src/BitbucketPharoAPI/BitbucketUpdateFileParams.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Class {
#name : 'BitbucketUpdateFileParams',
#superclass : 'Object',
#instVars : [
'newContent',
'commitMessage',
'sourceCommitId',
'branch'
],
#category : 'BitbucketPharoAPI',
#package : 'BitbucketPharoAPI'
}

{ #category : 'converting' }
BitbucketUpdateFileParams >> asDictionary [

| dict |
dict := Dictionary new.

branch ifNotNil: [ dict at: #branch put: branch ].
newContent ifNotNil: [ dict at: #content put: newContent ].
commitMessage ifNotNil: [ dict at: #message put: commitMessage ].
sourceCommitId ifNotNil: [ dict at: #sourceCommitId put: sourceCommitId ].

^ dict
]

{ #category : 'accessing' }
BitbucketUpdateFileParams >> branch: anObject [

branch := anObject
]

{ #category : 'accessing' }
BitbucketUpdateFileParams >> commitMessage: anObject [

commitMessage := anObject
]

{ #category : 'accessing' }
BitbucketUpdateFileParams >> newContent: anObject [

newContent := anObject
]

{ #category : 'accessing' }
BitbucketUpdateFileParams >> sourceCommitId: anObject [

sourceCommitId := anObject
]

0 comments on commit 190ff80

Please sign in to comment.