Skip to content

Commit

Permalink
feat: load README from the project repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Gastón Durand Vadas committed May 25, 2023
1 parent bed797d commit 3eaf058
Show file tree
Hide file tree
Showing 15 changed files with 239 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

<!-- toc -->

- [Table of contents](#table-of-contents)
- [Features](#features)
- [Screenshots](#screenshots)
- [Setup](#setup)
- [Annotations](#annotations)
- [Code owners file](#code-owners-file)
- [Old/New GitLab Versions](#oldnew-gitlab-versions)
- [Migration guides](#migration-guides)
- [Support & Contribute](#support--contribute)
- [Support \& Contribute](#support--contribute)
- [License](#license)

<!-- tocstop -->
Expand Down Expand Up @@ -216,6 +217,9 @@ gitlab:
# Default path for CODEOWNERS file
# Default: CODEOWNERS
defaultCodeOwnersPath: .gitlab/CODEOWNERS
# Default path for README file
# Default: README.md
defaultReadmePath: .gitlab/README.md
# Entity Kinds to witch the plugin works, if you want to render gitlab
# information for one Kind you have to add it in this list.
# Default: ['Component']
Expand Down Expand Up @@ -282,6 +286,9 @@ export const apis: AnyApiFactory[] = [
codeOwnersPath: configApi.getOptionalString(
'gitlab.defaultCodeOwnersPath'
),
readmePath: configApi.getOptionalString(
'gitlab.defaultReadmePath'
),
}),
}),
];
Expand Down
3 changes: 3 additions & 0 deletions docs/migration-guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export const apis: AnyApiFactory[] = [
codeOwnersPath: configApi.getOptionalString(
'gitlab.defaultCodeOwnersPath'
),
readmePath: configApi.getOptionalString(
'gitlab.defaultReadmePath'
),
}),
}),
];
Expand Down
1 change: 1 addition & 0 deletions packages/gitlab/app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ integrations:

gitlab:
defaultCodeOwnersPath: .gitlab/CODEOWNERS
defaultReadmePath: .gitlab/README.md
proxyPath: /gitlabci
6 changes: 6 additions & 0 deletions packages/gitlab/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ export interface Config {
* @visibility frontend
*/
defaultCodeOwnersPath?: string;
/**
* Default path for README file
* @default "README.md"
* @visibility frontend
*/
defaultReadmePath?: string;
};
}
4 changes: 4 additions & 0 deletions packages/gitlab/dev/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ createDevApp()
codeOwnersPath: configApi.getOptionalString(
'gitlab.defaultCodeOwnersPath'
),
readmePath: configApi.getOptionalString(
'gitlab.defaultReadmePath'
),
}).build('0');

