aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2019-12-08 02:48:54 -0500
committerKevin J Hoerr <kjhoerr@protonmail.com>2019-12-08 02:48:54 -0500
commit08c5e492609c24aa3d410c645f9abed29a353794 (patch)
treeac76942b70eb6157da11f8c61744c935eeb59f06
parent413c6004b30d678b003483a8614291241973f3a8 (diff)
downloadao-coverage-08c5e492609c24aa3d410c645f9abed29a353794.tar.gz
ao-coverage-08c5e492609c24aa3d410c645f9abed29a353794.tar.bz2
ao-coverage-08c5e492609c24aa3d410c645f9abed29a353794.zip
Move from branch to repository document in MongoDB
-rw-r--r--src/metadata.ts89
-rw-r--r--src/routes.ts11
2 files changed, 70 insertions, 30 deletions
diff --git a/src/metadata.ts b/src/metadata.ts
index a25e8bf..c297b38 100644
--- a/src/metadata.ts
+++ b/src/metadata.ts
@@ -3,24 +3,27 @@ import winston from "winston";
import logger_config from "./util/logger";
-/** //FIXME fix document schema
- * Rather than using branches as the core, this should be adopted into the following document model:
- * repo:
- * - org
- * - name
- * - token
- * - branches: {
- * [branchname]: {
- * head
- * }
- * }
- */
-export interface Branch {
- org: string;
- repo: string;
- name: string;
+interface Branch {
+ head: string;
+}
+
+interface BranchList {
+ [branch: string]: Branch;
+}
+
+export interface HeadIdentity {
+ organization: string;
+ repository: string;
+ branch: string;
head: string;
}
+
+export interface Repository {
+ organization: string;
+ name: string;
+ branches: BranchList;
+}
+
const logger = winston.createLogger(logger_config("META"));
class Metadata {
@@ -31,33 +34,61 @@ class Metadata {
}
async getHeadCommit(
- org: string,
- repo: string,
+ organization: string,
+ repository: string,
branch: string
): Promise<string> {
const result = await this.database
- .collection<Branch>("branch")
- .findOne({ org, repo, name: branch });
+ .collection<Repository>("repository")
+ .findOne({
+ organization,
+ name: repository,
+ ["branches." + branch]: { $exists: true, $ne: null }
+ });
- if (result !== null) {
+ if (result !== null && Object.keys(result.branches).includes(branch)) {
+ const limb = result.branches[branch];
logger.debug(
"Found commit %s for ORB %s/%s/%s",
- result.head,
- org,
- repo,
+ limb.head,
+ organization,
+ repository,
branch
);
- return result.head;
+ return limb.head;
} else {
throw Error("Branch not found");
}
}
- async updateBranch(branch: Branch): Promise<boolean> {
- const { head, ...matcher } = branch;
+ async updateBranch(identity: HeadIdentity): Promise<boolean> {
+ const { organization, repository: name, branch, head } = identity;
+ const result = await this.database
+ .collection<Repository>("repository")
+ .findOneAndUpdate(
+ { organization, name },
+ { $set: { ["branches." + branch]: { head } } }
+ );
+
+ if (result.value == null) {
+ return this.createRepository(identity);
+ }
+
+ return result.ok === 1;
+ }
+
+ async createRepository(identity: HeadIdentity): Promise<boolean> {
+ const { organization, repository: name, branch, head } = identity;
+ const repo: Repository = {
+ organization,
+ name,
+ branches: { [branch]: { head } }
+ };
+
const { result } = await this.database
- .collection<Branch>("branch")
- .replaceOne(matcher, branch, { upsert: true });
+ .collection<Repository>("repository")
+ .insertOne(repo);
+
return result.ok === 1;
}
}
diff --git a/src/routes.ts b/src/routes.ts
index 1dbc94f..2c9cf45 100644
--- a/src/routes.ts
+++ b/src/routes.ts
@@ -65,7 +65,12 @@ export default (metadata: Metadata) => {
fs.promises.writeFile(path.join(report_path, "index.html"), contents)
)
.then(() =>
- metadata.updateBranch({ org, repo, name: branch, head: commit })
+ metadata.updateBranch({
+ organization: org,
+ repository: repo,
+ branch,
+ head: commit
+ })
)
.then(result =>
result
@@ -85,6 +90,7 @@ export default (metadata: Metadata) => {
);
},
() => {
+ //FIXME if ORB DNE, should be 404
res.status(500).send("Unknown error occurred");
}
);
@@ -100,6 +106,7 @@ export default (metadata: Metadata) => {
);
},
() => {
+ //FIXME if ORB DNE, should be 404
res.status(500).send("Unknown error occurred");
}
);
@@ -109,6 +116,7 @@ export default (metadata: Metadata) => {
router.get("/v1/:org/:repo/:branch/:commit.svg", (req, res) => {
const { org, repo, branch, commit } = req.params;
+ //FIXME prettify error message?
res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "badge.svg"));
});
@@ -116,6 +124,7 @@ export default (metadata: Metadata) => {
router.get("/v1/:org/:repo/:branch/:commit.html", (req, res) => {
const { org, repo, branch, commit } = req.params;
+ //FIXME prettify error message?
res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "index.html"));
});