Skip to content

Commit

Permalink
Merge pull request #47 from winebarrel/add_subdomain_to_config
Browse files Browse the repository at this point in the history
Add subdomain to config, and Remove getUser func
  • Loading branch information
winebarrel authored Nov 3, 2024
2 parents 3ca3079 + d37789a commit c6534ce
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 58 deletions.
43 changes: 2 additions & 41 deletions PagerCall/PagerDutyAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,12 @@ struct PagerDutyAPI {
}
}

func getUserID() async throws -> String {
if !userID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return userID
}

let user = try await getUser()

return user.id
}

private func getUser() async throws -> UsersMeResp.User {
let data = try await get("/users/me")
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let resp = try decoder.decode(UsersMeResp.self, from: data)

return resp.user
}

private struct OncallsResp: Codable {
let oncalls: [Oncall]
struct Oncall: Codable {}
}

func isOnCall(_ userID: String) async throws -> Bool {
func isOnCall() async throws -> Bool {
let data = try await get("/oncalls", ["user_ids[]": userID])
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
Expand All @@ -70,7 +51,7 @@ struct PagerDutyAPI {
let incidents: Incidents
}

func getIncidents(_ userID: String) async throws -> Incidents {
func getIncidents() async throws -> Incidents {
let data = try await get("/incidents", ["user_ids[]": userID])
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
Expand All @@ -79,26 +60,6 @@ struct PagerDutyAPI {
return resp.incidents
}

func getIncidentsURL() async throws -> URL {
let user = try await getUser()
let url = URL(string: user.htmlUrl)!
let uid = userID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? user.id : userID
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.path = "/incidents"
components.query = "assignedToUser=\(uid)"

return components.url!
}

func getOnCallShiftsURL() async throws -> URL {
let user = try await getUser()
let url = URL(string: user.htmlUrl)!
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.path = "/my-on-call/week"

return components.url!
}

private func get(_ path: String, _ query: [String: String] = [:]) async throws -> Data {
var url = endpoint.appendingPathComponent(path)
url.append(queryItems: query.map { key, val in URLQueryItem(name: key, value: val) })
Expand Down
5 changes: 2 additions & 3 deletions PagerCall/PagerDutyModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class PagerDuty: ObservableObject {

func update() async {
do {
let userID = try await api.getUserID()
let onCallNow = try await api.isOnCall(userID)
let currIncidents = try await api.getIncidents(userID)
let onCallNow = try await api.isOnCall()
let currIncidents = try await api.getIncidents()
let newIncidents = currIncidents - incidents
let hasIncidents = !currIncidents.isEmpty

Expand Down
15 changes: 3 additions & 12 deletions PagerCall/RightClickMenu.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import SwiftUI

struct RightClickMenu: View {
@AppStorage("subdomain") private var subdomain = ""
private let api = PagerDutyAPI()

var body: some View {
Button("My Incidents") {
Task {
do {
let url = try await api.getIncidentsURL()
NSWorkspace.shared.open(url)
} catch {
Logger.shared.error("failed to open 'My Incidents': \(error)")
}
NSWorkspace.shared.open(URL(string: "https://\(subdomain).pagerduty.com/incidents")!)
}
}
Button("My On-Call Shifts") {
Task {
do {
let url = try await api.getOnCallShiftsURL()
NSWorkspace.shared.open(url)
} catch {
Logger.shared.error("failed to open 'My On-Call Shifts': \(error)")
}
NSWorkspace.shared.open(URL(string: "https://\(subdomain).pagerduty.com/my-on-call/week")!)
}
}
Divider()
Expand Down
6 changes: 4 additions & 2 deletions PagerCall/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import SwiftUI

struct SettingView: View {
@State var apiKey: String = Vault.apiKey
@AppStorage("subdomain") private var subdomain = ""
@AppStorage("interval") private var interval = Constants.defaultInterval
@AppStorage("userID") private var userID = ""
@State private var launchAtLogin: Bool = SMAppService.mainApp.status == .enabled

var body: some View {
Form {
SecureField(text: $apiKey) {
Link("User API Key", destination: URL(string: "https://support.pagerduty.com/main/docs/api-access-keys#section-generate-a-user-token-rest-api-key")!)
Link("API Key", destination: URL(string: "https://support.pagerduty.com/main/docs/api-access-keys")!)
}.onChange(of: apiKey) {
Vault.apiKey = apiKey
}
TextField("Subdomain", text: $subdomain)
TextField("User ID", text: $userID)
TextField("Interval (sec)", value: $interval, format: .number.grouping(.never))
.onChange(of: interval) {
if interval < 1 {
Expand All @@ -22,7 +25,6 @@ struct SettingView: View {
interval = 3600
}
}
TextField("User ID (optional)", text: $userID)
Toggle("Launch at login", isOn: $launchAtLogin)
.onChange(of: launchAtLogin) {
do {
Expand Down

0 comments on commit c6534ce

Please sign in to comment.