Snippets

Runsun Pan Load Bitbucket files

Created by Runsun Pan last modified
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>bitbucket_aids demo</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <script src="http://runsun.info/prog/js/proj/jxlog/jxlog.js"></script>
  <script src="http://runsun.info/prog/js/proj/jxx/jxx.js"></script>  
  <script src="bitbucket_aids.js"></script>  
	
  <!-- https://highlightjs.org/usage/ -->
  <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/androidstudio.min.css">
  <script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
	
  <style>
	 div { margin: 10px; border:1px gray solid; padding: 10px}
  </style>
  
  <script>
  
	var log = jxlog
    var url_load_Bitbucket_files = 'https://bitbucket.org/!api/2.0/snippets/runsun/bMKzz/9c6667de03f50ecb6adf589c073aa54e80dbb5ae/files/load_Bitbucket_files.js'
/*
get_Bitbucket_repo_last_commit_id(
      acc_name, 
      repo_name, 
      cmtid_handler
*/			
$.get(url_load_Bitbucket_files
,(data)=>{
          log.b('To run get_Bitbucket_repo_last_commit_id(...):')
          eval(data); // activate load_Bitbucket_files.js
          get_Bitbucket_repo_last_commit_id( //<== defined in load_Bitbucket_files.js
            acc_name= 'birkenfeld',
            repo_name= 'pygments-main',
            cmtid_handler= (cmtid)=>{
						  document.getElementById('div_pygments_cmtid').innerHTML+=
							 "<code>"+cmtid+"</code>"
					  }
						)
					log.e()
          }  
)

/*
function get_Bitbucket_file_urls( 
    acc_name,
    repo_name,
    folder_name,
    urls_handler
)
*/
	
$.get( url_load_Bitbucket_files
,(data)=>{
          log.b('to run load_Bitbucket_files(...):')
          eval(data); // activate load_Bitbucket_files.js
          get_Bitbucket_file_urls( //<== defined in load_Bitbucket_files.js
            acc_name= 'birkenfeld',
            repo_name= 'pygments-main',
            folder_name= 'scripts',
            urls_handler= (urls)=>{
						  document.getElementById('div_pygments/scripts').innerHTML+=
							 urls.join('<br/>')
					  }
						)
					log.e()
          }  
)


function display_file(fileInfo)
{
	log.b()

		$('#div_pygments_loadfiles')[0].innerHTML +=
		` <details class="fileContent">
		  <summary><b>${fileInfo.path}</b></summary>
		  <pre><code class="python hljs">${fileInfo.data}</code></pre>
			</details>
		`
	log.e()	
}

/*
function load_Bitbucket_files( 
            acc_name,
            repo_name,
            folder_name,
            fileInfo_handler=null,
            onload= null
)
*/
$.get( url_load_Bitbucket_files
,(data)=>{
          log.b('To run get_Bitbucket_file_urls(...):')
          eval(data); // activate load_Bitbucket_files.js
          load_Bitbucket_files( //<== defined in load_Bitbucket_files.js
            acc_name= 'birkenfeld',
            repo_name= 'pygments-main',
            folder_name= 'scripts',
            fileInfo_handler= display_file,
						onload= rawfiles=>{
						        document.body.innerHTML += 
                    `
                      ==> Loaded total of ${rawfiles.length} files !! 
                    `		
										}
					)
					log.e()
          }  
)


</script>
<body>
<h1>Demo of <a href="https://bitbucket.org/!api/2.0/snippets/runsun/bMKzz/9c6667de03f50ecb6adf589c073aa54e80dbb5ae/files/load_Bitbucket_files.js">load_Bitbucket_files.js</a></h1>
In this demo we load birkenfeld's <a href="https://bitbucket.org/birkenfeld/pygments-main">pygments-main</a> files.
<div id='div_pygments_cmtid'>
Bitbucket repo <code><b><a href="https://bitbucket.org/birkenfeld/pygments-main">pygments-main</a></b></code>'s last (the newest) commit id</code>:
<pre><code class="javascript codebox">
get_Bitbucket_repo_last_commit_id(  //<== defined in load_Bitbucket_files.js
			acc_name= 'birkenfeld',
			repo_name= 'pygments-main',
			cmtid_handler= (cmtid)=>{
			document.getElementById('div_pygments_cmtid').innerHTML+=
			 "&lt;code>${cmtid}&lt;/code>"
			}
)
</code></pre>
Result:						
<br/><br/> 
</div>




