diff --git a/otterdog_database/doc/MongoDB_queries.md b/otterdog_database/doc/MongoDB_queries.md index 357537832807b240b7207994bf0fbc444bde466d..0c9425b07e1437c383166f985cfda7f453fa16d6 100644 --- a/otterdog_database/doc/MongoDB_queries.md +++ b/otterdog_database/doc/MongoDB_queries.md @@ -1,5 +1,72 @@ # MongoDB Queries +## A list of documents transformed into document with: github_id, default_branch and default_protection + +```javascript +db.getCollection("branch_security_rules").aggregate([{ + $project:{ + _id:0, + github_id: 1, + repositories: { + $map:{ + input: "$repositories", + as: "repo", + in: { + repo_name: "$$repo.name", + repo_default_branch: "$$repo.default_branch", + branch_protection_rules: "$$repo.branch_protection_rules", + } + } + }, + } +}, { + $unwind: "$repositories" +},{ + $project:{ + _id:0, + github_id: 1, + repo_name: "$repositories.repo_name", + repo_default_branch: "$repositories.repo_default_branch", + repo_branch_protection_rules: "$repositories.branch_protection_rules", + + } +}, { + $sort: { + total_repo_branch_protection_rules: -1 + } +}, { + $project: { + _id:0, + github_id: 1, + repo_name: 1, + repo_default_branch: 1, + repo_branch_protection_rules: { + $filter: { + input: "$repo_branch_protection_rules", + as: "bpr", + cond: { $eq: ["$$bpr.pattern", "$repo_default_branch"] } + } + } + } +},{ + $project: { + _id:0, + github_id: 1, + repo_name: 1, + repo_default_branch: 1, + repo_default_branch_protection: { + $cond: { + if: { $eq: [ "$repo_branch_protection_rules", []]}, + then: "not protected", + else: "protected" + } + } + } +} +]) + +``` + ## A list of documentos where repo_default_branch == repo.branch_protection_rules.pattern ```javascript @@ -65,4 +132,48 @@ db.branch_security_rules.aggregate([ } } ]) +``` + +## A list of documents with github_id, repo_name, repo_default_branch and branch_protection_rules_pattern +```javascript +db.branch_security_rules.aggregate([ + {$project: { + _id: 0, + github_id: 1, + repositories: { + $map: { + input: "$repositories", + as: "repo", + in:{ + default_branch: "$$repo.default_branch", + name: "$$repo.name", + branch_protection_rules: { + $map: { + input: "$$repo.branch_protection_rules", + as: "bpr", + in:{ + pattern: "$$bpr.pattern" + } + } + } + } + } + } + + } + },{ + $unwind: "$repositories" + },{ + $unwind: "$repositories.branch_protection_rules" + },{ + $project:{ + _id:1, + github_id: 1, + repo_name: "$repositories.name", + repo_default_branch: "$repositories.default_branch", + branch_protection_rules_pattern: "$repositories.branch_protection_rules.pattern" + } + }, +]) + ``` \ No newline at end of file