From 399873946a9643ff604d92579c2e6906bfe835c6 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Tue, 22 Oct 2019 10:47:59 +0200
Subject: [PATCH] fix(platform): encode branchName in URLs (#4694)

---
 lib/platform/bitbucket-server/index.ts |  8 +++++++-
 lib/platform/bitbucket/index.ts        |  6 +++++-
 lib/platform/github/index.ts           | 14 ++++++++++----
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts
index 138b3a509b..56901a18c2 100644
--- a/lib/platform/bitbucket-server/index.ts
+++ b/lib/platform/bitbucket-server/index.ts
@@ -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,
@@ -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 }) => ({
diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts
index 2ae735f1f3..0044ea3064 100644
--- a/lib/platform/bitbucket/index.ts
+++ b/lib/platform/bitbucket/index.ts
@@ -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 */ {
diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts
index 7d47d25d42..0bfc58a4ac 100644
--- a/lib/platform/github/index.ts
+++ b/lib/platform/github/index.ts
@@ -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,
@@ -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 */ {
@@ -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;
 }
@@ -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;
@@ -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',
-- 
GitLab