import { ICreateRefParams , IPatchRefParams , IRef } from "@fluidframework/gitresources" ; import { Router } from "express" ; import * as git from "isomorphic-git" ; import nconf from "nconf" ; import * as utils from "../utils" ; function refToIRef ( ref , sha ) { return { object : { sha , type : "" , url : "" , } , ref , url : "" , } ; } export async function getRefs ( store , tenantId , authorization ) { const branches = await git . listBranches ( { dir : utils . getGitDir ( store , tenantId ) , } ) ; return Promise . all ( branches . map ( async ( branch ) => getRef ( store , tenantId , authorization , branch ) ) ) ; } export async function getRef ( store , tenantId , authorization , ref ) { const resolved = await git . resolveRef ( { dir : utils . getGitDir ( store , tenantId ) , ref , } ) ; return refToIRef ( ref , resolved ) ; } export async function createRef ( store , tenantId , authorization , params ) { await git . writeRef ( { dir : utils . getGitDir ( store , tenantId ) , ref : params . ref , value : params . sha , } ) ; return refToIRef ( params . ref , params . sha ) ; } export async function updateRef ( store , tenantId , authorization , ref , params ) { const dir = utils . getGitDir ( store , tenantId ) ; const rebasedRef = `refs/${ ref }` ; await git . writeRef ( { dir , force : true , ref : rebasedRef , value : params . sha , } ) ; return refToIRef ( ref , params . sha ) ; } export async function deleteRef ( store , tenantId , authorization , ref ) { throw new Error ( "Not implemented" ) ; } export function create ( store ) { const router = Router ( ) ; router . get ( "/repos/:ignored?/:tenantId/git/refs" , ( request , response ) => { const refsP = getRefs ( store , request . params . tenantId , request . get ( "Authorization" ) ) ; utils . handleResponse ( refsP , response , false ) ; } ) ; router . get ( "/repos/:ignored?/:tenantId/git/refs/*" , ( request , response ) => { const refP = getRef ( store , request . params . tenantId , request . get ( "Authorization" ) , request . params [ 0 ] ) ; utils . handleResponse ( refP , response , false ) ; } ) ; router . post ( "/repos/:ignored?/:tenantId/git/refs" , ( request , response ) => { const refP = createRef ( store , request . params . tenantId , request . get ( "Authorization" ) , request . body ) ; utils . handleResponse ( refP , response , false , 201 ) ; } ) ; router . patch ( "/repos/:ignored?/:tenantId/git/refs/*" , ( request , response ) => { const refP = updateRef ( store , request . params . tenantId , request . get ( "Authorization" ) , request . params [ 0 ] , request . body ) ; utils . handleResponse ( refP , response , false ) ; } ) ; router . delete ( "/repos/:ignored?/:tenantId/git/refs/*" , ( request , response ) => { const refP = deleteRef ( store , request . params . tenantId , request . get ( "Authorization" ) , request . params [ 0 ] ) ; utils . handleResponse ( refP , response , false , 204 ) ; } ) ; return router ; }