Skip to content

Commit

Permalink
make it almost ready
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiocampama committed May 7, 2022
1 parent bd19055 commit 0ad52ee
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions Sources/copilot-action/PRSizeLabeler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ struct PullRequest: Codable {
let additions: Int
let deletions: Int
}
struct PullRequestEvent: Codable {

struct PullRequestEvent: Codable {
let pull_request: PullRequest
let number: Int
}
Expand All @@ -17,8 +17,6 @@ struct LabelsChangeRequest: Codable {
}

struct GithubConfiguration: APIConfiguration {
let token: String

let host = URL(string: "https://api.github.com")!

var requestHeaders: [String : String] {
Expand All @@ -28,6 +26,8 @@ struct GithubConfiguration: APIConfiguration {
]
}

let token: String

init(token: String) {
self.token = token
}
Expand All @@ -52,19 +52,35 @@ struct PRSizeLabeler: AsyncParsableCommand {
let repo = try getEnv(key: "GITHUB_REPOSITORY")
let eventPath = try getEnv(key: "GITHUB_EVENT_PATH")

// guard let eventData = try String(contentsOfFile: eventPath).data(using: .utf8) else {
// throw StringError("could not load event data at \(eventPath)")
// }
//
// let pullRequestEvent = try JSONDecoder().decode(PullRequestEvent.self, from: eventData)
guard let eventData = try String(contentsOfFile: eventPath).data(using: .utf8) else {
throw StringError("could not load event data at \(eventPath)")
}

let pullRequestEvent = try JSONDecoder().decode(PullRequestEvent.self, from: eventData)

let totalLinesChanged = pullRequestEvent.pull_request.additions + pullRequestEvent.pull_request.deletions
print("The pull has \(totalLinesChanged) changed lines")

let label: String
if totalLinesChanged < 10 {
label = "XS"
} else if totalLinesChanged < 100 {
label = "S"
} else if totalLinesChanged < 500 {
label = "M"
} else if totalLinesChanged < 1000 {
label = "L"
} else {
label = "XL"
}

// print("The pull has \(pullRequestEvent.pull_request.additions + pullRequestEvent.pull_request.deletions) changed lines")
print("Assigning the \(label) label")

let provider = APIProvider(configuration: GithubConfiguration(token: githubToken))

let body = LabelsChangeRequest(labels: ["XS"])
let body = LabelsChangeRequest(labels: [label])
try await provider.request(
.setLabels(repo: "copilotmoney/copilot-ios", pullRequestID: 1535),
.setLabels(repo: repo, pullRequestID: pullRequestEvent.number),
body: body
)
}
Expand Down

0 comments on commit 0ad52ee

Please sign in to comment.