Skip to content

Commit

Permalink
bitbucket-copy-commit-reference: support Bitbucket Server 8.9.*
Browse files Browse the repository at this point in the history
  • Loading branch information
rybak committed Sep 6, 2024
1 parent 11f7f2b commit 7f3790b
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions bitbucket-copy-commit-reference.user.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ==UserScript==
// @name Bitbucket: copy commit reference
// @namespace https://github.com/rybak/atlassian-tweaks
// @version 10
// @version 11
// @description Adds a "Copy commit reference" link to every commit page on Bitbucket Cloud and Bitbucket Server.
// @license AGPL-3.0-only
// @author Andrei Rybak
Expand Down Expand Up @@ -292,6 +292,7 @@
* clicking to a commit page.
*/
static #SHA_LINK_SELECTOR = '.commit-badge-oneline .commit-details .commitid';
static #BITBUCKET_SERVER_8_COMMIT_HASH = '#commit-details-container .commit-hash a';

getLoadedSelector() {
/*
Expand All @@ -303,11 +304,13 @@
}

isRecognized() {
return document.querySelector(BitbucketServer.#SHA_LINK_SELECTOR) != null;
return document.querySelector(BitbucketServer.#SHA_LINK_SELECTOR) != null ||
document.querySelector(BitbucketServer.#BITBUCKET_SERVER_8_COMMIT_HASH != null) ||
document.querySelector('html.cm-s-stash-default') != null;
}

getTargetSelector() {
return '.plugin-section-secondary';
return '.plugin-section-secondary, .commit-details-summary-panel';
}

wrapButtonContainer(container) {
Expand All @@ -317,9 +320,15 @@

wrapButton(button) {
const icon = document.createElement('span');
icon.classList.add('aui-icon', 'aui-icon-small', 'aui-iconfont-copy');
icon.classList.add('aui-icon', 'aui-icon-small', 'aui-iconfont-copy',
'css-1ujqpe8' // BitbucketServer 8.9.*
);
const buttonText = this.getButtonText();
button.replaceChildren(icon, document.createTextNode(` ${buttonText}`));
const buttonTextSpan = document.createElement('span');
buttonTextSpan.classList.add('css-19r5em7'); // BitbucketServer 8.9.*
buttonTextSpan.appendChild(document.createTextNode(` ${buttonText}`));
button.classList.add('css-9bherd'); // BitbucketServer 8.9.*
button.replaceChildren(icon, buttonTextSpan);
button.title = "Copy commit reference to clipboard";
return button;
}
Expand All @@ -344,21 +353,39 @@
}

getFullHash() {
const commitAnchor = document.querySelector(BitbucketServer.#SHA_LINK_SELECTOR);
const commitHash = commitAnchor.getAttribute('data-commitid');
return commitHash;
return this.onAuiVersion(
() => {
const commitAnchor = document.querySelector(BitbucketServer.#SHA_LINK_SELECTOR);
const commitHash = commitAnchor.getAttribute('data-commitid');
return commitHash;
}, () => {
const commitAnchor = document.querySelector(BitbucketServer.#BITBUCKET_SERVER_8_COMMIT_HASH);
return commitAnchor.href.slice(-40, -1);
}
);
}

getDateIso(hash) {
const commitTimeTag = document.querySelector('.commit-badge-oneline .commit-details time');
const commitTimeTag = this.onAuiVersion(
() => document.querySelector('.commit-badge-oneline .commit-details time'),
() => document.querySelector('#commit-details-container .commit-summary-date')
);
const dateIso = commitTimeTag.getAttribute('datetime').slice(0, 'YYYY-MM-DD'.length);
return dateIso;

}

getCommitMessage(hash) {
const commitAnchor = document.querySelector(BitbucketServer.#SHA_LINK_SELECTOR);
const commitMessage = commitAnchor.getAttribute('data-commit-message');
return commitMessage;
return this.onAuiVersion(
() => {
const commitAnchor = document.querySelector(BitbucketServer.#SHA_LINK_SELECTOR);
const commitMessage = commitAnchor.getAttribute('data-commit-message');
return commitMessage;
},
() => {
return document.querySelector('#commit-details-container .commit-message').innerText;
}
);
}

async convertPlainSubjectToHtml(plainTextSubject, commitHash) {
Expand Down Expand Up @@ -491,6 +518,14 @@
return text;
}
}

onAuiVersion(eight, nine) {
if (parseInt(document.body.dataset.auiVersion.split('.')[0]) > 8) {
return nine();
} else {
return eight();
}
}
}

CopyCommitReference.runForGitHostings(new BitbucketCloud(), new BitbucketServer());
Expand Down

0 comments on commit 7f3790b

Please sign in to comment.