Skip to content

Commit

Permalink
fix(platform): encode branchName in URLs (#4694)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Oct 22, 2019
1 parent c78d651 commit 3998739
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/platform/bitbucket-server/index.ts
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
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
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

0 comments on commit 3998739

Please sign in to comment.