Skip to content

Commit

Permalink
gitea
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangndst committed Sep 18, 2024
1 parent d219292 commit de3dab5
Show file tree
Hide file tree
Showing 14 changed files with 1,256 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ s3:
timeout: { minutes: 1 }

permission:
enabled: false
enabled: true
rbac:
pluginsWithPermission:
- kubernetes
Expand Down
1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@enfuse/plugin-chatgpt-backend": "^1.1.1",
"@immobiliarelabs/backstage-plugin-gitlab-backend": "^6.6.0",
"@internal/backstage-plugin-auth-backend-module-oidc-provider": "^0.1.0",
"@internal/backstage-plugin-catalog-backend-module-gitea": "^0.1.0",
"@janus-idp/backstage-plugin-rbac-backend": "^4.10.4",
"@roadiehq/scaffolder-backend-module-aws": "^2.4.25",
"@spreadshirt/backstage-plugin-s3-viewer-backend": "^0.9.5",
Expand Down
1 change: 1 addition & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ backend.add(import('@janus-idp/backstage-plugin-rbac-backend'));
// notifications plugin
backend.add(import('@backstage/plugin-notifications-backend'));

backend.add(import('@internal/backstage-plugin-catalog-backend-module-gitea'));
backend.start();
1 change: 1 addition & 0 deletions plugins/catalog-backend-module-gitea/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
5 changes: 5 additions & 0 deletions plugins/catalog-backend-module-gitea/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# @internal/backstage-plugin-catalog-backend-module-gitea

The gitea backend module for the catalog plugin.

_This plugin was created through the Backstage CLI_
37 changes: 37 additions & 0 deletions plugins/catalog-backend-module-gitea/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@internal/backstage-plugin-catalog-backend-module-gitea",
"description": "The gitea backend module for the catalog plugin.",
"version": "0.1.0",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
"private": true,
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
},
"backstage": {
"role": "backend-plugin-module"
},
"scripts": {
"start": "backstage-cli package start",
"build": "backstage-cli package build",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test",
"clean": "backstage-cli package clean",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack"
},
"dependencies": {
"@backstage/backend-common": "^0.24.0",
"@backstage/backend-plugin-api": "^0.8.0"
},
"devDependencies": {
"@backstage/backend-test-utils": "^0.5.0",
"@backstage/cli": "^0.27.0"
},
"files": [
"dist"
]
}
8 changes: 8 additions & 0 deletions plugins/catalog-backend-module-gitea/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/***/
/**
* The gitea backend module for the catalog plugin.
*
* @packageDocumentation
*/

export { catalogModuleGitea as default } from './module';
21 changes: 21 additions & 0 deletions plugins/catalog-backend-module-gitea/src/lib/annotation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* The value of this annotation is the so-called login that identifies a user on
* [GitHub](https://github.com) (either the public one, or a private GitHub
* Enterprise installation) that is related to this entity. It is on the format
* `<username>`, and is the same as can be seen in the URL location bar of the
* browser when viewing that user.
*
* @public
*/
export const ANNOTATION_GITHUB_USER_LOGIN = 'github.com/user-login';

/**
* The value of this annotation is the so-called slug that identifies a team on
* [GitHub](https://github.com) (either the public one, or a private GitHub
* Enterprise installation) that is related to this entity. It is on the format
* `<organization>/<team>`, and is the same as can be seen in the URL location
* bar of the browser when viewing that team.
*
* @public
*/
export const ANNOTATION_GITHUB_TEAM_SLUG = 'github.com/team-slug';
17 changes: 17 additions & 0 deletions plugins/catalog-backend-module-gitea/src/lib/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
getGiteaRequestOptions,
GiteaIntegrationConfig,
} from '@backstage/integration';
import axios from 'axios';

// client for Gitea API using axios
const createGiteaClient = async (
config: GiteaIntegrationConfig
) => {
const options = getGiteaRequestOptions(config);
const client = axios.create(options);

return client;
}

export default createGiteaClient;
128 changes: 128 additions & 0 deletions plugins/catalog-backend-module-gitea/src/lib/defaultTransformers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2020 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 { Entity, GroupEntity, UserEntity } from '@backstage/catalog-model';
import { AxiosInstance } from 'axios';
import {
ANNOTATION_GITHUB_TEAM_SLUG,
ANNOTATION_GITHUB_USER_LOGIN,
} from './annotation';
import { GiteaOrganization, GiteaUser } from './gitea';

/**
* Context passed to Transformers
*
* @public
*/
export interface TransformerContext {
client: AxiosInstance;
org: string;
}

/**
* Transformer for Gitea users to an Entity
*
* @public
*/
export type UserTransformer = (
item: GiteaUser,
ctx: TransformerContext,
) => Promise<Entity | undefined>;

/**
* Transformer for Gitea Organization to an Entity
*
* @public
*/
export type OrganizationTransformer = (
item: GiteaOrganization,
ctx: TransformerContext,
) => Promise<Entity | undefined>;

/**
* Default transformer for Gitea users to UserEntity
*
* @public
*/
export const defaultUserTransformer = async (
item: GiteaUser,
_ctx: TransformerContext,
): Promise<UserEntity | undefined> => {
const entity: UserEntity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'User',
metadata: {
name: item.username,
annotations: {
[ANNOTATION_GITHUB_USER_LOGIN]: item.html_url,
},
},
spec: {
profile: {},
memberOf: [],
},
};

if (item.description) entity.metadata.description = item.description;
if (item.full_name) entity.spec.profile!.displayName = item.full_name;
if (item.email) entity.spec.profile!.email = item.email;
if (item.avatar_url) entity.spec.profile!.picture = item.avatar_url;
return entity;
};

/**
* Default transformer for Gitea Organization to GroupEntity
*
* @public
*/
export const defaultOrganizationTransformer: OrganizationTransformer =
async org => {
const annotations: { [annotationName: string]: string } = {
[ANNOTATION_GITHUB_TEAM_SLUG]: org.full_path,
};

if (org.edit_url) {
annotations['backstage.io/edit-url'] = org.edit_url;
}

const entity: GroupEntity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Group',
metadata: {
name: org.name,
annotations,
},
spec: {
type: 'org',
profile: {},
children: [],
},
};

if (org.description) {
entity.metadata.description = org.description;
}
if (org.full_name) {
entity.spec.profile!.displayName = org.full_name;
}
if (org.avatar_url) {
entity.spec.profile!.picture = org.avatar_url;
}

entity.spec.members = org.members.map(user => user.username);

return entity;
};
Loading

0 comments on commit de3dab5

Please sign in to comment.