Skip to content

Commit 4fae119

Browse files
refactor: extract findJobCrumb()
1 parent fbe4125 commit 4fae119

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

src/JenkinsPage.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,25 @@ export class JenkinsPage {
3535
}
3636
}
3737

38+
findJobCrumb() : Crumb | null {
39+
let result: Crumb | null = null;
40+
this.reverseCrumb((crumb: Crumb, key: number) => {
41+
const urlParts = JenkinsHelpers.splitPath(crumb.href);
42+
const numParts = urlParts.length;
43+
if (numParts >= 2) {
44+
const isUrlToJob =
45+
"job" === urlParts[numParts - 2];
46+
if (isUrlToJob) {
47+
result = crumb;
48+
return false;
49+
}
50+
return true;
51+
}
52+
return false;
53+
});
54+
return result;
55+
}
56+
3857
firstCrumb(): Crumb {
3958
return this.crumbs[0];
4059
}

src/actions/JenkinsConfigure.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,17 @@ export class JenkinsConfigure extends GoToAction {
88
return null;
99
}
1010

11-
const selector = JenkinsHelpers.getBreadcrumbItemSelector(bodyElement);
12-
const breadCrumbListItems = doc.querySelectorAll(selector);
13-
if (breadCrumbListItems) {
14-
const urlParts = JenkinsHelpers.splitUrlPath(urlString);
15-
const isUrlToRun =
16-
"job" === urlParts[urlParts.length - 3] &&
17-
JenkinsHelpers.isInteger(urlParts[urlParts.length - 1]);
18-
if (isUrlToRun) {
19-
const penultimateItem = breadCrumbListItems.item(
20-
breadCrumbListItems.length - 2
21-
);
22-
const anchor = penultimateItem.querySelector("a");
23-
if (anchor) {
24-
const path = anchor.getAttribute("href");
25-
if (path) {
26-
return (
27-
JenkinsHelpers.buildUrl(urlString, path) +
28-
"configure"
29-
);
30-
}
31-
}
32-
return null;
33-
}
34-
const lastItem = breadCrumbListItems.item(
35-
breadCrumbListItems.length - 1
36-
);
37-
const anchor = lastItem.querySelector("a");
38-
if (anchor) {
39-
const path = anchor.getAttribute("href");
40-
if (path) {
41-
return (
42-
JenkinsHelpers.buildUrl(urlString, path) + "configure"
43-
);
44-
}
45-
}
11+
const jenkinsPage = JenkinsHelpers.parsePage(
12+
bodyElement,
13+
urlString
14+
);
15+
const jobCrumb = jenkinsPage.findJobCrumb();
16+
if (jobCrumb) {
17+
const path = jobCrumb.href;
18+
return jenkinsPage.buildUrl(path + "configure");
4619
}
47-
return null;
20+
const lastCrumb = jenkinsPage.lastCrumb();
21+
const path = lastCrumb.href;
22+
return jenkinsPage.buildUrl(path + "configure");
4823
}
4924
}

0 commit comments

Comments
 (0)