Skip to content

Commit 2a57af9

Browse files
committed
Update README
1 parent f9e4231 commit 2a57af9

File tree

1 file changed

+124
-54
lines changed

1 file changed

+124
-54
lines changed

README.md

Lines changed: 124 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,155 @@ builds.forEach {
1212

1313
## Setup
1414

15-
Add a dependency by copying these lines to the top of your `kts` scripts:
15+
Set up your environment once and use the library from any script in your machine.
16+
17+
- `GRADLE_ENTERPRISE_URL` environment variable: the URL of your Gradle Enterprise instance
18+
- `GRADLE_ENTERPRISE_API_TOKEN` environment variable: an API access token for the Gradle
19+
Enterprise instance. Alternatively, can be a macOS keychain entry labeled
20+
`gradle-enterprise-api-token` (recommended).
21+
22+
That's it! From any `kts` script, you can add a dependency on the library and start querying the
23+
API:
1624

1725
```kotlin
1826
@file:Repository("https://jitpack.io")
1927
@file:DependsOn("com.github.gabrielfeo:gradle-enterprise-api-kotlin:1.0")
28+
29+
val builds = api.getBuilds(since = 0, maxBuilds = 10).execute().body()!!
30+
builds.forEach {
31+
println(it)
32+
}
2033
```
2134

22-
Following convention over configuration, set:
23-
- the URL of your Gradle Enterprise instance, in environment variable `GRADLE_ENTERPRISE_URL`
24-
- an API access token, either in macOS keychain labeled as "gradle-enterprise-api-token"
25-
or in an environment variable named `GRADLE_ENTERPRISE_API_TOKEN`
35+
See the [sample script](./sample.main.kts) for a complete example.
36+
37+
<details>
38+
<summary>Optional setup</summary>
39+
40+
All of the following have default values and are completely optional. See
41+
[Api.kt](src/main/kotlin/com/gabrielfeo/gradle/enterprise/api/Api.kt) for details.
42+
43+
##### Caching
44+
45+
Gradle Enterprise API disallows HTTP caching, but this library forces
46+
caching for faster queries. Caching is split in two categories:
47+
48+
1. Short-term cache (default max-age of 1 day)
49+
- `/api/builds`
50+
2. Long-term cache (default max-age of 1 year)
51+
- `/api/builds/{id}/gradle-attributes`
52+
- `/api/builds/{id}/maven-attributes`
2653

27-
By doing so, any script can query `api` without additional setup. See the [sample
28-
script](./sample.main.kts) for a complete example.
54+
max-age and cached URLs can be changed with options below.
55+
56+
- `GRADLE_ENTERPRISE_API_CACHE_DIR`: HTTP cache location. Defaults to the system temporary
57+
directory.
58+
- `GRADLE_ENTERPRISE_API_MAX_CACHE_SIZE`: Max size of the HTTP cache in bytes. Defaults to ~1GB.
59+
- `GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_URL_PATTERN`: Regex pattern to match API URLs that are
60+
OK to store short-term in the HTTP cache.
61+
- `GRADLE_ENTERPRISE_API_SHORT_TERM_CACHE_MAX_AGE`: Max age in seconds of each response stored
62+
short-term in the HTTP cache.
63+
- `GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_URL_PATTERN`: Regex pattern to match API URLs that are
64+
OK to store long-term in the HTTP cache.
65+
- `GRADLE_ENTERPRISE_API_LONG_TERM_CACHE_MAX_AGE`: Max age in seconds of each response stored
66+
long-term in the HTTP cache.
67+
68+
##### Concurrency
69+
70+
- `GRADLE_ENTERPRISE_API_MAX_CONCURRENT_REQUESTS`: Maximum amount of concurrent requests
71+
allowed. Defaults to 15.
72+
73+
##### Debugging
74+
75+
- `GRADLE_ENTERPRISE_API_DEBUG_LOGGING`: `true` to enable debug logging from the library. Defaults
76+
to `false`.
77+
78+
</details>
79+
80+
<details>
81+
<summary>Setup in full projects (non-scripts)</summary>
82+
83+
You can also use it in a full Kotlin project instead of a script. Just add a dependency:
84+
85+
```kotlin
86+
repositories {
87+
maven(url = "https://jitpack.io")
88+
}
89+
dependencies {
90+
implementation("com.github.gabrielfeo:gradle-enterprise-api-kotlin:1.0")
91+
}
92+
```
93+
94+
<details>
95+
<summary>Groovy</summary>
96+
97+
```groovy
98+
repositories {
99+
maven { url = 'https://jitpack.io' }
100+
}
101+
dependencies {
102+
implementation 'com.github.gabrielfeo:gradle-enterprise-api-kotlin:1.0'
103+
}
104+
```
105+
106+
</details>
107+
108+
Any option can also be changed from code rather than from environment, as long as it's done
109+
before the first `api` usage.
110+
111+
```kotlin
112+
baseUrl = { "https://my.ge.org" }
113+
accessToken = { getFromVault("ge-api-token") }
114+
api.getBuilds(id = "hy5nxbzfjxe5k")
115+
```
116+
117+
</details>
29118

30119
## Usage
31120

32-
The library provides API endpoints as a single [Retrofit][2] Service interface:
33-
`GradleEnterpriseApi`. It's ready-to-use as the global `api` instance:
121+
API endpoints are provided as a single interface: `GradleEnterpriseApi`. It's
122+
initialized and ready-to-use as the global `api` instance:
34123

35124
```kotlin
36125
api.getBuild(id = "hy5nxbzfjxe5k")
37126
```
38127

39128
It's recommended to learn about endpoints and their responses through IDE auto-complete. Javadoc
40-
appearing in auto-complete is the full API manual. Each method is documented with
41-
params and possible status codes.
129+
appearing in auto-complete is the full API manual, same as Gradle's online docs.
130+
131+
This library provides a few helper functions on top of the regular API:
132+
133+
```kotlin
134+
// Regular query to /api/builds, limited to 1000 builds server-side
135+
api.getBuilds(since = lastMonth)
136+
// Streams all available builds from a given date, split in as getBuilds
137+
// as needed
138+
api.getBuildsFlow(since = lastMonth)
139+
```
140+
141+
```kotlin
142+
// To get build scan data such as username, tags and custom values, one
143+
// must usually query /api/builds/{id}/gradle-attributes per-build, which
144+
// is verbose and slow (1 request at a time)
145+
api.getBuildsFlow(since = lastMonth)
146+
.map { build -> api.getGradleAttributes(id = build.id) }
147+
// Streams all available builds as GradleAttributes from a given date,
148+
// requesting more than 1 build at a time.
149+
api.getGradleAttributesFlow(since = lastMonth)
150+
```
42151

43-
Helper functions are also available:
44-
- `GradleEnterpriseApi.buildsSequence()` returns a sequence making paged requests underneath
45152

46153
## More info
47154

48155
- Currently built for Gradle Enterprise `2022.4`, but can be used with previous versions.
49156
- Use JDK 8 or 14+ to run, if you want to avoid the ["illegal reflective access" warning about
50157
Retrofit][3]
51-
- There is a global instance `okHttpClient` so you can change what's needed, but also concurrency
158+
- There is a global instance `okHttpClient` so you can change what's needed, but also concurrency
52159
shortcuts `maxConcurrentRequests` and `shutdown()`.
53160
- `maxConcurrentRequests` is useful to speed up scripts, but if you start getting HTTP 504 from
54-
your GE instance, decreasing this value should help.
161+
your GE instance, decreasing this value should help.
55162
- The script will keep running for an extra ~60s after code finishes, as an [expected behavior
56-
of OkHttp][4], unless you call `shutdown()` (global function).
163+
of OkHttp][4], unless you call `shutdown()` (global function).
57164
- All classes are in the same package, so that if you need to make small edits to scripts where
58165
there's no auto-complete, a single wildcard import can be used:
59166

