@@ -10,6 +10,7 @@ const Path = require('path');
1010const Url = require ( 'url' ) ;
1111const request = require ( 'screwdriver-request' ) ;
1212const schema = require ( 'screwdriver-data-schema' ) ;
13+ const CHECKOUT_URL_REGEX = schema . config . regex . CHECKOUT_URL ;
1314const API_URL_V2 = 'https://api.bitbucket.org/2.0' ;
1415const REPO_URL = `${ API_URL_V2 } /repositories` ;
1516const USER_URL = `${ API_URL_V2 } /users` ;
@@ -49,8 +50,7 @@ function throwError(errorReason, errorCode = 500) {
4950 * @return {Object } An object with hostname, repo, branch, and username
5051 */
5152function getRepoInfo ( checkoutUrl , rootDir ) {
52- const regex = schema . config . regex . CHECKOUT_URL ;
53- const matched = regex . exec ( checkoutUrl ) ;
53+ const matched = CHECKOUT_URL_REGEX . exec ( checkoutUrl ) ;
5454
5555 // Check if regex did not pass
5656 if ( ! matched ) {
@@ -582,17 +582,35 @@ class BitbucketScm extends Scm {
582582 * @async getFile
583583 * @param {Object } config Configuration
584584 * @param {String } config.scmUri The scmUri
585- * @param {String } config.path The file in the repo to fetch
585+ * @param {String } config.path The file in the repo to fetch or full checkout URL
586586 * @param {String } config.token The token used to authenticate to the SCM
587587 * @param {String } [config.ref] The reference to the SCM, either branch or sha
588588 * @return {Promise } Resolves to the content of the file
589589 */
590590 async _getFile ( { scmUri, ref, path } ) {
591- const { branch : branchFromScmUri , repoId, rootDir } = getScmUriParts ( scmUri ) ;
592- const branch = ref || branchFromScmUri ;
593- const fullPath = rootDir ? Path . join ( rootDir , path ) : path ;
594- const fileUrl = `${ REPO_URL } /${ repoId } /src/${ branch } /${ fullPath } ` ;
591+ let fullPath = path ;
592+ let username ;
593+ let repo ;
594+ let branch ;
595+ let rootDir ;
596+ let repoId ;
595597 const token = await this . _getToken ( ) ;
598+
599+ // If full path to a file is provided, e.g. git@github.com:screwdriver-cd/scm-github.git:path/to/a/file.yaml
600+ if ( CHECKOUT_URL_REGEX . test ( path ) ) {
601+ ( { username, repo, branch, rootDir } = getRepoInfo ( fullPath ) ) ;
602+ fullPath = rootDir ;
603+ repoId = `${ username } /${ repo } ` ;
604+ } else {
605+ const scmUriParts = getScmUriParts ( scmUri ) ;
606+
607+ ( { repoId, rootDir } = scmUriParts ) ;
608+ branch = ref || scmUriParts . branch ;
609+
610+ fullPath = rootDir ? Path . join ( rootDir , path ) : path ;
611+ }
612+
613+ const fileUrl = `${ REPO_URL } /${ repoId } /src/${ branch } /${ fullPath } ` ;
596614 const options = {
597615 url : fileUrl ,
598616 method : 'GET' ,
0 commit comments