Skip to content

fix(github-issues): handle file: type source-locations in getHostnameFromEntity#9105

Open
Fortune-Ndlovu wants to merge 1 commit intobackstage:mainfrom
Fortune-Ndlovu:fix/github-issues-file-source-location
Open

fix(github-issues): handle file: type source-locations in getHostnameFromEntity#9105
Fortune-Ndlovu wants to merge 1 commit intobackstage:mainfrom
Fortune-Ndlovu:fix/github-issues-file-source-location

Conversation

@Fortune-Ndlovu
Copy link
Copy Markdown
Contributor

@Fortune-Ndlovu Fortune-Ndlovu commented May 11, 2026

Summary

  • getHostnameFromEntity() in @backstage-community/plugin-github-issues unconditionally passes the entity's source-location target to new URL(), which throws TypeError: Invalid URL when the source-location is file: type
  • The fix checks the type field from getEntitySourceLocation(): for url: type it parses the hostname as before, for file: type it falls back to github.com
  • Added unit tests for getHostnameFromEntity and getProjectNameFromEntity

The Problem

The getHostnameFromEntity function extracts the GitHub hostname from an entity's source-location:

export const getHostnameFromEntity = (entity: Entity): string => {
  const { target } = getEntitySourceLocation(entity);
  return new URL(target).hostname;
};

getEntitySourceLocation() returns { type, target } where:

  • url:https://github.com/org/repo -> { type: "url", target: "https://github.com/org/repo" }
  • file:./catalog-entities/foo.yaml -> { type: "file", target: "./catalog-entities/foo.yaml" }

The function ignores the type field and unconditionally calls new URL(target). When the source-location is file: type, the target is a relative filesystem path, not a valid URL, so new URL() throws TypeError: Invalid URL, crashing the Issues tab.

Who's Affected

Any Backstage deployment where entities are registered via file: type catalog locations

The Fix

export const getHostnameFromEntity = (entity: Entity): string => {
  const { type, target } = getEntitySourceLocation(entity);
  if (type === 'url') {
    return new URL(target).hostname;
  }
  return 'github.com';
};

Test plan

  • Added unit tests for url: type source-location (parses hostname correctly)
  • Added unit tests for GitHub Enterprise url: source-location (parses custom hostname)
  • Added unit tests for file: type source-location (falls back to github.com)
  • Added unit tests for managed-by-location fallback with file: type
  • Added unit tests for getProjectNameFromEntity
  • All 6 tests pass

…FromEntity

`getHostnameFromEntity` unconditionally passes the entity's source-location
target to `new URL()`, which throws `TypeError: Invalid URL` when the
source-location is `file:` type (e.g. `file:./catalog-entities/foo.yaml`).

This affects any Backstage deployment where entities are registered via
`file:` type catalog locations — the standard approach for local, Helm,
and operator-managed instances.

The fix checks the `type` field returned by `getEntitySourceLocation()`:
- `url:` type → parse hostname from the URL (existing behavior)
- `file:` type → fall back to `github.com`

Signed-off-by: Fortune-Ndlovu <fndlovu@redhat.com>
Copilot AI review requested due to automatic review settings May 11, 2026 10:07
@Fortune-Ndlovu Fortune-Ndlovu requested a review from a team as a code owner May 11, 2026 10:07
@Fortune-Ndlovu Fortune-Ndlovu requested a review from BethGriggs May 11, 2026 10:07
@backstage-goalie
Copy link
Copy Markdown
Contributor

Changed Packages

Package Name Package Path Changeset Bump Current Version
@backstage-community/plugin-github-issues workspaces/github/plugins/github-issues patch v1.0.0

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a runtime crash in @backstage-community/plugin-github-issues when entities are registered from file: catalog locations by preventing new URL() from being called on non-URL targets.

Changes:

  • Updated getHostnameFromEntity to branch on getEntitySourceLocation(entity).type, parsing hostnames only for url and defaulting to github.com otherwise.
  • Added unit tests covering getHostnameFromEntity (including file: and managed-by-location fallback) and getProjectNameFromEntity.
  • Added a changeset to ship the fix as a patch release.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
workspaces/github/plugins/github-issues/src/hooks/useEntityGithubRepositories.ts Avoids new URL() on file: source-locations by checking the location type first.
workspaces/github/plugins/github-issues/src/hooks/useEntityGithubRepositories.test.ts Adds focused unit tests for hostname/project-slug extraction, including file: scenarios.
.changeset/fix-github-issues-file-source-location.md Declares a patch release note for the crash fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@hopehadfield hopehadfield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one small nitpick but otherwise LGTM! Thanks for adding the tests.

if (type === 'url') {
return new URL(target).hostname;
}
return 'github.com';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd extract this as a const

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants