-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Link to executed workflow #31906
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
base: main
Are you sure you want to change the base?
Link to executed workflow #31906
Changes from all commits
07a81a1
360b64a
c91396d
5dbaf6e
d096c97
a53ff72
907176f
fb57043
96fdaa3
0807b6f
b1eadac
c18e6b9
9ded4a7
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 |
---|---|---|
|
@@ -5,9 +5,11 @@ package actions | |
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
actions_model "code.gitea.io/gitea/models/actions" | ||
"code.gitea.io/gitea/modules/git" | ||
"code.gitea.io/gitea/modules/log" | ||
api "code.gitea.io/gitea/modules/structs" | ||
|
@@ -69,6 +71,42 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) { | |
return ret, nil | ||
} | ||
|
||
// GetRunsWorkflowFileLink return url for the source of the workflow file for an ActionRun | ||
func GetRunsWorkflowFileLink(run *actions_model.ActionRun, gitRepo *git.Repository) string { | ||
if run.Repo == nil || run.CommitSHA == "" { | ||
return "" | ||
} | ||
|
||
commit, err := gitRepo.GetCommit(run.CommitSHA) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
entries, err := ListWorkflows(commit) | ||
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. OK, I think I could understand the problem now. The proper approach should be like this: add a "WorkflowFilePath" column into the "Actions" model and just use it. It shouldn't call |
||
if err != nil { | ||
return "" | ||
} | ||
|
||
var workflowEntry *git.TreeEntry | ||
for _, entry := range entries { | ||
if entry.Name() == run.WorkflowID { | ||
workflowEntry = entry | ||
break | ||
} | ||
} | ||
|
||
if workflowEntry == nil { | ||
return "" | ||
} | ||
|
||
workflowFilePath := workflowEntry.GetPathInRepo() | ||
if workflowFilePath == "" { | ||
return "" | ||
} | ||
|
||
return fmt.Sprintf("%s/src/commit/%s/%s", run.Repo.Link(), run.CommitSHA, workflowFilePath) | ||
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. It needs proper escaping, otherwise the link might be invalid 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. Agreed, we can't influence the workflow file |
||
} | ||
|
||
func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) { | ||
f, err := entry.Blob().DataAsync() | ||
if err != nil { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import ( | |
"io" | ||
"sort" | ||
"strings" | ||
|
||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
// Type returns the type of the entry (commit, tree, blob) | ||
|
@@ -179,3 +181,30 @@ func (tes Entries) Sort() { | |
func (tes Entries) CustomSort(cmp func(s1, s2 string) bool) { | ||
sort.Sort(customSortableEntries{cmp, tes}) | ||
} | ||
|
||
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. Adding a note for other reviewers: this file doesn't differentiate between go-git and non-go-git impl, so it won't need multiple implementations based on which backend is used. 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. Total side-tangent, but does anyone use go-git? 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. go-git version has been used in almost all Windows users. |
||
// GetPathInRepo returns the relative path in the tree to this entry | ||
func (te *TreeEntry) GetPathInRepo() string { | ||
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. This file was organized by this:
But now it becomes:
The newly added code should be in the existing "TreeEntry code" section. |
||
if te == nil { | ||
return "" | ||
} | ||
|
||
path := te.Name() | ||
current := te.ptree | ||
|
||
for current != nil && current.ptree != nil { | ||
entries, err := current.ptree.ListEntries() | ||
if err != nil { | ||
log.Error("Failed to climb git tree %v", err) | ||
return "" | ||
} | ||
for _, entry := range entries { | ||
if entry.ID.String() == current.ID.String() { | ||
path = entry.Name() + "/" + path | ||
break | ||
} | ||
} | ||
current = current.ptree | ||
} | ||
Comment on lines
+194
to
+207
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. This looks like a really inefficient way to gather the full path of a file. 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. Agreed. I think a TreeEntry should have its absolute path. But, felt out of scope for this requested feature. 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. I also agree with delvh, we can't do so. #31906 (comment) |
||
|
||
return path | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
//go:build gogit | ||
|
||
package git | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-git/go-git/v5/plumbing/filemode" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func getTestEntries() Entries { | ||
return Entries{ | ||
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v1.0", Mode: filemode.Dir}}, | ||
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.0", Mode: filemode.Dir}}, | ||
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.1", Mode: filemode.Dir}}, | ||
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.12", Mode: filemode.Dir}}, | ||
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v2.2", Mode: filemode.Dir}}, | ||
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "v12.0", Mode: filemode.Dir}}, | ||
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "abc", Mode: filemode.Regular}}, | ||
&TreeEntry{gogitTreeEntry: &object.TreeEntry{Name: "bcd", Mode: filemode.Regular}}, | ||
} | ||
} | ||
|
||
func TestEntriesSort(t *testing.T) { | ||
entries := getTestEntries() | ||
entries.Sort() | ||
assert.Equal(t, "v1.0", entries[0].Name()) | ||
assert.Equal(t, "v12.0", entries[1].Name()) | ||
assert.Equal(t, "v2.0", entries[2].Name()) | ||
assert.Equal(t, "v2.1", entries[3].Name()) | ||
assert.Equal(t, "v2.12", entries[4].Name()) | ||
assert.Equal(t, "v2.2", entries[5].Name()) | ||
assert.Equal(t, "abc", entries[6].Name()) | ||
assert.Equal(t, "bcd", entries[7].Name()) | ||
} | ||
|
||
func TestEntriesCustomSort(t *testing.T) { | ||
entries := getTestEntries() | ||
entries.CustomSort(func(s1, s2 string) bool { | ||
return s1 > s2 | ||
}) | ||
assert.Equal(t, "v2.2", entries[0].Name()) | ||
assert.Equal(t, "v2.12", entries[1].Name()) | ||
assert.Equal(t, "v2.1", entries[2].Name()) | ||
assert.Equal(t, "v2.0", entries[3].Name()) | ||
assert.Equal(t, "v12.0", entries[4].Name()) | ||
assert.Equal(t, "v1.0", entries[5].Name()) | ||
assert.Equal(t, "bcd", entries[6].Name()) | ||
assert.Equal(t, "abc", entries[7].Name()) | ||
} |
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.
modules
is the wrong place for this.Go does not allow import cycles.
Please do not add any imports to
models
frommodules
.Uh oh!
There was an error while loading. Please reload this page.
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.
@delvh can you expand on what you are asking for?
It sounds like you are asking to move this function and the ListWorkflows function to models? In my humble opinion, I don't think this makes sense because there is no existing concept of a workflow in the database which to me is the equivalent of the
models
. Happy to hear a different opinion.I could refactor a lot of the workflow logic, including ListWorkflows, to live in a new file called models/actions/workflows. But it feels odd since there is no database table for a workflow and would violate this principle:
Outlined here: https://docs.gitea.com/contributing/guidelines-backend
This
module
is already dependent on theactions model
here: https://github.com/go-gitea/gitea/blob/main/modules/actions/task_state.go#L7There 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.
🤮 Okay, that is one of the places we didn't fix yet.
We are still working on removing all the historically grown errors.
For the time being, I think it is better not to introduce any already known technical debt.
As to where to add it, I would have added it as a type method on
actions_model.ActionRun
which is defined inmodels/actions/run.go