Skip to content

Commit ef5d412

Browse files
authored
✨ Add GitHub Action template with action.yml (#156)
* ✨ Add GitHub Action template with action.yml - Introduced a new GitHub Action template to streamline CI/CD workflows. - Created an `action.yml` file to define the action metadata and inputs. - This addition will help automate processes and improve project maintainability. 🔧 This enhancement sets the foundation for future automation tasks. * added params for github actions * updated readme and spelling * updated input args * updated input args * updated input args * updated input args * try pre-built image * try pre-built image * try pre-built image * simplified ruleset param * added github output file support * added github output file support * 🐛 lint issues * tagged docker image * tagged docker image * added branding and author details
1 parent a48aa20 commit ef5d412

File tree

8 files changed

+92
-33
lines changed

8 files changed

+92
-33
lines changed

.github/workflows/docker-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ jobs:
4848
run: |
4949
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
5050
VERSION=$(cat sourcecode-parser/VERSION)
51-
echo "tag=${VERSION}" >> $GITHUB_OUTPUT
51+
echo "tag=v${VERSION}, stable-latest" >> $GITHUB_OUTPUT
5252
elif [ "${{ github.event_name }}" == "push" ] && [ "${{ github.ref }}" == "refs/heads/main" ]; then
5353
VERSION=$(cat sourcecode-parser/VERSION)
54-
echo "tag=dev-${VERSION}" >> $GITHUB_OUTPUT
54+
echo "tag=dev-${VERSION}, nightly-latest" >> $GITHUB_OUTPUT
5555
else
5656
echo "tag=dev" >> $GITHUB_OUTPUT
5757
fi

Dockerfile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ FROM cgr.dev/chainguard/wolfi-base:latest
2222

2323
WORKDIR /app
2424

25-
COPY --from=builder /app/pathfinder /usr/local/bin/pathfinder
25+
COPY --from=builder /app/pathfinder /usr/bin/pathfinder
2626

27-
RUN chmod +x /usr/local/bin/pathfinder
27+
COPY entrypoint.sh /usr/bin/entrypoint.sh
2828

29-
CMD ["pathfinder", "version"]
29+
RUN chmod +x /usr/bin/pathfinder
3030

31-
LABEL maintainer="shiva@shivasurya.me"
31+
RUN chmod +x /usr/bin/entrypoint.sh
32+
33+
LABEL maintainer="shiva@shivasurya.me"
34+
35+
ENTRYPOINT ["/usr/bin/entrypoint.sh"]

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ Code Pathfinder, the open-source alternative to GitHub CodeQL. Built for advance
1313
[![codecov](https://codecov.io/gh/shivasurya/code-pathfinder/graph/badge.svg?token=VYQLI49TF4)](https://codecov.io/gh/shivasurya/code-pathfinder)
1414
</div>
1515

16-
## Documentation
16+
## :tv: Demo
17+
18+
```bash
19+
docker run --rm -v "./src:/src" shivasurya/code-pathfinder:stable-latest pathfinder ci --project /src/code-pathfinder/test-src --ruleset cpf/java
20+
```
21+
22+
## :book: Documentation
1723

1824
- [Documentation](https://codepathfinder.dev/)
1925
- [Pathfinder Queries](https://github.com/shivasurya/code-pathfinder/tree/main/pathfinder-rules)
2026

2127

22-
## Installation
28+
## :floppy_disk: Installation
29+
30+
### :whale: Using Docker
31+
32+
```bash
33+
$ docker pull shivasurya/code-pathfinder:dev
34+
```
2335

2436
### From npm
2537

action.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# action.yml
2+
name: 'Code-Pathfinder OSS'
3+
description: 'Code-Pathfinder open-source alternative to CodeQL'
4+
author: 'Shivasurya shiva@shivasurya.me'
5+
branding:
6+
icon: "shield"
7+
color: "blue"
8+
inputs:
9+
command:
10+
description: 'Command to run example: ci, query'
11+
required: true
12+
default: 'ci'
13+
project:
14+
description: 'Source code to scan. eg: /path/to/project'
15+
required: true
16+
default: ${{ github.workspace }}
17+
ruleset:
18+
description: 'Predefined rules eg: cpf/java or rules directory'
19+
required: false
20+
output:
21+
description: 'Output format eg: json'
22+
required: false
23+
default: 'json'
24+
output-file:
25+
description: 'Output file name eg: output.json'
26+
required: false
27+
default: 'output.json'
28+
runs:
29+
using: 'docker'
30+
image: 'docker://shivasurya/code-pathfinder:stable-latest'
31+
args:
32+
- pathfinder
33+
- ${{ inputs.command }}
34+
- --project
35+
- ${{ inputs.project }}
36+
- --ruleset
37+
- ${{ inputs.ruleset }}
38+
- --output
39+
- ${{ inputs.output }}
40+
- --output-file
41+
- ${{ inputs.output-file }}

entrypoint.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env sh
2+
3+
if [ $# -eq 0 ]; then
4+
/usr/bin/pathfinder version
5+
else
6+
/usr/bin/pathfinder "$@"
7+
fi

sourcecode-parser/cmd/ci.go

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"path/filepath"
1010
"strings"
1111

12+
"github.com/shivasurya/code-pathfinder/sourcecode-parser/graph"
13+
1214
"github.com/spf13/cobra"
1315
)
1416

@@ -17,7 +19,6 @@ var ciCmd = &cobra.Command{
1719
Short: "Scan a project for vulnerabilities with ruleset in ci mode",
1820
Run: func(cmd *cobra.Command, _ []string) {
1921
rulesetConfig := cmd.Flag("ruleset").Value.String()
20-
rulesetDirectory := cmd.Flag("rules-directory").Value.String()
2122
projectInput := cmd.Flag("project").Value.String()
2223
output := cmd.Flag("output").Value.String()
2324
outputFile := cmd.Flag("output-file").Value.String()
@@ -31,8 +32,8 @@ var ciCmd = &cobra.Command{
3132
fmt.Println("Executing in CI mode")
3233
}
3334

34-
if rulesetConfig == "" && rulesetDirectory == "" {
35-
fmt.Println("Ruleset or rules directory not specified")
35+
if rulesetConfig == "" {
36+
fmt.Println("ruleset are not specified. Please specify a ruleset eg: cpf/java or directory path")
3637
os.Exit(1)
3738
}
3839

@@ -41,26 +42,12 @@ var ciCmd = &cobra.Command{
4142
os.Exit(1)
4243
}
4344

44-
if rulesetConfig != "" {
45-
if !strings.HasPrefix(rulesetConfig, "cpf/") {
46-
fmt.Println("Ruleset not specified")
47-
os.Exit(1)
48-
}
49-
ruleset, err = loadRules(rulesetConfig, true)
50-
if err != nil {
51-
if verboseFlag {
52-
fmt.Printf("%s - error loading rules or ruleset not found: \nStacktrace: \n%s \n", rulesetConfig, err)
53-
}
54-
os.Exit(1)
55-
}
56-
} else if rulesetDirectory != "" {
57-
ruleset, err = loadRules(rulesetDirectory, false)
58-
if err != nil {
59-
if verboseFlag {
60-
fmt.Printf("%s - error loading rules or ruleset not found: \nStacktrace: \n%s \n", rulesetDirectory, err)
61-
}
62-
os.Exit(1)
45+
ruleset, err = loadRules(rulesetConfig, strings.HasPrefix(rulesetConfig, "cpf/"))
46+
if err != nil {
47+
if verboseFlag {
48+
fmt.Printf("%s - error loading rules or ruleset not found: \nStacktrace: \n%s \n", rulesetConfig, err)
6349
}
50+
os.Exit(1)
6451
}
6552
codeGraph := initializeProject(projectInput)
6653
for _, rule := range ruleset {
@@ -85,6 +72,10 @@ var ciCmd = &cobra.Command{
8572
// TODO: Add sarif file support
8673
if output == "json" {
8774
if outputFile != "" {
75+
if graph.IsGitHubActions() {
76+
// append GITHUB_WORKSPACE to output file path
77+
outputFile = os.Getenv("GITHUB_WORKSPACE") + "/" + outputFile
78+
}
8879
file, err := os.Create(outputFile)
8980
if err != nil {
9081
fmt.Println("Error creating output file: ", err)
@@ -115,8 +106,7 @@ func init() {
115106
ciCmd.Flags().StringP("output", "o", "", "Supported output format: json")
116107
ciCmd.Flags().StringP("output-file", "f", "", "Output file path")
117108
ciCmd.Flags().StringP("project", "p", "", "Project to analyze")
118-
ciCmd.Flags().StringP("ruleset", "q", "", "Ruleset to use example: cfp/java")
119-
ciCmd.Flags().StringP("rules-directory", "r", "", "Rules directory to use")
109+
ciCmd.Flags().StringP("ruleset", "r", "", "Ruleset to use example: cfp/java or directory path")
120110
}
121111

122112
func loadRules(rulesDirectory string, isHosted bool) ([]string, error) {

sourcecode-parser/cmd/ci_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestCiCmd(t *testing.T) {
2020
{
2121
name: "Basic CI command",
2222
args: []string{"ci", "--help"},
23-
expectedOutput: "Scan a project for vulnerabilities with ruleset in ci mode\n\nUsage:\n pathfinder ci [flags]\n\nFlags:\n -h, --help help for ci\n -o, --output string Supported output format: json\n -f, --output-file string Output file path\n -p, --project string Project to analyze\n -r, --rules-directory string Rules directory to use\n -q, --ruleset string Ruleset to use example: cfp/java\n",
23+
expectedOutput: "Scan a project for vulnerabilities with ruleset in ci mode\n\nUsage:\n pathfinder ci [flags]\n\nFlags:\n -h, --help help for ci\n -o, --output string Supported output format: json\n -f, --output-file string Output file path\n -p, --project string Project to analyze\n -r, --ruleset string Ruleset to use example: cfp/java or directory path\n",
2424
},
2525
}
2626

sourcecode-parser/graph/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"log"
9+
"os"
910
)
1011

1112
var verboseFlag bool
@@ -63,3 +64,7 @@ func Fmt(format string, args ...interface{}) {
6364
fmt.Printf(format, args...)
6465
}
6566
}
67+
68+
func IsGitHubActions() bool {
69+
return os.Getenv("GITHUB_ACTIONS") == "true"
70+
}

0 commit comments

Comments
 (0)