<div id='div_pygments/scripts'>
Bitbucket repo <code><b>pygments-main</b></code>'s file urls in <code>/src/scripts</code>:
<pre><code class="javascript codebox">
get_Bitbucket_file_urls( //<== defined in load_Bitbucket_files.js
	acc_name= 'birkenfeld',
	repo_name= 'pygments-main',
	folder_name= 'scripts',
	urls_handler= (urls)=>{
		document.getElementById('div_pygments/scripts').innerHTML+=
		 urls.join('&lt;br/>')
	}
)
</code></pre>
Results:
<br/><br/>
</div>


<div id='div_pygments_loadfiles'>
Load Bitbucket repo <code><b>pygments-main</b></code>'s files in <code>/src/scripts</code>:
<pre><code class="javascript codebox">
function display_file(fileInfo)
{
 $('#div_pygments_loadfiles')[0].innerHTML +=
 ` &lt;details class="fileContent">
&lt;summary>&lt;b>${fileInfo.path}&lt;/b>&lt;/summary>
&lt;div style="background:#eee;border:1px black inset;margin:5px">&lt;code>&lt;pre>${fileInfo.data}&lt;/pre>&lt;/code>&lt;/div>
&lt;/details>
`
}

load_Bitbucket_files( //<== defined in load_Bitbucket_files.js
	acc_name= 'birkenfeld',
	repo_name= 'pygments-main',
	folder_name= 'scripts',
	fileInfo_handler= display_file,
	onload= rawfiles=>
		document.body.innerHTML += 
		` ==> Loaded total of ${rawfiles.length} files !! 
		`	
)
</code></pre>

Results:
<br/><br/> 
</div>

</body>
</html>
/*
    This snippet provides three Bitbucket-related functions:
    
    get_Bitbucket_repo_last_commit_id( acc_name, repo_name, cmtid_handler )
    
    -- Get the last commit id from a poblic Bitbucket repo
    -- Need a cmtid_handler( cmtid ) to handle it

    get_Bitbucket_file_urls( acc_name, repo_name, folder_name, urls_handler )

    -- Get urls of files from a poblic Bitbucket repo
    -- Need a urls_handler( urls ) to handle it

    load_Bitbucket_files( acc_name, repo_name, folder_name, onload, fileInfo_handler=null )

    -- Load files from a folder of public Bitbucket repo
    -- For the repo root folder, set folder_name = ''
    -- onload( rawfiles ): 
            called after ALL files are loaded.
            rawfiles is an array of loaded raw files
    -- fileInfo_handler( fileInfo ) : 
            called after EACH file is loaded.
            fileInfo is an object: 
            {
            "node": "59f0df03c063",
            "path": "scores/Amazing_Grace.json",
            "data": " (file content in string) ",
            "size": 416
            }
            
    The function log.b(), log(), log.e() are from https://bitbucket.org/runsun/jxlog  
    with log = jxlog
    
    demo: https://jsfiddle.net/runsun/kv5vo6g2/2/
*/

function get_Bitbucket_repo_last_commit_id(
      acc_name, 
      repo_name, 
      cmtid_handler
){
  // 
  // Get the last commit id from a poblic Bitbucket repo
  // Need a cmtid_handler( cmtid ) to handle it
  //
    let url = 'https://api.bitbucket.org/2.0/repositories/'
              + acc_name + '/' + repo_name +'/commits'
    $.get( 
             url,
            (commits)=> 
             {
                let cmtid = commits.values[0].hash
                //log('last commit uuid= ', cmtid)
                if(cmtid_handler) cmtid_handler(cmtid)
             }   
   ) 
}

