Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

アクセストークンとともにリポジトリ情報を取得する #31

Merged
merged 3 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/scheduled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
${{ runner.os }}-spm-
- name: Run releaseSubscriptions
run: |
swift run -c release releaseSubscriptions --primary-slack-url ${{ secrets.SLACK_WEBHOOK_PRIMARY_URL }} --secondary-slack-url ${{ secrets.SLACK_WEBHOOK_SECONDARY_URL }}
swift run -c release releaseSubscriptions --access-token ${{ secrets.RELEASE_SUBSCRIPTIONS_FINE_GRAINED_PAT }} --primary-slack-url ${{ secrets.SLACK_WEBHOOK_PRIMARY_URL }} --secondary-slack-url ${{ secrets.SLACK_WEBHOOK_SECONDARY_URL }}
- name: Commit and push if needed
run: |
git config user.name github-actions[bot]
Expand Down
8 changes: 7 additions & 1 deletion Sources/ReleaseSubscriptions/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import ReleaseSubscriptionsCore

@main
struct App: AsyncParsableCommand {
@Option(name: .shortAndLong, help: "GitHub Access Token to fetch repositories.")
var accessToken: String?

@Option(name: .shortAndLong, help: "Slack Webhook URL (primary) to be notified of updates.", transform: URL.init(string:))
var primarySlackURL: URL?

Expand All @@ -24,6 +27,9 @@ struct App: AsyncParsableCommand {
}
Logger.app.info("ℹ️ \(#function) started")
do {
if accessToken == nil {
Logger.app.info("🔔 accessToken is nil")
}
if primarySlackURL == nil {
Logger.app.info("🔔 primarySlackURL is nil")
}
Expand All @@ -32,7 +38,7 @@ struct App: AsyncParsableCommand {
}
let repositories = try Parser.parse()
let oldContents = try FileHelper.load(repositories: repositories)
let newContents = try await Fetcher.fetch(repositories: repositories)
let newContents = try await Fetcher.fetch(repositories: repositories, accessToken: accessToken)
let combinedContents = oldContents.merging(newContents) { ($0 + $1).identified().sorted() }
let updatedContents = DifferenceComparator.insertions(repositories: repositories, old: oldContents, new: combinedContents)
try await SlackNotifier.notify(to: slackURLs(), updates: updatedContents)
Expand Down
36 changes: 27 additions & 9 deletions Sources/ReleaseSubscriptionsCore/Fetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,34 @@ public struct Fetcher {
return decoder
}()

static func fetch(repository: GitHubRepository) async throws -> [Release] {
Logger.shared.info("ℹ️ Fetching \(repository.apiURL)")
let url = repository.apiURL
let (data, _) = try await URLSession.shared.data(from: url)
let releases = try decoder.decode([Release].self, from: data)
Logger.shared.info("✅ Fetched \(repository.apiURL)")
return releases
static func fetch(repository: GitHubRepository, accessToken: String?) async throws -> [Release] {
do {
Logger.shared.info("ℹ️ Fetching \(repository.apiURL)")
let url = repository.apiURL
var request = URLRequest(url: url)
request.setValue("application/vnd.github+json", forHTTPHeaderField: "Accept")
request.setValue("2022-11-28", forHTTPHeaderField: "X-GitHub-Api-Version")
if let accessToken {
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
Logger.shared.info("ℹ️ Set bearer token")
}
let (data, _) = try await URLSession.shared.data(for: request)
do {
Logger.shared.info("✅ Fetched \(repository.apiURL)")
let releases = try decoder.decode([Release].self, from: data)
Logger.shared.info("✅ Parsed \(repository.apiURL)")
return releases
} catch {
Logger.shared.info("❌ Parse failed \(repository.apiURL), error: \(error), data: \(String(data: data, encoding: .utf8) ?? "nil")")
throw error
}
} catch {
Logger.shared.info("❌ Fetch failed \(repository.apiURL), error: \(error)")
throw error
}
}

public static func fetch(repositories: [GitHubRepository]) async throws -> [GitHubRepository : [Release]] {
public static func fetch(repositories: [GitHubRepository], accessToken: String?) async throws -> [GitHubRepository : [Release]] {
defer {
Logger.shared.info("🎉 \(#function) finished")
}
Expand All @@ -36,7 +54,7 @@ public struct Fetcher {
for repository in repositories {
group.addTask {
do {
let releases = try await fetch(repository: repository)
let releases = try await fetch(repository: repository, accessToken: accessToken)
return (repository, releases)
} catch {
Logger.shared.error("❌ \(#function) failed")
Expand Down
Loading