Repository provider detection and URL parsing with support for GitHub, GitLab, Bitbucket, and self-hosted instances. This module provides intelligent repository configuration resolution and reference formatting for multiple git hosting providers.
Parses repository URLs and determines provider-specific settings.
/**
* Parse repository URL into structured configuration
* @param repoUrl - Repository URL in various formats
* @returns Repository configuration with provider and metadata
*/
function getRepoConfig(repoUrl?: string): RepoConfig;Usage Examples:
import { getRepoConfig } from "changelogen";
// GitHub URLs
const github1 = getRepoConfig("user/repo");
const github2 = getRepoConfig("https://github.com/user/repo");
const github3 = getRepoConfig("git@github.com:user/repo.git");
console.log(github1);
// { provider: "github", repo: "user/repo", domain: "github.com" }
// GitLab URLs
const gitlab = getRepoConfig("https://gitlab.com/user/repo");
// { provider: "gitlab", repo: "user/repo", domain: "gitlab.com" }
// Bitbucket URLs
const bitbucket = getRepoConfig("https://bitbucket.org/user/repo");
// { provider: "bitbucket", repo: "user/repo", domain: "bitbucket.org" }
// Self-hosted instances
const selfhosted = getRepoConfig("https://git.company.com/user/repo");
// { repo: "user/repo", domain: "git.company.com" }Automatically resolves repository configuration from project context.
/**
* Resolve repository configuration from package.json and git remote
* @param cwd - Working directory to search for configuration
* @returns Promise resolving to repository configuration or undefined if not found
*/
function resolveRepoConfig(cwd: string): Promise<RepoConfig | undefined>;Usage Examples:
import { resolveRepoConfig } from "changelogen";
// Auto-resolve from current project
const config = await resolveRepoConfig(process.cwd());
// Resolve from specific directory
const repoConfig = await resolveRepoConfig("/path/to/git/repo");
console.log(config);
// { provider: "github", repo: "user/project", domain: "github.com" }Resolution Sources (in order of precedence):
package.json repository field:
{
"repository": "user/repo",
"repository": {
"type": "git",
"url": "https://github.com/user/repo.git"
}
}Git remote origin URL:
git remote get-url originFormats git references as provider-specific links.
/**
* Format reference as a provider-specific link
* @param ref - Reference object with type and value
* @param repo - Repository configuration for link generation
* @returns Formatted reference string (plain text or markdown link)
*/
function formatReference(ref: Reference, repo?: RepoConfig): string;Usage Examples:
import { formatReference, getRepoConfig } from "changelogen";
const repo = getRepoConfig("user/repo");
// Hash references
const hashRef = formatReference(
{ type: "hash", value: "abc123" },
repo
);
// Result: "[abc123](https://github.com/user/repo/commit/abc123)"
// Issue references
const issueRef = formatReference(
{ type: "issue", value: "#123" },
repo
);
// Result: "[#123](https://github.com/user/repo/issues/123)"
// Pull request references
const prRef = formatReference(
{ type: "pull-request", value: "#456" },
repo
);
// Result: "[#456](https://github.com/user/repo/pull/456)"
// Without repository config (fallback to plain text)
const plainRef = formatReference({ type: "hash", value: "abc123" });
// Result: "abc123"Generates provider-specific compare/diff URLs.
/**
* Format compare changes link for repository provider
* @param v - Version or commit reference
* @param config - Resolved changelog configuration
* @returns Markdown link to compare changes
*/
function formatCompareChanges(
v: string,
config: ResolvedChangelogConfig
): string;Usage Examples:
import { formatCompareChanges } from "changelogen";
const compareLink = formatCompareChanges("v1.2.0", config);
// GitHub: "[compare changes](https://github.com/user/repo/compare/v1.1.0...v1.2.0)"
// GitLab: "[compare changes](https://gitlab.com/user/repo/compare/v1.1.0...v1.2.0)"
// Bitbucket: "[compare changes](https://bitbucket.org/user/repo/branches/compare/v1.2.0%0Dv1.1.0)"Supported URL formats:
user/repo (shorthand)github:user/repohttps://github.com/user/repohttps://github.com/user/repo.gitgit@github.com:user/repo.gitReference formats:
/commit/{hash}/issues/{number}/pull/{number}Supported URL formats:
gitlab:user/repohttps://gitlab.com/user/repohttps://gitlab.com/user/repo.gitgit@gitlab.com:user/repo.gitReference formats:
/commit/{hash}/issues/{number}/merge_requests/{number}Supported URL formats:
bitbucket:user/repohttps://bitbucket.org/user/repohttps://bitbucket.org/user/repo.gitgit@bitbucket.org:user/repo.gitReference formats:
/commit/{hash}/issues/{number}/pull-requests/{number}Supported for:
URL format:
https://git.company.com/user/repogit@git.company.com:user/repo.gitNote: Self-hosted instances fall back to plain text references unless provider is explicitly configured.
type RepoProvider = "github" | "gitlab" | "bitbucket";
interface RepoConfig {
/** Repository domain (e.g., "github.com", "gitlab.company.com") */
domain?: string;
/** Repository path (e.g., "user/repository") */
repo?: string;
/** Detected or configured provider */
provider?: RepoProvider;
/** Authentication token for API access */
token?: string;
}
interface Reference {
/** Type of reference */
type: "hash" | "issue" | "pull-request";
/** Reference value (commit hash, issue number, etc.) */
value: string;
}Repository URLs are parsed using flexible regex patterns:
// Supports various formats:
// - SSH: git@domain:user/repo.git
// - HTTPS: https://domain/user/repo(.git)
// - Provider shortcuts: provider:user/repo
// - Shorthand: user/repo (assumes GitHub)Provider detection based on domain:
const domainToProvider = {
"github.com": "github",
"gitlab.com": "gitlab",
"bitbucket.org": "bitbucket"
};When provider cannot be determined:
import {
resolveRepoConfig,
formatReference,
formatCompareChanges,
loadChangelogConfig
} from "changelogen";
// Auto-resolve repository
const repoConfig = await resolveRepoConfig(process.cwd());
console.log(`Detected ${repoConfig.provider} repository: ${repoConfig.repo}`);
// Load configuration with repository
const config = await loadChangelogConfig(process.cwd(), {
repo: repoConfig
});
// Format references in changelog
const references = [
{ type: "hash", value: "abc123" },
{ type: "issue", value: "#456" },
{ type: "pull-request", value: "#789" }
];
references.forEach(ref => {
const formatted = formatReference(ref, config.repo);
console.log(`${ref.type}: ${formatted}`);
});
// Generate compare link
const compareUrl = formatCompareChanges("v1.2.0", config);
console.log(`Compare: ${compareUrl}`);import { getRepoConfig } from "changelogen";
// Configure custom GitHub Enterprise
const enterpriseRepo = getRepoConfig("https://github.company.com/team/project");
// Manual provider override needed for enterprise instances
const customConfig = {
...enterpriseRepo,
provider: "github" as const,
token: process.env.GITHUB_ENTERPRISE_TOKEN
};Repository analysis handles various edge cases:
Example Error Handling:
import { resolveRepoConfig } from "changelogen";
try {
const config = await resolveRepoConfig("/path/to/repo");
if (!config.repo) {
console.log("Could not determine repository - using defaults");
}
if (!config.provider) {
console.log("Unknown provider - references will be plain text");
}
} catch (error) {
console.error("Failed to resolve repository config:", error.message);
// Fallback to manual configuration
}