Skip to content

Commit

Permalink
bitbucket-copy-commit-reference: BitbucketServer: get date from REST API
Browse files Browse the repository at this point in the history
Bitbucket Server has a user setting "Timestamp" with two possible
values: "Relative" and "Absolute".  I was testing only with "Relative",
and unfortunately, the script is broken for users with "Absolute".  In
particular, Bitbucket Server does not store a machine-readable timestamp
in HTML attributes, as I assumed.  For users with "Absolute" timestamps,
the HTML contains the same human-readable string as the UI part.[1]

Rewrite method `getDateIso` to make it independent of the HTML -- use
the REST API to fetch the data about the commit, which includes the
timestamps.[2]  This should not be too big of a performance hit, because
we already do a similar request for pull request links.  Though ideally,
we would want these requests to be parallel.

[1] Side note: the timestamps do not appear to be localized -- I checked
    German and French and both them show the date in English.
[2] Unfortunately, Bitbucket does not include the timezone information,
    unlike Git CLI.
  • Loading branch information
rybak committed Dec 10, 2024
1 parent ab90b33 commit 4af5f80
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 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 15
// @version 16
// @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 @@ -365,14 +365,8 @@
);
}

getDateIso(hash) {
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;

async getDateIso(commitHash) {
return this.#getApiDateIso(commitHash);
}

getCommitMessage(hash) {
Expand All @@ -389,7 +383,7 @@
}

async convertPlainSubjectToHtml(plainTextSubject, commitHash) {
const escapedHtml = await super.convertPlainSubjectToHtml(plainTextSubject);
const escapedHtml = await super.convertPlainSubjectToHtml(plainTextSubject, commitHash);
return await this.#insertPrLinks(await this.#insertJiraLinks(escapedHtml), commitHash);
}

Expand Down Expand Up @@ -529,6 +523,26 @@
}
}

async #getApiDateIso(commitHash) {
const t = await this.#getApiTimestamp(commitHash);
const d = new Date(t);
return d.toISOString().slice(0, 'YYYY-MM-DD'.length);
}

async #getApiTimestamp(commitHash) {
const projectKey = this.#getProjectKey();
const repoSlug = this.#getRepositorySlug();
const url = `/rest/api/latest/projects/${projectKey}/repos/${repoSlug}/commits/${commitHash}`;
try {
const response = await fetch(url);
const obj = await response.json();
return obj.authorTimestamp;
} catch (e) {
error(`Cannot getApiTimestamp url="${url}"`, e);
return NaN;
}
}

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

0 comments on commit 4af5f80

Please sign in to comment.