-
-
Save smoya/4617304ec510c6f13e2bb9978b0ec824 to your computer and use it in GitHub Desktop.
List of repositories that contain workflow files not included in `.github` org repository at Wed 3 Nov 2021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/google/go-github/v39/github" | |
"golang.org/x/oauth2" | |
"net/http" | |
"os" | |
) | |
func main() { | |
token := os.Getenv("GH_TOKEN") | |
if token == "" { | |
panic("GH_TOKEN env var is required") | |
} | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: token}, | |
) | |
ctx := context.Background() | |
tc := oauth2.NewClient(ctx, ts) | |
client := github.NewClient(tc) | |
repositories, err := getRepositories(ctx, client) | |
if err != nil { | |
panic(err) | |
} | |
if len(repositories) == 0 { | |
panic("Repositories not found") | |
} | |
mainWorkflowsSlice, err := getWorkflows(ctx,client, ".github") | |
if err != nil { | |
panic(err) | |
} | |
mainWorkflows := make(map[string]*github.RepositoryContent, len(mainWorkflowsSlice)) | |
for _, mw := range mainWorkflowsSlice { | |
mainWorkflows[mw.GetPath()] = mw | |
} | |
fmt.Println("Repository,File name,URL") | |
for _, repo := range repositories { | |
r := repo | |
if r.GetName() == ".github" { | |
continue | |
} | |
workflows, err := getWorkflows(ctx, client, r.GetName()) | |
if err != nil { | |
panic(err) | |
} | |
if len(workflows) == 0 { | |
// no workflows found | |
continue | |
} | |
for _, w := range workflows { | |
if _, existsInMain := mainWorkflows[w.GetPath()]; existsInMain { | |
continue | |
} | |
fmt.Printf("%s,%s,%s\n", r.GetName(), w.GetName(), w.GetHTMLURL()) | |
} | |
} | |
} | |
func getWorkflows(ctx context.Context, client *github.Client, repoName string) ([]*github.RepositoryContent, error) { | |
_, workflows, resp, err := client.Repositories.GetContents(ctx, "asyncapi", repoName, ".github/workflows", nil) | |
if resp.StatusCode == http.StatusNotFound { | |
// no content | |
return nil, nil | |
} | |
return workflows, err | |
} | |
func getRepositories (ctx context.Context, client *github.Client) ([]*github.Repository, error) { | |
var repos []*github.Repository | |
opt := &github.RepositoryListByOrgOptions{ | |
ListOptions: github.ListOptions{PerPage: 50}, | |
} | |
for { | |
r, resp, err := client.Repositories.ListByOrg(ctx, "asyncapi", opt) | |
if err != nil { | |
return nil, err | |
} | |
repos = append(repos, r...) | |
if resp.NextPage == 0 { | |
break | |
} | |
opt.Page = resp.NextPage | |
} | |
return repos, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment