From ce2c2efebb334dc7b3b14d8cff94a3889ed0da2f Mon Sep 17 00:00:00 2001 From: Sergio Campama Date: Sat, 7 May 2022 09:36:04 -0400 Subject: [PATCH] remote input_ --- Sources/copilot-action/CopilotAction.swift | 13 +++--------- Sources/copilot-action/IssueChecker.swift | 4 ++-- Sources/copilot-action/PRSizeLabeler.swift | 24 +++++++++++----------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/Sources/copilot-action/CopilotAction.swift b/Sources/copilot-action/CopilotAction.swift index 6e15d52..c8fbdf4 100644 --- a/Sources/copilot-action/CopilotAction.swift +++ b/Sources/copilot-action/CopilotAction.swift @@ -15,7 +15,7 @@ struct CopilotAction: AsyncParsableCommand { } extension ParsableCommand { - func getEnv(_ key: String, defaultValue: String? = nil) throws -> String { + func getStringEnv(_ key: String, defaultValue: String? = nil) throws -> String { guard let value = ProcessInfo.processInfo.environment[key] ?? defaultValue else { throw StringError("\(key) environment variable not set") } @@ -23,16 +23,9 @@ extension ParsableCommand { return value } - func getInputEnv(_ key: String, defaultValue: String? = nil) throws -> String { - guard let value = ProcessInfo.processInfo.environment["INPUT_" + key] ?? defaultValue else { - throw StringError("\(key) environment variable not set") - } - - return value - } - func getInputEnv(_ key: String, defaultValue: Int? = nil) throws -> Int { - let envValue = ProcessInfo.processInfo.environment["INPUT_" + key] + func getIntEnv(_ key: String, defaultValue: Int? = nil) throws -> Int { + let envValue = ProcessInfo.processInfo.environment[key] guard let value = envValue.flatMap({ Int($0) }) ?? defaultValue else { throw StringError("\(key) environment variable not set") } diff --git a/Sources/copilot-action/IssueChecker.swift b/Sources/copilot-action/IssueChecker.swift index 8d2cad2..e367498 100644 --- a/Sources/copilot-action/IssueChecker.swift +++ b/Sources/copilot-action/IssueChecker.swift @@ -22,7 +22,7 @@ struct IssueChecker: AsyncParsableCommand { ) func run() async throws { - let eventPath = try getEnv("GITHUB_EVENT_PATH") + let eventPath = try getStringEnv("GITHUB_EVENT_PATH") as String guard let eventData = try String(contentsOfFile: eventPath).data(using: .utf8) else { throw StringError("could not load event data at \(eventPath)") @@ -34,7 +34,7 @@ struct IssueChecker: AsyncParsableCommand { print(pullRequestEvent.pull_request.title) print(pullRequestEvent.pull_request.head.ref) - let issuePrefix = try getInputEnv("ISSUE_CHECKER_PREFIX") as String + let issuePrefix = try getStringEnv("ISSUE_CHECKER_PREFIX") as String let inputsToCheck = [ pullRequestEvent.pull_request.body, diff --git a/Sources/copilot-action/PRSizeLabeler.swift b/Sources/copilot-action/PRSizeLabeler.swift index 257a066..6ee7c90 100644 --- a/Sources/copilot-action/PRSizeLabeler.swift +++ b/Sources/copilot-action/PRSizeLabeler.swift @@ -35,9 +35,9 @@ struct PRSizeLabeler: AsyncParsableCommand { ) func run() async throws { - let githubToken = try getEnv("GITHUB_TOKEN") - let repo = try getEnv("GITHUB_REPOSITORY") - let eventPath = try getEnv("GITHUB_EVENT_PATH") + let githubToken = try getStringEnv("GITHUB_TOKEN") + let repo = try getStringEnv("GITHUB_REPOSITORY") + let eventPath = try getStringEnv("GITHUB_EVENT_PATH") guard let eventData = try String(contentsOfFile: eventPath).data(using: .utf8) else { throw StringError("could not load event data at \(eventPath)") @@ -51,16 +51,16 @@ struct PRSizeLabeler: AsyncParsableCommand { print("The pull request has \(totalLinesChanged) changed lines") let label: String - if try totalLinesChanged < getInputEnv("PR_SIZE_XS_LIMIT", defaultValue: 10) { - label = try getInputEnv("PR_SIZE_XS_LABEL", defaultValue: "XS") - } else if try totalLinesChanged < getInputEnv("PR_SIZE_S_LIMIT", defaultValue: 100) { - label = try getInputEnv("PR_SIZE_S_LABEL", defaultValue: "S") - } else if try totalLinesChanged < getInputEnv("PR_SIZE_M_LIMIT", defaultValue: 500) { - label = try getInputEnv("PR_SIZE_M_LABEL", defaultValue: "M") - } else if try totalLinesChanged < getInputEnv("PR_SIZE_L_LIMIT", defaultValue: 1000) { - label = try getInputEnv("PR_SIZE_L_LABEL", defaultValue: "L") + if try totalLinesChanged < getIntEnv("PR_SIZE_XS_LIMIT", defaultValue: 10) { + label = try getStringEnv("PR_SIZE_XS_LABEL", defaultValue: "XS") + } else if try totalLinesChanged < getIntEnv("PR_SIZE_S_LIMIT", defaultValue: 100) { + label = try getStringEnv("PR_SIZE_S_LABEL", defaultValue: "S") + } else if try totalLinesChanged < getIntEnv("PR_SIZE_M_LIMIT", defaultValue: 500) { + label = try getStringEnv("PR_SIZE_M_LABEL", defaultValue: "M") + } else if try totalLinesChanged < getIntEnv("PR_SIZE_L_LIMIT", defaultValue: 1000) { + label = try getStringEnv("PR_SIZE_L_LABEL", defaultValue: "L") } else { - label = try getInputEnv("PR_SIZE_XL_LABEL", defaultValue: "XL") + label = try getStringEnv("PR_SIZE_XL_LABEL", defaultValue: "XL") } print("Assigning the \(label) label to pull request #\(pullRequestEvent.number)")