Browse Source

Fixed lint errors (errcheck,naming conventions)

lint-fixes
localleon 4 years ago
parent
commit
9da159e90b
  1. 25
      gitea-group-sync.go
  2. 33
      requests.go
  3. 22
      types.go

25
gitea-group-sync.go

@ -20,7 +20,7 @@ func AddUsersToTeam(apiKeys GiteaKeys, users []Account, team int) bool {


for i := 0; i < len(users); i++ { 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=" apiKeys.Command = "/api/v1/users/search?q=" + fullusername + "&access_token="
foundUsers := RequestSearchResults(apiKeys) foundUsers := RequestSearchResults(apiKeys)


@ -42,7 +42,7 @@ func DelUsersFromTeam(apiKeys GiteaKeys, Users []Account, team int) bool {


for i := 0; i < len(Users); i++ { 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) foundUser := RequestSearchResults(apiKeys)


@ -70,7 +70,7 @@ func main() {
c.AddFunc(repTime, mainJob) c.AddFunc(repTime, mainJob)
c.Start() c.Start()
fmt.Println(c.Entries()) fmt.Println(c.Entries())
for true { for {
time.Sleep(100 * time.Second) time.Sleep(100 * time.Second)
} }
} }
@ -103,8 +103,8 @@ func importEnvVars() Config {
if err != nil { if err != nil {
log.Println("LDAP_TLS_PORT is invalid.") log.Println("LDAP_TLS_PORT is invalid.")
} }
} else { } else if len(os.Getenv("LDAP_PORT")) > 0 {
if len(os.Getenv("LDAP_PORT")) > 0 {
port, err := strconv.Atoi(os.Getenv("LDAP_PORT")) port, err := strconv.Atoi(os.Getenv("LDAP_PORT"))
envConfig.LdapPort = port envConfig.LdapPort = port
envConfig.LdapTLS = false envConfig.LdapTLS = false
@ -113,7 +113,6 @@ func importEnvVars() Config {
log.Println("LDAP_PORT is invalid.") log.Println("LDAP_PORT is invalid.")
} }
} }
}
// Set defaults for user Attributes // Set defaults for user Attributes
if len(os.Getenv("LDAP_USER_IDENTITY_ATTRIBUTE")) == 0 { if len(os.Getenv("LDAP_USER_IDENTITY_ATTRIBUTE")) == 0 {
envConfig.LdapUserIdentityAttribute = "uid" envConfig.LdapUserIdentityAttribute = "uid"
@ -151,7 +150,7 @@ func importYAMLConfig(path string) (Config, error) {
} }


func (c Config) checkConfig() { func (c Config) checkConfig() {
if len(c.ApiKeys.TokenKey) <= 0 { if len(c.ApiKeys.TokenKey) == 0 {
log.Println("GITEA_TOKEN is empty or invalid.") log.Println("GITEA_TOKEN is empty or invalid.")
} }
if len(c.ApiKeys.BaseUrl) == 0 { if len(c.ApiKeys.BaseUrl) == 0 {
@ -242,7 +241,7 @@ func mainJob() {


log.Println(organizationList) 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=" cfg.ApiKeys.Command = "/api/v1/orgs/" + organizationList[i].Name + "/teams?access_token="
teamList := RequestTeamList(cfg.ApiKeys) teamList := RequestTeamList(cfg.ApiKeys)
@ -274,14 +273,14 @@ func mainJob() {
for _, entry := range sr.Entries { for _, entry := range sr.Entries {


AccountsLdap[entry.GetAttributeValue(cfg.LdapUserIdentityAttribute)] = Account{ AccountsLdap[entry.GetAttributeValue(cfg.LdapUserIdentityAttribute)] = Account{
Full_name: entry.GetAttributeValue(cfg.LdapUserFullName), FullName: entry.GetAttributeValue(cfg.LdapUserFullName),
Login: entry.GetAttributeValue(cfg.LdapUserIdentityAttribute), 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) 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 { for k, v := range AccountsLdap {
if AccountsGitea[k].Login != v.Login { if AccountsGitea[k].Login != v.Login {
@ -289,7 +288,7 @@ func mainJob() {
} }
} }
log.Printf("can be added users list %v", addUserToTeamList) 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 { for k, v := range AccountsGitea {
if AccountsLdap[k].Login != v.Login { if AccountsLdap[k].Login != v.Login {
@ -297,7 +296,7 @@ func mainJob() {
} }
} }
log.Printf("must be del users list %v", delUserToTeamlist) log.Printf("must be del users list %v", delUserToTeamlist)
DelUsersFromTeam(cfg.ApiKeys, delUserToTeamlist, teamList[j].Id) DelUsersFromTeam(cfg.ApiKeys, delUserToTeamlist, teamList[j].ID)


} else { } else {
log.Printf("The LDAP %s not found users corresponding to team %s", cfg.LdapURL, teamList[j].Name) log.Printf("The LDAP %s not found users corresponding to team %s", cfg.LdapURL, teamList[j].Name)

33
requests.go

@ -7,6 +7,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"

// "reflect" // "reflect"
"net" "net"
"net/url" "net/url"
@ -34,11 +35,11 @@ func hasTimedOut(err error) bool {
if err, ok := err.Err.(net.Error); ok && err.Timeout() { if err, ok := err.Err.(net.Error); ok && err.Timeout() {
return true return true
} }
case net.Error: case *net.OpError:
if err.Timeout() { if err.Timeout() {
return true return true
} }
case *net.OpError: case net.Error:
if err.Timeout() { if err.Timeout() {
return true return true
} }
@ -73,6 +74,9 @@ func RequestPut(apiKeys GiteaKeys) []byte {
cc := &http.Client{Timeout: time.Second * 2} cc := &http.Client{Timeout: time.Second * 2}
url := apiKeys.BaseUrl + apiKeys.Command + apiKeys.TokenKey[apiKeys.BruteforceTokenKey] url := apiKeys.BaseUrl + apiKeys.Command + apiKeys.TokenKey[apiKeys.BruteforceTokenKey]
request, err := http.NewRequest("PUT", url, nil) request, err := http.NewRequest("PUT", url, nil)
if err != nil {
log.Fatal(err)
}
res, err := cc.Do(request) res, err := cc.Do(request)
CheckStatusCode(res) CheckStatusCode(res)
if err != nil && hasTimedOut(err) { if err != nil && hasTimedOut(err) {
@ -92,6 +96,9 @@ func RequestDel(apiKeys GiteaKeys) []byte {
cc := &http.Client{Timeout: time.Second * 2} cc := &http.Client{Timeout: time.Second * 2}
url := apiKeys.BaseUrl + apiKeys.Command + apiKeys.TokenKey[apiKeys.BruteforceTokenKey] url := apiKeys.BaseUrl + apiKeys.Command + apiKeys.TokenKey[apiKeys.BruteforceTokenKey]
request, err := http.NewRequest("DELETE", url, nil) request, err := http.NewRequest("DELETE", url, nil)
if err != nil {
log.Fatal(err)
}
res, err := cc.Do(request) res, err := cc.Do(request)
CheckStatusCode(res) CheckStatusCode(res)
if err != nil && hasTimedOut(err) { if err != nil && hasTimedOut(err) {
@ -121,7 +128,7 @@ func RequestSearchResults(ApiKeys GiteaKeys) SearchResults {
func RequestUsersList(ApiKeys GiteaKeys) (map[string]Account, int) { func RequestUsersList(ApiKeys GiteaKeys) (map[string]Account, int) {


b := RequestGet(ApiKeys) b := RequestGet(ApiKeys)
var Account_u = make(map[string]Account) var AccountU = make(map[string]Account)


var f []Account var f []Account
jsonErr := json.Unmarshal(b, &f) jsonErr := json.Unmarshal(b, &f)
@ -135,19 +142,19 @@ func RequestUsersList(ApiKeys GiteaKeys) (map[string]Account, int) {
if ApiKeys.BruteforceTokenKey < len(ApiKeys.TokenKey)-1 { if ApiKeys.BruteforceTokenKey < len(ApiKeys.TokenKey)-1 {
ApiKeys.BruteforceTokenKey++ ApiKeys.BruteforceTokenKey++
log.Printf("BruteforceTokenKey=%d", 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++ { for i := 0; i < len(f); i++ {
Account_u[f[i].Login] = Account{ AccountU[f[i].Login] = Account{
// Email: f[i].Email, // Email: f[i].Email,
Id: f[i].Id, ID: f[i].ID,
Full_name: f[i].Full_name, FullName: f[i].FullName,
Login: f[i].Login, Login: f[i].Login,
} }
} }
return Account_u, ApiKeys.BruteforceTokenKey return AccountU, ApiKeys.BruteforceTokenKey
} }


func RequestOrganizationList(apiKeys GiteaKeys) []Organization { func RequestOrganizationList(apiKeys GiteaKeys) []Organization {
@ -184,13 +191,3 @@ func parseJson(b []byte) interface{} {
m := f.(interface{}) m := f.(interface{})
return m 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
}

22
types.go

@ -1,10 +1,10 @@
package main package main


type Organization struct { type Organization struct {
Id int `json:"id"` ID int `json:"id"`
Avatar_url string `json:"avatar_url"` AvatarURL string `json:"avatar_url"`
Description string `json:"description"` Description string `json:"description"`
Full_name string `json:"full_name"` FullName string `json:"full_name"`
Location string `json:"location"` Location string `json:"location"`
Name string `json:"username"` Name string `json:"username"`
Visibility string `json:"visibility"` Visibility string `json:"visibility"`
@ -12,26 +12,26 @@ type Organization struct {
} }


type Team struct { type Team struct {
Id int `json:"id"` ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`
Permission string `json:"permission"` Permission string `json:"permission"`
} }
type User struct { type User struct {
Id int `json:"id"` ID int `json:"id"`
Avatar_url string `json:"avatar_url"` AvatarURL string `json:"avatar_url"`
Created string `json:"created"` Created string `json:"created"`
Email string `json:"email"` Email string `json:"email"`
Full_name string `json:"full_name"` FullName string `json:"full_name"`
Is_admin bool `json:"is_admin"` IsAdmin bool `json:"is_admin"`
Language string `json:"language"` Language string `json:"language"`
Last_login string `json:"last_login"` LastLogin string `json:"last_login"`
Login string `json:"login"` Login string `json:"login"`
} }


type Account struct { type Account struct {
Id int `json:"id"` ID int `json:"id"`
Full_name string `json:"full_name"` FullName string `json:"full_name"`
Login string `json:"login"` Login string `json:"login"`
} }



Loading…
Cancel
Save