Skip to content

Commit e654691

Browse files
feat(core/github): support custom pullsURL and commitHistoryURL (#5100)
1 parent fa293d2 commit e654691

File tree

2 files changed

+148
-9
lines changed

2 files changed

+148
-9
lines changed

src/core/github.js

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,81 @@ export async function run(conf) {
9393
}
9494
const branch = conf.github.branch || "gh-pages";
9595
const issueBase = new URL("./issues/", ghURL).href;
96-
const commitHistoryURL = new URL(
97-
`./commits/${conf.github.branch ?? ""}`,
98-
ghURL.href
99-
);
96+
97+
// Allow custom pullsURL and commitHistoryURL for monorepo scenarios
98+
let pullsURL;
99+
if (
100+
typeof conf.github === "object" &&
101+
conf.github.hasOwnProperty("pullsURL")
102+
) {
103+
pullsURL = conf.github.pullsURL;
104+
} else {
105+
pullsURL = new URL("./pulls/", ghURL).href;
106+
}
107+
108+
// Validate pullsURL if it's provided
109+
if (pullsURL) {
110+
try {
111+
const pullsURLObj = new URL(pullsURL);
112+
if (pullsURLObj.origin !== "https://github.com") {
113+
const msg = docLink`${"[github.pullsURL]"} must be HTTPS and pointing to GitHub. (${pullsURL}).`;
114+
rejectGithubPromise(msg);
115+
return;
116+
}
117+
if (!pullsURLObj.pathname.includes("/pulls")) {
118+
const msg = docLink`${"[github.pullsURL]"} must point to pull requests. (${pullsURL}).`;
119+
rejectGithubPromise(msg);
120+
return;
121+
}
122+
} catch {
123+
const msg = docLink`${"[github.pullsURL]"} is not a valid URL. (${pullsURL}).`;
124+
rejectGithubPromise(msg);
125+
return;
126+
}
127+
}
128+
129+
let commitHistoryURL;
130+
if (
131+
typeof conf.github === "object" &&
132+
conf.github.hasOwnProperty("commitHistoryURL")
133+
) {
134+
commitHistoryURL = conf.github.commitHistoryURL;
135+
} else {
136+
commitHistoryURL = new URL(
137+
`./commits/${conf.github.branch ?? ""}`,
138+
ghURL.href
139+
).href;
140+
}
141+
142+
// Validate commitHistoryURL if it's provided
143+
if (commitHistoryURL) {
144+
try {
145+
const commitURLObj = new URL(commitHistoryURL);
146+
if (commitURLObj.origin !== "https://github.com") {
147+
const msg = docLink`${"[github.commitHistoryURL]"} must be HTTPS and pointing to GitHub. (${commitHistoryURL}).`;
148+
rejectGithubPromise(msg);
149+
return;
150+
}
151+
if (!commitURLObj.pathname.includes("/commits")) {
152+
const msg = docLink`${"[github.commitHistoryURL]"} must point to commits. (${commitHistoryURL}).`;
153+
rejectGithubPromise(msg);
154+
return;
155+
}
156+
} catch {
157+
const msg = docLink`${"[github.commitHistoryURL]"} is not a valid URL. (${commitHistoryURL}).`;
158+
rejectGithubPromise(msg);
159+
return;
160+
}
161+
}
162+
100163
const newProps = {
101164
edDraftURI: `https://${org.toLowerCase()}.github.io/${repo}/`,
102165
githubToken: undefined,
103166
githubUser: undefined,
104167
issueBase,
105168
atRiskBase: issueBase,
106169
otherLinks: [],
107-
pullBase: new URL("./pulls/", ghURL).href,
170+
pullBase: pullsURL,
108171
shortName: repo,
109172
};
110173
// Assign new properties, but retain existing ones
@@ -133,11 +196,11 @@ export async function run(conf) {
133196
},
134197
{
135198
value: l10n.commit_history,
136-
href: commitHistoryURL.href,
199+
href: commitHistoryURL,
137200
},
138201
{
139202
value: "Pull requests",
140-
href: newProps.pullBase,
203+
href: pullsURL,
141204
},
142205
],
143206
};
@@ -152,9 +215,9 @@ export async function run(conf) {
152215
apiBase: githubAPI,
153216
fullName: `${org}/${repo}`,
154217
issuesURL: issueBase,
155-
pullsURL: newProps.pullBase,
218+
pullsURL,
156219
newIssuesURL: new URL("./new/choose", issueBase).href,
157-
commitHistoryURL: commitHistoryURL.href,
220+
commitHistoryURL,
158221
};
159222
resolveGithubPromise(normalizedGHObj);
160223

