Merge pull request #20590 from parmar-abhinav/master2
Add examples for github:webhook scaffolder actions
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-scaffolder-backend': patch
|
||||
---
|
||||
|
||||
Add examples for `github:webhook` scaffolder action & improve related tests
|
||||
+209
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
|
||||
import { getVoidLogger } from '@backstage/backend-common';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import {
|
||||
DefaultGithubCredentialsProvider,
|
||||
GithubCredentialsProvider,
|
||||
ScmIntegrations,
|
||||
} from '@backstage/integration';
|
||||
import { PassThrough } from 'stream';
|
||||
import { createGithubWebhookAction } from './githubWebhook';
|
||||
import yaml from 'yaml';
|
||||
import { examples } from './githubWebhook.examples';
|
||||
|
||||
const mockOctokit = {
|
||||
rest: {
|
||||
repos: {
|
||||
createWebhook: jest.fn(),
|
||||
},
|
||||
},
|
||||
};
|
||||
jest.mock('octokit', () => ({
|
||||
Octokit: class {
|
||||
constructor() {
|
||||
return mockOctokit;
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
describe('github:webhook examples', () => {
|
||||
const config = new ConfigReader({
|
||||
integrations: {
|
||||
github: [
|
||||
{ host: 'github.com', token: 'tokenlols' },
|
||||
{ host: 'ghe.github.com' },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const defaultWebhookSecret = 'aafdfdivierernfdk23f';
|
||||
const integrations = ScmIntegrations.fromConfig(config);
|
||||
let githubCredentialsProvider: GithubCredentialsProvider;
|
||||
let action: TemplateAction<any>;
|
||||
|
||||
const mockContext = {
|
||||
workspacePath: 'lol',
|
||||
logger: getVoidLogger(),
|
||||
logStream: new PassThrough(),
|
||||
output: jest.fn(),
|
||||
createTemporaryDirectory: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
githubCredentialsProvider =
|
||||
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
|
||||
action = createGithubWebhookAction({
|
||||
integrations,
|
||||
defaultWebhookSecret,
|
||||
githubCredentialsProvider,
|
||||
});
|
||||
});
|
||||
|
||||
it('Create a GitHub webhook for a repository', async () => {
|
||||
const input = yaml.parse(examples[0].example).steps[0].input;
|
||||
|
||||
await action.handler({
|
||||
...mockContext,
|
||||
input,
|
||||
});
|
||||
|
||||
expect(mockOctokit.rest.repos.createWebhook).toHaveBeenCalledWith({
|
||||
owner: 'owner',
|
||||
repo: 'repo',
|
||||
config: {
|
||||
url: input.webhookUrl,
|
||||
content_type: input.contentType,
|
||||
secret: input.webhookSecret,
|
||||
insecure_ssl: '0',
|
||||
},
|
||||
events: input.events,
|
||||
active: input.active,
|
||||
});
|
||||
});
|
||||
|
||||
it('Create a GitHub webhook with minimal configuration', async () => {
|
||||
const input = yaml.parse(examples[1].example).steps[0].input;
|
||||
|
||||
await action.handler({
|
||||
...mockContext,
|
||||
input,
|
||||
});
|
||||
|
||||
expect(mockOctokit.rest.repos.createWebhook).toHaveBeenCalledWith({
|
||||
owner: 'owner',
|
||||
repo: 'repo',
|
||||
config: {
|
||||
url: 'https://example.com/my-webhook',
|
||||
content_type: 'form',
|
||||
secret: defaultWebhookSecret,
|
||||
insecure_ssl: '0',
|
||||
},
|
||||
events: ['push'],
|
||||
active: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('Create a GitHub webhook with custom events', async () => {
|
||||
const input = yaml.parse(examples[2].example).steps[0].input;
|
||||
|
||||
await action.handler({
|
||||
...mockContext,
|
||||
input,
|
||||
});
|
||||
|
||||
expect(mockOctokit.rest.repos.createWebhook).toHaveBeenCalledWith({
|
||||
owner: 'owner',
|
||||
repo: 'repo',
|
||||
config: {
|
||||
url: 'https://example.com/my-webhook',
|
||||
content_type: 'form',
|
||||
secret: defaultWebhookSecret,
|
||||
insecure_ssl: '0',
|
||||
},
|
||||
events: ['push', 'pull_request'],
|
||||
active: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('Create a GitHub webhook with JSON content type', async () => {
|
||||
const input = yaml.parse(examples[3].example).steps[0].input;
|
||||
|
||||
await action.handler({
|
||||
...mockContext,
|
||||
input,
|
||||
});
|
||||
|
||||
expect(mockOctokit.rest.repos.createWebhook).toHaveBeenCalledWith({
|
||||
owner: 'owner',
|
||||
repo: 'repo',
|
||||
config: {
|
||||
url: 'https://example.com/my-webhook',
|
||||
content_type: 'json',
|
||||
secret: defaultWebhookSecret,
|
||||
insecure_ssl: '0',
|
||||
},
|
||||
events: ['push'],
|
||||
active: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('Create a GitHub webhook with insecure SSL', async () => {
|
||||
const input = yaml.parse(examples[4].example).steps[0].input;
|
||||
|
||||
await action.handler({
|
||||
...mockContext,
|
||||
input,
|
||||
});
|
||||
|
||||
expect(mockOctokit.rest.repos.createWebhook).toHaveBeenCalledWith({
|
||||
owner: 'owner',
|
||||
repo: 'repo',
|
||||
config: {
|
||||
url: 'https://example.com/my-webhook',
|
||||
content_type: 'form',
|
||||
secret: defaultWebhookSecret,
|
||||
insecure_ssl: '1',
|
||||
},
|
||||
events: ['push'],
|
||||
active: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('Create an inactive GitHub webhook', async () => {
|
||||
const input = yaml.parse(examples[5].example).steps[0].input;
|
||||
|
||||
await action.handler({
|
||||
...mockContext,
|
||||
input,
|
||||
});
|
||||
|
||||
expect(mockOctokit.rest.repos.createWebhook).toHaveBeenCalledWith({
|
||||
owner: 'owner',
|
||||
repo: 'repo',
|
||||
config: {
|
||||
url: 'https://example.com/my-webhook',
|
||||
content_type: 'form',
|
||||
secret: defaultWebhookSecret,
|
||||
insecure_ssl: '0',
|
||||
},
|
||||
events: ['push'],
|
||||
active: false,
|
||||
});
|
||||
});
|
||||
});
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright 2023 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { TemplateExample } from '@backstage/plugin-scaffolder-node';
|
||||
import yaml from 'yaml';
|
||||
|
||||
export const examples: TemplateExample[] = [
|
||||
{
|
||||
description: 'Create a GitHub webhook for a repository',
|
||||
example: yaml.stringify({
|
||||
steps: [
|
||||
{
|
||||
action: 'github:webhook',
|
||||
name: 'Create GitHub Webhook',
|
||||
input: {
|
||||
repoUrl: 'github.com?repo=repo&owner=owner',
|
||||
webhookUrl: 'https://example.com/my-webhook',
|
||||
webhookSecret: 'mysecret',
|
||||
events: ['push'],
|
||||
active: true,
|
||||
contentType: 'json',
|
||||
insecureSsl: false,
|
||||
token: 'my-github-token',
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
{
|
||||
description: 'Create a GitHub webhook with minimal configuration',
|
||||
example: yaml.stringify({
|
||||
steps: [
|
||||
{
|
||||
action: 'github:webhook',
|
||||
name: 'Create GitHub Webhook',
|
||||
input: {
|
||||
repoUrl: 'github.com?repo=repo&owner=owner',
|
||||
webhookUrl: 'https://example.com/my-webhook',
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
{
|
||||
description: 'Create a GitHub webhook with custom events',
|
||||
example: yaml.stringify({
|
||||
steps: [
|
||||
{
|
||||
action: 'github:webhook',
|
||||
name: 'Create GitHub Webhook',
|
||||
input: {
|
||||
repoUrl: 'github.com?repo=repo&owner=owner',
|
||||
webhookUrl: 'https://example.com/my-webhook',
|
||||
events: ['push', 'pull_request'],
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
{
|
||||
description: 'Create a GitHub webhook with JSON content type',
|
||||
example: yaml.stringify({
|
||||
steps: [
|
||||
{
|
||||
action: 'github:webhook',
|
||||
name: 'Create GitHub Webhook',
|
||||
input: {
|
||||
repoUrl: 'github.com?repo=repo&owner=owner',
|
||||
webhookUrl: 'https://example.com/my-webhook',
|
||||
contentType: 'json',
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
{
|
||||
description: 'Create a GitHub webhook with insecure SSL',
|
||||
example: yaml.stringify({
|
||||
steps: [
|
||||
{
|
||||
action: 'github:webhook',
|
||||
name: 'Create GitHub Webhook',
|
||||
input: {
|
||||
repoUrl: 'github.com?repo=repo&owner=owner',
|
||||
webhookUrl: 'https://example.com/my-webhook',
|
||||
insecureSsl: true,
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
{
|
||||
description: 'Create an inactive GitHub webhook',
|
||||
example: yaml.stringify({
|
||||
steps: [
|
||||
{
|
||||
action: 'github:webhook',
|
||||
name: 'Create GitHub Webhook',
|
||||
input: {
|
||||
repoUrl: 'github.com?repo=repo&owner=owner',
|
||||
webhookUrl: 'https://example.com/my-webhook',
|
||||
active: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
},
|
||||
];
|
||||
@@ -24,6 +24,7 @@ import { assertError, InputError } from '@backstage/errors';
|
||||
import { Octokit } from 'octokit';
|
||||
import { getOctokitOptions } from './helpers';
|
||||
import { parseRepoUrl } from '../publish/util';
|
||||
import { examples } from './githubWebhook.examples';
|
||||
|
||||
/**
|
||||
* Creates new action that creates a webhook for a repository on GitHub.
|
||||
@@ -51,6 +52,7 @@ export function createGithubWebhookAction(options: {
|
||||
}>({
|
||||
id: 'github:webhook',
|
||||
description: 'Creates webhook for a repository on GitHub.',
|
||||
examples,
|
||||
schema: {
|
||||
input: {
|
||||
type: 'object',
|
||||
|
||||
Reference in New Issue
Block a user