// Here we mock the client requests to GitLab
Expand Down Expand Up @@ -48,6 +51,7 @@ createDevApp()
annotations: {
'gitlab.com/project-id': `${projectId}`,
'gitlab.com/codeowners-path': `CODEOWNERS`,
'gitlab.com/readme-path': `README.md`,
},
name: 'backstage',
},
Expand Down
69 changes: 69 additions & 0 deletions packages/gitlab/dev/mock-gitlab/api-v4-v15.7.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6215,6 +6215,75 @@ Cargo2.toml @veloren
Cargo3.toml @veloren/dev
rust-toolchain @xMAC94x
.gitlab/scripts/* @xMAC94x`,
'projects/10174980/repository/files/README.md/raw?ref=master': `
[![pipeline status](https://gitlab.com/veloren/veloren/badges/master/pipeline.svg)](https://gitlab.com/veloren/veloren/commits/master)
[![coverage report](https://gitlab.com/veloren/veloren/badges/master/coverage.svg)](https://gitlab.com/veloren/veloren/commits/master)
[![license](https://img.shields.io/github/license/veloren/veloren.svg)](https://gitlab.com/veloren/veloren/blob/master/LICENSE)
[![discord](https://img.shields.io/discord/449602562165833758.svg)](https://discord.gg/veloren-community-449602562165833758)
[![lines of code](https://tokei.rs/b1/gitlab/veloren/veloren)](https://tokei.rs/b1/gitlab/veloren/veloren)
[![contributor count](https://img.shields.io/github/contributors/veloren/veloren)](https://gitlab.com/veloren/veloren/-/graphs/master)
## Welcome to Veloren!
Veloren is a multiplayer voxel RPG written in Rust. It is inspired by games such as Cube World, The Legend of Zelda: Breath of the Wild, Dwarf Fortress and Minecraft. The game is in active development and enjoys a flourishing player community.
## Development
Currently, most communication among contributors takes place on our [official Discord server](https://discord.gg/veloren-community-449602562165833758). You can join it to keep up with the game development, talk to us or even contribute something yourself. Anyone who shows genuine effort to help is welcome in our team. You do not need to know how to code to contribute to the game!
## Useful links
[Sign up](https://veloren.net/account/) - Here you can create an official account for Veloren.
Most Veloren servers require you to register with the official authentication server to play.
<details>
<summary>Click here for more details about authentication</summary>
The official authentication server uses TLS encryption extensively. It also employs salted hashing to ensure your login credentials are stored securely. Additionally, the server neither tracks any metrics nor uses data for any purpose other than providing the authentication service.
</details>
[The wiki](https://wiki.veloren.net) - The official wiki which provides various information about the game.
[The book](https://book.veloren.net) - The book is a collection of supplementary information about the game. It includes instructions on how to compile the game.
[Future plans](https://gitlab.com/veloren/veloren/-/milestones) - This is the development roadmap and what issues the community is currently working on.
### Official social media and websites
- [Website](https://veloren.net)
- [Discord server](https://discord.gg/veloren-community-449602562165833758)
- [Matrix server](https://matrix.to/#/#veloren-space:fachschaften.org)
- [Subreddit](https://www.reddit.com/r/Veloren)
## Get Veloren
We provide builds for Windows, macOS and Linux. The game can be downloaded on the official website:
[https://veloren.net/download/](https://veloren.net/download/)
Due to rapid game development, stable versions become outdated fast and might be **incompatible with the public server**.
It is recommended to use [Airshipper](https://book.veloren.net/players/airshipper.html), our official launcher, to keep your game up-to-date easily.
If you prefer to compile the game yourself, follow the instructions in our [book](https://book.veloren.net/contributors/introduction.html).
## F.A.Q.
### **Q:** How is this game licensed?
**A:** **It is free to play, modify and distribute. Forever.** Since it is a community project, we decided to license it under the **[GNU GPL 3.0](https://www.gnu.org/licenses/gpl-3.0.txt)** license which means it will always stay free and open source.
### **Q:** What platforms are supported?
**A:** Veloren can run on Windows, macOS and Linux, on all CPU architectures. However, the x86_64 architecture is the main focus in development. Official builds for the ARM64 architecture are also provided for macOS and Linux. It is likely possible to compile the game on other platforms as well.
### **Q:** Do you accept donations?
**A:** You can support the project on our [Open Collective](https://opencollective.com/veloren) page.
## Credit
Many thanks to everyone who has contributed to Veloren's source code, provided ideas for the game, crafted game art, composed music, hunted bugs, created tools and, overall, supported the project.
`,
'users?username=xMAC94x': [
{
id: 276442,
Expand Down
6 changes: 6 additions & 0 deletions packages/gitlab/src/api/GitlabCIApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,10 @@ export type GitlabCIApi = {
projectDefaultBranch: string | undefined,
codeOwnersPath: string
): string;

getReadme(
projectID?: string,
branch?: string,
filePath?: string
): Promise<string | undefined>;
};
28 changes: 28 additions & 0 deletions packages/gitlab/src/api/GitlabCIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,24 @@ export class GitlabCIClient implements GitlabCIApi {
identityApi: IdentityApi;
codeOwnersPath: string;
gitlabInstance: string;
readmePath: string;

constructor({
discoveryApi,
identityApi,
codeOwnersPath,
readmePath,
gitlabInstance,
}: {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
codeOwnersPath?: string;
readmePath?: string;
gitlabInstance: string;
}) {
this.discoveryApi = discoveryApi;
this.codeOwnersPath = codeOwnersPath || 'CODEOWNERS';
this.readmePath = readmePath || 'README.md';
this.gitlabInstance = gitlabInstance;
this.identityApi = identityApi;
}
Expand All @@ -46,17 +50,20 @@ export class GitlabCIClient implements GitlabCIApi {
discoveryApi,
identityApi,
codeOwnersPath,
readmePath,
}: {
discoveryApi: DiscoveryApi;
identityApi: IdentityApi;
codeOwnersPath?: string;
readmePath?: string;
}) {
return {
build: (gitlabInstance: string) =>
new this({
discoveryApi,
identityApi,
codeOwnersPath,
readmePath,
gitlabInstance,
}),
};
Expand Down Expand Up @@ -338,6 +345,27 @@ export class GitlabCIClient implements GitlabCIApi {
return owners;
}

async getReadme(
projectID?: string,
branch = 'HEAD',
filePath?: string
): Promise<string | undefined> {
filePath = filePath || this.readmePath;
// Removing starting './'
if (filePath.startsWith('./')) filePath = filePath.slice(2);

const readmeStr = await this.callApi<string>(
`projects/${projectID}/repository/files/${encodeURI(filePath)}/raw`,
{ ref: branch }
);

if (!readmeStr) {
throw Error(`README file not found`);
}

return readmeStr;
}

getContributorsLink(
projectWebUrl: string | undefined,
projectDefaultBranch: string | undefined
Expand Down
4 changes: 4 additions & 0 deletions packages/gitlab/src/components/GitlabCI/GitlabCI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import {
MergeRequestStats,
PipelinesTable,
ReleasesCard,
ReadmeCard,
} from '../widgets';

export const GitlabCI = () => (
<Page themeId="tool">
<Content>
<Grid container spacing={6} direction="row" alignItems="stretch">
<Grid item md={12}>
<ReadmeCard />
</Grid>
<Grid item sm={12} md={3} lg={3}>
<PeopleCard />
</Grid>
Expand Down
10 changes: 10 additions & 0 deletions packages/gitlab/src/components/gitlabAppData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const GITLAB_ANNOTATION_PROJECT_ID = 'gitlab.com/project-id';
export const GITLAB_ANNOTATION_PROJECT_SLUG = 'gitlab.com/project-slug';
export const GITLAB_ANNOTATION_INSTANCE = 'gitlab.com/instance';
export const GITLAB_ANNOTATION_CODEOWNERS_PATH = 'gitlab.com/codeowners-path';
export const GITLAB_ANNOTATION_README_PATH = 'gitlab.com/readme-path';
const defaultGitlabIntegration = {
hostname: 'gitlab.com',
baseUrl: 'https://gitlab.com/api/v4',
Expand Down Expand Up @@ -92,3 +93,12 @@ export const gitlabCodeOwnerPath = () => {

return codeowners_path;
};

export const gitlabReadmePath = () => {
const { entity } = useEntity();

const readme_path =
entity.metadata.annotations?.[GITLAB_ANNOTATION_README_PATH] ?? '';

return readme_path;
};
84 changes: 84 additions & 0 deletions packages/gitlab/src/components/widgets/ReadmeCard/ReadmeCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Alert from '@material-ui/lab/Alert';
import {
InfoCard,
Progress,
MarkdownContent,
} from '@backstage/core-components';
import { GitlabCIApiRef } from '../../../api';
import { useApi } from '@backstage/core-plugin-api';
import { useAsync } from 'react-use';
import {
gitlabProjectId,
gitlabProjectSlug,
gitlabReadmePath,
gitlabInstance,
} from '../../gitlabAppData';
import { ProjectDetail } from '../../types';

const useStyles = makeStyles((theme) => ({
infoCard: {
marginBottom: theme.spacing(3),
'& + .MuiAlert-root': {
marginTop: theme.spacing(3),
},
},
}));

export const ReadmeCard = ({}) => {
const classes = useStyles();
const project_id = gitlabProjectId();
const project_slug = gitlabProjectSlug();
const gitlab_instance = gitlabInstance();
const readme_path = gitlabReadmePath();

const GitlabCIAPI = useApi(GitlabCIApiRef).build(
gitlab_instance || 'gitlab.com'
);

const { value, loading, error } = useAsync(async (): Promise<{
readme: string | undefined;
}> => {
const projectDetails: any = await GitlabCIAPI.getProjectDetails(
project_slug || project_id
);
const projectId = project_id || projectDetails?.id;
const projectDetailsData: ProjectDetail = {
project_web_url: projectDetails?.web_url,
project_default_branch: projectDetails?.default_branch,
};
let readmeData: string | undefined = undefined;
try {
readmeData = await GitlabCIAPI.getReadme(
projectId,
projectDetailsData.project_default_branch,
readme_path
);
} catch (error) {
readmeData = undefined;
}
return {
readme: readmeData,
};
}, []);

if (loading) {
return <Progress />;
} else if (error) {
return (
<Alert severity="error" className={classes.infoCard}>
{error.message}
</Alert>
);
}

return (
<InfoCard title="README" className={classes.infoCard}>
<MarkdownContent
content={value?.readme || 'No README found'}
dialect="gfm"
/>
</InfoCard>
);
};
1 change: 1 addition & 0 deletions packages/gitlab/src/components/widgets/ReadmeCard/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { ReadmeCard } from './ReadmeCard';
1 change: 1 addition & 0 deletions packages/gitlab/src/components/widgets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { MergeRequestsTable } from './MergeRequestsTable';
export { PipelinesTable } from './PipelinesTable';
export { ReleasesCard } from './ReleasesCard';
export { IssuesTable } from './IssuesTable';
export { ReadmeCard } from './ReadmeCard';
1 change: 1 addition & 0 deletions packages/gitlab/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
EntityGitlabMergeRequestStatsCard,
EntityGitlabPipelinesTable,
EntityGitlabReleasesCard,
EntityGitlabReadmeCard,
gitlabPlugin,
} from './plugin';
export * from './api';
Expand Down
Loading

0 comments on commit 3eaf058

Please sign in to comment.