tests/spec/core/github-spec.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,33 @@ describe("Core - Github", () => {
8181
const doc = await makeRSDoc(opts);
8282
doesntOverrideTest(doc);
8383
});
84+
it("normalizes github object with custom pullsURL and commitHistoryURL", async () => {
85+
const opts = {
86+
config: Object.assign(makeBasicConfig(), {
87+
github: {
88+
repoURL: "https://github.com/w3c/core-aam/",
89+
pullsURL: "https://github.com/w3c/aria/pulls/",
90+
commitHistoryURL: "https://github.com/w3c/aria/commits/",
91+
},
92+
}),
93+
body: makeDefaultBody(),
94+
};
95+
delete opts.config.edDraftURI;
96+
delete opts.config.shortName;
97+
98+
const doc = await makeRSDoc(opts);
99+
const { respecConfig: conf } = doc.defaultView;
100+
101+
// Check that the github object is normalized correctly
102+
expect(conf.github.pullsURL).toBe("https://github.com/w3c/aria/pulls/");
103+
expect(conf.github.commitHistoryURL).toBe(
104+
"https://github.com/w3c/aria/commits/"
105+
);
106+
expect(conf.github.issuesURL).toBe(
107+
"https://github.com/w3c/core-aam/issues/"
108+
);
109+
expect(conf.github.repoURL).toBe("https://github.com/w3c/core-aam/");
110+
});
84111
});
85112
describe("the definition list items (localized)", () => {
86113
const l10n = {
@@ -135,5 +162,54 @@ describe("Core - Github", () => {
135162
"https://github.com/speced/respec/commits/develop"
136163
);
137164
});
165+
it("supports custom pullsURL and commitHistoryURL for monorepo scenarios", async () => {
166+
const customOpt = {
167+
config: Object.assign(makeBasicConfig(), {
168+
github: {
169+
repoURL: "https://github.com/w3c/core-aam/",
170+
pullsURL:
171+
"https://github.com/w3c/aria/pulls?q=+label%3A%22spec%3Acore-aam%22is%3Apr+is%3Aopen+",
172+
commitHistoryURL:
173+
"https://github.com/w3c/aria/commits/main/core-aam",
174+
},
175+
excludeGithubLinks: false,
176+
}),
177+
body: makeDefaultBody(),
178+
htmlAttrs: {
179+
lang: "nl",
180+
},
181+
};
182+
delete customOpt.config.edDraftURI;
183+
delete customOpt.config.shortName;
184+
185+
const doc = await makeRSDoc(customOpt);
186+
187+
// Check that the custom pull request URL with parameters is used
188+
const pullRequests = Array.from(doc.querySelectorAll("dd")).find(
189+
elem => elem.textContent.trim() === "Pull requests"
190+
);
191+
expect(pullRequests).toBeTruthy();
192+
expect(pullRequests.querySelector("a").href).toBe(
193+
"https://github.com/w3c/aria/pulls?q=+label%3A%22spec%3Acore-aam%22is%3Apr+is%3Aopen+"
194+
);
195+
196+
// Check that the custom commit history URL is used
197+
const commitHistory = Array.from(doc.querySelectorAll("dd")).find(
198+
elem => elem.textContent.trim() === "Revisiehistorie"
199+
);
200+
expect(commitHistory).toBeTruthy();
201+
expect(commitHistory.querySelector("a").href).toBe(
202+
"https://github.com/w3c/aria/commits/main/core-aam"
203+
);
204+
205+
// Issue base should still use repoURL
206+
const fileABug = Array.from(doc.querySelectorAll("dd")).find(
207+
elem => elem.textContent.trim() === "Dien een melding in"
208+
);
209+
expect(fileABug).toBeTruthy();
210+
expect(fileABug.querySelector("a").href).toBe(
211+
"https://github.com/w3c/core-aam/issues/"
212+
);
213+
});
138214
});
139215
});

0 commit comments

Comments
 (0)