Skip to content

Commit

Permalink
feat(github): support overriding the GitHub API URL (#512)
Browse files Browse the repository at this point in the history
via `GITHUB_API_URL`
  • Loading branch information
orhun authored Feb 22, 2024
1 parent 1aaa9b2 commit 8199699
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
25 changes: 19 additions & 6 deletions git-cliff-core/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use serde::{
Deserialize,
Serialize,
};
use std::env;
use std::hash::{
Hash,
Hasher,
Expand All @@ -36,6 +37,9 @@ use std::time::Duration;
/// GitHub REST API url.
const GITHUB_API_URL: &str = "https://api.github.com";

/// Environment variable for overriding the GitHub REST API url.
const GITHUB_API_URL_ENV: &str = "GITHUB_API_URL";

/// User agent for interacting with the GitHub API.
///
/// This is needed since GitHub API does not accept empty user agent.
Expand All @@ -56,6 +60,13 @@ pub const START_FETCHING_MSG: &str = "Retrieving data from GitHub...";
/// Log message to show when done fetching from GitHub.
pub const FINISHED_FETCHING_MSG: &str = "Done fetching GitHub data.";

/// Returns the GitHub API url either from environment or from default value.
fn get_github_api_url() -> String {
env::var(GITHUB_API_URL_ENV)
.ok()
.unwrap_or_else(|| GITHUB_API_URL.to_string())
}

/// Trait for handling the different entries returned from the GitHub API.
trait GitHubEntry {
/// Returns the API URL for fetching the entries at the specified page.
Expand All @@ -76,9 +87,10 @@ pub struct GitHubCommit {
impl GitHubEntry for GitHubCommit {
fn url(owner: &str, repo: &str, page: i32) -> String {
format!(
"{GITHUB_API_URL}/repos/{}/{}/commits?per_page={MAX_PAGE_SIZE}&\
page={page}",
owner, repo
"{}/repos/{}/{}/commits?per_page={MAX_PAGE_SIZE}&page={page}",
get_github_api_url(),
owner,
repo
)
}
fn buffer_size() -> usize {
Expand Down Expand Up @@ -117,9 +129,10 @@ pub struct GitHubPullRequest {
impl GitHubEntry for GitHubPullRequest {
fn url(owner: &str, repo: &str, page: i32) -> String {
format!(
"{GITHUB_API_URL}/repos/{}/{}/pulls?per_page={MAX_PAGE_SIZE}&\
page={page}&state=closed",
owner, repo
"{}/repos/{}/{}/pulls?per_page={MAX_PAGE_SIZE}&page={page}&state=closed",
get_github_api_url(),
owner,
repo
)
}

Expand Down
6 changes: 6 additions & 0 deletions website/docs/integration/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ For example:
GITHUB_TOKEN="***" git cliff --github-repo "orhun/git-cliff"
```

:::tip

You can use the [`GITHUB_API_URL`](https://docs.github.com/en/actions/learn-github-actions/variables) environment variable want to override the API URL. This is useful if you are using GitHub enterprise.

:::

## Templating

:::tip
Expand Down

0 comments on commit 8199699

Please sign in to comment.