11import { cwd , resolve } from "@fern-api/fs-utils" ;
2- import { cloneRepository } from "@fern-api/github" ;
2+ import { cloneRepository , parseRepository } from "@fern-api/github" ;
33import type { ClonedRepository } from "@fern-api/github/src/ClonedRepository" ;
44import { Octokit } from "@octokit/rest" ;
55
@@ -27,16 +27,29 @@ export class GitHub {
2727 installationToken : this . githubConfig . token
2828 } ) ;
2929
30- const branch = this . githubConfig . branch ?? ( await repository . getDefaultBranch ( ) ) ;
30+ const isEmptyRepo = await repository . isRemoteEmpty ( ) ;
31+
32+ let branch : string ;
33+ if ( isEmptyRepo ) {
34+ branch = this . githubConfig . branch ?? "main" ;
35+ await repository . checkoutOrCreateLocal ( branch ) ;
36+ } else {
37+ branch = this . githubConfig . branch ?? ( await repository . getDefaultBranch ( ) ) ;
38+ await repository . checkout ( branch ) ;
39+ await repository . pull ( branch ) ;
40+ }
3141
32- await repository . checkout ( branch ) ;
33- await repository . pull ( branch ) ;
3442 const fernIgnoreFiles = await this . getFernignoreFiles ( repository ) ;
3543 await repository . overwriteLocalContents ( sourceDirectory ) ;
3644 await repository . add ( "." ) ;
3745 await this . restoreFiles ( repository , fernIgnoreFiles ) ;
3846 await repository . commit ( "SDK Generation" ) ;
39- await repository . push ( ) ;
47+
48+ if ( isEmptyRepo ) {
49+ await repository . pushUpstream ( branch ) ;
50+ } else {
51+ await repository . push ( ) ;
52+ }
4053 } catch ( error ) {
4154 // TODO: migrate this to use @fern -api/logger
4255 console . error ( "Error during GitHub push:" , error ) ;
@@ -54,13 +67,23 @@ export class GitHub {
5467 installationToken : this . githubConfig . token
5568 } ) ;
5669
57- const baseBranch = this . githubConfig . branch ?? ( await repository . getDefaultBranch ( ) ) ;
70+ const isEmptyRepo = await repository . isRemoteEmpty ( ) ;
71+
72+ let baseBranch : string ;
73+ if ( isEmptyRepo ) {
74+ baseBranch = this . githubConfig . branch ?? "main" ;
75+ await repository . checkoutOrCreateLocal ( baseBranch ) ;
76+ await repository . commit ( "Initial commit" ) ;
77+ await repository . pushUpstream ( baseBranch ) ;
78+ } else {
79+ baseBranch = this . githubConfig . branch ?? ( await repository . getDefaultBranch ( ) ) ;
80+ await repository . checkout ( baseBranch ) ;
81+ await repository . pull ( baseBranch ) ;
82+ }
5883
5984 const now = new Date ( ) ;
6085 const formattedDate = now . toISOString ( ) . replace ( "T" , "_" ) . replace ( / : / g, "-" ) . replace ( / \. .+ / , "" ) ;
6186 const prBranch = `fern-bot/${ formattedDate } ` ;
62- await repository . checkout ( baseBranch ) ;
63- await repository . pull ( baseBranch ) ;
6487 await repository . checkout ( prBranch ) ;
6588
6689 const fernIgnoreFiles = await this . getFernignoreFiles ( repository ) ;
@@ -74,10 +97,8 @@ export class GitHub {
7497 auth : this . githubConfig . token
7598 } ) ;
7699 // Use octokit directly to create the pull request
77- const [ owner , repo ] = this . githubConfig . uri . split ( "/" ) ;
78- if ( ! owner || ! repo ) {
79- throw new Error ( `Invalid repository URI: ${ this . githubConfig . uri } ` ) ;
80- }
100+ const parsedRepo = parseRepository ( this . githubConfig . uri ) ;
101+ const { owner, repo } = parsedRepo ;
81102 const head = `${ owner } :${ prBranch } ` ;
82103 try {
83104 await octokit . pulls . create ( {
0 commit comments