Last active
June 20, 2022 19:36
-
-
Save devtin/60ff76983449a428743ffd37c552cf5a to your computer and use it in GitHub Desktop.
checks repositories in an organization with circleci
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 ( | |
"encoding/json" | |
"fmt" | |
"os" | |
"os/exec" | |
"path" | |
"strings" | |
"sync" | |
) | |
type Repo struct { | |
Name string `json:"name"` | |
} | |
var cwd, _ = os.Getwd() | |
var organization = "rewardStyle" | |
var workingDir = path.Join(cwd, "./repos") | |
func execCmd(fullCommand string, workingDir string) ([]byte, error) { | |
splittedCmd := strings.Fields(fullCommand) | |
var args []string | |
app := splittedCmd[0] | |
if len(splittedCmd) > 1 { | |
args = splittedCmd[1:] | |
} | |
cmd := exec.Command(app, args...) | |
cmd.Dir = workingDir | |
return cmd.Output() | |
} | |
func exists(path string) (bool, error) { | |
_, err := os.Stat(path) | |
if err == nil { | |
return true, nil | |
} | |
if os.IsNotExist(err) { | |
return false, nil | |
} | |
return false, err | |
} | |
func getLisOfRepositories() ([]Repo, error) { | |
repoList, err := execCmd(fmt.Sprintf("gh repo list %s --no-archived --limit 1000 --json name", organization), workingDir) | |
if err != nil { | |
return nil, err | |
} | |
var arr []Repo | |
_ = json.Unmarshal([]byte(repoList), &arr) | |
// Print the output | |
return arr, nil | |
} | |
type HasCircleCi struct { | |
CircleCi bool | |
Branch string | |
Error string | |
} | |
func checkIfRepoHasCircleCi(repoName string) HasCircleCi { | |
defer execCmd(fmt.Sprintf("rm -rf %s", repoName), workingDir) | |
output, err := execCmd(fmt.Sprintf("git clone git@github.com:%s/%s %s", organization, repoName, path.Join(workingDir, repoName)), workingDir) | |
if err != nil { | |
return HasCircleCi{ | |
Error: string(output), | |
} | |
} | |
repoDir := path.Join(workingDir, repoName) | |
branchName := "main" | |
_, err = execCmd(fmt.Sprintf("git show-ref refs/heads/%s", branchName), repoDir) | |
if err != nil { | |
branchName = "master" | |
} | |
_, _ = execCmd("git clean -d -f .", repoDir) | |
_, _ = execCmd(fmt.Sprintf("git checkout %s", branchName), repoDir) | |
doExists, err := exists(path.Join(repoDir, ".circleci")) | |
if err != nil { | |
return HasCircleCi{ | |
Branch: branchName, | |
Error: err.Error(), | |
} | |
} | |
return HasCircleCi{ | |
CircleCi: doExists, | |
Branch: branchName, | |
} | |
} | |
func main() { | |
os.Mkdir(workingDir, 0755) | |
execCmd("rm -rf *", workingDir) | |
listOfRepos, err := getLisOfRepositories() | |
if err != nil { | |
fmt.Println("Error", err) | |
os.Exit(1) | |
} | |
var wg sync.WaitGroup | |
fmt.Println("repository,branch,found,error") | |
maxGoroutines := 10 | |
guard := make(chan struct{}, maxGoroutines) | |
for _, repo := range listOfRepos { | |
wg.Add(1) | |
guard <- struct{}{} | |
go func(repoName string) { | |
defer wg.Done() | |
hasCircleCi := checkIfRepoHasCircleCi(repoName) | |
fmt.Println(fmt.Sprintf("%s,%s,%t,%s", repoName, hasCircleCi.Branch, hasCircleCi.CircleCi, hasCircleCi.Error)) | |
<-guard | |
}(repo.Name) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment