-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "(Gitlab Project) Token" to commit file directly to GitLab (#307)
* Add "Gitlab Project" Auth Type on exportUrl UI * implement gitlab api * fix base64 and upload issue * address comments on package.json and urlExport.ts * Delete dist/plugin.js --------- Co-authored-by: Lukas Oppermann <[email protected]>
- Loading branch information
1 parent
2861fa9
commit 954e74a
Showing
6 changed files
with
207 additions
and
21 deletions.
There are no files selected for viewing
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
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,122 @@ | ||
import { utf8ToBase64 } from "@src/utilities/base64"; | ||
import { | ||
urlExportRequestBody, | ||
urlExportSettings, | ||
} from "@typings/urlExportData"; | ||
|
||
export class GitlabRepository { | ||
baseUrl: string; | ||
token: string; | ||
|
||
constructor(props: { baseUrl: string; token: string }) { | ||
this.baseUrl = props.baseUrl; | ||
this.token = props.token; | ||
} | ||
|
||
async upload( | ||
{ client_payload: clientPayload }: urlExportRequestBody, | ||
{ reference: branch }: urlExportSettings, | ||
responseHandler: { | ||
onError: () => void; | ||
onLoaded: (request: XMLHttpRequest) => void; | ||
} | ||
) { | ||
const encodedContent = utf8ToBase64(clientPayload.tokens); | ||
const encodedFilepath = encodeURIComponent(clientPayload.filename); | ||
|
||
let isFileExist: boolean; | ||
try { | ||
isFileExist = await this._checkFile(encodedFilepath, branch); | ||
} catch (error) { | ||
if (error && error.request && error.code === 401) { | ||
responseHandler.onLoaded(error.request); | ||
} | ||
return; | ||
} | ||
|
||
const uploadRequest = new XMLHttpRequest(); | ||
uploadRequest.onerror = (_err) => responseHandler.onError(); | ||
uploadRequest.onload = (event) => | ||
responseHandler.onLoaded(event.target as XMLHttpRequest); | ||
|
||
this._uploadFile({ | ||
request: uploadRequest, | ||
content: encodedContent, | ||
commitMessage: clientPayload.commitMessage, | ||
filepath: encodedFilepath, | ||
branch: branch, | ||
isFileExist: isFileExist, | ||
}); | ||
} | ||
|
||
private _checkFile( | ||
encodedFilepath: string, | ||
branch: string | ||
): Promise<boolean> { | ||
return new Promise<boolean>((resolve, reject) => { | ||
const request = new XMLHttpRequest(); | ||
request.open( | ||
"GET", | ||
`${this.baseUrl}/repository/files/${encodedFilepath}?ref=${branch}` | ||
); | ||
this._setRequestHeader(request); | ||
|
||
request.onreadystatechange = (_ev: ProgressEvent) => { | ||
if (request.readyState !== XMLHttpRequest.DONE) { | ||
return; | ||
} | ||
|
||
const statusCode = request.status; | ||
if (statusCode === 200) { | ||
resolve(true); | ||
return; | ||
} | ||
|
||
if (statusCode === 404) { | ||
resolve(false); | ||
return; | ||
} | ||
|
||
reject({ | ||
code: statusCode, | ||
message: request.response, | ||
request: request, | ||
}); | ||
}; | ||
|
||
request.send(); | ||
}); | ||
} | ||
|
||
private _uploadFile(args: { | ||
request: XMLHttpRequest; | ||
filepath: string; | ||
content: string; | ||
commitMessage: string; | ||
branch: string; | ||
isFileExist: boolean; | ||
}) { | ||
const { isFileExist, request, branch, content, commitMessage, filepath } = args; | ||
|
||
const body = { | ||
branch: branch, | ||
content: content, | ||
commit_message: commitMessage || `Design token update at ${Date.now()}`, | ||
encoding: "base64", | ||
}; | ||
const encodedFilepath = encodeURIComponent(filepath); | ||
|
||
request.open( | ||
isFileExist ? "PUT" : "POST", | ||
`${this.baseUrl}/repository/files/${encodedFilepath}` | ||
); | ||
this._setRequestHeader(request); | ||
|
||
request.send(JSON.stringify(body)); | ||
} | ||
|
||
private _setRequestHeader(request: XMLHttpRequest) { | ||
request.setRequestHeader("Authorization", `Bearer ${this.token}`); | ||
request.setRequestHeader("Content-Type", `application/json`); | ||
} | ||
} |
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,9 @@ | ||
const utf8ToBase64 = (text: string): string => { | ||
const utf8EncodedBytes = new TextEncoder().encode(text) | ||
const binString = Array.from(utf8EncodedBytes, (byte) => | ||
String.fromCodePoint(byte) | ||
).join('') | ||
return btoa(binString) | ||
} | ||
|
||
export { utf8ToBase64 } |