diff --git a/server/src/deployment-center/github/github.controller.ts b/server/src/deployment-center/github/github.controller.ts index 1b9acfc1b8..ad4e1c4acb 100644 --- a/server/src/deployment-center/github/github.controller.ts +++ b/server/src/deployment-center/github/github.controller.ts @@ -722,6 +722,12 @@ export class GithubController { await this._makeGetCallWithLinkAndOAuthHeaders(url, gitHubToken, res); } + @Put('api/github/updateGitHubContent') + @HttpCode(200) + async updateGitHubContent(@Body('gitHubToken') gitHubToken: string, @Body('commit') commit: GitHubCommit) { + await this._commitGitHubFile(gitHubToken, commit); + } + private _getAuthorizationHeader(accessToken: string): { Authorization: string } { return { Authorization: `token ${accessToken}`, @@ -821,6 +827,33 @@ export class GithubController { } } + private async _commitGitHubFile(gitHubToken: string, commit: GitHubCommit) { + const url = `${this.githubApiUrl}/repos/${commit.repoName}/contents/${commit.filePath}`; + + const commitContent = { + message: commit.message, + content: commit.contentBase64Encoded, + sha: commit.sha, + branch: commit.branchName, + comitter: commit.committer, + }; + + try { + await this.httpService.put(url, commitContent, { + headers: this._getAuthorizationHeader(gitHubToken), + }); + } catch (err) { + this.loggingService.error( + `Failed to commit GitHub '${commit.filePath}' on branch '${commit.branchName}' in repo '${commit.repoName}'.` + ); + + if (err.response) { + throw new HttpException(err.response.data, err.response.status); + } + throw new HttpException(err, 500); + } + } + private _getEnvironment(hostUrl: string): Environments { const hostUrlToLower = (hostUrl || '').toLocaleLowerCase(); for (const url in EnvironmentUrlMappings.urlToEnvironmentMap) {