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

Support proxy #30

Merged
merged 4 commits into from
Feb 12, 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
24 changes: 12 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ concurrency:
cancel-in-progress: true

jobs:
test:
strategy:
matrix:
os: ["ubuntu-latest", "macos-14"]

runs-on: ${{ matrix.os }}
macos:
runs-on: macos-14

steps:
- uses: actions/checkout@v4
- uses: Cyberbeni/install-swift-tool@v2
if: matrix.os == 'ubuntu-latest'
with:
url: https://github.com/tuist/xcbeautify
version: '*'
- name: Run test
run: |
set -o pipefail
swift test 2>&1 | xcbeautify

ubuntu:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Run test
run: |
set -o pipefail
swift test 2>&1
- name: Docker image
if: matrix.os == 'ubuntu-latest'
run: make docker_image
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ Choose the `create` and `revoke` subcommands for `github-apps-token`.
### Create an access token

```command
$ github-apps-token create --help
OVERVIEW: Create an access token.

USAGE: github-apps-token create [<options>] --app-id <app-id> --private-key <private-key> --owner <owner> --repositories <repositories> ... --organization_self_hosted_runners <organization_self_hosted_runners>
USAGE: github-apps-token create [<options>] --app-id <app-id> --private-key <private-key> --proxy <proxy> --owner <owner> --repositories <repositories> ... --organization_self_hosted_runners <organization_self_hosted_runners>

OPTIONS:
-a, --app-id <app-id> The App ID of GitHub Apps.
-p, --private-key <private-key>
The private key of GitHub Apps.
-h, --host-url <host-url>
GitHub API Host URL. (default: https://api.github.com)
-x, --proxy <proxy> Your proxy server URL
--owner <owner> Owner of repositories
-r, --repositories <repositories>
List of repositories that need permissions.
Expand Down Expand Up @@ -146,11 +148,12 @@ ghs_Hqu93EIWNm5HS8DPxuQiKABWOAsKlB3k6tYV
$ github-apps-token revoke --help
OVERVIEW: Revoke an access token.

USAGE: github-apps-token revoke [--host-url <host-url>] --token <token>
USAGE: github-apps-token revoke [--host-url <host-url>] --proxy <proxy> --token <token>

OPTIONS:
-h, --host-url <host-url>
GitHub API Host URL. (default: https://api.github.com)
-x, --proxy <proxy> Your proxy server URL
-t, --token <token> Access token to be revoked.
--version Show the version.
--help Show help information.
Expand Down
18 changes: 16 additions & 2 deletions Sources/GitHubAppsTokenCLI/GitHubAppsTokenCLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ extension GitHubAppsTokenCLI {
)
private(set) var hostURL: URL? = URL(string: "https://api.github.com")

@Option(
name: [.customShort("x"), .customLong("proxy")],
help: "Your proxy server URL",
transform: URL.init(string:)
)
private(set) var proxyURL: URL?

@Option(
name: .long,
help: "Owner of repositories"
Expand Down Expand Up @@ -298,6 +305,13 @@ extension GitHubAppsTokenCLI {
)
private(set) var hostURL: URL? = URL(string: "https://api.github.com")

@Option(
name: [.customShort("x"), .customLong("proxy")],
help: "Your proxy server URL",
transform: URL.init(string:)
)
private(set) var proxyURL: URL?

@Option(
name: [.customShort("t"), .customLong("token")],
help: "Access token to be revoked.",
Expand All @@ -310,7 +324,7 @@ extension GitHubAppsTokenCLI {
// MARK: - Create
extension GitHubAppsTokenCLI.Create {
func run() async throws {
let apiClient = APIClient(baseURL: hostURL)
let apiClient = APIClient(baseURL: hostURL, proxyURL: proxyURL)
let runner = Runner(apiClient: apiClient)
let token = try await runner.create(
appID: appID,
Expand Down Expand Up @@ -364,7 +378,7 @@ extension GitHubAppsTokenCLI.Create {
// MARK: - Revoke
extension GitHubAppsTokenCLI.Revoke {
func run() async throws {
let apiClient = APIClient(baseURL: hostURL)
let apiClient = APIClient(baseURL: hostURL, proxyURL: proxyURL)
let runner = Runner(apiClient: apiClient)
try await runner.revoke(with: accessToken)
}
Expand Down
34 changes: 32 additions & 2 deletions Sources/GitHubAppsTokenCore/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
//

import Foundation
#if os(Linux) && canImport(FoundationNetworking)
import FoundationNetworking
#endif
import Get
import GitHubAppsAPI

Expand All @@ -14,8 +17,35 @@ public final class APIClient: APIClientProtocol {
private let apiClient: Get.APIClient

// MARK: - Initialize
public init(baseURL: URL?) {
self.apiClient = .init(baseURL: baseURL)
public init(baseURL: Foundation.URL?, proxyURL: Foundation.URL? = nil) {
let sessionConfiguration: URLSessionConfiguration = {
let configuration = URLSessionConfiguration.default
if let proxyURL {
#if os(Linux)
var connectionProxyDictionary: [AnyHashable: Any] = [
"HTTPEnable": 1,
"HTTPSEnable": 1
]
connectionProxyDictionary["HTTPProxy"] = proxyURL.host
connectionProxyDictionary["HTTPPort"] = proxyURL.port
connectionProxyDictionary["HTTPSProxy"] = proxyURL.host
connectionProxyDictionary["HTTPSPort"] = proxyURL.port
configuration.connectionProxyDictionary = connectionProxyDictionary
#else
configuration.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable: 1,
kCFNetworkProxiesHTTPProxy: proxyURL.host,
kCFNetworkProxiesHTTPPort: proxyURL.port,
kCFNetworkProxiesHTTPSEnable: 1,
kCFNetworkProxiesHTTPSProxy: proxyURL.host,
kCFNetworkProxiesHTTPSPort: proxyURL.port
].compactMapValues { $0 }
#endif
}
return configuration
}()
let configuration = Get.APIClient.Configuration(baseURL: baseURL, sessionConfiguration: sessionConfiguration)
self.apiClient = .init(configuration: configuration)
}

public func response<R: RequestType>(for request: R) async throws -> R.Response where R.Response: Decodable {
Expand Down
Loading