function get_Bitbucket_file_urls( 
    acc_name,
    repo_name,
    folder_name,
    urls_handler
){
  // 
  // Get urls of files from a poblic Bitbucket repo
  // Need a urls_handler( urls ) to handle it
  //
    
  get_Bitbucket_repo_last_commit_id( 
      acc_name, 
      repo_name,
      cmtid_handler
  )
  
  //--------------------------------------
  function cmtid_handler(cmtid){

    //log.b()
    let srcUrl='https://api.bitbucket.org/1.0/repositories/'
                + acc_name + '/'
                + repo_name + '/src/'
                + cmtid +'/'
    let folderUrl = srcUrl+ folder_name+ (folder_name?'/':'')
    
    //log('srcUrl = ', srcUrl)
    //log('folderUrl = ', folderUrl)
    
    $.get( folderUrl, folderInfo_handler )

  
    function folderInfo_handler( folderInfo ){
      /* 
         A folderInfo is a string containing:
         
          { ...
          "files": [{
                        "size": 416,
                        "path": "scores/Amazing_Grace.json",
                        "timestamp": "2016-09-03T23:13:07Z",
                        "utctimestamp": "2016-09-03 23:13:07+00:00",
                        "revision": "fdf715cc14f8"
                    }, {
                        "size": 449,
                        "path": "scores/Come_Home_Sooner.json",
                        "timestamp": "2016-09-03T23:13:07Z",
                        "utctimestamp": "2016-09-03 23:13:07+00:00",
                        "revision": "fdf715cc14f8"
                    }, { ...]
                    
        eval( folderInfo ).files ==> array of fileInfo             
      */    
      //log.b('get_Bitbucket_file_urls/cmtid_handler/folderInfo_handler(folderInfo)')
      
      //log('folderInfo = ', folderInfo)
      folderInfo = eval(folderInfo)  
      //log('folderInfo = ', folderInfo)

      let fileUrls=[], fileInfos = folderInfo.files
      //fileUrls = fileInfos.map( (x)=> x.path )
      
      for(var i=0;i<fileInfos.length;i++){
          fileUrls.push( srcUrl+ fileInfos[i].path )
      }   
      //fileUrls = Array(folderInfo.files).map( (o)=> folderUrl+ o.path )
      //log('fileUrls = ', JSON.stringify(fileUrls) )
      if(urls_handler) urls_handler(fileUrls)
      //log.e() 
    }//_folderInfo_handler
    
    //log.e()
  }//_cmtid_handler

}

function load_Bitbucket_files( 
            acc_name,
            repo_name,
            folder_name,
            fileInfo_handler=null,
            onload= null
){
  /* --------------------------------------------------
   Load files from a folder of public Bitbucket repo
   For the repo root folder, set folder_name = ''
   
   Two types of file handlers:
  
   - onload( rawfiles ): 
   
      called after ALL files are loaded.
      rawfiles is an array of loaded raw files
  
   - fileInfo_handler( fileInfo ) : 
   
      called after EACH file is loaded.
      fileInfo is an object: 
      {
        "node": "59f0df03c063",
        "path": "scores/Amazing_Grace.json",
        "data": " (file content in string) ",
        "size": 416
      }      
  ---------------------------------------------------- */ 
  //log.b()
  let rawfiles=[]
  //log('fileInfo_handler = ', fileInfo_handler)
  
  get_Bitbucket_file_urls( 
      acc_name,
      repo_name,
      folder_name,
      url_handler
  )

  //--------------------------------------
  function url_handler( urls ){
    //log.b()
    //log('urls: ', JSON.stringify( urls ) )
    //log('len(scoreUrls) = ', scoreUrls.length)
    //log('load_OcaNotation_scores handler= ', handler)
    $.get( 
      urls.shift(), //[0],
      (fileInfo)=>{ 
         /* a string:
            {
            "node": "59f0df03c063",
            "path": "scores/Amazing_Grace.json",
            "data": "score= { 
                    \"title\": \"Amazing Grace\"\n
                    , \"label\": \"\"\n
                    , \"author\": \"\"\n
                    , \"score\": \n\n`Amazing Grace\n\n(a\nv5 | 1 - 3....}",
            "size": 416
          }
        */
        //log.b('url_handler get fileInfo_handler')
        rawfiles.push( fileInfo.data )
        //log('rawfiles length= ', rawfiles.length)
        //log('rawfiles stringed: ', JSON.stringify(rawfiles))
        //log('fileInfo_handler= ', ''+fileInfo_handler)
        if(fileInfo_handler) fileInfo_handler( eval(fileInfo) );
        //log('Checking urls.length = ', urls.length)
        if(urls.length) { 
          url_handler( urls ) 
          }
        else if(onload) { 
          //log.b('\n\nAll files loaded, calling load_Bitbucket_files onload')
          onload(rawfiles)
          //log.e()
          }
        //log.e()  
      }
    ) 
    //log.e() 
  }//_url_handler

  //log.e()
}//_load_Bitbucket_files

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.