Merge pull request #26016 from Bonial-International-GmbH/pjungermann/events/github/fix-repository-events
fix GitHub repository events
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
---
|
||||
'@backstage/plugin-catalog-backend-module-github': patch
|
||||
---
|
||||
|
||||
Fix GitHub `repository` event support.
|
||||
|
||||
- `$.repository.organization` is only provided for `push` events. Switched to `$.organization.login` instead.
|
||||
- `$.repository.url` is not always returning the expected and required value. Use `$.repository.html_url` instead.
|
||||
@@ -665,7 +665,7 @@ describe('GithubEntityProvider', () => {
|
||||
event: EventParams<PushEvent | RepositoryEvent>,
|
||||
options?: { branch?: string; catalogFilePath?: string },
|
||||
): DeferredEntity[] => {
|
||||
const url = `${event.eventPayload.repository.url}/blob/${
|
||||
const url = `${event.eventPayload.repository.html_url}/blob/${
|
||||
options?.branch ?? 'main'
|
||||
}/${options?.catalogFilePath ?? 'catalog-info.yaml'}`;
|
||||
return createExpectedEntitiesForUrl(url);
|
||||
@@ -687,6 +687,7 @@ describe('GithubEntityProvider', () => {
|
||||
name: 'test-repo',
|
||||
organization,
|
||||
topics: [],
|
||||
html_url: `https://github.com/${organization}/test-repo`,
|
||||
url: `https://github.com/${organization}/test-repo`,
|
||||
} as Partial<PushEvent['repository']>;
|
||||
|
||||
@@ -702,6 +703,9 @@ describe('GithubEntityProvider', () => {
|
||||
const event = {
|
||||
ref: options?.ref ?? 'refs/heads/main',
|
||||
repository: repo as PushEvent['repository'],
|
||||
organization: {
|
||||
login: organization,
|
||||
},
|
||||
created: true,
|
||||
deleted: false,
|
||||
forced: false,
|
||||
@@ -960,10 +964,10 @@ describe('GithubEntityProvider', () => {
|
||||
): EventParams<RepositoryEvent> => {
|
||||
const repo = {
|
||||
name: 'test-repo',
|
||||
url: 'https://github.com/test-org/test-repo',
|
||||
html_url: 'https://github.com/test-org/test-repo',
|
||||
url: 'https://api.github.com/repos/test-org/test-repo',
|
||||
default_branch: 'main',
|
||||
master_branch: 'main',
|
||||
organization: 'test-org',
|
||||
topics: [],
|
||||
archived: action === 'archived',
|
||||
private: action !== 'publicized',
|
||||
@@ -972,6 +976,9 @@ describe('GithubEntityProvider', () => {
|
||||
const event = {
|
||||
action,
|
||||
repository: repo as RepositoryEvent['repository'],
|
||||
organization: {
|
||||
login: 'test-org',
|
||||
},
|
||||
} as RepositoryEvent;
|
||||
|
||||
if (action === 'renamed') {
|
||||
@@ -1285,7 +1292,7 @@ describe('GithubEntityProvider', () => {
|
||||
const event = createRepoEvent(
|
||||
'renamed',
|
||||
) as EventParams<RepositoryRenamedEvent>;
|
||||
const urlOldRepo = `https://github.com/${event.eventPayload.repository.organization}/${event.eventPayload.changes.repository.name.from}/blob/main/catalog-info.yaml`;
|
||||
const urlOldRepo = `https://github.com/${event.eventPayload.organization?.login}/${event.eventPayload.changes.repository.name.from}/blob/main/catalog-info.yaml`;
|
||||
const expectedEntitiesRemoved =
|
||||
createExpectedEntitiesForUrl(urlOldRepo);
|
||||
|
||||
@@ -1314,7 +1321,7 @@ describe('GithubEntityProvider', () => {
|
||||
const event = createRepoEvent(
|
||||
'renamed',
|
||||
) as EventParams<RepositoryRenamedEvent>;
|
||||
const urlOldRepo = `https://github.com/${event.eventPayload.repository.organization}/${event.eventPayload.changes.repository.name.from}/blob/main/catalog-info.yaml`;
|
||||
const urlOldRepo = `https://github.com/${event.eventPayload.organization?.login}/${event.eventPayload.changes.repository.name.from}/blob/main/catalog-info.yaml`;
|
||||
const expectedEntitiesRemoved =
|
||||
createExpectedEntitiesForUrl(urlOldRepo);
|
||||
const expectedEntitiesAdded = createExpectedEntitiesForEvent(event);
|
||||
|
||||
@@ -294,7 +294,7 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
|
||||
/** {@inheritdoc @backstage/plugin-events-node#EventSubscriber.onEvent} */
|
||||
async onEvent(params: EventParams): Promise<void> {
|
||||
this.logger.debug(`Received event from ${params.topic}`);
|
||||
this.logger.debug(`Received event for topic ${params.topic}`);
|
||||
if (EVENT_TOPICS.some(topic => topic === params.topic)) {
|
||||
if (!this.connection) {
|
||||
throw new Error('Not initialized');
|
||||
@@ -323,15 +323,15 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
}
|
||||
|
||||
private async onPush(event: PushEvent) {
|
||||
if (this.config.organization !== event.repository.organization) {
|
||||
if (this.config.organization !== event.organization?.login) {
|
||||
this.logger.debug(
|
||||
`skipping push event from organization ${event.repository.organization}`,
|
||||
`skipping push event from organization ${event.organization?.login}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const repoName = event.repository.name;
|
||||
const repoUrl = event.repository.url;
|
||||
const repoUrl = event.repository.html_url;
|
||||
this.logger.debug(`handle github:push event for ${repoName} - ${repoUrl}`);
|
||||
|
||||
const branch =
|
||||
@@ -356,13 +356,13 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
// so we will process the change based in this data
|
||||
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#push
|
||||
const added = this.collectDeferredEntitiesFromCommit(
|
||||
event.repository.url,
|
||||
repoUrl,
|
||||
branch,
|
||||
event.commits,
|
||||
(commit: Commit) => [...commit.added],
|
||||
);
|
||||
const removed = this.collectDeferredEntitiesFromCommit(
|
||||
event.repository.url,
|
||||
repoUrl,
|
||||
branch,
|
||||
event.commits,
|
||||
(commit: Commit) => [...commit.removed],
|
||||
@@ -381,14 +381,12 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
keys: [
|
||||
...new Set([
|
||||
...modified.map(
|
||||
filePath =>
|
||||
`url:${event.repository.url}/tree/${branch}/${filePath}`,
|
||||
filePath => `url:${repoUrl}/tree/${branch}/${filePath}`,
|
||||
),
|
||||
...modified.map(
|
||||
filePath =>
|
||||
`url:${event.repository.url}/blob/${branch}/${filePath}`,
|
||||
filePath => `url:${repoUrl}/blob/${branch}/${filePath}`,
|
||||
),
|
||||
`url:${event.repository.url}/tree/${branch}/${catalogPath}`,
|
||||
`url:${repoUrl}/tree/${branch}/${catalogPath}`,
|
||||
]),
|
||||
],
|
||||
});
|
||||
@@ -408,9 +406,9 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
}
|
||||
|
||||
private async onRepoChange(event: RepositoryEvent) {
|
||||
if (this.config.organization !== event.repository.organization) {
|
||||
if (this.config.organization !== event.organization?.login) {
|
||||
this.logger.debug(
|
||||
`skipping repository event from organization ${event.repository.organization}`,
|
||||
`skipping repository event from organization ${event.organization?.login}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -527,7 +525,7 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
private async onRepoRenamed(event: RepositoryRenamedEvent) {
|
||||
const repository = this.createRepoFromEvent(event);
|
||||
const oldRepoName = event.changes.repository.name.from;
|
||||
const urlParts = event.repository.url.split('/');
|
||||
const urlParts = repository.url.split('/');
|
||||
urlParts[urlParts.length - 1] = oldRepoName;
|
||||
const oldRepoUrl = urlParts.join('/');
|
||||
const oldRepository: Repository = {
|
||||
@@ -540,7 +538,7 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
const matchingTargets = this.matchesFilters([repository]);
|
||||
if (matchingTargets.length === 0) {
|
||||
this.logger.debug(
|
||||
`skipping repository transferred event for repository ${repository.name} because it didn't match provider filters`,
|
||||
`skipping repository renamed event for repository ${repository.name} because it didn't match provider filters`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -587,7 +585,7 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
const matchingTargets = this.matchesFilters([repository]);
|
||||
if (matchingTargets.length === 0) {
|
||||
this.logger.debug(
|
||||
`skipping repository transferred event for repository ${repository.name} because it didn't match provider filters`,
|
||||
`skipping repository unarchived event for repository ${repository.name} because it didn't match provider filters`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
@@ -632,7 +630,10 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber {
|
||||
|
||||
private createRepoFromEvent(event: RepositoryEvent | PushEvent): Repository {
|
||||
return {
|
||||
url: event.repository.url,
|
||||
// $.repository.url can be a value like
|
||||
// "https://api.github.com/repos/{org}/{repo}"
|
||||
// or "https://github.com/{org}/{repo}"
|
||||
url: event.repository.html_url,
|
||||
name: event.repository.name,
|
||||
defaultBranchRef: event.repository.default_branch,
|
||||
repositoryTopics: event.repository.topics,
|
||||
|
||||
Reference in New Issue
Block a user