@@ -95,22 +95,194 @@ async function copyToClipboard(event){
9595 document . execCommand ( "copy" )
9696}
9797
98+ const NEW_BRANCH = 'code-json-branch' ;
99+
100+ function getOrgAndRepoArgsGitHub ( url )
101+ {
102+ const pattern = / h t t p s : \/ \/ g i t h u b \. c o m \/ ( [ ^ \/ ] + ) \/ ( [ ^ \/ ] + ) / ;
103+ const match = url . match ( pattern ) ;
104+
105+ if ( match )
106+ {
107+ const owner = match [ 1 ] ;
108+ const repo = match [ 2 ] ;
109+ return { owner, repo} ;
110+ }
111+ else
112+ {
113+ throw new Error ( 'Invalid URL!' ) ;
114+ }
115+ }
116+
117+
118+ async function createBranchOnProject ( projectURL , token )
119+ {
120+ const { owner, repo} = getOrgAndRepoArgsGitHub ( projectURL ) ;
121+
122+ const response = await fetch ( `https://api.github.com/repos/${ owner } /${ repo } /git/refs/heads/main}` ,
123+ {
124+ method : 'GET' ,
125+ headers : {
126+ 'Authorization' : 'token ' . concat ( token ) ,
127+ } ,
128+ }
129+ ) ;
130+
131+ const data = await response . json ( ) ;
132+
133+ if ( response . ok )
134+ {
135+ const sha = data . object . sha ;
136+
137+ const createBranchApiUrl = `https://api.github.com/repos/${ owner } /${ repo } /git/refs` ;
138+
139+ // Create the new branch from the base branch
140+ const newBranchResponse = await fetch ( createBranchApiUrl , {
141+ method : 'POST' ,
142+ headers : {
143+ 'Content-Type' : 'application/json' ,
144+ 'Authorization' : `token ${ token } ` ,
145+ } ,
146+ body : JSON . stringify ( {
147+ ref : `refs/heads/${ NEW_BRANCH } ` , // Name of the new branch
148+ sha : sha , // SHA of the base branch (main)
149+ } ) ,
150+ } ) ;
151+
152+ const newBranchData = await newBranchResponse . json ( ) ;
153+
154+ if ( newBranchResponse . ok )
155+ {
156+ console . log ( 'New branch created successfully: ' , newBranchData ) ;
157+ return true ;
158+ }
159+ else
160+ {
161+ console . error ( 'Error creating new branch: ' , newBranchData ) ;
162+ return false ;
163+ }
164+ }
165+ else
166+ {
167+ console . error ( 'Error fetching base branch info:' , data ) ;
168+ return false ;
169+ }
170+ }
171+
172+
173+ async function addFileToBranch ( projectURL , token , codeJSONObj )
174+ {
175+ const { owner, repo} = getOrgAndRepoArgsGitHub ( projectURL ) ;
176+ const FILE_PATH = 'code.json'
177+ const createFileApiUrl = `https://api.github.com/repos/${ owner } /${ repo } /contents/${ FILE_PATH } ` ;
178+ const encodedContent = Buffer . from ( codeJSONObj ) . toString ( 'base64' ) ;
179+
180+ const response = await fetch ( createFileApiUrl ,
181+ {
182+ method : 'PUT' ,
183+ headers : {
184+ 'Content-Type' : 'application/json' ,
185+ 'Authorization' : 'token' . concat ( token ) ,
186+ } ,
187+ body : JSON . stringify ( {
188+ message : "Add codejson to project" ,
189+ content : encodedContent ,
190+ branch : NEW_BRANCH ,
191+ } ) ,
192+ }
193+ ) ;
194+
195+ const data = await response . json ( )
196+
197+ if ( response . ok )
198+ {
199+ console . log ( 'File added successfully: ' , data ) ;
200+ return true ;
201+ }
202+ else
203+ {
204+ console . error ( 'Error adding file: ' , data ) ;
205+ return false ;
206+ }
207+ }
208+
209+ async function createPR ( projectURL , token )
210+ {
211+ const { owner, repo} = getOrgAndRepoArgsGitHub ( projectURL ) ;
212+ const createPrApiUrl = `https://api.github.com/repos/${ owner } /${ repo } /pulls` ;
213+ const response = await fetch ( createPrApiUrl ,
214+ {
215+ method : 'POST' ,
216+ headers : {
217+ 'Content-Type' : 'application/json' ,
218+ 'Authorization' : 'token ' . concat ( token ) ,
219+ } ,
220+ body : JSON . stringify ( {
221+ title : "Add code.json to Project" ,
222+ body : "Add generated code.json file to project. Code.json was generated via a form.io form site." ,
223+ head : NEW_BRANCH ,
224+ base : 'main' ,
225+
226+ } ) ,
227+ }
228+ ) ;
229+
230+ const data = await response . json ( ) ;
231+
232+ if ( response . ok )
233+ {
234+ console . log ( 'Pull request created successfully: ' , data ) ;
235+ return true ;
236+ }
237+ else
238+ {
239+ console . error ( "Error creating PR!: " , data ) ;
240+ return false ;
241+ }
242+ }
243+
98244// Creates PR on requested project
99245async function createProjectPR ( event ) {
100246 event . preventDefault ( ) ;
101247
102- var textArea = document . getElementById ( "json-result" ) ;
103- var codeJSONObj = JSON . parse ( textArea . value )
248+ var textArea = document . getElementById ( "json-result" ) ; //Step 1
249+ var codeJSONObj = JSON . parse ( textArea . value )
104250
105251 if ( 'gh_api_key' in window )
106252 {
107- var apiKey = window . gh_api_key
108- console . log ( apiKey )
253+ var apiKey = window . gh_api_key ;
254+
255+ if ( 'repositoryURL' in codeJSONObj )
256+ {
257+ var prCreated = false ;
258+ //Step 1
259+ const branchCreated = await createBranchOnProject ( codeJSONObj . repositoryURL , apiKey ) ;
260+ if ( branchCreated )
261+ {
262+ const fileAdded = await addFileToBranch ( codeJSONObj . repositoryURL , apiKey , textArea . value ) ;
263+
264+ if ( fileAdded )
265+ {
266+ prCreated = await createPR ( codeJSONObj . repositoryURL , apiKey ) ;
267+ if ( prCreated )
268+ {
269+ console . log ( "PR successfully created!" ) ;
270+ alert ( "PR has been created!" ) ;
271+ }
272+ }
273+ }
274+ }
275+ else
276+ {
277+ console . error ( "No URL found!" ) ;
278+ alert ( "No URL given for project!" ) ;
279+ }
280+
109281 }
110282 else
111283 {
112- console . error ( "No API key found!" )
113- alert ( "No Api Key in submitted data!" )
284+ console . error ( "No API key found!" ) ;
285+ alert ( "No Api Key in submitted data!" ) ;
114286 }
115287 //console.log(codeJSONObj)
116288}
0 commit comments