@@ -64,44 +171,7 @@ import com.gabrielfeo.gradle.enterprise.api.*
64171
### Internals
65172

66173
API classes such as `GradleEnterpriseApi` and response models are generated from the offical
67-
[API spec][5], using the [OpenAPI Generator Gradle Plugin][6]. Actual project classes are a thin
68-
layer over the generated code, to make it easy to use from scripts.
69-
70-
### Custom setups
71-
72-
You can also use it in a full Kotlin project instead of a script. Just add a dependency:
73-
74-
```kotlin
75-
repositories {
76-
maven(url = "https://jitpack.io")
77-
}
78-
dependencies {
79-
implementation("com.github.gabrielfeo:gradle-enterprise-api-kotlin:1.0")
80-
}
81-
```
82-
83-
<details>
84-
<summary>Groovy</summary>
85-
86-
```groovy
87-
repositories {
88-
maven { url = 'https://jitpack.io' }
89-
}
90-
dependencies {
91-
implementation 'com.github.gabrielfeo:gradle-enterprise-api-kotlin:1.0'
92-
}
93-
```
94-
95-
</details>
96-
97-
`api` is ready-to-use if conventions are followed, but if you'd rather you can set `baseUrl` and
98-
`accessToken` from code instead, before using `api`:
99-
100-
```kotlin
101-
baseUrl = "https://my.ge.org"
102-
accessToken = "abcdefg"
103-
api.getBuild(id = "hy5nxbzfjxe5k")
104-
```
174+
[API spec][5], using the [OpenAPI Generator Gradle Plugin][6].
105175

106176
[1]: https://docs.gradle.com/enterprise/api-manual/
107177
[2]: https://square.github.io/retrofit/

0 commit comments

Comments
 (0)