Skip to content

Commit

Permalink
Merge pull request #4408 from nickmango/revert-gerrit-post
Browse files Browse the repository at this point in the history
Revert "Feature/Gerrit Post response update"
  • Loading branch information
nickmango authored Aug 13, 2024
2 parents cd9b083 + e1f605c commit 961f4f9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 386 deletions.
27 changes: 0 additions & 27 deletions cla-backend-go/api_client/api_client.go

This file was deleted.

54 changes: 0 additions & 54 deletions cla-backend-go/api_client/mocks/mock_client.go

This file was deleted.

143 changes: 0 additions & 143 deletions cla-backend-go/gerrits/mocks/mock_service.go

This file was deleted.

74 changes: 12 additions & 62 deletions cla-backend-go/gerrits/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"

// "github.com/LF-Engineering/lfx-kit/auth"

"github.com/go-openapi/strfmt"
"github.com/go-resty/resty/v2"

// "github.com/go-resty/resty/v2"
apiclient "github.com/communitybridge/easycla/cla-backend-go/api_client"
"github.com/go-resty/resty/v2"
"github.com/sirupsen/logrus"

"github.com/communitybridge/easycla/cla-backend-go/utils"
Expand Down Expand Up @@ -89,27 +84,6 @@ func (s service) AddGerrit(ctx context.Context, claGroupID string, projectSFID s
ProjectSFID: projectSFID,
Version: params.Version,
}

// Get the gerrit repos
log.WithFields(f).Debugf("fetching gerrit repos for gerrit instance: %s", *params.GerritURL)
gerritHost, err := extractGerritHost(*params.GerritURL, f)
if err != nil {
return nil, err
}
gerritRepoList, getRepoErr := s.GetGerritRepos(ctx, gerritHost)
if getRepoErr != nil {
log.WithFields(f).WithError(getRepoErr).Warnf("problem fetching gerrit repos, error: %+v", getRepoErr)
return nil, getRepoErr
}

log.WithFields(f).Debugf("discovered %d gerrit repos", len(gerritRepoList.Repos))
log.WithFields(f).Debugf("gerrit repo list %+v", gerritRepoList)
// Set the connected flag - for now, we just set this value to true
for _, repo := range gerritRepoList.Repos {
repo.Connected = true
}
input.GerritRepoList = gerritRepoList
log.WithFields(f).Debugf("gerrit input %+v", input)
return s.repo.AddGerrit(ctx, input)
}

Expand Down Expand Up @@ -242,7 +216,6 @@ func convertModel(responseModel map[string]GerritRepoInfo, serverInfo *ServerInf
URL: strfmt.URI(weblink.URL),
})
}
log.Debugf("Processing repo: %s, weblinks: %+v", name, weblinks)

claEnabled := false
if serverInfo != nil && serverInfo.Auth.UseContributorAgreements {
Expand Down Expand Up @@ -287,11 +260,7 @@ func listGerritRepos(ctx context.Context, gerritHost string) (map[string]GerritR
utils.XREQUESTID: ctx.Value(utils.XREQUESTID),
"gerritHost": gerritHost,
}
client := &apiclient.RestAPIClient{
Client: &http.Client{
Timeout: 10 * time.Second,
},
}
client := resty.New()

base := "https://" + gerritHost

Expand All @@ -300,40 +269,28 @@ func listGerritRepos(ctx context.Context, gerritHost string) (map[string]GerritR
return nil, gerritAPIPathErr
}

log.WithFields(f).Debugf("gerrit API path using client: %s", gerritAPIPath)

if gerritAPIPath != "" {
base = fmt.Sprintf("https://%s/%s", gerritHost, gerritAPIPath)
}

url := fmt.Sprintf("%s/projects/?d&pp=0", base)
resp, err := client.GetData(ctx, url)

resp, err := client.R().
EnableTrace().
Get(fmt.Sprintf("%s/projects/?d&pp=0", base))
if err != nil {
log.WithFields(f).Warnf("problem querying gerrit host: %s, error: %+v", gerritHost, err)
return nil, err
}

defer func() {
if err = resp.Body.Close(); err != nil {
log.WithFields(f).Debugf("Failed to close response body; %v", err)
}
}()

log.WithFields(f).Debugf("response: %+v", resp.Body)

// Get the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
if resp.IsError() {
msg := fmt.Sprintf("non-success response from list gerrit host repos for gerrit %s, error code: %s", gerritHost, resp.Status())
log.WithFields(f).Warn(msg)
return nil, errors.New(msg)
}

var result map[string]GerritRepoInfo
// Need to strip off the leading "magic prefix line" from the response payload, which is: )]}'
// See: https://gerrit.linuxfoundation.org/infra/Documentation/rest-api.html#output
strippedBody := stripMagicPrefix(body)

var result map[string]GerritRepoInfo

err = json.Unmarshal(strippedBody, &result)
err = json.Unmarshal(resp.Body()[4:], &result)
if err != nil {
log.WithFields(f).Warnf("problem unmarshalling response for gerrit host: %s, error: %+v", gerritHost, err)
return nil, err
Expand All @@ -342,13 +299,6 @@ func listGerritRepos(ctx context.Context, gerritHost string) (map[string]GerritR
return result, nil
}

func stripMagicPrefix(data []byte) []byte {
if len(data) > 4 {
return data[4:]
}
return data
}

// getGerritConfig returns the gerrit configuration for the specified host
func getGerritConfig(ctx context.Context, gerritHost string) (*ServerInfo, error) {
f := logrus.Fields{
Expand Down
Loading

0 comments on commit 961f4f9

Please sign in to comment.