Skip to content
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

Filter actions by name if desired #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"text/tabwriter"
"time"

units "github.com/docker/go-units"
"github.com/docker/go-units"

"github.com/google/go-github/v50/github"
"golang.org/x/oauth2"
Expand All @@ -28,9 +28,9 @@ type RepoSummary struct {

func main() {
var (
orgName, userName, token, tokenFile, repoList, reposFile, apiUrl string
days int
punchCard, byRepo bool
orgName, userName, token, tokenFile, repoList, reposFile, actionList, apiUrl string
days int
punchCard, byRepo bool
)

flag.StringVar(&orgName, "org", "", "Organization name")
Expand All @@ -40,6 +40,7 @@ func main() {
flag.StringVar(&tokenFile, "token-file", "", "Path to the file containing the GitHub token")
flag.StringVar(&repoList, "include", "", "List of repos you want stats for eg. 'org/repo1,org/repo2'")
flag.StringVar(&reposFile, "include-file", "", "Path to file containing the Github repos list or '-' for stdin")
flag.StringVar(&actionList, "include-actions", "", "List of actions you want stats for eg. 'Build")

flag.BoolVar(&byRepo, "by-repo", false, "Show breakdown by repository")

Expand Down Expand Up @@ -213,6 +214,7 @@ func main() {
page = res.NextPage
}

workflowRuns = actionNameFilter(actionList, workflowRuns)
totalRuns += len(workflowRuns)

var owner string
Expand Down Expand Up @@ -516,3 +518,24 @@ func filterRepositories(repos []*github.Repository, repoList, reposFile string)

return repos, nil
}

func actionNameFilter(actionList string, runs []*github.WorkflowRun) []*github.WorkflowRun {
if len(actionList) == 0 {
return runs
}

actionsFromList := parseInclude(actionList)
byName := make(map[string]bool, len(actionsFromList))
for _, name := range actionsFromList {
byName[name] = true
}

var filtered []*github.WorkflowRun
for _, run := range runs {
if run.Name != nil && byName[*run.Name] {
filtered = append(filtered, run)
}
}

return filtered
}