1
1
# Gradle Enterprise API Kotlin
2
2
3
+ [ ![ Release] ( https://jitpack.io/v/gabrielfeo/gradle-enterprise-api-kotlin.svg )] ( https://jitpack.io/#gabrielfeo/gradle-enterprise-api-kotlin )
4
+ [ ![ Javadoc] ( https://img.shields.io/badge/javadoc-0.9-orange )] ( https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/ )
5
+
3
6
A Kotlin library to access the [ Gradle Enterprise API] [ 1 ] , easy to use from Kotlin
4
7
scripts:
5
8
6
9
``` kotlin
7
- val builds = api.getBuilds(since = 0 , maxBuilds = 10 ).execute().body()!!
8
- builds.forEach {
10
+ api.getBuilds(since = yesterday).forEach {
9
11
println (it)
10
12
}
11
13
```
12
14
13
15
## Setup
14
16
15
- Set up your environment once and use the library from any script in your machine.
17
+ Set up once and use the library from any script in your machine:
16
18
17
19
- ` GRADLE_ENTERPRISE_URL ` environment variable: the URL of your Gradle Enterprise instance
18
20
- ` GRADLE_ENTERPRISE_API_TOKEN ` environment variable: an API access token for the Gradle
26
28
@file:Repository(" https://jitpack.io" )
27
29
@file:DependsOn(" com.github.gabrielfeo:gradle-enterprise-api-kotlin:1.0" )
28
30
29
- val builds = api.getBuilds(since = 0 , maxBuilds = 10 ).execute().body()!!
30
- builds.forEach {
31
- println (it)
32
- }
31
+ api.getBuild(id = " hy5nxbzfjxe5k" )
33
32
```
34
33
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 `
53
-
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 >
34
+ For configuring base URL and token via code and other available options, see the
35
+ [ ` Options ` object] [ 8 ] .
79
36
80
37
<details >
81
38
<summary >Setup in full projects (non-scripts)</summary >
@@ -104,63 +61,43 @@ See the [sample script](./sample.main.kts) for a complete example.
104
61
```
105
62
106
63
</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
64
</details >
118
65
119
66
## Usage
120
67
121
- API endpoints are provided as a single interface: ` GradleEnterpriseApi ` . It's
68
+ API endpoints are provided as a single interface: [ ` GradleEnterpriseApi ` ] [ 9 ] . The Javadoc is a
69
+ the same as Gradle's online docs, as they're generated from the same spec. An instance is
122
70
initialized and ready-to-use as the global ` api ` instance:
123
71
124
72
``` kotlin
125
73
api.getBuild(id = " hy5nxbzfjxe5k" )
126
74
```
127
75
128
- It's recommended to learn about endpoints and their responses through IDE auto-complete. Javadoc
129
- appearing in auto-complete is the full API manual, same as Gradle's online docs.
130
-
131
- This library provides a few extension functions on top of the regular API:
76
+ The library also provides a few extension functions on top of the regular API, such as paged
77
+ requests and joining. See [ ` GradleEnterpriseApi ` extensions] [ 10 ] .
132
78
133
79
``` kotlin
134
- // Regular query to /api/builds, limited to 1000 builds server-side
80
+ // Standard query to /api/builds, limited to 1000 builds server-side
135
81
api.getBuilds(since = lastMonth)
136
- // Streams all available builds from a given date, split in as getBuilds
137
- // as needed
82
+ // Extension: Streams all available builds since given date (paging underneath)
138
83
api.getBuildsFlow(since = lastMonth)
139
84
```
140
85
86
+ It's recommended to call [ ` shutdown() ` ] [ 11 ] at the end of scripts to release resources and let the
87
+ program exit. Otherwise, it'll keep running for an extra ~ 60s after code finishes, as an [ expected
88
+ behavior of OkHttp] [ 4 ] .
89
+
141
90
``` 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)
91
+ val builds = api.getBuilds()
92
+ // do work ...
93
+ shutdown()
150
94
```
151
95
152
-
153
96
## More info
154
97
155
- - Currently built for Gradle Enterprise ` 2022.4 ` , but can be used with previous versions.
98
+ - Currently built for Gradle Enterprise ` 2022.4 ` , but should work fine with previous versions.
156
99
- Use JDK 8 or 14+ to run, if you want to avoid the [ "illegal reflective access" warning about
157
100
Retrofit] [ 3 ]
158
- - There is a global instance ` okHttpClient ` so you can change what's needed, but also concurrency
159
- shortcuts ` maxConcurrentRequests ` and ` shutdown() ` .
160
- - ` maxConcurrentRequests ` is useful to speed up scripts, but if you start getting HTTP 504 from
161
- your GE instance, decreasing this value should help.
162
- - The script will keep running for an extra ~ 60s after code finishes, as an [ expected behavior
163
- of OkHttp] [ 4 ] , unless you call ` shutdown() ` (global function).
164
101
- All classes live in these two packages. If you need to make small edits to scripts where
165
102
there's no auto-complete, wildcard imports can be used:
166
103
@@ -180,3 +117,8 @@ API classes such as `GradleEnterpriseApi` and response models are generated from
180
117
[ 4 ] : https://github.com/square/retrofit/issues/3144#issuecomment-508300518
181
118
[ 5 ] : https://docs.gradle.com/enterprise/api-manual/#reference_documentation
182
119
[ 6 ] : https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-gradle-plugin/README.adoc
120
+ [ 7 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/
121
+ [ 8 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/gradle-enterprise-api-kotlin/com.gabrielfeo.gradle.enterprise.api/-options/
122
+ [ 9 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/gradle-enterprise-api-kotlin/com.gabrielfeo.gradle.enterprise.api/-gradle-enterprise-api/
123
+ [ 10 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/gradle-enterprise-api-kotlin/com.gabrielfeo.gradle.enterprise.api/-gradle-enterprise-api/index.html#373241164%2FExtensions%2F769193423
124
+ [ 11 ] : https://gabrielfeo.github.io/gradle-enterprise-api-kotlin/gradle-enterprise-api-kotlin/com.gabrielfeo.gradle.enterprise.api/shutdown.html
0 commit comments