2020-09-08 11:08:41 -04:00
import produce from 'immer' ;
2019-11-15 13:06:24 -05:00
import { normalizeData } from 'ee_else_ce/repository/utils/commit' ;
2019-12-10 04:07:51 -05:00
import axios from '~/lib/utils/axios_utils' ;
2020-07-29 11:09:39 -04:00
import commitsQuery from './queries/commits.query.graphql' ;
import projectPathQuery from './queries/project_path.query.graphql' ;
import refQuery from './queries/ref.query.graphql' ;
2019-06-25 10:26:55 -04:00
2020-09-22 11:09:37 -04:00
const fetchpromises = { } ;
const resolvers = { } ;
2021-06-11 08:09:49 -04:00
let maxOffset ;
2021-06-14 08:10:13 -04:00
let nextOffset ;
let currentPath ;
function setNextOffset ( offset ) {
nextOffset = offset || null ;
}
2019-06-25 10:26:55 -04:00
2019-11-25 07:06:13 -05:00
export function resolveCommit ( commits , path , { resolve , entry } ) {
2020-12-23 13:10:19 -05:00
const commit = commits . find (
( c ) => c . filePath === ` ${ path } / ${ entry . name } ` && c . type === entry . type ,
) ;
2019-06-25 10:26:55 -04:00
if ( commit ) {
resolve ( commit ) ;
}
}
2021-06-11 08:09:49 -04:00
export function fetchLogsTree ( client , path , offset , resolver = null , _maxOffset = null ) {
if ( _maxOffset ) {
maxOffset = _maxOffset ;
}
2021-06-14 08:10:13 -04:00
if ( ! currentPath || currentPath !== path ) {
// ensures the nextOffset is reset if the user changed directories
setNextOffset ( null ) ;
}
currentPath = path ;
const offsetNumber = Number ( offset ) ;
if ( ! nextOffset && offsetNumber > maxOffset ) {
setNextOffset ( offsetNumber - 25 ) ; // ensures commit data is fetched for newly added rows that need data from the previous request (requests are made in batches of 25).
2021-06-11 08:09:49 -04:00
return Promise . resolve ( ) ;
}
2019-06-25 10:26:55 -04:00
if ( resolver ) {
2020-09-22 11:09:37 -04:00
if ( ! resolvers [ path ] ) {
resolvers [ path ] = [ resolver ] ;
} else {
resolvers [ path ] . push ( resolver ) ;
}
2019-06-25 10:26:55 -04:00
}
2020-09-22 11:09:37 -04:00
if ( fetchpromises [ path ] ) return fetchpromises [ path ] ;
2019-06-25 10:26:55 -04:00
2020-07-29 11:09:39 -04:00
const { projectPath } = client . readQuery ( { query : projectPathQuery } ) ;
const { escapedRef } = client . readQuery ( { query : refQuery } ) ;
2019-06-25 10:26:55 -04:00
2020-09-22 11:09:37 -04:00
fetchpromises [ path ] = axios
2019-11-18 13:06:53 -05:00
. get (
2020-04-29 08:10:00 -04:00
` ${ gon . relative _url _root } / ${ projectPath } /-/refs/ ${ escapedRef } /logs_tree/ ${ encodeURIComponent (
path . replace ( /^\// , '' ) ,
) } ` ,
2019-11-18 13:06:53 -05:00
{
2021-06-14 08:10:13 -04:00
params : { format : 'json' , offset : nextOffset || offset } ,
2019-11-18 13:06:53 -05:00
} ,
)
2020-09-08 11:08:41 -04:00
. then ( ( { data : newData , headers } ) => {
2019-06-25 10:26:55 -04:00
const headerLogsOffset = headers [ 'more-logs-offset' ] ;
2020-09-08 11:08:41 -04:00
const sourceData = client . readQuery ( { query : commitsQuery } ) ;
2020-12-23 13:10:19 -05:00
const data = produce ( sourceData , ( draftState ) => {
2020-09-08 11:08:41 -04:00
draftState . commits . push ( ... normalizeData ( newData , path ) ) ;
} ) ;
2019-06-25 10:26:55 -04:00
client . writeQuery ( {
2020-07-29 11:09:39 -04:00
query : commitsQuery ,
2020-09-08 11:08:41 -04:00
data ,
2019-06-25 10:26:55 -04:00
} ) ;
2020-12-23 13:10:19 -05:00
resolvers [ path ] . forEach ( ( r ) => resolveCommit ( data . commits , path , r ) ) ;
2019-06-25 10:26:55 -04:00
2020-09-22 11:09:37 -04:00
delete fetchpromises [ path ] ;
2019-06-25 10:26:55 -04:00
if ( headerLogsOffset ) {
2021-06-14 08:10:13 -04:00
setNextOffset ( null ) ;
2019-06-25 10:26:55 -04:00
fetchLogsTree ( client , path , headerLogsOffset ) ;
} else {
2020-09-22 11:09:37 -04:00
delete resolvers [ path ] ;
2021-06-11 08:09:49 -04:00
maxOffset = null ;
2021-06-14 08:10:13 -04:00
setNextOffset ( null ) ;
2019-06-25 10:26:55 -04:00
}
} ) ;
2020-09-22 11:09:37 -04:00
return fetchpromises [ path ] ;
2019-06-25 10:26:55 -04:00
}