-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
WIP: Add require signed commits feature #8584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2019 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"xorm.io/xorm" | ||
) | ||
|
||
func addRequireSignedCommitsOnProtectedBranch(x *xorm.Engine) error { | ||
type ProtectedBranch struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RequireSignedCommits bool `xorm:"NOT NULL DEFAULT false"` | ||
} | ||
|
||
return x.Sync2(new(ProtectedBranch)) | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1336,6 +1336,8 @@ settings.protect_merge_whitelist_teams = Whitelisted teams for merging: | |||||
settings.protect_check_status_contexts = Enable Status Check | ||||||
settings.protect_check_status_contexts_desc = Require status checks to pass before merging Choose which status checks must pass before branches can be merged into a branch that matches this rule. When enabled, commits must first be pushed to another branch, then merged or pushed directly to a branch that matches this rule after status checks have passed. If no contexts are selected, the last commit must be successful regardless of context. | ||||||
settings.protect_check_status_contexts_list = Status checks found in the last week for this repository | ||||||
settings.requrie_signed_commits = Require signed commits | ||||||
settings.requrie_signed_commits_desc = Commits pushed to this branch must have verified signatures. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
settings.protect_required_approvals = Required approvals: | ||||||
settings.protect_required_approvals_desc = Allow only to merge pull request with enough positive reviews of whitelisted users or teams. | ||||||
settings.protect_approvals_whitelist_users = Whitelisted reviewers: | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||||||||||||||||||||||||||||||||
package private | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||||||
"bytes" | ||||||||||||||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||||||||||||||
"net/http" | ||||||||||||||||||||||||||||||||||
"os" | ||||||||||||||||||||||||||||||||||
|
@@ -119,6 +120,48 @@ func HookPreReceive(ctx *macaron.Context) { | |||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
// check signed commits | ||||||||||||||||||||||||||||||||||
if protectBranch.RequireSignedCommits { | ||||||||||||||||||||||||||||||||||
gitRepo, err := git.OpenRepository(repo.RepoPath()) | ||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||
log.Error("Unable to find the git repository %s/%s: %v", ownerName, repoName, err) | ||||||||||||||||||||||||||||||||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||||||||||||||||||||||||||||||||||
"err": fmt.Sprintf("Unable to find the git repository %s/%s", ownerName, repoName), | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
stdout, err := git.NewCommand("rev-list", oldCommitID+"..."+newCommitID).RunInDirBytes(repo.RepoPath()) | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In pre-receive the new commits and objects are not placed directly into the object tree (depending on your git version of course). They are placed in to Quarantine and/or various other places. You need to set the environment for any git commands to tell it to look in those directories for objects too. Lines 67 to 82 in 0a004a6
I wouldn't use OpenRepository either just use the git repo commands directly. If you want to use gogit commands I'm fairly certain you can set its environs similarly. |
||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||
log.Error("Unable to get commit via id %v : %v", newCommitID, err) | ||||||||||||||||||||||||||||||||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||||||||||||||||||||||||||||||||||
"err": fmt.Sprintf("Unable to get commit via id %v", newCommitID), | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
parts := bytes.Split(stdout, []byte{'\n'}) | ||||||||||||||||||||||||||||||||||
for _, commitID := range parts { | ||||||||||||||||||||||||||||||||||
commit, err := gitRepo.GetCommit(string(commitID)) | ||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||
log.Error("Unable to get commit via id %v : %v", newCommitID, err) | ||||||||||||||||||||||||||||||||||
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ | ||||||||||||||||||||||||||||||||||
"err": fmt.Sprintf("Unable to get commit via id %v", newCommitID), | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
v := models.ParseCommitWithSignature(commit) | ||||||||||||||||||||||||||||||||||
if !v.Verified { | ||||||||||||||||||||||||||||||||||
log.Warn("protected branch %s require signed commits, but %v isn't signed", branchName, commit.ID) | ||||||||||||||||||||||||||||||||||
ctx.JSON(http.StatusForbidden, map[string]interface{}{ | ||||||||||||||||||||||||||||||||||
"err": fmt.Sprintf("protected branch %s require signed commits, but %v isn't signed", branchName, commit.ID), | ||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||
return | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
ctx.PlainText(http.StatusOK, []byte("ok")) | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -136,6 +136,14 @@ | |||||
</div> | ||||||
</div> | ||||||
|
||||||
<div class="field"> | ||||||
<div class="ui checkbox"> | ||||||
<input class="require-signedcommits" name="require_signed_commits" type="checkbox" {{if .Branch.RequireSignedCommits}}checked{{end}}> | ||||||
<label>{{.i18n.Tr "repo.settings.requrie_signed_commits"}}</label> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
<p class="help">{{.i18n.Tr "repo.settings.requrie_signed_commits_desc"}}</p> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
</div> | ||||||
</div> | ||||||
|
||||||
<div class="field"> | ||||||
<label for="required-approvals">{{.i18n.Tr "repo.settings.protect_required_approvals"}}</label> | ||||||
<input name="required_approvals" id="required-approvals" type="number" value="{{.Branch.RequiredApprovals}}"> | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.