Skip to content

Commit

Permalink
Added support for fetching latest release from GitLab (#7418)
Browse files Browse the repository at this point in the history
* Fetch version for GitLab repositories

* Update documentation about repositories

* Fix code formatting
  • Loading branch information
joaopalmeiro authored Aug 5, 2024
1 parent a5438a6 commit fde6040
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
13 changes: 9 additions & 4 deletions docs/setup/adding-a-git-repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ repo_url: https://github.com/squidfunk/mkdocs-material
The link to the repository will be rendered next to the search bar on big
screens and as part of the main navigation drawer on smaller screen sizes.
Additionally, for public repositories hosted on [GitHub] or [GitLab], the
number of stars and forks is automatically requested and rendered.
GitHub repositories also include the tag of the latest release.[^1]
Additionally, for public repositories hosted on [GitHub] or [GitLab], the
latest release tag[^1], as well as the number of stars and forks, are
automatically requested and rendered.
[^1]:
Unfortunately, GitHub only provides an API endpoint to obtain the [latest
release] - not the latest tag. Thus, make sure to [create a release] (not
pre-release) for the latest tag you want to display next to the number of
stars and forks.
stars and forks. For GitLab, although it is possible to get a [list of tags
sorted by update time], the [equivalent API endpoint] is used. So, make sure
you also [create a release for GitLab repositories].
[repo_url]: https://www.mkdocs.org/user-guide/configuration/#repo_url
[latest release]: https://docs.github.com/en/rest/reference/releases#get-the-latest-release
[create a release]: https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository#creating-a-release
[list of tags sorted by update time]: https://docs.gitlab.com/ee/api/tags.html#list-project-repository-tags
[equivalent API endpoint]: https://docs.gitlab.com/ee/api/releases/#get-the-latest-release
[create a release for GitLab repositories]: https://docs.gitlab.com/ee/user/project/releases/#create-a-release
#### Repository name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,25 @@ import {
Observable,
catchError,
defaultIfEmpty,
map
map,
zip
} from "rxjs"

import { requestJSON } from "~/browser"

import { SourceFacts } from "../_"

/* ----------------------------------------------------------------------------
* Helper types
* ------------------------------------------------------------------------- */

/**
* GitLab release (partial)
*/
interface Release { // @todo remove and use the ReleaseSchema type instead after switching from gitlab to @gitbeaker/rest
tag_name: string /* Tag name */
}

/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
Expand All @@ -49,13 +61,30 @@ export function fetchSourceFactsFromGitLab(
base: string, project: string
): Observable<SourceFacts> {
const url = `https://${base}/api/v4/projects/${encodeURIComponent(project)}`
return requestJSON<ProjectSchema>(url)
return zip(

/* Fetch version */
requestJSON<Release>(`${url}/releases/permalink/latest`)
.pipe(
catchError(() => EMPTY), // @todo refactor instant loading
map(({ tag_name }) => ({
version: tag_name
})),
defaultIfEmpty({})
),

/* Fetch stars and forks */
requestJSON<ProjectSchema>(url)
.pipe(
catchError(() => EMPTY), // @todo refactor instant loading
map(({ star_count, forks_count }) => ({
stars: star_count,
forks: forks_count
})),
defaultIfEmpty({})
)
)
.pipe(
catchError(() => EMPTY), // @todo refactor instant loading
map(({ star_count, forks_count }) => ({
stars: star_count,
forks: forks_count
})),
defaultIfEmpty({})
map(([release, info]) => ({ ...release, ...info }))
)
}

0 comments on commit fde6040

Please sign in to comment.