Skip to content

Commit fde6040

Browse files
authored
Added support for fetching latest release from GitLab (#7418)
* Fetch version for GitLab repositories * Update documentation about repositories * Fix code formatting
1 parent a5438a6 commit fde6040

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

docs/setup/adding-a-git-repository.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,25 @@ repo_url: https://github.com/squidfunk/mkdocs-material
2222
2323
The link to the repository will be rendered next to the search bar on big
2424
screens and as part of the main navigation drawer on smaller screen sizes.
25-
Additionally, for public repositories hosted on [GitHub] or [GitLab], the
26-
number of stars and forks is automatically requested and rendered.
2725
28-
GitHub repositories also include the tag of the latest release.[^1]
26+
Additionally, for public repositories hosted on [GitHub] or [GitLab], the
27+
latest release tag[^1], as well as the number of stars and forks, are
28+
automatically requested and rendered.
2929
3030
[^1]:
3131
Unfortunately, GitHub only provides an API endpoint to obtain the [latest
3232
release] - not the latest tag. Thus, make sure to [create a release] (not
3333
pre-release) for the latest tag you want to display next to the number of
34-
stars and forks.
34+
stars and forks. For GitLab, although it is possible to get a [list of tags
35+
sorted by update time], the [equivalent API endpoint] is used. So, make sure
36+
you also [create a release for GitLab repositories].
3537
3638
[repo_url]: https://www.mkdocs.org/user-guide/configuration/#repo_url
3739
[latest release]: https://docs.github.com/en/rest/reference/releases#get-the-latest-release
3840
[create a release]: https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository#creating-a-release
41+
[list of tags sorted by update time]: https://docs.gitlab.com/ee/api/tags.html#list-project-repository-tags
42+
[equivalent API endpoint]: https://docs.gitlab.com/ee/api/releases/#get-the-latest-release
43+
[create a release for GitLab repositories]: https://docs.gitlab.com/ee/user/project/releases/#create-a-release
3944
4045
#### Repository name
4146

src/templates/assets/javascripts/components/source/facts/gitlab/index.ts

+37-8
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,25 @@ import {
2626
Observable,
2727
catchError,
2828
defaultIfEmpty,
29-
map
29+
map,
30+
zip
3031
} from "rxjs"
3132

3233
import { requestJSON } from "~/browser"
3334

3435
import { SourceFacts } from "../_"
3536

37+
/* ----------------------------------------------------------------------------
38+
* Helper types
39+
* ------------------------------------------------------------------------- */
40+
41+
/**
42+
* GitLab release (partial)
43+
*/
44+
interface Release { // @todo remove and use the ReleaseSchema type instead after switching from gitlab to @gitbeaker/rest
45+
tag_name: string /* Tag name */
46+
}
47+
3648
/* ----------------------------------------------------------------------------
3749
* Functions
3850
* ------------------------------------------------------------------------- */
@@ -49,13 +61,30 @@ export function fetchSourceFactsFromGitLab(
4961
base: string, project: string
5062
): Observable<SourceFacts> {
5163
const url = `https://${base}/api/v4/projects/${encodeURIComponent(project)}`
52-
return requestJSON<ProjectSchema>(url)
64+
return zip(
65+
66+
/* Fetch version */
67+
requestJSON<Release>(`${url}/releases/permalink/latest`)
68+
.pipe(
69+
catchError(() => EMPTY), // @todo refactor instant loading
70+
map(({ tag_name }) => ({
71+
version: tag_name
72+
})),
73+
defaultIfEmpty({})
74+
),
75+
76+
/* Fetch stars and forks */
77+
requestJSON<ProjectSchema>(url)
78+
.pipe(
79+
catchError(() => EMPTY), // @todo refactor instant loading
80+
map(({ star_count, forks_count }) => ({
81+
stars: star_count,
82+
forks: forks_count
83+
})),
84+
defaultIfEmpty({})
85+
)
86+
)
5387
.pipe(
54-
catchError(() => EMPTY), // @todo refactor instant loading
55-
map(({ star_count, forks_count }) => ({
56-
stars: star_count,
57-
forks: forks_count
58-
})),
59-
defaultIfEmpty({})
88+
map(([release, info]) => ({ ...release, ...info }))
6089
)
6190
}

0 commit comments

Comments
 (0)