-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add commit status on repo and user pull request lists #2519
Conversation
Integration test would be nice for this |
@lafriks It seems some difficult since our integration tests didn't run a drone instance? |
Codecov Report
@@ Coverage Diff @@
## master #2519 +/- ##
==========================================
+ Coverage 20.62% 26.68% +6.06%
==========================================
Files 166 90 -76
Lines 32261 17893 -14368
==========================================
- Hits 6654 4775 -1879
+ Misses 24628 12438 -12190
+ Partials 979 680 -299
Continue to review full report at Codecov.
|
@lunny you can just call status api to set status for commit you dont need real CI |
routers/user/home.go
Outdated
|
||
if issue.IsPull { | ||
issue.LoadAttributes() | ||
statuses, err := models.GetLatestCommitStatus(issue.Repo, issue.PullRequest.MergeBase, 0) |
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.
Can you extract this block to new function? I've found this (almost) same code block 3 times in existing codebase.
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.
I think they are different for ... range
.
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.
Maybe we should have a issue.GetLatestCommitStatus
that does what this if
-block does?
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.
so instead of
if issue.IsPull {
...
append(...)
}
it would be
issueStatuses = append(issueStatuses, models.CalcCommitStatus(issue.GetLatestCommitStatus()))
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.
It's same, the only difference is you are adding result of models.CalcCommitStatus(statuses)
to slice. Instead of calling 2 functions and 1 err check, you can call just 1 function (err check can be inside too, it changes nothing). Best thing would be to rename GetLatestCommitStatus
to GetCommitStatuses
(as it returns array, not just 1 status) and create/use GetLatestCommitStatus
to return just one status (for now, this few lines of code can be used, but I want to change it to pure SQL select).
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.
ok, GetCommitStatuses
is here also, I don't understand GetLatestCommitStatus
implementation... As I understand, it should return only 1 status in slice.
routers/user/home.go
Outdated
|
||
if issue.IsPull { | ||
issue.LoadAttributes() | ||
statuses, err := models.GetLatestCommitStatus(issue.Repo, issue.PullRequest.MergeBase, 0) |
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.
Maybe we should have a issue.GetLatestCommitStatus
that does what this if
-block does?
routers/user/home.go
Outdated
|
||
if issue.IsPull { | ||
issue.LoadAttributes() | ||
statuses, err := models.GetLatestCommitStatus(issue.Repo, issue.PullRequest.MergeBase, 0) |
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.
so instead of
if issue.IsPull {
...
append(...)
}
it would be
issueStatuses = append(issueStatuses, models.CalcCommitStatus(issue.GetLatestCommitStatus()))
templates/repo/issue/list.tmpl
Outdated
<div class="ui {{if $issue.IsRead}}black{{else}}green{{end}} label">#{{$issue.Index}}</div> | ||
<a class="title has-emoji" href="{{$.Link}}/{{$issue.Index}}">{{$issue.Title}}</a> | ||
{{if $issue.IsPull}} | ||
{{template "repo/commit_status" (index $.IssuesStates $index)}} |
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.
indentation
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.
done.
In fact I prefer to implement |
@lunny |
@bkcsoft |
routers/repo/issue.go
Outdated
// Check read status | ||
if !ctx.IsSigned { | ||
issues[i].IsRead = true | ||
} else if err = issues[i].GetIsRead(ctx.User.ID); err != nil { | ||
ctx.Handle(500, "GetIsRead", err) | ||
return | ||
} | ||
|
||
if issue.IsPull { | ||
issue.LoadAttributes() |
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.
Let's not ignore the error returned here
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.
done.
routers/user/home.go
Outdated
for _, issue := range issues { | ||
issue.Repo = showReposMap[issue.RepoID] | ||
|
||
if issue.IsPull { | ||
issue.LoadAttributes() |
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.
Let's not ignore the error returned here
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.
done.
@lunny That function name is waaaay to long 😂 I say that |
d9892f8
to
f285da9
Compare
routers/repo/issue.go
Outdated
@@ -207,17 +207,44 @@ func Issues(ctx *context.Context) { | |||
} | |||
} | |||
|
|||
var repoIDs = make([]int64, 0, len(issues)) | |||
var shas = make([]string, 0, len(issues)) | |||
var pullIDs = make([]int64, 0, len(issues)) |
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.
Can you use 1 slice of structs instead?
type pull struct{
repoID int64
sha string
pullID int64
}
var pulls = make([]pull , 0, len(issues))
...
pulls = append(pulls, pull{ctx.Repo.Repository.ID, issue.PullRequest.MergeBase, issue.ID})
routers/repo/issue.go
Outdated
// Check read status | ||
if !ctx.IsSigned { | ||
issues[i].IsRead = true | ||
} else if err = issues[i].GetIsRead(ctx.User.ID); err != nil { | ||
ctx.Handle(500, "GetIsRead", err) | ||
return | ||
} | ||
|
||
if issue.IsPull { | ||
if err := issue.LoadAttributes(); err != nil { |
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.
I think you don't need to run issue.LoadAttributes()
again, models.Issues()
has already did it.
models/status.go
Outdated
var results = make([]struct { | ||
ID int64 | ||
RepoID int64 | ||
}, 0, 10*len(repoIDs)) |
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.
Why 10*len(repoIDs)
?
var issuesStates = make(map[int64]*models.CommitStatus, len(issues)) | ||
for i, statuses := range commitStatuses { | ||
issuesStates[pullIDs[i]] = models.CalcCommitStatus(statuses) | ||
} |
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.
Is there way to reduce all of this lines above to just 1 function call? I am thinking about doing just 1 sql select to get what you need. There are too many for loops and also nested loop.
I think we can add something like "StateLevel" or "StateCode" to CommitStatus
table (there are only strings - CommitStatus.State
- right now). Then we can do simple select to get "worst" status. For "latests" status there is CommitStatus.Index
or max CommitStatus.ID
.
routers/user/home.go
Outdated
@@ -307,10 +307,37 @@ func Issues(ctx *context.Context) { | |||
return | |||
} | |||
|
|||
var repoIDs = make([]int64, 0, len(issues)) | |||
var shas = make([]string, 0, len(issues)) | |||
var pullIDs = make([]int64, 0, len(issues)) |
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.
Same comment as for "routers/repo/issue.go".
models/status.go
Outdated
err := x.Table(&CommitStatus{}). | ||
Where(cond). | ||
Select("max( id ) as id, repo_id"). | ||
GroupBy("repo_id, sha, context").OrderBy("max( id ) desc").Find(&results) |
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.
Is not this select everything you need to get latest commit for each repo commit (I was just writing about adding columns to "group by" when you updated it)? You can use this as subselect (just remove repo_id
from select) for mainy "where" clause.
75f273b
to
9b7d379
Compare
@lafriks integration tests added |
@Morlinest I think the best way is add a field |
routers/repo/issue.go
Outdated
} else { | ||
var repoIDs = make([]int64, 0, len(issues)) | ||
var shas = make([]string, 0, len(issues)) | ||
var pullIDs = make([]int64, 0, len(issues)) |
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.
You can use just 1 slice for repoIDs + shas and 1 for pullIDs (see comment for GetLatestCommitStatuses
function). Or you can use composition and create just 1 slice.
// can be used as arg for GetLatestCommitStatuses
type CommitOption struct {
RepoID int64
Sha string
}
type results struct {
CommitOption
pullID int64
}
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.
Yes, you are right. But I think store the last commit id in pullrequest table is the best way. I will send another PR to do that.
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.
If you do, call it RepoSHA
instead of CommitOption
since that better explains the purpose of the struct. Though in general I dislike FunctionOptions
structs since they hide the arguments from the docs.
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.
@bkcsoft But 2 slices is not solution to that as you can't be sure about the order nor dependency between values on same key. I don't think that you are hiding something with structs in go.
models/status.go
Outdated
@@ -170,6 +172,60 @@ func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitSta | |||
return statuses, x.In("id", ids).Find(&statuses) | |||
} | |||
|
|||
// GetLatestCommitStatuses returns all statuses with given repoIDs and shas | |||
func GetLatestCommitStatuses(repoIDs []int64, shas []string) ([][]*CommitStatus, error) { |
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.
Instead of 2 slices it should be 1 slice of structs with 2 values. You can't be sure, both slices are in same order and values on concrete index belongs each other.
models/status.go
Outdated
var repoIDsMap = make(map[string][]int64, len(repoIDs)) | ||
for _, res := range results { | ||
ids = append(ids, res.ID) | ||
repoIDsMap[fmt.Sprintf("%d-%s", res.RepoID, res.SHA)] = append(repoIDsMap[fmt.Sprintf("%d-%s", res.RepoID, res.SHA)], res.ID) |
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.
Save fmt.Sprintf("%d-%s", res.RepoID, res.SHA)
to variable and use it as key.
models/status.go
Outdated
var repoIDsMap = make(map[string][]int64, len(repoIDs)) | ||
for _, res := range results { | ||
ids = append(ids, res.ID) | ||
repoIDsMap[fmt.Sprintf("%d-%s", res.RepoID, res.SHA)] = append(repoIDsMap[fmt.Sprintf("%d-%s", res.RepoID, res.SHA)], res.ID) |
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.
This will overwrite value of repoIDsMap
if you have 2 different context for same commit (res.RepoID
and res.SHA
is same). What is the point here?
routers/user/home.go
Outdated
var repoIDs = make([]int64, 0, len(issues)) | ||
var shas = make([]string, 0, len(issues)) | ||
var pullIDs = make([]int64, 0, len(issues)) | ||
var repoCache = make(map[int64]*git.Repository) |
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.
Same comment as for "routers/repo/issue.go". Also looks like duplicated code (one if/else check less).
@lunny OK, I'll wait for new PR and updated version because I have more comments to this PR :) |
@Morlinest |
integrations/editor_test.go
Outdated
|
||
req = NewRequestWithValues(t, "POST", url, | ||
map[string]string{ | ||
"_csrf": htmlDoc.GetCSRF(), |
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.
nit: you can use GetCSRF(..)
, so you don't have to manually make GET request just for the csrf token.
{{end}} | ||
{{if eq .State "warning"}} | ||
<a href="{{.TargetURL}}" target=_blank><i class="commit-status warning sign icon yellow"></i></a> | ||
{{if .}} |
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.
This file should use tabs instead of spaces
routers/user/home.go
Outdated
|
||
var rep *git.Repository | ||
var ok bool | ||
if rep, ok = repoCache[issue.PullRequest.HeadRepoID]; !ok { |
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.
nit: could we call the variable gitRepo
, or something more descriptive? Without context, it's not obvious (at least not to me) that rep stands for repo.
templates/repo/issue/list.tmpl
Outdated
@@ -162,36 +162,38 @@ | |||
</div> | |||
|
|||
<div class="issue list"> | |||
{{range .Issues}} | |||
{{ $timeStr:= TimeSince .Created $.Lang }} | |||
{{range $index, $issue := .Issues}} |
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.
Is $index
ever used? Similarly for the other template.
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.
Seems no needed after my second push.
will resolve #996 |
9b7d379
to
03649d9
Compare
315fc0c
to
207fccd
Compare
@SijmenSchoon if you want to work on this, just take it and create another PR. I think this PR needs to fix tests. I'm now busy on other PRs. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs during the next 2 months. Thank you for your contributions. |
Since #6465 merged, I will close this one. |
And something confusing me is should I use
issue.PullRequest.MergeBase
as the last commit of a pull request? Anyway, see the screenshots below: