Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(api/scm)!: return affected repos in API response and do not assume 404 on GetRepo error #1015

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/repo/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func CreateRepo(c *gin.Context) {
}).Infof("creating new repo %s", input.GetFullName())

// get repo information from the source
r, err := scm.FromContext(c).GetRepo(ctx, u, input)
r, _, err := scm.FromContext(c).GetRepo(ctx, u, input)
if err != nil {
retErr := fmt.Errorf("unable to retrieve repo info for %s from source: %w", r.GetFullName(), err)

Expand Down
36 changes: 22 additions & 14 deletions api/scm/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
// '200':
// description: Successfully synchronized repo
// schema:
// type: string
// "$ref": "#/definitions/Repo"
// '500':
// description: Unable to synchronize repo
// schema:
Expand Down Expand Up @@ -71,25 +71,33 @@ func SyncRepo(c *gin.Context) {
logger.Infof("syncing repo %s", r.GetFullName())

// retrieve repo from source code manager service
_, err := scm.FromContext(c).GetRepo(ctx, u, r)
_, respCode, err := scm.FromContext(c).GetRepo(ctx, u, r)

// if there is an error retrieving repo, we know it is deleted: set to inactive
if err != nil {
// set repo to inactive - do not delete
r.SetActive(false)
if respCode == http.StatusNotFound {
// set repo to inactive - do not delete
r.SetActive(false)

// update repo in database
_, err := database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)
// update repo in database
r, err = database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)

util.HandleError(c, http.StatusInternalServerError, retErr)
util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

// exit with success as hook sync will be unnecessary
c.JSON(http.StatusOK, r)

return
}

// exit with success as hook sync will be unnecessary
c.JSON(http.StatusOK, fmt.Sprintf("repo %s synced", r.GetFullName()))
retErr := fmt.Errorf("error while retrieving repo %s from %s: %w", r.GetFullName(), scm.FromContext(c).Driver(), err)

util.HandleError(c, respCode, retErr)

return
}
Expand Down Expand Up @@ -130,7 +138,7 @@ func SyncRepo(c *gin.Context) {
if !webhookExists {
r.SetActive(false)

_, err := database.FromContext(c).UpdateRepo(ctx, r)
r, err = database.FromContext(c).UpdateRepo(ctx, r)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)

Expand All @@ -139,7 +147,7 @@ func SyncRepo(c *gin.Context) {
return
}

c.JSON(http.StatusOK, fmt.Sprintf("webhook not found, repo %s deactivated", r.GetFullName()))
c.JSON(http.StatusOK, r)

return
}
Expand All @@ -152,5 +160,5 @@ func SyncRepo(c *gin.Context) {
}
}

c.JSON(http.StatusOK, fmt.Sprintf("repo %s synced", r.GetFullName()))
c.Status(http.StatusNoContent)
}
37 changes: 29 additions & 8 deletions api/scm/sync_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import (
// '200':
// description: Successfully synchronized repos
// schema:
// type: string
// type: array
// items:
// "$ref": "#/definitions/Repo"
// '500':
// description: Unable to synchronize org repositories
// schema:
Expand Down Expand Up @@ -105,18 +107,28 @@ func SyncReposForOrg(c *gin.Context) {
page++
}

var results []*library.Repo

// iterate through captured repos and check if they are in GitHub
for _, repo := range repos {
_, err := scm.FromContext(c).GetRepo(ctx, u, repo)
_, respCode, err := scm.FromContext(c).GetRepo(ctx, u, repo)
// if repo cannot be captured from GitHub, set to inactive in database
if err != nil {
repo.SetActive(false)
if respCode == http.StatusNotFound {
_, err := database.FromContext(c).UpdateRepo(ctx, repo)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)

_, err := database.FromContext(c).UpdateRepo(ctx, repo)
if err != nil {
retErr := fmt.Errorf("unable to update repo for org %s: %w", o, err)
util.HandleError(c, http.StatusInternalServerError, retErr)

util.HandleError(c, http.StatusInternalServerError, retErr)
return
}

results = append(results, repo)
} else {
retErr := fmt.Errorf("error while retrieving repo %s from %s: %w", repo.GetFullName(), scm.FromContext(c).Driver(), err)

util.HandleError(c, respCode, retErr)

return
}
Expand Down Expand Up @@ -152,6 +164,8 @@ func SyncReposForOrg(c *gin.Context) {
return
}

results = append(results, repo)

continue
}

Expand All @@ -164,5 +178,12 @@ func SyncReposForOrg(c *gin.Context) {
}
}

