Skip to content

Commit

Permalink
Added handling of rate limit problems when adding workflows.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdoe committed Apr 22, 2024
1 parent ec05b2c commit a7104b5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import Foundation

// TODO: Refactor the list classes (repo, workflow, branch) to use some common code
@MainActor
class GitHubRepositoryList: ObservableObject {
@Published private(set) var items = [GitHubRepository()] { didSet { selected = items[0] }}
Expand Down Expand Up @@ -45,6 +46,15 @@ class GitHubRepositoryList: ObservableObject {
do {
let (data, response) = try await URLSession.feedSession.data(for: request)
guard let response = response as? HTTPURLResponse else { throw URLError(.unsupportedURL) }
// TODO: Somehow refactor this to use the same code as feed reader
if response.statusCode == 403 || response.statusCode == 429 {
if let v = response.value(forHTTPHeaderField: "x-ratelimit-remaining"), Int(v) == 0 {
// HTTPURLResponse doesn't have a specific message for code 429
return [GitHubRepository(message: "too many requests")]
} else {
return [GitHubRepository(message: HTTPURLResponse.localizedString(forStatusCode: response.statusCode))]
}
}
if response.statusCode != 200 {
return [GitHubRepository(message: HTTPURLResponse.localizedString(forStatusCode: response.statusCode))]
}
Expand Down
27 changes: 27 additions & 0 deletions CCMenuUITests/GitHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,33 @@ class GitHubTests: XCTestCase {
XCTAssertTrue(repositoryPicker.menuItems["jekyll-site-test"].exists)
}

func testShowsRateLimitExceededForRepositories() throws {
webapp.router.get("/users/erikdoe/repos", options: .editResponse) { r -> String in
r.response.status = .forbidden
r.response.headers.replaceOrAdd(name: "x-ratelimit-remaining", value: "0")
return "{ \"message\": \"API rate limit exceeded for ...\" } "
}

let app = TestHelper.launchApp(pipelines: "EmptyPipelines.json", pauseMonitor: false)
let window = app.windows["Pipelines"]
let sheet = window.sheets.firstMatch

// Navigate to add workflow sheet
window.toolbars.popUpButtons["Add pipeline menu"].click()
window.toolbars.menuItems["Add GitHub Actions workflow..."].click()

// Enter owner
sheet.textFields["Owner field"].click()
sheet.typeText("erikdoe" + "\n")

// Make sure that the repository list shows rate limit exceeded message
let repositoryPicker = sheet.popUpButtons["Repository picker"]
expectation(for: NSPredicate(format: "value == '(too many requests)'"), evaluatedWith: repositoryPicker)
waitForExpectations(timeout: 2)

// TODO: The other pickers should also show the error
}

func testDoesntDoubleFetchRepositories() throws {
var fetchCount = 0
webapp.router.get("/users/erikdoe/repos") { _ in
Expand Down

0 comments on commit a7104b5

Please sign in to comment.