Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204import Foundation
enum PolicyTools { static let commands: Set<String> = [ "policy-environment", "policy-mirror-environment", "policy-branch-protection", "policy-branch-ruleset", "policy-ruleset", "policy-secrets", "policy-repository-secrets", "policy-release-tag-ids", "policy-branch-rule-ids", "policy-release-run", "check-ruby-syntax", ]
static func run(_ command: String, _ args: [String]) throws { if command == "check-ruby-syntax" { try rubySyntax(args) return } do { switch (command, args.count) { case ("policy-environment", 4): try releaseEnvironment(load(args[0]), policies: load(args[1]), owner: args[2], branch: branch(args[3])) case ("policy-mirror-environment", 3): try deploymentPolicy(load(args[0]), policies: load(args[1]), branch: branch(args[2]), label: "mirror") case ("policy-branch-protection", 1): try branchProtection(load(args[0])) case ("policy-branch-ruleset", 2): let (includes, types) = try ruleset(load(args[0]), target: "branch") let allowed: Set<String> = ["refs/heads/\(try branch(args[1]))", "~DEFAULT_BRANCH", "~ALL"] guard !allowed.isDisjoint(with: includes), types.isSuperset(of: ["non_fast_forward", "deletion"]) else { throw DevError("The default branch ruleset must prohibit force pushes and deletions") } case ("policy-ruleset", 1): let (includes, types) = try ruleset(load(args[0]), target: "tag") guard includes == ["refs/tags/v*"], types.isSuperset(of: ["update", "deletion"]) else { throw DevError("The release tag ruleset must prohibit updates and deletions for v* tags") } case ("policy-secrets", 2...), ("policy-repository-secrets", 2...): let names = try secretNames(load(args[0])) let requested = Set(args.dropFirst().map { $0.uppercased() }) let invalid = command == "policy-secrets" ? requested.subtracting(names) : requested.intersection(names) if !invalid.isEmpty { let prefix = command == "policy-secrets" ? "Missing environment secrets: " : "Remove repository-level signing or mirror secrets in Settings: " throw DevError(prefix + invalid.sorted().joined(separator: ", ")) } case ("policy-release-tag-ids", 1), ("policy-branch-rule-ids", 1): let entries = try array(load(args[0]), "Rulesets") var ids: [String] = [] for entry in entries { let rule = try object(entry, "Ruleset") let matches = command == "policy-release-tag-ids" ? rule["name"] == .string("release-tags") : rule["target"] == .string("branch") && rule["enforcement"] == .string("active") if matches { ids.append(try identifier(rule["id"], "Ruleset has an invalid ID")) } } if !ids.isEmpty { print(ids.joined(separator: "\n")) } case ("policy-release-run", 3): for entry in try array(load(args[0]), "Release runs") { let run = try object(entry, "Release run") if run["headBranch"] == .string(args[1]), run["displayTitle"] == .string(args[2]), run["event"] == .string("workflow_dispatch") { print(try identifier(run["databaseId"], "Release run has an invalid database ID")) break } } default: throw DevError( "Expected environment, mirror-environment, branch-protection, branch-ruleset, ruleset, secrets, or repository-secrets validation" ) } } catch { throw DevError("Release policy: \(error)") } }
static func load(_ path: String) throws -> DevJSON { try JSONDecoder().decode(DevJSON.self, from: Data(contentsOf: URL(fileURLWithPath: path))) }
static func object(_ value: DevJSON?, _ label: String) throws -> [String: DevJSON] { guard case .object(let result) = value else { throw DevError("\(label) must be a JSON object") } return result }
static func array(_ value: DevJSON?, _ label: String) throws -> [DevJSON] { guard case .array(let result) = value else { throw DevError("\(label) must be a JSON array") } return result }
private static func branch(_ value: String) throws -> String { guard !value.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { throw DevError("The configured default branch must not be empty") } return value }
private static func identifier(_ value: DevJSON?, _ message: String) throws -> String { guard case .number(let number) = value, number.isFinite, number >= 1, number <= 9_007_199_254_740_991, number.rounded() == number else { throw DevError(message) } return String(Int64(number)) }
private static func deploymentPolicy(_ value: DevJSON, policies: DevJSON, branch: String, label: String) throws { let environment = try object(value, "\(label.capitalized) environment") let policy = try object(environment["deployment_branch_policy"], "Deployment branch policy") guard policy["protected_branches"] == .bool(false), policy["custom_branch_policies"] == .bool(true) else { throw DevError("The \(label) environment must use custom deployment branch policies") } let entries = try array(object(policies, "Deployment policies")["branch_policies"], "Deployment policies") guard entries.count == 1 else { throw DevError("The \(label) environment must allow only branch \(branch)") } let entry = try object(entries[0], "Deployment policy") guard entry["type"] == .string("branch"), entry["name"] == .string(branch) else { throw DevError("The \(label) environment must allow only branch \(branch)") } }
private static func releaseEnvironment(_ value: DevJSON, policies: DevJSON, owner: String, branch: String) throws { let environment = try object(value, "Release environment") guard let ownerID = Int64(owner), ownerID > 0, ownerID <= 9_007_199_254_740_991 else { throw DevError("The repository owner ID must be positive") } var reviewers: [DevJSON] = [] for entry in try array(environment["protection_rules"], "Environment protection rules") { let rule = try object(entry, "Environment protection rule") if rule["type"] == .string("required_reviewers") { reviewers += try array(rule["reviewers"], "Environment reviewers") } } let message = "The release environment must require the repository owner as its only reviewer" guard reviewers.count == 1 else { throw DevError(message) } let reviewer = try object(reviewers[0], "Environment reviewer") let identity = try object(reviewer["reviewer"], "Reviewer identity") guard reviewer["type"] == .string("User"), identity["id"] == .number(Double(ownerID)) else { throw DevError(message) } guard environment["can_admins_bypass"] == .bool(false) else { throw DevError("The release environment must disable administrator review bypass") } try deploymentPolicy(value, policies: policies, branch: branch, label: "release") }
private static func ruleset(_ value: DevJSON, target: String) throws -> ([String], Set<String>) { let rule = try object(value, "Ruleset") guard rule["target"] == .string(target), rule["enforcement"] == .string("active") else { throw DevError("The \(target) ruleset must be active") } guard rule["bypass_actors"] == .array([]) else { throw DevError("The \(target) ruleset must prohibit bypasses") } let conditions = try object( object(rule["conditions"], "Ruleset conditions")["ref_name"], "Ruleset ref conditions") let includes = try array(conditions["include"], "Ruleset included refs").map { ref in guard case .string(let name) = ref, !name.isEmpty else { throw DevError("Ruleset included refs must be nonempty strings") } return name } guard conditions["exclude"] == .array([]) else { throw DevError("The \(target) ruleset must not exclude refs") } let types = try array(rule["rules"], "Ruleset rules").map { entry in guard case .string(let kind) = try object(entry, "Ruleset rule")["type"], !kind.isEmpty else { throw DevError("Ruleset rule types must be nonempty strings") } return kind } return (includes, Set(types)) }
private static func branchProtection(_ value: DevJSON) throws { let protection = try object(value, "Branch protection") for name in ["allow_force_pushes", "allow_deletions"] { guard try object(protection[name], name)["enabled"] == .bool(false) else { throw DevError("Default branch protection must prohibit force pushes and deletions") } } guard try object(protection["enforce_admins"], "Administrator enforcement")["enabled"] == .bool(true) else { throw DevError("Default branch protection must enforce restrictions for administrators") } }
private static func secretNames(_ value: DevJSON) throws -> Set<String> { Set( try array(value, "Secret names").map { secret in guard case .string(let name) = try object(secret, "Secret name")["name"], !name.isEmpty else { throw DevError("Secret names must be nonempty strings") } return name.uppercased() }) }
private static func rubySyntax(_ args: [String]) throws { guard !args.isEmpty else { throw DevError("Pass at least one Ruby source file") } var failures: [String] = [] for path in args { let result = try DevProcess.run("ruby", ["-c", path]) if result.status != 0 { failures.append("\(path): invalid Ruby syntax\n" + String(decoding: result.stderr, as: UTF8.self)) } } if !failures.isEmpty { throw DevError(failures.joined(separator: "\n")) } }}