c.JSON(http.StatusOK, fmt.Sprintf("org %s repos synced", o))
// if no repo was changed, return no content status
if len(results) == 0 {
c.Status(http.StatusNoContent)

return
}

c.JSON(http.StatusOK, results)
}
9 changes: 5 additions & 4 deletions scm/github/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (c *client) Status(ctx context.Context, u *library.User, b *library.Build,
}

// GetRepo gets repo information from Github.
func (c *client) GetRepo(ctx context.Context, u *library.User, r *library.Repo) (*library.Repo, error) {
func (c *client) GetRepo(ctx context.Context, u *library.User, r *library.Repo) (*library.Repo, int, error) {
c.Logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
Expand All @@ -378,12 +378,12 @@ func (c *client) GetRepo(ctx context.Context, u *library.User, r *library.Repo)
client := c.newClientToken(u.GetToken())

// send an API call to get the repo info
repo, _, err := client.Repositories.Get(ctx, r.GetOrg(), r.GetName())
repo, resp, err := client.Repositories.Get(ctx, r.GetOrg(), r.GetName())
if err != nil {
return nil, err
return nil, resp.StatusCode, err
}

return toLibraryRepo(*repo), nil
return toLibraryRepo(*repo), resp.StatusCode, nil
}

// GetOrgAndRepoName returns the name of the org and the repository in the SCM.
Expand Down Expand Up @@ -557,6 +557,7 @@ func (c *client) GetBranch(ctx context.Context, u *library.User, r *library.Repo

maxRedirects := 3
data, _, err := client.Repositories.GetBranch(ctx, r.GetOrg(), r.GetName(), branch, maxRedirects)

if err != nil {
return "", "", err
}
Expand Down
10 changes: 7 additions & 3 deletions scm/github/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,9 +1106,9 @@ func TestGithub_GetRepo(t *testing.T) {
client, _ := NewTest(s.URL)

// run test
got, err := client.GetRepo(context.TODO(), u, r)
got, code, err := client.GetRepo(context.TODO(), u, r)

if resp.Code != http.StatusOK {
if code != http.StatusOK {
t.Errorf("GetRepo returned %v, want %v", resp.Code, http.StatusOK)
}

Expand Down Expand Up @@ -1149,11 +1149,15 @@ func TestGithub_GetRepo_Fail(t *testing.T) {
client, _ := NewTest(s.URL)

// run test
_, err := client.GetRepo(context.TODO(), u, r)
_, code, err := client.GetRepo(context.TODO(), u, r)

if err == nil {
t.Error("GetRepo should return error")
}

if code != http.StatusNotFound {
t.Errorf("GetRepo should have returned %d status, got %d", http.StatusNotFound, code)
}
}

func TestGithub_GetOrgAndRepoName(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion scm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ type Service interface {
GetPullRequest(context.Context, *library.User, *library.Repo, int) (string, string, string, string, error)
// GetRepo defines a function that retrieves
// details for a repo.
GetRepo(context.Context, *library.User, *library.Repo) (*library.Repo, error)
GetRepo(context.Context, *library.User, *library.Repo) (*library.Repo, int, error)
// GetOrgAndRepoName defines a function that retrieves
// the name of the org and repo in the SCM.
GetOrgAndRepoName(context.Context, *library.User, string, string) (string, string, error)
Expand Down
Loading