Skip to content

Commit

Permalink
fix: Make special characters available in branch names (#542)
Browse files Browse the repository at this point in the history
* update for full-width space

* Fix regex and Add tests
  • Loading branch information
yakanechi authored Nov 13, 2023
1 parent b78e80f commit a2c16b6
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 103 deletions.
4 changes: 2 additions & 2 deletions config/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ module.exports = {
// Third group: repo (e.g. data-schema)
// Fourth group: branch name (e.g. #branchName)
// Fifth group: root dir (e.g. :path/to/source/dir)
// eslint-disable-next-line max-len
CHECKOUT_URL:
/^(?:(?:https:\/\/(?:[^@/:\s]+@)?)|git@|org-\d+@)+([^/:\s]+)(?:\/|:)([^/:\s]+)\/([^\s]+?)(?:\.git)(#[^:\s]+)?(:[^\s]+)?$/,
// eslint-disable-next-line no-irregular-whitespace
/^(?:(?:https:\/\/(?:[^@/:\s]+@)?)|git@|org-\d+@)+([^/:\s]+)(?:\/|:)([^/:\s]+)\/([^\s]+?)(?:\.git)(#[^: \s]* [^: \s]*|#[^: \s]+)?(:[^: \s]* [^: \s]*|:[^: \s]+)?$/,
// scmUri. For example: github.com:abc-123:master or bitbucket.org:{123}:master
// Optionally, can have rootDir. For example: github.com:abc-123:master:src/app/component
SCM_URI: /^([^:]+):([^:]+):([^:]+)(?::([^:]+))?$/,
Expand Down
226 changes: 125 additions & 101 deletions test/config/regex.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,116 +311,140 @@ describe('config regex', () => {
const org = 'screwdriver-cd';
const repo = 'data-schema';
const bitbucketRepo = 'data.schema';
const branchName = '#foobar';
const rootDir = ':path/to/source/dir';
const generalBranchName = '#foobar';
const generalRootDir = ':path/to/source/dir';
const specialBranchName = '#!"#$%&\'()-=|@`{;+]},<.>/ a';
const specialRootDir = ':!"#$%&\'()-=|@`{;+]},<.>/ b';

describe('checks good checkout Url', () => {
const githubHttps = `https://${github}/${org}/${repo}.git`;
const tests = [
{
url: githubHttps,
match: [githubHttps, github, org, repo, null, null]
},
{
url: `${githubHttps}${branchName}`,
match: [`${githubHttps}${branchName}`, github, org, repo, branchName, null]
},
{
url: `${githubHttps}${branchName}${rootDir}`,
match: [`${githubHttps}${branchName}${rootDir}`, github, org, repo, branchName, rootDir]
},
{
url: `git@${github}:${org}/${repo}.git`,
match: [`git@${github}:${org}/${repo}.git`, github, org, repo, null, null]
},
{
url: `git@${github}:${org}/${repo}.git${branchName}`,
match: [`git@${github}:${org}/${repo}.git${branchName}`, github, org, repo, branchName, null]
},
{
url: `git@${github}:${org}/${repo}.git${branchName}${rootDir}`,
match: [
`git@${github}:${org}/${repo}.git${branchName}${rootDir}`,
github,
org,
repo,
branchName,
rootDir
]
},
{
url: `https://screwdriver-cd@${bitbucket}/${org}/${repo}.git`,
match: [`https://screwdriver-cd@${bitbucket}/${org}/${repo}.git`, bitbucket, org, repo, null, null]
},
{
url: `https://screwdriver-cd@${bitbucket}/${org}/${repo}.git${branchName}`,
match: [
`https://screwdriver-cd@${bitbucket}/${org}/${repo}.git${branchName}`,
bitbucket,
org,
repo,
branchName,
null
]
},
{
url: `https://user@${bitbucket}/${org}/${repo}.git${branchName}${rootDir}`,
match: [
`https://user@${bitbucket}/${org}/${repo}.git${branchName}${rootDir}`,
bitbucket,
org,
repo,
branchName,
rootDir
]
},
{
url: `git@${bitbucket}:${org}/${repo}.git`,
match: [`git@${bitbucket}:${org}/${repo}.git`, bitbucket, org, repo, null, null]
},
{
url: `git@${bitbucket}:${org}/${repo}.git${branchName}`,
match: [`git@${bitbucket}:${org}/${repo}.git${branchName}`, bitbucket, org, repo, branchName, null]
},
{
url: `git@${bitbucket}:${org}/${bitbucketRepo}.git${branchName}`,
match: [
`git@${bitbucket}:${org}/${bitbucketRepo}.git${branchName}`,
bitbucket,
org,
bitbucketRepo,
branchName,
null
]
},
{
url: `org-1234@${bitbucket}:${org}/${bitbucketRepo}.git${branchName}`,
match: [
`org-1234@${bitbucket}:${org}/${bitbucketRepo}.git${branchName}`,
bitbucket,
org,
bitbucketRepo,
branchName,
null
]
const createTests = (branchName, rootDir) => {
return [
{
url: githubHttps,
match: [githubHttps, github, org, repo, null, null]
},
{
url: `${githubHttps}${branchName}`,
match: [`${githubHttps}${branchName}`, github, org, repo, branchName, null]
},
{
url: `${githubHttps}${branchName}${rootDir}`,
match: [`${githubHttps}${branchName}${rootDir}`, github, org, repo, branchName, rootDir]
},
{
url: `git@${github}:${org}/${repo}.git`,
match: [`git@${github}:${org}/${repo}.git`, github, org, repo, null, null]
},
{
url: `git@${github}:${org}/${repo}.git${branchName}`,
match: [`git@${github}:${org}/${repo}.git${branchName}`, github, org, repo, branchName, null]
},
{
url: `git@${github}:${org}/${repo}.git${branchName}${rootDir}`,
match: [
`git@${github}:${org}/${repo}.git${branchName}${rootDir}`,
github,
org,
repo,
branchName,
rootDir
]
},
{
url: `https://screwdriver-cd@${bitbucket}/${org}/${repo}.git`,
match: [
`https://screwdriver-cd@${bitbucket}/${org}/${repo}.git`,
bitbucket,
org,
repo,
null,
null
]
},
{
url: `https://screwdriver-cd@${bitbucket}/${org}/${repo}.git${branchName}`,
match: [
`https://screwdriver-cd@${bitbucket}/${org}/${repo}.git${branchName}`,
bitbucket,
org,
repo,
branchName,
null
]
},
{
url: `https://user@${bitbucket}/${org}/${repo}.git${branchName}${rootDir}`,
match: [
`https://user@${bitbucket}/${org}/${repo}.git${branchName}${rootDir}`,
bitbucket,
org,
repo,
branchName,
rootDir
]
},
{
url: `git@${bitbucket}:${org}/${repo}.git`,
match: [`git@${bitbucket}:${org}/${repo}.git`, bitbucket, org, repo, null, null]
},
{
url: `git@${bitbucket}:${org}/${repo}.git${branchName}`,
match: [
`git@${bitbucket}:${org}/${repo}.git${branchName}`,
bitbucket,
org,
repo,
branchName,
null
]
},
{
url: `git@${bitbucket}:${org}/${bitbucketRepo}.git${branchName}`,
match: [
`git@${bitbucket}:${org}/${bitbucketRepo}.git${branchName}`,
bitbucket,
org,
bitbucketRepo,
branchName,
null
]
},
{
url: `org-1234@${bitbucket}:${org}/${bitbucketRepo}.git${branchName}`,
match: [
`org-1234@${bitbucket}:${org}/${bitbucketRepo}.git${branchName}`,
bitbucket,
org,
bitbucketRepo,
branchName,
null
]
}
];
};

[createTests(generalBranchName, generalRootDir), createTests(specialBranchName, specialRootDir)].forEach(
tests => {
tests.forEach(test => {
it(`correctly validates ${test.url}`, () => {
assert.deepEqual(
JSON.stringify(config.regex.CHECKOUT_URL.exec(test.url), null, 4),
JSON.stringify(test.match, null, 4)
);
});
});
}
];

tests.forEach(test => {
it(`correctly validates ${test.url}`, () => {
assert.deepEqual(
JSON.stringify(config.regex.CHECKOUT_URL.exec(test.url), null, 4),
JSON.stringify(test.match, null, 4)
);
});
});
);
});

it('fails on bad checkout Url', () => {
assert.isFalse(config.regex.CHECKOUT_URL.test('https://github.com/screwdriver-cd/'));
assert.isFalse(config.regex.CHECKOUT_URL.test(`git@${org}/${repo}.git`));
assert.isFalse(config.regex.CHECKOUT_URL.test(`git@${org}/${repo}.git#${rootDir}`));
assert.isFalse(config.regex.CHECKOUT_URL.test(`git@${org}/${repo}.git${branchName}:`));
assert.isFalse(config.regex.CHECKOUT_URL.test(`git@${org}/${repo}.git#${generalRootDir}`));
assert.isFalse(config.regex.CHECKOUT_URL.test(`git@${org}/${repo}.git${generalBranchName}:`));
assert.isFalse(config.regex.CHECKOUT_URL.test(`git@${org}/${repo}.git#${specialRootDir}`));
assert.isFalse(config.regex.CHECKOUT_URL.test(`git@${org}/${repo}.git${specialBranchName}:`));
});
});

Expand Down

0 comments on commit a2c16b6

Please sign in to comment.