11import { Body , Controller , Post , Req } from '@nestjs/common' ;
22import type { RestEndpointMethodTypes } from '@octokit/rest' ;
33import type { RequestPrivate } from '../../typings/express-jwt.js' ;
4+ import type { CodeDict } from '../export-code/code.model.js' ;
45import { fetchGithubAccessToken } from './github-api-fetch.js' ;
56import type { GHContext } from './github-service.js' ;
6- import { listBranches , fetchUser , listRepos } from './github-service.js' ;
7+ import { commitChanges , listBranches , fetchUser , listRepos } from './github-service.js' ;
78import { getOctokit } from './octokit.js' ;
89
910interface GithubCredentials {
@@ -79,21 +80,30 @@ export class GithubController {
7980 let { githubAccessToken : accessToken , owner, repo } = body ;
8081 if ( ! owner ) throw new Error ( 'Missing `owner` in body, cannot list branches.' ) ;
8182 if ( ! repo ) throw new Error ( 'Missing `repo` in body, cannot list branches.' ) ;
83+
8284 const octokit = getOctokit ( accessToken ) ;
8385 const context : GHContext = { accessToken, auth0UserId, octokit, owner, repo } ;
8486
8587 return listBranches ( context ) ;
88+ }
8689
87- // const files: CodeDict = {
88- // 'codegen/foo.ts': "console.log('Hello world!');\n",
89- // };
90- //
91- // const { data } = await listBranches(context);
92- // const data = await getCommit(context);
93- // const { data } = await createBranch(context, 'gencode');
94- // const data = await commitChanges(context, files, 'Auto-commit powered by Clapy');
95- // console.log(data);
96- // return data;
90+ @Post ( 'gencode-tmp' )
91+ async genCodeTmp ( @Req ( ) req : RequestPrivate , @Body ( ) body : GenCodeReq ) {
92+ const auth0UserId = req . auth . sub ;
93+ let { githubAccessToken : accessToken , owner, repo, codegenBranch, mergeToBranch } = body ;
94+ if ( ! owner ) throw new Error ( 'Missing `owner` in body, cannot generate code.' ) ;
95+ if ( ! repo ) throw new Error ( 'Missing `repo` in body, cannot generate code.' ) ;
96+ if ( ! codegenBranch ) throw new Error ( 'Missing `codegenBranch` in body, cannot generate code.' ) ;
97+ if ( ! mergeToBranch ) throw new Error ( 'Missing `mergeToBranch` in body, cannot generate code.' ) ;
98+
99+ const octokit = getOctokit ( accessToken ) ;
100+ const context : GHContext = { accessToken, auth0UserId, octokit, owner, repo, codegenBranch, mergeToBranch } ;
101+
102+ const files : CodeDict = {
103+ 'codegen/foo.ts' : "console.log('Hello world!');\n" ,
104+ } ;
105+
106+ return commitChanges ( context , files ) ;
97107 }
98108
99109 // @Post ('search-repos')
@@ -106,3 +116,10 @@ export class GithubController {
106116 // return searchRepos(context);
107117 // }
108118}
119+
120+ interface GenCodeReq extends TokenPayload {
121+ owner : string ;
122+ repo : string ;
123+ codegenBranch : string ;
124+ mergeToBranch : string ;
125+ }
0 commit comments