Skip to content

Commit f55d51f

Browse files
committed
Add read-only flag for deploy keys (default true)
1 parent f6f6684 commit f55d51f

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ You can install the tool by copying [the binary](bin/gitea) onto your `PATH`, or
2424

2525
* `gitea new` *repo [create-opts...]* -- create *repo* with the specified options
2626
* `gitea delete` *repo* -- delete the named repo (use with caution!)
27-
* `gitea deploy-key` *repo keytitle publickey* -- add the named key as a deployment key for *repo*
27+
* `gitea deploy-key` *repo keytitle publickey [readonly=true]* -- add the named key as a deployment key for *repo*
2828
* `gitea exists` *repo* -- return success if *repo* exists, otherwise fail
2929
* `gitea vendor` *[create-opts...]* -- import current directory to a vendor branch, optionally creating a repository; for full details see [Vendor Imports](#vendor-imports) below.
3030

@@ -104,7 +104,7 @@ In order for the predefined subcommands to access your gitea instance, you must
104104
These variables can also be set via config file(s) or runtime environment:
105105

106106
* `GITEA_GIT_URL` -- the URL prefix used by the `vendor` command for `git clone` operations; defaults to `GITEA_URL` if not specified. Can be a hostname and `:` for simple ssh URLs, or a full URL base like `git+ssh://user@host`.
107-
* `GITEA_DEPLOY_KEY`, `GITEA_DEPLOY_KEY_TITLE` -- if set, new repositories will have this key automatically added to their deploy keys
107+
* `GITEA_DEPLOY_KEY`, `GITEA_DEPLOY_KEY_TITLE` -- if set, new repositories will have this key automatically added to their deploy keys. `GITEA_DEPLOY_READONLY` is a boolean that defaults to `true`; you must explicitly set it to `false` if you want the key to be read/write.
108108
* `GITEA_CREATE` -- an array of options used as defaults for repository creation. For example setting `GITEA_CREATE=("private=" "true")` will make new repositories private by default, unless you do `gitea new some/repo private= false` (or the equivalent, `gitea --public new some/repo`) to override it.
109109

110110
### Creating Custom Commands

bin/gitea

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,16 @@ gitea.exists() { split_repo "$1" && auth api 200 404 "repos/$REPLY" ; }
185185
gitea.delete() { split_repo "$1" && auth api 204 "" "/repos/$REPLY" -X DELETE; }
186186
gitea.deploy-key() {
187187
split_repo "$1"
188-
jmap title "$2" key "$3" | json auth api 201 "" /repos/$REPLY/keys
188+
jmap title "$2" key "$3" read_only= "${4-true}" | json auth api 201 "" /repos/$REPLY/keys
189189
}
190190
gitea.new() {
191191
split_repo "$1"; local org="${REPLY[1]}" repo="${REPLY[2]}"
192192
if [[ $org == "$GITEA_USER" ]]; then org=user; else org="org/$org"; fi
193193
jmap name "$repo" ${GITEA_CREATE[@]+"${GITEA_CREATE[@]}"} "${@:2}" |
194194
json api "200|201" "" "$org/repos?token=$GITEA_API_TOKEN"
195-
[[ ! "${GITEA_DEPLOY_KEY-}" ]] || gitea deploy-key "$1" "${GITEA_DEPLOY_KEY_TITLE:-default}" "$GITEA_DEPLOY_KEY"
195+
[[ ! "${GITEA_DEPLOY_KEY-}" ]] ||
196+
gitea deploy-key "$1" "${GITEA_DEPLOY_KEY_TITLE:-default}" \
197+
"$GITEA_DEPLOY_KEY" "${GITEA_DEPLOY_READONLY:-true}"
196198
}
197199
gitea.vendor-merge() { :; }
198200

gitea.md

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* [Commands](#commands)
2525
+ [gitea exists *repo*](#gitea-exists-repo)
2626
+ [gitea delete *repo*](#gitea-delete-repo)
27-
+ [gitea deploy-key *repo keytitle key*](#gitea-deploy-key-repo-keytitle-key)
27+
+ [gitea deploy-key *repo keytitle key [readonly=true]*](#gitea-deploy-key-repo-keytitle-key-readonlytrue)
2828
+ [gitea new *repo [opts...]*](#gitea-new-repo-opts)
2929
+ [gitea vendor [create-opts...]](#gitea-vendor-create-opts)
3030
* [Utilities](#utilities)
@@ -222,23 +222,24 @@ gitea.delete() { split_repo "$1" && auth api 204 "" "/repos/$REPLY" -X DELETE; }
222222
[0]
223223
~~~
224224
225-
### gitea deploy-key *repo keytitle key*
225+
### gitea deploy-key *repo keytitle key [readonly=true]*
226226
227227
Add a deployment key *key* named *keytitle* to *repo*. Returns success if the key was successfully added.
228228
229229
```shell
230230
gitea.deploy-key() {
231231
split_repo "$1"
232-
jmap title "$2" key "$3" | json auth api 201 "" /repos/$REPLY/keys
232+
jmap title "$2" key "$3" read_only= "${4-true}" | json auth api 201 "" /repos/$REPLY/keys
233233
}
234234
```
235235
236236
~~~shell
237-
$ curl_status=201 gitea deploy-key foo/bar baz spam
237+
$ curl_status=201 gitea deploy-key foo/bar baz spam false
238238
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
239239
{
240240
"title": "baz",
241-
"key": "spam"
241+
"key": "spam",
242+
"read_only": false
242243
}
243244
~~~
244245
@@ -252,7 +253,9 @@ gitea.new() {
252253
if [[ $org == "$GITEA_USER" ]]; then org=user; else org="org/$org"; fi
253254
jmap name "$repo" ${GITEA_CREATE[@]+"${GITEA_CREATE[@]}"} "${@:2}" |
254255
json api "200|201" "" "$org/repos?token=$GITEA_API_TOKEN"
255-
[[ ! "${GITEA_DEPLOY_KEY-}" ]] || gitea deploy-key "$1" "${GITEA_DEPLOY_KEY_TITLE:-default}" "$GITEA_DEPLOY_KEY"
256+
[[ ! "${GITEA_DEPLOY_KEY-}" ]] ||
257+
gitea deploy-key "$1" "${GITEA_DEPLOY_KEY_TITLE:-default}" \
258+
"$GITEA_DEPLOY_KEY" "${GITEA_DEPLOY_READONLY:-true}"
256259
}
257260
```
258261
@@ -287,11 +290,16 @@ gitea.new() {
287290
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
288291
{
289292
"title": "default",
290-
"key": "example-key"
293+
"key": "example-key",
294+
"read_only": true
291295
}
292296
293-
# and it can have a GITEA_DEPLOY_KEY_TITLE
294-
$ GITEA_DEPLOY_KEY=example-key GITEA_DEPLOY_KEY_TITLE=sample-title curl_status=201 gitea new foo/bar
297+
# and it can have a GITEA_DEPLOY_KEY_TITLE and GITEA_DEPLOY_KEY_READONLY
298+
$ GITEA_DEPLOY_KEY=example-key \
299+
> GITEA_DEPLOY_KEY_TITLE=sample-title \
300+
> GITEA_DEPLOY_READONLY=false \
301+
> curl_status=201 \
302+
> gitea new foo/bar
295303
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- https://example.com/gitea/api/v1/org/foo/repos\?token=EXAMPLE_TOKEN
296304
{
297305
"name": "bar",
@@ -300,7 +308,8 @@ gitea.new() {
300308
curl --silent --write-out %\{http_code\} --output /dev/null -X POST -H Content-Type:\ application/json -d @- -H Authorization:\ token\ EXAMPLE_TOKEN https://example.com/gitea/api/v1/repos/foo/bar/keys
301309
{
302310
"title": "sample-title",
303-
"key": "example-key"
311+
"key": "example-key",
312+
"read_only": false
304313
}
305314
~~~
306315

0 commit comments

Comments
 (0)