Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(platform): encode branchName in URLs #4694

Merged
merged 6 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/platform/bitbucket-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,8 @@ export async function findPr(

// Pull Request

const escapeHash = input => (input ? input.replace(/#/g, '%23') : input);

export async function createPr(
branchName: string,
title: string,
Expand All @@ -743,7 +745,11 @@ export async function createPr(
)).body;

const defReviewers = (await api.get(
`./rest/default-reviewers/1.0/projects/${config.projectKey}/repos/${config.repositorySlug}/reviewers?sourceRefId=refs/heads/${branchName}&targetRefId=refs/heads/${base}&sourceRepoId=${id}&targetRepoId=${id}`
`./rest/default-reviewers/1.0/projects/${config.projectKey}/repos/${
config.repositorySlug
}/reviewers?sourceRefId=refs/heads/${escapeHash(
branchName
)}&targetRefId=refs/heads/${base}&sourceRepoId=${id}&targetRepoId=${id}`
)).body;

reviewers = defReviewers.map((u: { name: string }) => ({
Expand Down
6 changes: 5 additions & 1 deletion lib/platform/bitbucket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,11 +712,15 @@ export function getPrBody(input: string) {
.replace(/\]\(\.\.\/pull\//g, '](../../pull-requests/');
}

const escapeHash = input => (input ? input.replace(/#/g, '%23') : input);

// Return the commit SHA for a branch
async function getBranchCommit(branchName: string) {
try {
const branch = (await api.get(
`/2.0/repositories/${config.repository}/refs/branches/${branchName}`
`/2.0/repositories/${config.repository}/refs/branches/${escapeHash(
branchName
)}`
)).body;
return branch.target.hash;
} catch (err) /* istanbul ignore next */ {
Expand Down
14 changes: 10 additions & 4 deletions lib/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const defaults = {
endpoint: 'https://api.github.com/',
};

const escapeHash = input => (input ? input.replace(/#/g, '%23') : input);

export async function initPlatform({
endpoint,
token,
Expand Down Expand Up @@ -431,7 +433,7 @@ export async function getRepoForceRebase() {
async function getBranchCommit(branchName: string) {
try {
const res = await api.get(
`repos/${config.repository}/git/refs/heads/${branchName}`
`repos/${config.repository}/git/refs/heads/${escapeHash(branchName)}`
);
return res.body.object.sha;
} catch (err) /* istanbul ignore next */ {
Expand Down Expand Up @@ -459,7 +461,7 @@ async function getBranchProtection(branchName: string) {
return {};
}
const res = await api.get(
`repos/${config.repository}/branches/${branchName}/protection`
`repos/${config.repository}/branches/${escapeHash(branchName)}/protection`
);
return res.body;
}
Expand Down Expand Up @@ -574,7 +576,9 @@ export async function getBranchStatus(
logger.warn({ requiredStatusChecks }, `Unsupported requiredStatusChecks`);
return 'failed';
}
const commitStatusUrl = `repos/${config.repository}/commits/${branchName}/status`;
const commitStatusUrl = `repos/${config.repository}/commits/${escapeHash(
branchName
)}/status`;
let commitStatus;
try {
commitStatus = (await api.get(commitStatusUrl)).body;
Expand All @@ -595,7 +599,9 @@ export async function getBranchStatus(
let checkRuns: { name: string; status: string; conclusion: string }[] = [];
if (!config.isGhe) {
try {
const checkRunsUrl = `repos/${config.repository}/commits/${branchName}/check-runs`;
const checkRunsUrl = `repos/${config.repository}/commits/${escapeHash(
branchName
)}/check-runs`;
const opts = {
headers: {
Accept: 'application/vnd.github.antiope-preview+json',
Expand Down