Upates to allow users to subscribe to the newly created GitHub repo

Signed-off-by: Jonathan Sundquist <jsundquist@gmail.com>
This commit is contained in:
Jonathan Sundquist
2024-12-16 13:41:38 -06:00
parent eb1262a2e4
commit edaf925841
8 changed files with 82 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-github': patch
---
Updates to allow users to subscribe to the newly created repository within GitHub to mimic similar functionality found within the GitHub UI.
@@ -279,6 +279,7 @@ export function createGithubRepoCreateAction(options: {
[key: string]: string;
}
| undefined;
subscribe?: boolean | undefined;
},
JsonObject
>;
@@ -441,6 +442,7 @@ export function createPublishGithubAction(options: {
[key: string]: string;
}
| undefined;
subscribe?: boolean | undefined;
},
JsonObject
>;
@@ -74,6 +74,9 @@ const mockOctokit = {
createOrUpdateRepoSecret: jest.fn(),
getRepoPublicKey: jest.fn(),
},
activity: {
setRepoSubscription: jest.fn(),
},
},
request: jest.fn(),
};
@@ -1796,4 +1799,26 @@ describe('publish:github', () => {
});
},
);
it('should add user subscription', async () => {
mockOctokit.rest.users.getByUsername.mockResolvedValue({
data: { type: 'Organization' },
});
mockOctokit.rest.repos.createInOrg.mockResolvedValue({ data: {} });
await action.handler({
...mockContext,
input: {
...mockContext.input,
subscribe: true,
},
});
expect(mockOctokit.rest.activity.setRepoSubscription).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
subscribed: true,
ignored: false,
});
});
});
@@ -117,6 +117,7 @@ export function createPublishGithubAction(options: {
requiredCommitSigning?: boolean;
requiredLinearHistory?: boolean;
customProperties?: { [key: string]: string };
subscribe?: boolean;
}>({
id: 'publish:github',
description:
@@ -168,6 +169,7 @@ export function createPublishGithubAction(options: {
requiredCommitSigning: inputProps.requiredCommitSigning,
requiredLinearHistory: inputProps.requiredLinearHistory,
customProperties: inputProps.customProperties,
subscribe: inputProps.subscribe,
},
},
output: {
@@ -218,6 +220,7 @@ export function createPublishGithubAction(options: {
oidcCustomization,
token: providedToken,
customProperties,
subscribe = false,
requiredCommitSigning = false,
requiredLinearHistory = false,
} = ctx.input;
@@ -260,6 +263,7 @@ export function createPublishGithubAction(options: {
secrets,
oidcCustomization,
customProperties,
subscribe,
ctx.logger,
);
@@ -55,6 +55,9 @@ const mockOctokit = {
createOrUpdateRepoSecret: jest.fn(),
getRepoPublicKey: jest.fn(),
},
activity: {
setRepoSubscription: jest.fn(),
},
},
request: jest.fn(),
};
@@ -754,4 +757,26 @@ describe('github:repo:create', () => {
'https://github.com/clone/url.git',
);
});
it('should subscribe user to repository', async () => {
mockOctokit.rest.users.getByUsername.mockResolvedValue({
data: { type: 'Organization' },
});
mockOctokit.rest.repos.createInOrg.mockResolvedValue({ data: {} });
await action.handler({
...mockContext,
input: {
...mockContext.input,
subscribe: true,
},
});
expect(mockOctokit.rest.activity.setRepoSubscription).toHaveBeenCalledWith({
owner: 'owner',
repo: 'repo',
subscribed: true,
ignored: false,
});
});
});
@@ -102,6 +102,7 @@ export function createGithubRepoCreateAction(options: {
requireCommitSigning?: boolean;
requiredLinearHistory?: boolean;
customProperties?: { [key: string]: string };
subscribe?: boolean;
}>({
id: 'github:repo:create',
description: 'Creates a GitHub repository.',
@@ -143,6 +144,7 @@ export function createGithubRepoCreateAction(options: {
requiredCommitSigning: inputProps.requiredCommitSigning,
requiredLinearHistory: inputProps.requiredLinearHistory,
customProperties: inputProps.customProperties,
subscribe: inputProps.subscribe,
},
},
output: {
@@ -176,6 +178,7 @@ export function createGithubRepoCreateAction(options: {
secrets,
oidcCustomization,
customProperties,
subscribe,
token: providedToken,
} = ctx.input;
@@ -217,6 +220,7 @@ export function createGithubRepoCreateAction(options: {
secrets,
oidcCustomization,
customProperties,
subscribe,
ctx.logger,
);
@@ -149,6 +149,7 @@ export async function createGithubRepoWithCollaboratorsAndTopics(
}
| undefined,
customProperties: { [key: string]: string } | undefined,
subscribe: boolean | undefined,
logger: LoggerService,
) {
// eslint-disable-next-line testing-library/no-await-sync-queries
@@ -330,6 +331,15 @@ export async function createGithubRepoWithCollaboratorsAndTopics(
);
}
if (subscribe) {
await client.rest.activity.setRepoSubscription({
subscribed: true,
ignored: false,
owner,
repo,
});
}
return newRepo;
}
@@ -317,6 +317,12 @@ const customProperties = {
type: 'object',
};
const subscribe = {
title: 'Subscribe to repository',
description: `Subscribe to the repository. The default value is 'false'`,
type: 'boolean',
};
export { access };
export { allowMergeCommit };
export { allowRebaseMerge };
@@ -357,3 +363,4 @@ export { repoVariables };
export { secrets };
export { oidcCustomization };
export { customProperties };
export { subscribe };