diff --git a/gitea-group-sync.go b/gitea-group-sync.go index f8698db..3df18a1 100644 --- a/gitea-group-sync.go +++ b/gitea-group-sync.go @@ -20,7 +20,7 @@ func AddUsersToTeam(apiKeys GiteaKeys, users []Account, team int) bool { for i := 0; i < len(users); i++ { - fullusername := url.PathEscape(fmt.Sprintf("%s", users[i].Full_name)) + fullusername := url.PathEscape(fmt.Sprintf("%s", users[i].FullName)) apiKeys.Command = "/api/v1/users/search?q=" + fullusername + "&access_token=" foundUsers := RequestSearchResults(apiKeys) @@ -42,7 +42,7 @@ func DelUsersFromTeam(apiKeys GiteaKeys, Users []Account, team int) bool { for i := 0; i < len(Users); i++ { - apiKeys.Command = "/api/v1/users/search?uid=" + fmt.Sprintf("%d", Users[i].Id) + "&access_token=" + apiKeys.Command = "/api/v1/users/search?uid=" + fmt.Sprintf("%d", Users[i].ID) + "&access_token=" foundUser := RequestSearchResults(apiKeys) @@ -70,7 +70,7 @@ func main() { c.AddFunc(repTime, mainJob) c.Start() fmt.Println(c.Entries()) - for true { + for { time.Sleep(100 * time.Second) } } @@ -103,15 +103,14 @@ func importEnvVars() Config { if err != nil { log.Println("LDAP_TLS_PORT is invalid.") } - } else { - if len(os.Getenv("LDAP_PORT")) > 0 { - port, err := strconv.Atoi(os.Getenv("LDAP_PORT")) - envConfig.LdapPort = port - envConfig.LdapTLS = false - log.Printf("Dial:=%v:%d", envConfig.LdapURL, envConfig.LdapPort) - if err != nil { - log.Println("LDAP_PORT is invalid.") - } + } else if len(os.Getenv("LDAP_PORT")) > 0 { + + port, err := strconv.Atoi(os.Getenv("LDAP_PORT")) + envConfig.LdapPort = port + envConfig.LdapTLS = false + log.Printf("Dial:=%v:%d", envConfig.LdapURL, envConfig.LdapPort) + if err != nil { + log.Println("LDAP_PORT is invalid.") } } // Set defaults for user Attributes @@ -151,7 +150,7 @@ func importYAMLConfig(path string) (Config, error) { } func (c Config) checkConfig() { - if len(c.ApiKeys.TokenKey) <= 0 { + if len(c.ApiKeys.TokenKey) == 0 { log.Println("GITEA_TOKEN is empty or invalid.") } if len(c.ApiKeys.BaseUrl) == 0 { @@ -242,7 +241,7 @@ func mainJob() { log.Println(organizationList) - log.Printf("Begin an organization review: OrganizationName= %v, OrganizationId= %d \n", organizationList[i].Name, organizationList[i].Id) + log.Printf("Begin an organization review: OrganizationName= %v, OrganizationId= %d \n", organizationList[i].Name, organizationList[i].ID) cfg.ApiKeys.Command = "/api/v1/orgs/" + organizationList[i].Name + "/teams?access_token=" teamList := RequestTeamList(cfg.ApiKeys) @@ -274,14 +273,14 @@ func mainJob() { for _, entry := range sr.Entries { AccountsLdap[entry.GetAttributeValue(cfg.LdapUserIdentityAttribute)] = Account{ - Full_name: entry.GetAttributeValue(cfg.LdapUserFullName), - Login: entry.GetAttributeValue(cfg.LdapUserIdentityAttribute), + FullName: entry.GetAttributeValue(cfg.LdapUserFullName), + Login: entry.GetAttributeValue(cfg.LdapUserIdentityAttribute), } } - cfg.ApiKeys.Command = "/api/v1/teams/" + fmt.Sprintf("%d", teamList[j].Id) + "/members?access_token=" + cfg.ApiKeys.Command = "/api/v1/teams/" + fmt.Sprintf("%d", teamList[j].ID) + "/members?access_token=" AccountsGitea, cfg.ApiKeys.BruteforceTokenKey = RequestUsersList(cfg.ApiKeys) - log.Printf("The gitea %s has %d users corresponding to team %s Teamid=%d", cfg.ApiKeys.BaseUrl, len(AccountsGitea), teamList[j].Name, teamList[j].Id) + log.Printf("The gitea %s has %d users corresponding to team %s Teamid=%d", cfg.ApiKeys.BaseUrl, len(AccountsGitea), teamList[j].Name, teamList[j].ID) for k, v := range AccountsLdap { if AccountsGitea[k].Login != v.Login { @@ -289,7 +288,7 @@ func mainJob() { } } log.Printf("can be added users list %v", addUserToTeamList) - AddUsersToTeam(cfg.ApiKeys, addUserToTeamList, teamList[j].Id) + AddUsersToTeam(cfg.ApiKeys, addUserToTeamList, teamList[j].ID) for k, v := range AccountsGitea { if AccountsLdap[k].Login != v.Login { @@ -297,7 +296,7 @@ func mainJob() { } } log.Printf("must be del users list %v", delUserToTeamlist) - DelUsersFromTeam(cfg.ApiKeys, delUserToTeamlist, teamList[j].Id) + DelUsersFromTeam(cfg.ApiKeys, delUserToTeamlist, teamList[j].ID) } else { log.Printf("The LDAP %s not found users corresponding to team %s", cfg.LdapURL, teamList[j].Name) diff --git a/requests.go b/requests.go index 43e2139..d739cf9 100644 --- a/requests.go +++ b/requests.go @@ -7,6 +7,7 @@ import ( "log" "net/http" "os" + // "reflect" "net" "net/url" @@ -34,11 +35,11 @@ func hasTimedOut(err error) bool { if err, ok := err.Err.(net.Error); ok && err.Timeout() { return true } - case net.Error: + case *net.OpError: if err.Timeout() { return true } - case *net.OpError: + case net.Error: if err.Timeout() { return true } @@ -73,6 +74,9 @@ func RequestPut(apiKeys GiteaKeys) []byte { cc := &http.Client{Timeout: time.Second * 2} url := apiKeys.BaseUrl + apiKeys.Command + apiKeys.TokenKey[apiKeys.BruteforceTokenKey] request, err := http.NewRequest("PUT", url, nil) + if err != nil { + log.Fatal(err) + } res, err := cc.Do(request) CheckStatusCode(res) if err != nil && hasTimedOut(err) { @@ -92,6 +96,9 @@ func RequestDel(apiKeys GiteaKeys) []byte { cc := &http.Client{Timeout: time.Second * 2} url := apiKeys.BaseUrl + apiKeys.Command + apiKeys.TokenKey[apiKeys.BruteforceTokenKey] request, err := http.NewRequest("DELETE", url, nil) + if err != nil { + log.Fatal(err) + } res, err := cc.Do(request) CheckStatusCode(res) if err != nil && hasTimedOut(err) { @@ -121,7 +128,7 @@ func RequestSearchResults(ApiKeys GiteaKeys) SearchResults { func RequestUsersList(ApiKeys GiteaKeys) (map[string]Account, int) { b := RequestGet(ApiKeys) - var Account_u = make(map[string]Account) + var AccountU = make(map[string]Account) var f []Account jsonErr := json.Unmarshal(b, &f) @@ -135,19 +142,19 @@ func RequestUsersList(ApiKeys GiteaKeys) (map[string]Account, int) { if ApiKeys.BruteforceTokenKey < len(ApiKeys.TokenKey)-1 { ApiKeys.BruteforceTokenKey++ log.Printf("BruteforceTokenKey=%d", ApiKeys.BruteforceTokenKey) - Account_u, ApiKeys.BruteforceTokenKey = RequestUsersList(ApiKeys) + AccountU, ApiKeys.BruteforceTokenKey = RequestUsersList(ApiKeys) } } for i := 0; i < len(f); i++ { - Account_u[f[i].Login] = Account{ + AccountU[f[i].Login] = Account{ // Email: f[i].Email, - Id: f[i].Id, - Full_name: f[i].Full_name, - Login: f[i].Login, + ID: f[i].ID, + FullName: f[i].FullName, + Login: f[i].Login, } } - return Account_u, ApiKeys.BruteforceTokenKey + return AccountU, ApiKeys.BruteforceTokenKey } func RequestOrganizationList(apiKeys GiteaKeys) []Organization { @@ -184,13 +191,3 @@ func parseJson(b []byte) interface{} { m := f.(interface{}) return m } - -func parseJsonArray(b []byte) []interface{} { - var f interface{} - jsonErr := json.Unmarshal(b, &f) - if jsonErr != nil { - log.Fatal(jsonErr) - } - m := f.([]interface{}) - return m -} diff --git a/types.go b/types.go index 1e90feb..075c600 100644 --- a/types.go +++ b/types.go @@ -1,10 +1,10 @@ package main type Organization struct { - Id int `json:"id"` - Avatar_url string `json:"avatar_url"` + ID int `json:"id"` + AvatarURL string `json:"avatar_url"` Description string `json:"description"` - Full_name string `json:"full_name"` + FullName string `json:"full_name"` Location string `json:"location"` Name string `json:"username"` Visibility string `json:"visibility"` @@ -12,27 +12,27 @@ type Organization struct { } type Team struct { - Id int `json:"id"` + ID int `json:"id"` Name string `json:"name"` Description string `json:"description"` Permission string `json:"permission"` } type User struct { - Id int `json:"id"` - Avatar_url string `json:"avatar_url"` - Created string `json:"created"` - Email string `json:"email"` - Full_name string `json:"full_name"` - Is_admin bool `json:"is_admin"` - Language string `json:"language"` - Last_login string `json:"last_login"` - Login string `json:"login"` + ID int `json:"id"` + AvatarURL string `json:"avatar_url"` + Created string `json:"created"` + Email string `json:"email"` + FullName string `json:"full_name"` + IsAdmin bool `json:"is_admin"` + Language string `json:"language"` + LastLogin string `json:"last_login"` + Login string `json:"login"` } type Account struct { - Id int `json:"id"` - Full_name string `json:"full_name"` - Login string `json:"login"` + ID int `json:"id"` + FullName string `json:"full_name"` + Login string `json:"login"` } type SearchResults struct {