diff --git a/src/adapters/github.js b/src/adapters/github.js index 4507acf4ed5462b447bfd8680a63f5371fdf1b0b..a081d5855f620408a4267dc41d6f5643222e726e 100644 --- a/src/adapters/github.js +++ b/src/adapters/github.js @@ -112,17 +112,30 @@ class GitHub extends PjaxAdapter { if (!showInNonCodePage && type && !~['tree', 'blob'].indexOf(type)) { return cb() } - + const branchDropdownMenuSummary = $('.branch-select-menu summary'); + const branchNameInTitle = branchDropdownMenuSummary.attr('title'); + const branchNameInSpan = branchDropdownMenuSummary.find('span').text(); + const branchFromSummary = + branchNameInTitle && branchNameInTitle.toLowerCase().startsWith('switch branches') + ? branchNameInSpan + : branchNameInTitle; + // Get branch by inspecting page, quite fragile so provide multiple fallbacks const branch = - // Code page - $('.branch-select-menu .select-menu-item.selected').data('name') || + // Pick the commit ID as branch name when the code page is listing tree in a particular commit + (type === 'commit' && typeId) || + // Pick the commit ID or branch name from the DOM + branchFromSummary || + ($('.overall-summary .numbers-summary .commits a').attr('href') || '').replace( + `/${username}/${reponame}/commits/`, + '' + ) || // Pull requests page ($('.commit-ref.base-ref').attr('title') || ':').match(/:(.*)/)[1] || // Reuse last selected branch if exist (currentRepo.username === username && currentRepo.reponame === reponame && currentRepo.branch) || // Get default branch from cache - this._defaultBranch[username + '/' + reponame] + this._defaultBranch[username + '/' + reponame]; // Still no luck, get default branch for real const repo = {username: username, reponame: reponame, branch: branch, pullNumber: pullNumber}