Skip to content

Commit d2ff679

Browse files
authored
[#149]: feature: better topological sorting
2 parents f0b1f9b + 7a7485c commit d2ff679

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ name: "CodeQL"
77

88
on:
99
push:
10-
branches: [master]
10+
branches: [ master ]
1111
pull_request:
1212
# The branches below must be a subset of the branches above
13-
branches: [master]
13+
branches: [ master ]
1414
schedule:
15-
- cron: '0 16 * * 6'
15+
- cron: '0 15 * * 6'
1616

1717
jobs:
1818
analyze:
@@ -24,48 +24,50 @@ jobs:
2424
matrix:
2525
# Override automatic language detection by changing the below list
2626
# Supported options are ['csharp', 'cpp', 'go', 'java', 'javascript', 'python']
27-
language: ['go']
27+
language: [ 'go' ]
2828
# Learn more...
2929
# https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection
3030

3131
steps:
32-
- name: Checkout repository
33-
uses: actions/checkout@v4
34-
with:
35-
# We must fetch at least the immediate parents so that if this is
36-
# a pull request then we can checkout the head.
37-
fetch-depth: 2
32+
- name: Checkout repository
33+
uses: actions/checkout@v4
34+
with:
35+
# We must fetch at least the immediate parents so that if this is
36+
# a pull request then we can checkout the head.
37+
fetch-depth: 2
3838

39-
# If this run was triggered by a pull request event, then checkout
40-
# the head of the pull request instead of the merge commit.
41-
- run: git checkout HEAD^2
42-
if: ${{ github.event_name == 'pull_request' }}
39+
# Initializes the Golang environment for the CodeQL tools.
40+
# https://github.com/github/codeql-action/issues/1842#issuecomment-1704398087
41+
- name: Install Go
42+
uses: actions/setup-go@v4
43+
with:
44+
go-version-file: go.mod
4345

44-
# Initializes the CodeQL tools for scanning.
45-
- name: Initialize CodeQL
46-
uses: github/codeql-action/init@v2
47-
with:
48-
languages: ${{ matrix.language }}
49-
# If you wish to specify custom queries, you can do so here or in a config file.
50-
# By default, queries listed here will override any specified in a config file.
51-
# Prefix the list here with "+" to use these queries and those in the config file.
52-
# queries: ./path/to/local/query, your-org/your-repo/queries@main
46+
# Initializes the CodeQL tools for scanning.
47+
- name: Initialize CodeQL
48+
uses: github/codeql-action/init@v2
49+
with:
50+
languages: ${{ matrix.language }}
51+
# If you wish to specify custom queries, you can do so here or in a config file.
52+
# By default, queries listed here will override any specified in a config file.
53+
# Prefix the list here with "+" to use these queries and those in the config file.
54+
# queries: ./path/to/local/query, your-org/your-repo/queries@main
5355

54-
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
55-
# If this step fails, then you should remove it and run the build manually (see below)
56-
- name: Autobuild
57-
uses: github/codeql-action/autobuild@v2
56+
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
57+
# If this step fails, then you should remove it and run the build manually (see below)
58+
- name: Autobuild
59+
uses: github/codeql-action/autobuild@v2
5860

59-
# ℹ️ Command-line programs to run using the OS shell.
60-
# 📚 https://git.io/JvXDl
61+
# ℹ️ Command-line programs to run using the OS shell.
62+
# 📚 https://git.io/JvXDl
6163

62-
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
63-
# and modify them (or add more) to build your code if your project
64-
# uses a compiled language
64+
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
65+
# and modify them (or add more) to build your code if your project
66+
# uses a compiled language
6567

66-
#- run: |
67-
# make bootstrap
68-
# make release
68+
#- run: |
69+
# make bootstrap
70+
# make release
6971

70-
- name: Perform CodeQL Analysis
71-
uses: github/codeql-action/analyze@v2
72+
- name: Perform CodeQL Analysis
73+
uses: github/codeql-action/analyze@v2

edges.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (e *Endure) resolveCollectorEdges(plugin any) error {
3535
}
3636

3737
// resolveEdges adds edges between the vertices
38-
// At this point, we know all plugins and all provides values
38+
// At this point, we know all plugins, and all provides values
3939
func (e *Endure) resolveEdges() error {
4040
vertices := e.graph.Vertices()
4141

@@ -90,7 +90,7 @@ func (e *Endure) resolveEdges() error {
9090

9191
// we should have here exact the same number of the deps implementing every particular arg
9292
if count != len(args[1:]) {
93-
// if there are no plugins which implement Init deps, remove this vertex from the tree
93+
// if there are no plugins that implement Init deps, remove this vertex from the tree
9494
del := e.graph.Remove(vertices[i].Plugin())
9595
for k := 0; k < len(del); k++ {
9696
e.registar.Remove(del[k].Plugin())
@@ -115,9 +115,24 @@ func (e *Endure) resolveEdges() error {
115115
}
116116
}
117117

118-
ok := e.graph.TopologicalSort()
119-
if !ok {
120-
return errors.E("cyclic dependencies found, see the DEBUG log")
118+
e.graph.TopologicalSort()
119+
120+
// notify user about the disabled plugins
121+
// after topological sorting we remove all plugins with indegree > 0, because there are no edges to them
122+
if len(e.graph.TopologicalOrder()) != len(e.graph.Vertices()) {
123+
tpl := e.graph.TopologicalOrder()
124+
vrt := e.graph.Vertices()
125+
126+
tmpM := make(map[string]struct{}, 2)
127+
for _, v := range tpl {
128+
tmpM[v.ID().String()] = struct{}{}
129+
}
130+
131+
for _, v := range vrt {
132+
if _, ok := tmpM[v.ID().String()]; !ok {
133+
e.log.Warn("topological sort, plugin disabled", slog.String("plugin", v.ID().String()))
134+
}
135+
}
121136
}
122137

123138
return nil

graph/toposort.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package graph
22

3-
func (g *Graph) TopologicalSort() bool {
3+
func (g *Graph) TopologicalSort() {
44
heap := &VertexHeap{}
55

66
for _, v := range g.vertices {
@@ -33,6 +33,4 @@ func (g *Graph) TopologicalSort() bool {
3333
}
3434
}
3535
}
36-
37-
return len(g.topologicalOrder) >= len(g.vertices)
3836
}

0 commit comments

Comments
 (0)