Skip to content

Commit 8b85fee

Browse files
committed
Add private information to the deploy keys api
This commit adds more information to the deploy keys to allow for back reference in to the main keys list. It also adds information about the repository that the key is referring to. Signed-off-by: Andrew Thornton <[email protected]>
1 parent ad4160c commit 8b85fee

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

routers/api/v1/convert/convert.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,14 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook {
167167
// ToDeployKey convert models.DeployKey to api.DeployKey
168168
func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
169169
return &api.DeployKey{
170-
ID: key.ID,
171-
Key: key.Content,
172-
URL: apiLink + com.ToStr(key.ID),
173-
Title: key.Name,
174-
Created: key.CreatedUnix.AsTime(),
175-
ReadOnly: true, // All deploy keys are read-only.
170+
ID: key.ID,
171+
KeyID: key.KeyID,
172+
Key: key.Content,
173+
Fingerprint: key.Fingerprint,
174+
URL: apiLink + com.ToStr(key.ID),
175+
Title: key.Name,
176+
Created: key.CreatedUnix.AsTime(),
177+
ReadOnly: key.Mode == models.AccessModeRead, // All deploy keys are read-only.
176178
}
177179
}
178180

routers/api/v1/repo/key.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ import (
1515
api "code.gitea.io/sdk/gitea"
1616
)
1717

18+
// appendPrivateInformation appends the owner and key type information to api.PublicKey
19+
func appendPrivateInformation(apiKey *api.DeployKey, key *models.DeployKey, repository *models.Repository) (*api.DeployKey, error) {
20+
apiKey.ReadOnly = key.Mode == models.AccessModeRead
21+
if repository.ID == key.RepoID {
22+
apiKey.Repository = repository.APIFormat(key.Mode)
23+
} else {
24+
repo, err := models.GetRepositoryByID(key.RepoID)
25+
if err != nil {
26+
return apiKey, err
27+
}
28+
apiKey.Repository = repo.APIFormat(key.Mode)
29+
}
30+
return apiKey, nil
31+
}
32+
1833
func composeDeployKeysAPILink(repoPath string) string {
1934
return setting.AppURL + "api/v1/repos/" + repoPath + "/keys/"
2035
}
@@ -54,6 +69,9 @@ func ListDeployKeys(ctx *context.APIContext) {
5469
return
5570
}
5671
apiKeys[i] = convert.ToDeployKey(apiLink, keys[i])
72+
if ctx.User.IsAdmin || ((ctx.Repo.Repository.ID == keys[i].RepoID) && (ctx.User.ID == ctx.Repo.Owner.ID)) {
73+
apiKeys[i], _ = appendPrivateInformation(apiKeys[i], keys[i], ctx.Repo.Repository)
74+
}
5775
}
5876

5977
ctx.JSON(200, &apiKeys)
@@ -102,7 +120,11 @@ func GetDeployKey(ctx *context.APIContext) {
102120
}
103121

104122
apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name)
105-
ctx.JSON(200, convert.ToDeployKey(apiLink, key))
123+
apiKey := convert.ToDeployKey(apiLink, key)
124+
if ctx.User.IsAdmin || ((ctx.Repo.Repository.ID == key.RepoID) && (ctx.User.ID == ctx.Repo.Owner.ID)) {
125+
apiKey, _ = appendPrivateInformation(apiKey, key, ctx.Repo.Repository)
126+
}
127+
ctx.JSON(200, apiKey)
106128
}
107129

108130
// HandleCheckKeyStringError handle check key error

0 commit comments

Comments
 (0)