Skip to content

Instantly share code, notes, and snippets.

@pashtet04
Last active March 14, 2023 14:20
Show Gist options
  • Select an option

  • Save pashtet04/c399bc15520dedf62a177729e580338d to your computer and use it in GitHub Desktop.

Select an option

Save pashtet04/c399bc15520dedf62a177729e580338d to your computer and use it in GitHub Desktop.
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/google/go-github/v39/github"
"golang.org/x/exp/slices"
"golang.org/x/oauth2"
)
type Repos struct {
Values []string `json:"values"`
}
func main() {
e := os.Remove("admin_permissions.log")
if e != nil {
log.Fatal(e)
}
n := os.Remove("none_permissions.log")
if e != nil {
log.Fatal(n)
}
d := os.Remove("notadmin_permissions.log")
if e != nil {
log.Fatal(d)
}
// repos.json file example
// {
// "values": [
// "dp--deploy-airflow",
// "test"
// ]
//}
jsonFile, err := os.Open("repos.json")
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened repos.json")
byteValue, _ := ioutil.ReadAll(jsonFile)
var repos Repos
json.Unmarshal([]byte(byteValue), &repos)
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "<AUTH_TOKEN>"},
)
oauth2Client := oauth2.NewClient(context.Background(), tokenSource)
// Create a new GitHub client with the OAuth2 client
githubClient := github.NewClient(oauth2Client)
// Get the authenticated user
user, _, err := githubClient.Users.Get(context.Background(), "")
if err != nil {
fmt.Println("Error getting authenticated user:", err)
os.Exit(1)
}
for _, repoName := range repos.Values {
// Check whether the user is an admin of the repository in the organization
_, _, err := githubClient.Repositories.Get(context.Background(), "adeo", repoName)
if err != nil {
fmt.Println("Error getting repository:", err)
}
permissionLevel, _, err := githubClient.Repositories.GetPermissionLevel(context.Background(), "adeo", repoName, *user.Login)
if err != nil {
fmt.Println("Error getting permission level for user:", err)
f, err := os.OpenFile("none_permissions.log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
fmt.Fprintln(f, repoName)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
}
if err != nil {
if _, ok := err.(*github.RateLimitError); ok {
fmt.Println("API rate limit exceeded")
os.Exit(1)
} else if _, ok := err.(*github.AcceptedError); ok {
fmt.Println("Request is still being processed")
os.Exit(1)
} else if _, ok := err.(*github.TwoFactorAuthError); ok {
fmt.Println("Two-factor authentication is required")
os.Exit(1)
} else if _, ok := err.(*github.ErrorResponse); ok {
// If the authenticated user does not have access to the repository, skip it
continue
} else {
fmt.Println("Error getting permission level for user:", err)
os.Exit(1)
}
}
if *permissionLevel.Permission == "admin" {
fmt.Printf("%s is an admin of %s/%s\n", *user.Login, "adeo", repoName)
f, err := os.OpenFile("admin_permissions.log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
fmt.Fprintln(f, repoName)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
if strings.Contains(repoName, "dp--batch-proc") || strings.Contains(repoName, "airflow") {
// Get the existing repository topics
topics, _, err := githubClient.Repositories.ListAllTopics(context.Background(), "adeo", repoName)
if err != nil {
fmt.Println("Error getting repository topics:", err)
os.Exit(1)
}
fmt.Println("Existing topics:", topics, repoName)
// Append a new topic to the existing topics
newTopic := "p0036"
if slices.Contains(topics, newTopic) {
fmt.Println("Topics already present", topics, repoName)
} else {
topics = append(topics, newTopic)
// Replace all repository topics with the updated set of topics
_, _, err = githubClient.Repositories.ReplaceAllTopics(context.Background(), "adeo", repoName, topics)
if err != nil {
fmt.Println("Error replacing repository topics:", err)
os.Exit(1)
}
fmt.Println("Updated topics:", topics, repoName)
}
} else if strings.Contains(repoName, "dpda") || strings.Contains(repoName, "accelerator") {
// Get the existing repository topics
topics, _, err := githubClient.Repositories.ListAllTopics(context.Background(), "adeo", repoName)
if err != nil {
fmt.Println("Error getting repository topics:", err)
os.Exit(1)
}
fmt.Println("Existing topics:", topics, repoName)
// Append a new topic to the existing topics
newTopic := "p0319"
if slices.Contains(topics, newTopic) {
fmt.Println("Topics already present", topics, repoName)
} else {
topics = append(topics, newTopic)
// Replace all repository topics with the updated set of topics
_, _, err = githubClient.Repositories.ReplaceAllTopics(context.Background(), "adeo", repoName, topics)
if err != nil {
fmt.Println("Error replacing repository topics:", err)
os.Exit(1)
}
fmt.Println("Updated topics:", topics, repoName)
}
} else if strings.Contains(repoName, "dm--") {
// Get the existing repository topics
topics, _, err := githubClient.Repositories.ListAllTopics(context.Background(), "adeo", repoName)
if err != nil {
fmt.Println("Error getting repository topics:", err)
os.Exit(1)
}
fmt.Println("Existing topics:", topics, repoName)
// Append a new topic to the existing topics
newTopic := "p0320"
if slices.Contains(topics, newTopic) {
fmt.Println("Topics already present", topics, repoName)
} else {
topics = append(topics, newTopic)
// Replace all repository topics with the updated set of topics
_, _, err = githubClient.Repositories.ReplaceAllTopics(context.Background(), "adeo", repoName, topics)
if err != nil {
fmt.Println("Error replacing repository topics:", err)
os.Exit(1)
}
fmt.Println("Updated topics:", topics, repoName)
}
}
} else {
fmt.Printf("%s is not an admin of %s/%s\n", *user.Login, "adeo", repoName)
f, err := os.OpenFile("notadmin_permissions.log",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
fmt.Fprintln(f, repoName)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
}
defer jsonFile.Close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment