RSC-Labs / backstage-highlights-plugin

Backstage Highlights Plugin is configurable and customizable plugin for viewing the most important information about your entity.
Mozilla Public License 2.0
5 stars 1 forks source link

Support for multiple gitlab instances #1

Closed Zigu closed 6 months ago

Zigu commented 9 months ago

Hi,

this plugin looks really nice. Unfortunately, we are using multiple instances of Gitlab in the company. It would be great if you could adjust the backend-plugin to support multiple instances. Similar to the integrations configuration.

integrations:
  gitlab:
    - host: gitlab-host-1
       token: token 1
       apiBaseUrl: https://gitlab-host-1/api/v4
    - host: gitlab-host-2
      token: token 2
      apiBaseUrl: https://gitlab-host-2/api/v4

Thank you in advance

radoslaw-sz commented 9 months ago

hi @Zigu , thanks for the interest. Let me take a look at this.

radoslaw-sz commented 9 months ago

hi @Zigu, New versions of plugin landed in npm. They shall support multiple Gitlab instances. See also update in README: https://github.com/RSC-Labs/backstage-highlights-plugin#multiple-gitlab-instances

Please kindly check if it is working.

Zigu commented 9 months ago

great. thx.

Unfortunately, I will be on vacation until end of the year. But will check as soon as possible.

radoslaw-sz commented 7 months ago

hi @Zigu - did you have a chance to check it?

radoslaw-sz commented 6 months ago

hi @Zigu I am closing this issue, if you would see any problem, please do not hesitate to open it again.

Zigu commented 6 months ago

Hi @radoslaw-sz,

thx for the update. Unfortunately, there is still a problem with the integration. The fetching of commits is not working properly.

It only works when I reference a personal project in a self-hosted gitlab instance. On gitlab.com or on other projects of the self-hosted instance it is failing with the below error. I assume the problem is that you try to use the project-slug as id. You have to request the project-details first with the slug to retrieve the id (see immobiliare-gitlab-plugin). Not sure, whether I will find time by end of February but I could try to provide a PR.

Error:

[1] 2024-02-18T17:02:55.603Z backstage error Request failed with status 500 Request failed with 404 Error type=errorHandler response=[object Response] body=[object Object] cause=Error: Request failed with status 404 Not Found, {"message":"404 Project Not Found"} name=ResponseError stack=ResponseError: Request failed with 404 Error
[1]     at Function.fromResponse (/Users/r.porscha/IdeaProjects/devtools/developer-portal/node_modules/@backstage/errors/src/errors/ResponseError.ts:73:12)
[1]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[1]     at fetchGitlabCommits (/Users/r.porscha/IdeaProjects/devtools/developer-portal/node_modules/@rsc-labs/backstage-highlights-plugin-backend/src/lib/fetchCommits.ts:82:15)
[1]     at <anonymous> (/Users/r.porscha/IdeaProjects/devtools/developer-portal/node_modules/@rsc-labs/backstage-highlights-plugin-backend/src/service/router.ts:252:24)
radoslaw-sz commented 6 months ago

@Zigu thanks for raising it, I am reopening this issue. I will let you know if I also would have a time (busy week).

Zigu commented 6 months ago

unfortunately, I cannot push to this repo. Here my change proposal.

in backend-plugin:

export async function getGitlabProjectDetails(projectSlug:string, apiBaseUrl:string, token:string){ return callAPI(${apiBaseUrl}/projects/${encodeURIComponent(projectSlug)}, token); }

export async function getGitlabTags(projectId:string, apiBaseUrl:string, token:string) { return callAPI(${apiBaseUrl}/projects/${projectId}/repository/tags, token); }

export async function getGitlabBranches(projectId:string, apiBaseUrl:string, token:string) { return callAPI(${apiBaseUrl}/projects/${projectId}/repository/branches, token); }

export async function getGitlabCommits(projectId:string, apiBaseUrl:string, token:string) { return callAPI(${apiBaseUrl}/projects/${projectId}/repository/commits, token); }

async function callAPI(url:string, token:string) { const options = { headers: { 'PRIVATE-TOKEN': token}};

const response = await fetch(url, options);

if (response.status !== 200) {
    throw await ResponseError.fromResponse(response);
}

return await response.json();

}


- src/lib/fetchTags.ts

... export async function fetchGitlabTags(projectSlug: string, token: string, apiBaseUrl: string): Promise<GitTag[]> {

const projectDetails = await getGitlabProjectDetails(projectSlug, apiBaseUrl, token);
const projectId = projectDetails.id;

const tagsJson = await getGitlabTags(projectId, apiBaseUrl, token);

const tags = tagsJson.map((singleResult: { name: any; commit: { id: any; message: any; }; }) => {
    return {
        name: singleResult.name,
        tagUrl: `${projectDetails.web_url}/-/releases/${singleResult.name}`,
        commitId: singleResult.commit.id,
        commitUrl: `${projectDetails.web_url}/-/commit/${singleResult.commit.id}`,
        commitMessage: singleResult.commit.message
    }
})

return tags;

} ...


- src/lib/fetchCommits.ts

... export async function fetchGitlabCommits(projectSlug: string, token: string, apiBaseUrl: string): Promise<GitCommit[]> {

const projectDetails = await getGitlabProjectDetails(projectSlug, apiBaseUrl, token);
const projectId = projectDetails.id;

const resultJson = await getGitlabCommits(projectId, apiBaseUrl, token);

const commits = resultJson.map(((singleResult: { id: any; author_name: any; web_url: any; message: any; committed_date: any; committer_name: any; }) => {
    return {
        id: singleResult.id,
        author: singleResult.author_name,
        htmlUrl: singleResult.web_url,
        message: singleResult.message,
        date: singleResult.committed_date
    }
}));

return commits;

} ...

- src/lib/fetchBranches.ts

... export async function fetchGitlabBranches(projectSlug: string, token: string, apiBaseUrl: string): Promise<GitBranch[]> {

const projectDetails = await getGitlabProjectDetails(projectSlug, apiBaseUrl, token);
const projectId = projectDetails.id;

const resultJson = await getGitlabBranches(projectId, apiBaseUrl, token);

const branches = resultJson.map((singleResult: { name: any; }) => singleResult.name);

return branches;

} ...

radoslaw-sz commented 6 months ago

@Zigu wow, thanks for the proposal! This was not my intention to forbid people to make a pull requests, so let me check settings of this repo.

edit: @Zigu are you able to create a fork, make a commit and create pull request? It would be easier way to review these changes. If you are not familiar with approach then I will check this code in the next days.

radoslaw-sz commented 6 months ago

hi @Zigu - your changes have been merged and package has been published with version 0.2.1. Kindly check if it works, thanks!

Zigu commented 6 months ago

fetchCommits is not yet using the recent gitlab lib function. could you please change that. thx.

fetchTags and fetchBranches are adjusted.

radoslaw-sz commented 6 months ago

@Zigu right, sorry, I missed it. I have published new version with that - 0.2.2

Zigu commented 6 months ago

@radoslaw-sz I am sorry but according to https://socket.dev/npm/package/@rsc-labs/backstage-highlights-plugin-backend/versions/0.2.2 the change was not part of the package. could you please trigger it again? thx.

radoslaw-sz commented 6 months ago

hi @Zigu I am really sorry - I just wanted to do so fast that I forgot to build changes... 0.2.3 release, I hope now it will be ok.

Zigu commented 6 months ago

@radoslaw-sz works. thx a lot :)