Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stash review support #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "wakatime-review",
"version": "1.0",
"description": "Measure time spent on review and sends it to wakatime",
"description": "Measures time spent on review and sends it to wakatime",

"icons": {
"48": "icons/pull_request-48.png"
Expand All @@ -20,7 +20,7 @@

"content_scripts": [
{
"matches": ["*://bitbucket.org/*/pull-requests/*"],
"matches": ["<all_urls>"],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That won't work in current incarnation. The script will load for every page, which means it will measure time for every page and we don't want that.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess this would have to stay more or less open like this and we have to guard it in the script itself

"js": ["wakatime-review.js"]
}
],
Expand Down
2 changes: 1 addition & 1 deletion options.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>

<form>
<label>wakatime key<input type="text" id="key"></label>
<label>wakatime key: <input type="text" id="key"></label>
<button type="submit">Save</button>
</form>

Expand Down
78 changes: 59 additions & 19 deletions wakatime-review.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,62 @@

const bitbucket = {
readBranch: () => document.querySelector("#id_source_group .branch a").textContent,
readDomainOwnerAndProject: () => {
const url = window.location.href;
const regexp = /http[s]?:\/\/([a-zA-Z0-9]*\.[a-z]*)\/(\w*)\/([\w-]*).*/g;
const matched = regexp.exec(url);
const domain = matched[1];
const owner = matched[2];
const project = matched[3];
return {
domain: domain,
owner: owner,
project: project
};
}
};


const stash = {
readBranch: () => document.querySelector('div.pull-request-branches').textContent,
readDomainOwnerAndProject: () => {

let grabAfter = (array, after) => {
return array[array.lastIndexOf(after) + 1];
};

const url = new URL(window.location.href);
// example of URL to parse
// https://stash.clearcode.cc/projects/CCADS/repos/backend/pull-requests/137/commits
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. Now I see that for hosted solutions it might work, but for custom ones like yours we should have configuration. And I should definitely add support for github so I will now how much time I have spent on this review

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 exactly. So this would require a bit of work on the configuration part.

const splitUrl = url.pathname.split('/');
return {
domain: url.host,
owner: grabAfter(splitUrl, 'projects'),
project: grabAfter(splitUrl, 'repos')
};
}
};

const funcFactory = () => {
let siteParser;
const parsedUrl = new URL(window.location.href);

if (parsedUrl.host.includes('stash')) {
siteParser = stash;
} else if (parsedUrl.host.includes('bitbucket')) {
siteParser = bitbucket;
}

return {
readDomainOwnerAndProject: siteParser.readDomainOwnerAndProject,
readBranch: siteParser.readBranch
};
};

function trackTime(keyPromise) {
const {domain, owner, project} = readDomainOwnerAndProject();
const {readDomainOwnerAndProject, readBranch} = funcFactory();
const {entity, owner, project} = readDomainOwnerAndProject();
const branch = readBranch();
const entity = "bitbucket.org";
let havenOnlyScrolledInCurrentInterval = false;

function scrollHandler() {
Expand All @@ -15,30 +70,15 @@ function trackTime(keyPromise) {
}
}, 30000);

function readDomainOwnerAndProject() {
const url = window.location.href;
const regexp = /http[s]?:\/\/([a-zA-Z0-9]*\.[a-z]*)\/(\w*)\/([\w-]*).*/g;
const matched = regexp.exec(url);
const domain = matched[1];
const owner = matched[2];
const project = matched[3];
return {owner: owner,
project: project};
}

function readBranch() {
return document.querySelector("#id_source_group .branch a").textContent;
}

function preparePayload(entity, type, project, branch, is_write) {
return {
entity: entity,
type: type,
time: (new Date).getTime()/1000,
time: new Date().getTime()/1000,
project: project,
branch: branch,
is_write: is_write,
editor: "bitbucket.org"
editor: entity
};
}

Expand Down