Skip to content

Commit

Permalink
fix(2952): Make special characters available in branch name [3] (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
yakanechi authored Nov 15, 2023
1 parent 523a028 commit 989ad1b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
23 changes: 19 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ function escapeForDoubleQuoteEnclosure(command) {
return command.replace(/"/g, '\\"').replace(/`/g, '\\`');
}

/**
* Escape dollar for double quote enclosure
* @param {String} command escape command
* @returns {String}
*/
function escapeDollarForDoubleQuoteEnclosure(command) {
return command.replace(/\$/g, '\\$');
}

/**
* Throw error with error code
* @param {Number} errorCode Error code
Expand Down Expand Up @@ -640,7 +649,9 @@ class GithubScm extends Scm {
const sshCheckoutUrl = `git@${config.host}:${config.org}/${config.repo}`; // URL for ssh
const branch = config.commitBranch ? config.commitBranch : config.branch; // use commit branch
const singleQuoteEscapedBranch = escapeForSingleQuoteEnclosure(branch);
const doubleQuoteEscapedBranch = escapeForDoubleQuoteEnclosure(singleQuoteEscapedBranch);
const doubleQuoteEscapedBranch = escapeForDoubleQuoteEnclosure(
escapeDollarForDoubleQuoteEnclosure(singleQuoteEscapedBranch)
);
const ghHost = config.host || 'github.com'; // URL for host to checkout from
const gitConfigString = `
Host ${ghHost}
Expand Down Expand Up @@ -1019,7 +1030,7 @@ class GithubScm extends Scm {
action: 'getBranch',
token: config.token,
params: {
branch: scmInfo.branch.replace(/#/g, '%23'),
branch: scmInfo.branch,
owner: scmInfo.owner,
repo: scmInfo.repo
}
Expand Down Expand Up @@ -1362,12 +1373,16 @@ class GithubScm extends Scm {

const { host, owner, repo, branch, rootDir, privateRepo } = await this.lookupScmUri(lookupConfig);

const baseUrl = `${host}/${owner}/${repo}/tree/${branch}`;
const baseUrl = `${host}/${owner}/${repo}/tree/`;
const encodedUrl = `https://${Path.join(
baseUrl,
encodeURIComponent(rootDir ? Path.join(branch, rootDir) : branch)
)}`;

return {
branch,
name: `${owner}/${repo}`,
url: `https://${rootDir ? Path.join(baseUrl, rootDir) : baseUrl}`,
url: encodedUrl,
rootDir: rootDir || '',
private: privateRepo
};
Expand Down
2 changes: 1 addition & 1 deletion test/data/specialCharacterCommands.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "sd-checkout-code",
"command": "export SD_GIT_WRAPPER=\"$(if [ `uname` = 'Darwin' ] || [ ${SD_HAB_ENABLED:-false} = 'false' ]; then echo 'eval'; else echo 'sd-step exec core/git'; fi)\" && if [ ! -z $SD_SCM_DEPLOY_KEY ]; then export SCM_CLONE_TYPE=ssh; fi && echo Exporting environment variables && if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export SCM_URL=git@github.com:screwdriver-cd/guide; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@github.com/screwdriver-cd/guide; else export SCM_URL=https://github.com/screwdriver-cd/guide; fi && export GIT_URL=$SCM_URL.git && export GIT_MERGE_AUTOEDIT=no && if [ ! -z $SD_SCM_DEPLOY_KEY ] && [ $SCM_CLONE_TYPE = ssh ]; then echo $SD_SCM_DEPLOY_KEY | base64 -d > /tmp/git_key && echo \"\" >> /tmp/git_key && chmod 600 /tmp/git_key && export GIT_SSH_COMMAND=\"ssh -i /tmp/git_key\" && mkdir -p ~/.ssh/ && printf \"%s\n\" \"CiAgICAgICAgSG9zdCBnaXRodWIuY29tCiAgICAgICAgICAgIFN0cmljdEhvc3RLZXlDaGVja2luZyBubwogICAgICAgIA==\" | base64 -d >> ~/.ssh/config; fi && echo Setting user name and user email && $SD_GIT_WRAPPER \"git config --global user.name sd-buildbot\" && $SD_GIT_WRAPPER \"git config --global user.email dev-null@screwdriver.cd\" && export SD_CHECKOUT_DIR_FINAL=$SD_SOURCE_DIR && if [ ! -z $SD_CHECKOUT_DIR ]; then export SD_CHECKOUT_DIR_FINAL=$SD_CHECKOUT_DIR; fi && echo 'Cloning github.com/screwdriver-cd/guide, on branch '\"'\"'\"`/@.]!#&%$<>,🚗' && if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $SD_GIT_WRAPPER \"git clone --recursive --quiet --progress --branch ''\\\"'\\\"'\\\"\\`/@.]!#&%$<>,🚗' $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; else if [ ! -z \"$GIT_SHALLOW_CLONE_SINCE\" ]; then export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--shallow-since='$GIT_SHALLOW_CLONE_SINCE'\"; else if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then export GIT_SHALLOW_CLONE_DEPTH=50; fi; export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--depth=$GIT_SHALLOW_CLONE_DEPTH\"; fi; export GIT_SHALLOW_CLONE_BRANCH=\"--no-single-branch\"; if [ \"$GIT_SHALLOW_CLONE_SINGLE_BRANCH\" = true ]; then export GIT_SHALLOW_CLONE_BRANCH=\"\"; fi; $SD_GIT_WRAPPER \"git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH --recursive --quiet --progress --branch ''\\\"'\\\"'\\\"\\`/@.]!#&%$<>,🚗' $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; fi && $SD_GIT_WRAPPER \"git reset --hard '12345' --\" && echo 'Reset to 12345' && export GIT_BRANCH='origin/'\"'\"'\"`/@.]!#&%$<>,🚗' && $SD_GIT_WRAPPER \"git submodule init\" && $SD_GIT_WRAPPER \"git submodule update --recursive\""
"command": "export SD_GIT_WRAPPER=\"$(if [ `uname` = 'Darwin' ] || [ ${SD_HAB_ENABLED:-false} = 'false' ]; then echo 'eval'; else echo 'sd-step exec core/git'; fi)\" && if [ ! -z $SD_SCM_DEPLOY_KEY ]; then export SCM_CLONE_TYPE=ssh; fi && echo Exporting environment variables && if [ ! -z $SCM_CLONE_TYPE ] && [ $SCM_CLONE_TYPE = ssh ]; then export SCM_URL=git@github.com:screwdriver-cd/guide; elif [ ! -z $SCM_USERNAME ] && [ ! -z $SCM_ACCESS_TOKEN ]; then export SCM_URL=https://$SCM_USERNAME:$SCM_ACCESS_TOKEN@github.com/screwdriver-cd/guide; else export SCM_URL=https://github.com/screwdriver-cd/guide; fi && export GIT_URL=$SCM_URL.git && export GIT_MERGE_AUTOEDIT=no && if [ ! -z $SD_SCM_DEPLOY_KEY ] && [ $SCM_CLONE_TYPE = ssh ]; then echo $SD_SCM_DEPLOY_KEY | base64 -d > /tmp/git_key && echo \"\" >> /tmp/git_key && chmod 600 /tmp/git_key && export GIT_SSH_COMMAND=\"ssh -i /tmp/git_key\" && mkdir -p ~/.ssh/ && printf \"%s\n\" \"CiAgICAgICAgSG9zdCBnaXRodWIuY29tCiAgICAgICAgICAgIFN0cmljdEhvc3RLZXlDaGVja2luZyBubwogICAgICAgIA==\" | base64 -d >> ~/.ssh/config; fi && echo Setting user name and user email && $SD_GIT_WRAPPER \"git config --global user.name sd-buildbot\" && $SD_GIT_WRAPPER \"git config --global user.email dev-null@screwdriver.cd\" && export SD_CHECKOUT_DIR_FINAL=$SD_SOURCE_DIR && if [ ! -z $SD_CHECKOUT_DIR ]; then export SD_CHECKOUT_DIR_FINAL=$SD_CHECKOUT_DIR; fi && echo 'Cloning github.com/screwdriver-cd/guide, on branch '\"'\"'\"`/@.]!#&%$<>,🚗' && if [ ! -z $GIT_SHALLOW_CLONE ] && [ $GIT_SHALLOW_CLONE = false ]; then $SD_GIT_WRAPPER \"git clone --recursive --quiet --progress --branch ''\\\"'\\\"'\\\"\\`/@.]!#&%\\$<>,🚗' $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; else if [ ! -z \"$GIT_SHALLOW_CLONE_SINCE\" ]; then export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--shallow-since='$GIT_SHALLOW_CLONE_SINCE'\"; else if [ -z $GIT_SHALLOW_CLONE_DEPTH ]; then export GIT_SHALLOW_CLONE_DEPTH=50; fi; export GIT_SHALLOW_CLONE_DEPTH_OPTION=\"--depth=$GIT_SHALLOW_CLONE_DEPTH\"; fi; export GIT_SHALLOW_CLONE_BRANCH=\"--no-single-branch\"; if [ \"$GIT_SHALLOW_CLONE_SINGLE_BRANCH\" = true ]; then export GIT_SHALLOW_CLONE_BRANCH=\"\"; fi; $SD_GIT_WRAPPER \"git clone $GIT_SHALLOW_CLONE_DEPTH_OPTION $GIT_SHALLOW_CLONE_BRANCH --recursive --quiet --progress --branch ''\\\"'\\\"'\\\"\\`/@.]!#&%\\$<>,🚗' $SCM_URL $SD_CHECKOUT_DIR_FINAL\"; fi && $SD_GIT_WRAPPER \"git reset --hard '12345' --\" && echo 'Reset to 12345' && export GIT_BRANCH='origin/'\"'\"'\"`/@.]!#&%$<>,🚗' && $SD_GIT_WRAPPER \"git submodule init\" && $SD_GIT_WRAPPER \"git submodule update --recursive\""
}
30 changes: 19 additions & 11 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2273,7 +2273,7 @@ jobs:

describe('decorateUrl', () => {
it('decorates a scm uri', () => {
const scmUri = 'github.com:102498:boat';
const scmUri = 'github.com:102498:boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗';

githubMock.request.resolves({
data: {
Expand All @@ -2289,9 +2289,11 @@ jobs:
})
.then(data => {
assert.deepEqual(data, {
branch: 'boat',
branch: 'boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗',
name: 'iAm/theCaptain',
url: 'https://github.com/iAm/theCaptain/tree/boat',
url: `https://github.com/iAm/theCaptain/tree/${encodeURIComponent(
'boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗'
)}`,
rootDir: '',
private: false
});
Expand All @@ -2300,7 +2302,7 @@ jobs:
});

it('decorates a scm uri with rootDir', () => {
const scmUri = 'github.com:102498:boat:src/app/component';
const scmUri = 'github.com:102498:boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗:src/app/component';

githubMock.request.resolves({
data: {
Expand All @@ -2316,9 +2318,11 @@ jobs:
})
.then(data => {
assert.deepEqual(data, {
branch: 'boat',
branch: 'boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗',
name: 'iAm/theCaptain',
url: 'https://github.com/iAm/theCaptain/tree/boat/src/app/component',
url: `https://github.com/iAm/theCaptain/tree/${encodeURIComponent(
'boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗/src/app/component'
)}`,
rootDir: 'src/app/component',
private: false
});
Expand All @@ -2327,10 +2331,12 @@ jobs:
});

it('decorates a scm uri without querying github when scmRepo is passed', () => {
const scmUri = 'github.com:102498:boat';
const scmUri = 'github.com:102498:boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗';
const scmRepo = {
branch: 'boat',
url: 'https://github.com/iAm/theCaptain/tree/boat',
branch: 'boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗',
url: `https://github.com/iAm/theCaptain/tree/${encodeURIComponent(
'boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗'
)}`,
name: 'iAm/theCaptain'
};

Expand All @@ -2342,9 +2348,11 @@ jobs:
})
.then(data => {
assert.deepEqual(data, {
branch: 'boat',
branch: 'boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗',
name: 'iAm/theCaptain',
url: 'https://github.com/iAm/theCaptain/tree/boat',
url: `https://github.com/iAm/theCaptain/tree/${encodeURIComponent(
'boat!"#$%&\'()-=|@`{;+]},<.>/ 🚗'
)}`,
rootDir: '',
private: false
});
Expand Down

0 comments on commit 989ad1b

Please sign in to comment.