-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Notify about recent pushes if a branch has no PR yet #14003
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
Closed
Closed
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
b707f36
Add function to get recently pushed branches
kolaente c5dbe71
Fix query
kolaente bedef4e
Add template to show PR message
kolaente 40b2bad
Merge branch 'master' into feature/pr-prompt
kolaente b435472
Show message to create a PR
kolaente b3686af
Filter out pushes to the default branch of repositories
kolaente 83d3a5f
Make sure the group by statement works with other DBMSes too
kolaente 6d9e242
Add the actual message
kolaente 623c7ce
Merge branch 'master' into feature/pr-prompt
kolaente a785aff
Merge branch 'master' into feature/pr-prompt
kolaente cb6b6c2
Merge branch 'master' into feature/pr-prompt
kolaente 7f5e5d2
Merge branch 'main' into feature/pr-prompt
kolaente 3f6b989
chore: move code to get the recently pushed branches
kolaente 1e13689
feat: limit to max 3 recently pushed
kolaente a92a6d2
fix: u.id after rename
kolaente b80c8ac
chore: use CompareLink template function to build PR link
kolaente 50b52b4
feat: check if the push was to a fork and change the link accordingly
kolaente 05091d4
feat: show time when the push happened
kolaente a704503
Merge branch 'main' into feature/pr-prompt
kolaente 63df5c6
fix: lint
kolaente d193b0b
fix: group by statement
kolaente 1f32d43
feat: always align the create PR button to the right
kolaente 924384e
fix: don't use alias for clean_ref_name
kolaente 63c1767
fix: sort by
kolaente ab60ee0
chore: add some comments
kolaente ef9985d
fix: use builder expression instead of builder.Gt
kolaente 263c9f6
fix: escape user table
kolaente f4f54a9
chore: use a more positive attitude towards messages
kolaente 429dda0
chore: use helper classes instead of custom css
kolaente e41a255
fix: rename RefName to BranchName because that's what it is
kolaente 2708a90
feat: only show commits in the last two hours
kolaente 0608dee
feat: show the pull request message in repo code, issue and PR pages
kolaente 3d5a5f5
chore: use flexbox instead of foomatic grid
kolaente 9787916
fix: left align text
kolaente b013b53
fix: change test to only check for partial equality
kolaente 39e6f43
Trigger build
kolaente File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2023 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package models | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"time" | ||
|
||
"code.gitea.io/gitea/models/activities" | ||
"code.gitea.io/gitea/models/db" | ||
"code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/models/user" | ||
|
||
"xorm.io/builder" | ||
) | ||
|
||
type RecentlyPushedBranches struct { | ||
Repo *repo.Repository | ||
BaseRepo *repo.Repository | ||
BranchName string | ||
Time time.Time | ||
} | ||
|
||
// GetRecentlyPushedBranches returns all actions where a user recently pushed but no PRs are created yet. | ||
func GetRecentlyPushedBranches(ctx context.Context, u *user.User) (recentlyPushedBranches []*RecentlyPushedBranches, err error) { | ||
limit := time.Now().Add(-2 * time.Hour).Unix() | ||
|
||
actions := []*activities.Action{} | ||
// We're fetching the last three commits activity actions within the limit... | ||
err = db.GetEngine(ctx). | ||
Select("action.ref_name, action.repo_id, action.created_unix"). | ||
Join("LEFT", "pull_request", "pull_request.head_branch = replace(action.ref_name, 'refs/heads/', '')"). | ||
Join("LEFT", "issue", "pull_request.issue_id = issue.id"). | ||
Join("LEFT", "repository", "action.repo_id = repository.id"). | ||
Where(builder.And( | ||
builder.Eq{"action.op_type": activities.ActionCommitRepo}, | ||
// ...done by the current user | ||
builder.Eq{"action.act_user_id": u.ID}, | ||
// ...which have been pushed to a fork or a branch different from the default branch | ||
builder.Or( | ||
builder.Expr("repository.default_branch != replace(action.ref_name, 'refs/heads/', '')"), | ||
builder.Eq{"repository.is_fork": true}, | ||
), | ||
// ...and don't have an open or closed PR corresponding to that branch. | ||
builder.Or( | ||
builder.IsNull{"pull_request.id"}, | ||
builder.And( | ||
builder.Eq{"pull_request.has_merged": false}, | ||
builder.Eq{"issue.is_closed": true}, | ||
builder.Expr("action.created_unix > issue.closed_unix"), | ||
), | ||
), | ||
builder.Gte{"action.created_unix": limit}, | ||
)). | ||
Limit(3). | ||
GroupBy("action.ref_name, action.repo_id, action.created_unix"). | ||
Desc("action.created_unix"). | ||
Find(&actions) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
repoIDs := []int64{} | ||
for _, a := range actions { | ||
repoIDs = append(repoIDs, a.RepoID) | ||
} | ||
|
||
// Because we need the repo name and url, we need to fetch all repos from recent pushes | ||
// and, if they are forked, the parent repo as well. | ||
repos := make(map[int64]*repo.Repository, len(repoIDs)) | ||
err = db.GetEngine(ctx). | ||
Where(builder.Or( | ||
builder.In("repository.id", repoIDs), | ||
builder.In("repository.id", | ||
builder.Select("repository.fork_id"). | ||
From("repository"). | ||
Where(builder.In("repository.id", repoIDs)), | ||
), | ||
)). | ||
Find(&repos) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
owners := make(map[int64]*user.User) | ||
err = db.GetEngine(ctx). | ||
Where(builder.Or( | ||
builder.In("repository.id", repoIDs), | ||
builder.In("repository.id", | ||
builder.Select("repository.fork_id"). | ||
From("repository"). | ||
Where(builder.In("repository.id", repoIDs)), | ||
), | ||
)). | ||
Join("LEFT", "repository", "`repository`.owner_id = `user`.id"). | ||
Find(&owners) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
recentlyPushedBranches = []*RecentlyPushedBranches{} | ||
for _, a := range actions { | ||
pushed := &RecentlyPushedBranches{ | ||
Repo: repos[a.RepoID], | ||
BaseRepo: repos[a.RepoID], | ||
BranchName: strings.Replace(a.RefName, "refs/heads/", "", 1), | ||
Time: a.GetCreate(), | ||
} | ||
|
||
if pushed.Repo.IsFork { | ||
pushed.BaseRepo = repos[pushed.Repo.ForkID] | ||
pushed.BaseRepo.Owner = owners[pushed.BaseRepo.OwnerID] | ||
} | ||
|
||
pushed.Repo.Owner = owners[pushed.Repo.OwnerID] | ||
|
||
recentlyPushedBranches = append(recentlyPushedBranches, pushed) | ||
} | ||
|
||
return recentlyPushedBranches, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{{range .RecentlyPushedBranches}} | ||
<div class="ui positive message gt-p-4 gt-df gt-ac gt-tl"> | ||
<div class="gt-f1"> | ||
{{$timeSince := TimeSince .Time $.locale}} | ||
{{$.locale.Tr "repo.pulls.recently_pushed_to_branches" .Repo.Owner.Name .Repo.LowerName .BranchName $timeSince | Safe}} | ||
</div> | ||
<a aria-role="button" class="ui compact positive button gt-m-0" href="{{CompareLink .BaseRepo .Repo .BranchName}}"> | ||
{{$.locale.Tr "repo.pulls.compare_changes"}} | ||
</a> | ||
</div> | ||
{{end}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.