Skip to content

Commit 66c480b

Browse files
authored
Remember which PR files are collapsed/expanded (#231)
1 parent 5205e78 commit 66c480b

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ By default, Violentmonkey will auto-update scripts from the original install loc
3535
- ![Await comments are highlighted.](assets/await-comments.png)
3636
- **[Not working at the moment](https://github.com/alejandro5042/azdo-userscripts/issues/95):** Folder-level PR diffs are now syntax highlighted!
3737
> Note: Not all highlights will be correct; it can only highlight the code that appears in the diff; so multi-line strings may appear weird. Practically, these sorts of issues are rare and overshadowed by the benefit of syntax highlighting in all other cases.
38+
- In the multi-file diff view, which diffs you have expanded/collapsed will now be remembered as you navigate between folders (or between tabs) in the same PR.
3839

3940
### Better owners review (NI-only)
4041

4142
> These features are only available in [NI](https://www.ni.com) AzDO accounts.
4243
4344
- The PR file tree will now highlight the files you need to review with a letter to represent your role (Owner, Alternate, Reviewer):
4445
- ![Files tree highlighting.](assets/owners-file-tree.png)
45-
- Collapsed files are highlighted if they contain files you need to review:
46+
- Collapsed folders are highlighted if they contain files you need to review:
4647
- ![Highlighted folder.](assets/owners-collapsed-folders.png)
48+
- In the multi-file diff view, files that are not your files are automatically collapsed, unless you are the one that filed the PR
4749
- In the multi-file diff view, your files are also highlighted with a blue hedaer (vs. the typical gray)
4850
- Bypass owners reminder: For PRs into branches requiring a passing `ni/owners-approved` status, hovering over the Approve button pops up a reminder to consider bypassing owners
4951
- Some tags/labels are colored (e.g. red if the label contains "Blocked")

src/azdo-pr-dashboard.user.js

+37-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22

33
// @name More Awesome Azure DevOps (userscript)
4-
// @version 3.6.1
4+
// @version 3.7.0
55
// @author Alejandro Barreto (NI)
66
// @description Makes general improvements to the Azure DevOps experience, particularly around pull requests. Also contains workflow improvements for NI engineers.
77
// @license MIT
@@ -1775,26 +1775,50 @@
17751775
padding: 7px 10px;
17761776
}`);
17771777

1778+
// Update expandedFilesCache when an expand-button is clicked
1779+
// TODO: Make this optional.
1780+
let expandedFilesCache = {};
1781+
document.addEventListener('click', e => {
1782+
const collapseButton = e.target.closest('.bolt-card-expand-button');
1783+
if (collapseButton) {
1784+
const wasExpanded = collapseButton.getAttribute('aria-expanded') === 'true';
1785+
const isExpanded = !wasExpanded;
1786+
const pathWithLeadingSlash = collapseButton.parentElement.querySelector('.secondary-text.text-ellipsis').textContent;
1787+
expandedFilesCache[pathWithLeadingSlash] = isExpanded;
1788+
}
1789+
});
1790+
17781791
eus.onUrl(/\/pullrequest\//gi, async (session, urlMatch) => {
17791792
// Get the current iteration of the PR.
17801793
const prUrl = await getCurrentPullRequestUrlAsync();
1794+
const prCreatedBy = await getCurrentPullRequestCreatedBy();
17811795
// Get owners info for this PR.
17821796
const ownersInfo = await getNationalInstrumentsPullRequestOwnersInfo(prUrl);
17831797
const hasOwnersInfo = ownersInfo && ownersInfo.currentUserFileCount > 0;
1784-
if (!hasOwnersInfo) return;
1798+
const autoCollapse = hasOwnersInfo && currentUser.uniqueName !== prCreatedBy.uniqueName;
1799+
// Reset the cache for each new PR.
1800+
expandedFilesCache = {};
17851801

17861802
session.onEveryNew(document, '.repos-summary-header', diff => {
17871803
const header = diff.children[0];
1788-
const pathWithLeadingSlash = $(header).find('.secondary-text.text-ellipsis')[0].textContent;
1804+
const pathWithLeadingSlash = header.querySelector('.secondary-text.text-ellipsis').textContent;
17891805
const path = pathWithLeadingSlash.substring(1); // Remove leading slash.
17901806

1791-
if (ownersInfo.isCurrentUserResponsibleForFile(path)) {
1792-
$(header).addClass('file-to-review-header');
1807+
if (hasOwnersInfo && ownersInfo.isCurrentUserResponsibleForFile(path)) {
1808+
header.classList.add('file-to-review-header');
17931809

17941810
$('<div class="file-owners-role-header" />').text(`${ownersInfo.currentUserFilesToRole[path]}:`).prependTo(header.children[1]);
1795-
} else {
1796-
// TODO: Make this optional.
1797-
$(header).find('button[aria-label="Collapse"]').click();
1811+
}
1812+
1813+
if (pathWithLeadingSlash in expandedFilesCache) {
1814+
if (!expandedFilesCache[pathWithLeadingSlash]) {
1815+
header.querySelector('button[aria-label="Collapse"]').click();
1816+
}
1817+
} else if (autoCollapse) {
1818+
if (!ownersInfo.isCurrentUserResponsibleForFile(path)) {
1819+
// TODO: Make this optional.
1820+
header.querySelector('button[aria-label="Collapse"]').click();
1821+
}
17981822
}
17991823
});
18001824
});
@@ -2244,6 +2268,11 @@
22442268
return (await getCurrentPullRequestAsync()).url;
22452269
}
22462270

2271+
// Helper function to get the creator of the PR that's currently on screen.
2272+
async function getCurrentPullRequestCreatedBy() {
2273+
return (await getCurrentPullRequestAsync()).createdBy;
2274+
}
2275+
22472276
// Async helper function get info on a single PR. Defaults to the PR that's currently on screen.
22482277
function getPullRequestAsync(id = 0) {
22492278
const actualId = id || getCurrentPullRequestId();

0 commit comments

Comments
 (0)