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

WIP: Support multiple spec builds. #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class Controller {
}
let config = JSON.parse(params.config);
Config.validate(config);
config = Config.desugar(config);
if (config.specs.length !== 1) {
throw new Error("not supported with multiple page builds");
}
return require("./auth")(false).then(headers => {
return github({
headers: headers
Expand All @@ -57,7 +61,7 @@ class Controller {
let pr = new PR(`${ prs[0].base.repo.full_name }/${ prs[0].id }`, { id: -1 });
pr.payload = prs[0];
pr.config = config;
let h = Head.fromPR(pr);
let h = Head.fromPR(pr, pr.specs[0]);
return this.testUrl(h.github_url)
.then(_ => h.getUrl(h.urlOptions()))
.then(url => {
Expand Down
34 changes: 15 additions & 19 deletions lib/mixins/fetchable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,27 @@ mustache.escape = v => v;

module.exports = (superclass) => class extends superclass {
getUrl(options) {
if (this.pr.processor == "bikeshed") {
switch (this.specBuild.processor) {
case "bikeshed":
return this.getBikeshedUrl(options);
}

if (this.pr.processor == "respec") {
case "respec":
return this.getRespecUrl(options);
}

if (this.pr.processor == "html") {
case "html":
return this.getHtmlUrl(options);
default:
throw new Error(`Unknown processor ${ processor }`);
}

throw new Error(`Unknown processor ${ this.pr.processor }`);
}

getService() {
if (this.pr.processor == "bikeshed") {
switch (this.specBuild.processor) {
case "bikeshed":
return services.BIKESHED;
}

if (this.pr.processor == "respec") {
case "respec":
return services.RESPEC;
default:
return null;
}

return null;
}

getBikeshedUrl(data) {
Expand All @@ -58,7 +54,7 @@ module.exports = (superclass) => class extends superclass {
}

get file_path() {
return `${ this.owner }/${ this.repo }/${ this.sha }/${ this.pr.config.src_file }`;
return `${ this.owner }/${ this.repo }/${ this.sha }/${ this.specBuild.config.src_file }`;
}

get github_url() {
Expand All @@ -74,7 +70,7 @@ module.exports = (superclass) => class extends superclass {
}

getQuery(data, encode) {
let params = this.pr.config.params || {};
let params = this.specBuild.params || {};
return Object.keys(params).map(k => {
if (!Array.isArray(params[k])) {
params[k] = [params[k]];
Expand All @@ -90,7 +86,7 @@ module.exports = (superclass) => class extends superclass {

urlOptions() {
return {
config: this.pr.config,
config: this.specBuild.config,
pull_request: this.pr.payload,
owner: this.owner,
repo: this.repo,
Expand All @@ -104,7 +100,7 @@ module.exports = (superclass) => class extends superclass {
fetch() {
var url = this.getUrl(this.urlOptions());
console.log(`Fetch: ${ url }`);
var postProcess = postProcessor(this.pr.postProcessingConfig) || postProcessor.noop;
var postProcess = postProcessor(this.specBuild.postProcessingConfig) || postProcessor.noop;
return fetchUrl(url, this.getService()).then(postProcess);
}
};
2 changes: 1 addition & 1 deletion lib/mixins/uploadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require("fs");
module.exports = (superclass) => class extends superclass {
fetch() {
console.log(`Read file: ${ this.filepath }`);
var postProcess = postProcessor(this.pr.postProcessingConfig) || postProcessor.noop;
var postProcess = postProcessor(this.specBuild.postProcessingConfig) || postProcessor.noop;
return new Promise((resolve, reject) => {
fs.readFile(this.filepath, "utf8", (err, body) => {
if (err) {
Expand Down
85 changes: 47 additions & 38 deletions lib/models/branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const MutableCache = require("../mixins/mutable-cache");
const ImmutableCache = require("../mixins/immutable-cache");
const Fetchable = require("../mixins/fetchable");
const Uploadable = require("../mixins/uploadable");
const PROPS = ["pr", "owner", "repo", "branch", "sha"];
const PROPS = ["pr", "specBuild", "owner", "repo", "branch", "sha"];

class Branch {
constructor(options) {
Expand All @@ -22,6 +22,10 @@ class Branch {
return this._pr;
}

get specBuild() {
return this._specBuild;
}

get payload() {
return this._payload;
}
Expand Down Expand Up @@ -60,38 +64,41 @@ class Branch {
}

class Head extends mix(Branch).with(MutableCache, Fetchable) {
static fromPR(pr, filename) {
static fromPR(pr, specBuild, filename) {
return new this({
pr: pr,
owner: pr.payload.head.repo.owner.login,
repo: pr.payload.head.repo.name,
branch: pr.payload.head.ref,
sha: pr.payload.head.sha,
filename: filename
pr: pr,
specBuild: specBuild,
owner: pr.payload.head.repo.owner.login,
repo: pr.payload.head.repo.name,
branch: pr.payload.head.ref,
sha: pr.payload.head.sha,
filename: filename
});
}
}
class Base extends mix(Branch).with(ImmutableCache, Fetchable) {
static fromPR(pr, filename) {
static fromPR(pr, specBuild, filename) {
return new this({
pr: pr,
owner: pr.payload.base.repo.owner.login,
repo: pr.payload.base.repo.name,
branch: pr.payload.base.ref,
sha: pr.payload.base.sha,
filename: filename
pr: pr,
specBuild: specBuild,
owner: pr.payload.base.repo.owner.login,
repo: pr.payload.base.repo.name,
branch: pr.payload.base.ref,
sha: pr.payload.base.sha,
filename: filename
});
}
}
class MergeBase extends mix(Branch).with(ImmutableCache, Fetchable) {
static fromPR(pr, filename) {
static fromPR(pr, specBuild, filename) {
return new this({
pr: pr,
owner: pr.owner,
repo: pr.repo,
branch: pr.payload.base.ref,
sha: pr.merge_base_sha,
filename: filename
pr: pr,
specBuild: specBuild,
owner: pr.owner,
repo: pr.repo,
branch: pr.payload.base.ref,
sha: pr.merge_base_sha,
filename: filename
});
}
}
Expand All @@ -108,29 +115,31 @@ class Wattsi extends Branch {
}

class WattsiHead extends mix(Wattsi).with(MutableCache, Uploadable) {
static fromPR(pr, filepath, filename) {
static fromPR(pr, specBuild, filepath, filename) {
return new this({
pr: pr,
owner: pr.payload.head.repo.owner.login,
repo: pr.payload.head.repo.name,
branch: pr.payload.head.ref,
sha: pr.payload.head.sha,
filepath: filepath,
filename: filename
pr: pr,
specBuild: specBuild,
owner: pr.payload.head.repo.owner.login,
repo: pr.payload.head.repo.name,
branch: pr.payload.head.ref,
sha: pr.payload.head.sha,
filepath: filepath,
filename: filename
});
}
}

class WattsiMergeBase extends mix(Wattsi).with(ImmutableCache, Uploadable) {
static fromPR(pr, filepath, filename) {
static fromPR(pr, specBuild, filepath, filename) {
return new this({
pr: pr,
owner: pr.owner,
repo: pr.repo,
branch: pr.payload.base.ref,
sha: pr.merge_base_sha,
filepath: filepath,
filename: filename
pr: pr,
specBuild: specBuild,
owner: pr.owner,
repo: pr.repo,
branch: pr.payload.base.ref,
sha: pr.merge_base_sha,
filepath: filepath,
filename: filename
});
}
}
Expand Down
128 changes: 83 additions & 45 deletions lib/models/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,87 @@
"title": "PR Preview Config",
"description": "Config file for PR Preview.",
"properties": {
"type": {
"definition": "The spec processor used to render this spec.",
"enum": ["bikeshed", "Bikeshed", "BikeShed", "respec", "Respec", "ReSpec", "wattsi", "Wattsi", "html", "HTML"]
},
"src_file": {
"definition": "Relative path to the source file from the root of the repository",
"type": "string",
"minLength":1
},
"params": {
"definition": "parameters for the spec processor",
"type": "object",
"additionalProperties": {
"anyOf": [
{ "type": "string" },
{ "type": "number" },
{ "type": "boolean" },
{ "type": "null" }
]
}
},
"post_processing": {
"definition": "Set a post processor.",
"type": "object",
"properties": {
"name": {
"definition": "Name of the post-processor",
"enum": ["emu-algify", "webidl-grammar"]
},
"options": {
"definition": "Options object for the post-processor",
"type": "object",
"additionalProperties": true
}
},
"required": ["name"]
},
"multipage": {
"definition": "Whether the rendered spec is multipage.",
"type": "boolean"
}
"specs": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"allOf": [{"$ref": "#/$defs/common_config"}],
"required": ["src_file"]
}
}
},
"additionalProperties": true,
"required": ["src_file", "type"]
}

"allOf": [
{"$ref": "#/$defs/common_config"},
{
"oneOf": [
{"required": ["src_file"]},
{"required": ["specs"]}
]
},
{
"anyOf": [
{"required": ["type"]},
{"properties": {"specs": {"items": {"required": ["type"]}}}}
]
},
{
"anyOf": [
{"properties": {"specs": {"maxItems": 1}}},
{
"properties": {
"type": {"not": {"enum": ["wattsi", "Wattsi"]}},
"specs": {"items": {"properties": {"type": {"not": {"enum": ["wattsi", "Wattsi"]}}}}}
}
}
]
}
],
"$defs": {
"common_config": {
"properties": {
"src_file": {
"definition": "Relative path to the source file from the root of the repository",
"type": "string",
"minLength": 1
},
"type": {
"definition": "The spec processor used to render this spec.",
"enum": ["bikeshed", "Bikeshed", "BikeShed", "respec", "Respec", "ReSpec", "wattsi", "Wattsi", "html", "HTML"]
},
"params": {
"definition": "parameters for the spec processor",
"type": "object",
"additionalProperties": {
"anyOf": [
{"type": "string"},
{"type": "number"},
{"type": "boolean"},
{"type": "null"}
]
}
},
"post_processing": {
"definition": "Set a post processor.",
"type": "object",
"properties": {
"name": {
"definition": "Name of the post-processor",
"enum": ["emu-algify", "webidl-grammar"]
},
"options": {
"definition": "Options object for the post-processor",
"type": "object",
"additionalProperties": true
}
},
"required": ["name"]
},
"multipage": {
"definition": "Whether the rendered spec is multipage.",
"type": "boolean"
}
}
}
}
}
Loading