Skip to content

Commit

Permalink
Added fallback to feed with workflow ID. Fixes #17.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdoe committed Dec 11, 2024
1 parent c73dce6 commit 2d18173
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ struct AddGitHubPipelineSheet: View {
}
.keyboardShortcut(.cancelAction)
Button("Apply") {
if let p = builder.makePipeline() {
config.setPipeline(p)
presentation.dismiss()
Task {
if let p = await builder.makePipeline() {
config.setPipeline(p)
presentation.dismiss()
}
// TODO: show error
}
}
.keyboardShortcut(.defaultAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,57 @@ class GitHubPipelineBuilder: ObservableObject {
}

var canMakePipeline: Bool {
return makePipeline() != nil
guard !name.isEmpty else { return false }
guard let owner else { return false }
guard let repository, repository.isValid else { return false }
guard let workflow, workflow.isValid else { return false }
guard let branch, branch.isValid else { return false }
return true
}

func makePipeline() -> Pipeline? {
func makePipeline() async -> Pipeline? {
guard !name.isEmpty else { return nil }
guard let owner else { return nil }
guard let repository, repository.isValid else { return nil }
guard let workflow, workflow.isValid else { return nil }
guard let branch, branch.isValid else { return nil }
let branchName = branch.isAllBranchPlaceholder ? nil : branch.name
let url = GitHubAPI.feedUrl(owner: owner, repository: repository.name, workflow: workflow.filename, branch: branchName)

var url: URL? = nil
let workflowPathComponents = [ workflow.filename, String(workflow.id) ]
for wfid in workflowPathComponents {
url = GitHubAPI.feedUrl(owner: owner, repository: repository.name, workflow: wfid, branch: branchName)
if let url, let result = await fetchRuns(url: url), result == 200 {
break
}
url = nil
}
guard let url else {
return nil
}

let feed = PipelineFeed(type: .github, url:url)
let pipeline = Pipeline(name: name, feed: feed)
return pipeline
}

private func fetchRuns(url: URL) async -> Int? {
let feed = PipelineFeed(type: .github, url:url)
guard let request = GitHubAPI.requestForFeed(feed: feed, token: nil) else {
return nil
}
let result = await fetchRuns(request: request)
return result
}

private func fetchRuns(request: URLRequest) async -> Int? {
do {
let (data, response) = try await URLSession.feedSession.data(for: request)
guard let response = response as? HTTPURLResponse else { throw URLError(.unsupportedURL) }
return response.statusCode
} catch {
return nil
}
}
}

1 change: 1 addition & 0 deletions CCMenu/Source/Server Monitor/GitHubAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class GitHubAPI {
// MARK: - feed

static func feedUrl(owner: String, repository: String, workflow: String, branch: String?) -> URL {
// see https://docs.github.com/en/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow
var components = URLComponents(string: baseURL(forAPI: true))!
components.path = String(format: "/repos/%@/%@/actions/workflows/%@/runs", owner, repository, workflow)
if let branch {
Expand Down
46 changes: 46 additions & 0 deletions CCMenuUITests/GitHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,52 @@ class GitHubTests: XCTestCase {
waitForExpectations(timeout: 5)
}

func testAddsGitHubPipelineByIdIfNeccessary() throws {
webapp.router.get("/users/erikdoe/repos") { _ in
try TestHelper.contentsOfFile("GitHubReposByUserCCM2OnlyResponse.json")
}
webapp.router.get("/repos/erikdoe/ccmenu2/actions/workflows") { _ in
try TestHelper.contentsOfFile("GitHubWorkflowsResponse.json")
}
webapp.router.get("/repos/erikdoe/ccmenu2/branches") { _ in
try TestHelper.contentsOfFile("GitHubBranchesResponse.json")
}
webapp.router.get("/repos/erikdoe/ccmenu2/actions/workflows/build-and-test.yaml/runs", options: .editResponse) { r in
r.response.status = .notFound
return "{ } "
}
webapp.router.get("/repos/erikdoe/ccmenu2/actions/workflows/62921699/runs") { _ in
try TestHelper.contentsOfFile("GitHubWorkflowRunsResponse.json")
}

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")

let repositoryPicker = sheet.popUpButtons["Repository picker"]
expectation(for: NSPredicate(format: "value == 'ccmenu2'"), evaluatedWith: repositoryPicker)
let workflowPicker = sheet.popUpButtons["Workflow picker"]
expectation(for: NSPredicate(format: "value == 'Build and test'"), evaluatedWith: workflowPicker)
waitForExpectations(timeout: 5)

sheet.buttons["Apply"].click()

// Make sure the pipeline is shown, and that its status is fetched immediately
let titleText = window.tables.staticTexts["Pipeline title"]
expectation(for: NSPredicate(format: "value BEGINSWITH 'ccmenu2'"), evaluatedWith: titleText)
let descriptionText = window.tables.staticTexts["Status description"]
expectation(for: NSPredicate(format: "value CONTAINS 'Label: 42'"), evaluatedWith: descriptionText)
waitForExpectations(timeout: 5)
}

func testAddsGitHubPipelineWithBranch() throws {
var branchParam: String?
webapp.router.get("/users/erikdoe/repos") { _ in
Expand Down

0 comments on commit 2d18173

Please sign in to comment.