Skip to content

Commit ff48860

Browse files
committed
handle scenario described in feedback
* move logic into a shared function
1 parent 75ae415 commit ff48860

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

models/asymkey/display_key.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package asymkey
2+
3+
import (
4+
"os"
5+
6+
"code.gitea.io/gitea/modules/git"
7+
)
8+
9+
func GetDisplaySigningKey(key *git.SigningKey) (string, error) {
10+
if key != nil {
11+
switch key.Format {
12+
case git.SigningKeyFormatOpenPGP:
13+
return key.KeyID, nil
14+
case git.SigningKeyFormatSSH:
15+
content, readErr := os.ReadFile(key.KeyID)
16+
if readErr != nil {
17+
return "", readErr
18+
}
19+
return CalcFingerprint(string(content))
20+
}
21+
}
22+
return "", nil
23+
}

routers/web/repo/issue_view.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"math/big"
99
"net/http"
1010
"net/url"
11-
"os"
1211
"sort"
1312
"strconv"
1413

@@ -496,26 +495,11 @@ func preparePullViewSigning(ctx *context.Context, issue *issues_model.Issue) {
496495
if ctx.Doer != nil {
497496
sign, key, _, err := asymkey_service.SignMerge(ctx, pull, ctx.Doer, pull.BaseRepo.RepoPath(), pull.BaseBranch, pull.GetGitHeadRefName())
498497
ctx.Data["WillSign"] = sign
499-
if key != nil {
500-
switch key.Format {
501-
case git.SigningKeyFormatOpenPGP:
502-
ctx.Data["SigningKey"] = key.KeyID
503-
case git.SigningKeyFormatSSH:
504-
content, readErr := os.ReadFile(key.KeyID)
505-
if readErr != nil {
506-
log.Error("Error whilst reading public key of pr %d in repo %s. Error: %v", pull.ID, pull.BaseRepo.FullName(), readErr)
507-
ctx.Data["SigningKey"] = "Unknown"
508-
} else {
509-
var fingerprintErr error
510-
ctx.Data["SigningKey"], fingerprintErr = asymkey_model.CalcFingerprint(string(content))
511-
if fingerprintErr != nil {
512-
log.Error("Error whilst generating public key fingerprint of pr %d in repo %s. Error: %v", pull.ID, pull.BaseRepo.FullName(), fingerprintErr)
513-
} else {
514-
ctx.Data["SigningKey"] = "Unknown"
515-
}
516-
}
517-
}
498+
displayKeyID, displayKeyIDErr := asymkey_model.GetDisplaySigningKey(key)
499+
if displayKeyIDErr != nil {
500+
log.Error("Error whilst getting the display keyID: %s", displayKeyIDErr.Error())
518501
}
502+
ctx.Data["SigningKey"] = displayKeyID
519503
if err != nil {
520504
if asymkey_service.IsErrWontSign(err) {
521505
ctx.Data["WontSignReason"] = err.(*asymkey_service.ErrWontSign).Reason

services/context/repo.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path"
1515
"strings"
1616

17+
asymkey_model "code.gitea.io/gitea/models/asymkey"
1718
"code.gitea.io/gitea/models/db"
1819
git_model "code.gitea.io/gitea/models/git"
1920
issues_model "code.gitea.io/gitea/models/issues"
@@ -99,7 +100,7 @@ type CommitFormOptions struct {
99100
UserCanPush bool
100101
RequireSigned bool
101102
WillSign bool
102-
SigningKey *git.SigningKey
103+
SigningKey string
103104
WontSignReason string
104105
CanCreatePullRequest bool
105106
CanCreateBasePullRequest bool
@@ -139,7 +140,7 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
139140
protectionRequireSigned = protectedBranch.RequireSignedCommits
140141
}
141142

142-
willSign, signKeyID, _, err := asymkey_service.SignCRUDAction(ctx, targetRepo.RepoPath(), doer, targetRepo.RepoPath(), refName.String())
143+
willSign, signKey, _, err := asymkey_service.SignCRUDAction(ctx, targetRepo.RepoPath(), doer, targetRepo.RepoPath(), refName.String())
143144
wontSignReason := ""
144145
if asymkey_service.IsErrWontSign(err) {
145146
wontSignReason = string(err.(*asymkey_service.ErrWontSign).Reason)
@@ -155,14 +156,19 @@ func PrepareCommitFormOptions(ctx *Context, doer *user_model.User, targetRepo *r
155156
canCreateBasePullRequest := targetRepo.BaseRepo != nil && targetRepo.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests)
156157
canCreatePullRequest := targetRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || canCreateBasePullRequest
157158

159+
displayKeyID, displayKeyIDErr := asymkey_model.GetDisplaySigningKey(signKey)
160+
if displayKeyIDErr != nil {
161+
log.Error("Error whilst getting the display keyID: %s", displayKeyIDErr.Error())
162+
}
163+
158164
opts := &CommitFormOptions{
159165
TargetRepo: targetRepo,
160166
WillSubmitToFork: submitToForkedRepo,
161167
CanCommitToBranch: canCommitToBranch,
162168
UserCanPush: canPushWithProtection,
163169
RequireSigned: protectionRequireSigned,
164170
WillSign: willSign,
165-
SigningKey: signKeyID,
171+
SigningKey: displayKeyID,
166172
WontSignReason: wontSignReason,
167173

168174
CanCreatePullRequest: canCreatePullRequest,

0 commit comments

Comments
 (0)