Skip to content

Commit

Permalink
Added DefaultBranch in git RepoInfo.
Browse files Browse the repository at this point in the history
Added action GetRepoInfo to get repo info.
Updated gateway api to return return default branch into type ProjectResponse
  • Loading branch information
alessandro-sorint committed Feb 18, 2022
1 parent d0d219c commit ce8f384
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 20 deletions.
11 changes: 6 additions & 5 deletions internal/gitsources/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,12 @@ func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {

func fromGiteaRepo(rr *gitea.Repository) *gitsource.RepoInfo {
return &gitsource.RepoInfo{
ID: strconv.FormatInt(rr.ID, 10),
Path: path.Join(rr.Owner.UserName, rr.Name),
HTMLURL: rr.HTMLURL,
SSHCloneURL: rr.SSHURL,
HTTPCloneURL: rr.CloneURL,
ID: strconv.FormatInt(rr.ID, 10),
Path: path.Join(rr.Owner.UserName, rr.Name),
HTMLURL: rr.HTMLURL,
SSHCloneURL: rr.SSHURL,
HTTPCloneURL: rr.CloneURL,
DefaultBranch: rr.DefaultBranch,
}
}

Expand Down
11 changes: 6 additions & 5 deletions internal/gitsources/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,11 +418,12 @@ func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {

func fromGithubRepo(rr *github.Repository) *gitsource.RepoInfo {
return &gitsource.RepoInfo{
ID: strconv.FormatInt(*rr.ID, 10),
Path: path.Join(*rr.Owner.Login, *rr.Name),
HTMLURL: *rr.HTMLURL,
SSHCloneURL: *rr.SSHURL,
HTTPCloneURL: *rr.CloneURL,
ID: strconv.FormatInt(*rr.ID, 10),
Path: path.Join(*rr.Owner.Login, *rr.Name),
HTMLURL: *rr.HTMLURL,
SSHCloneURL: *rr.SSHURL,
HTTPCloneURL: *rr.CloneURL,
DefaultBranch: *rr.DefaultBranch,
}
}

Expand Down
11 changes: 6 additions & 5 deletions internal/gitsources/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,12 @@ func (c *Client) ListUserRepos() ([]*gitsource.RepoInfo, error) {

func fromGitlabRepo(rr *gitlab.Project) *gitsource.RepoInfo {
return &gitsource.RepoInfo{
ID: strconv.Itoa(rr.ID),
Path: rr.PathWithNamespace,
HTMLURL: rr.WebURL,
SSHCloneURL: rr.SSHURLToRepo,
HTTPCloneURL: rr.HTTPURLToRepo,
ID: strconv.Itoa(rr.ID),
Path: rr.PathWithNamespace,
HTMLURL: rr.WebURL,
SSHCloneURL: rr.SSHURLToRepo,
HTTPCloneURL: rr.HTTPURLToRepo,
DefaultBranch: rr.DefaultBranch,
}
}

Expand Down
11 changes: 6 additions & 5 deletions internal/gitsources/gitsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ type Oauth2Source interface {
}

type RepoInfo struct {
ID string
Path string
HTMLURL string
SSHCloneURL string
HTTPCloneURL string
ID string
Path string
HTMLURL string
SSHCloneURL string
HTTPCloneURL string
DefaultBranch string
}

type UserInfo struct {
Expand Down
64 changes: 64 additions & 0 deletions internal/services/gateway/action/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (h *ActionHandler) CreateProject(ctx context.Context, req *CreateProjectReq
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
SSHPrivateKey: string(privateKey),
PassVarsToForkedPR: req.PassVarsToForkedPR,
DefaultBranch: repo.DefaultBranch,
}

h.log.Infof("creating project")
Expand Down Expand Up @@ -187,6 +188,8 @@ type UpdateProjectRequest struct {

Visibility *cstypes.Visibility
PassVarsToForkedPR *bool

DefaultBranch *string
}

func (h *ActionHandler) UpdateProject(ctx context.Context, projectRef string, req *UpdateProjectRequest) (*csapitypes.Project, error) {
Expand Down Expand Up @@ -215,6 +218,9 @@ func (h *ActionHandler) UpdateProject(ctx context.Context, projectRef string, re
if req.PassVarsToForkedPR != nil {
p.PassVarsToForkedPR = *req.PassVarsToForkedPR
}
if req.DefaultBranch != nil {
p.DefaultBranch = *req.DefaultBranch
}

h.log.Infof("updating project")
rp, resp, err := h.configstoreClient.UpdateProject(ctx, p.ID, p.Project)
Expand Down Expand Up @@ -603,3 +609,61 @@ func (h *ActionHandler) getRemoteRepoAccessData(ctx context.Context, linkedAccou

return user, rs, la, nil
}

func (h *ActionHandler) GetRepoInfo(ctx context.Context, repoPath string, remoteSourceRef string) (*gitsource.RepoInfo, error) {
curUserID := h.CurrentUserID(ctx)

user, resp, err := h.configstoreClient.GetUser(ctx, curUserID)
if err != nil {
return nil, errors.Errorf("failed to get user %q: %w", curUserID, ErrFromRemote(resp, err))
}

rs, resp, err := h.configstoreClient.GetRemoteSource(ctx, remoteSourceRef)
if err != nil {
return nil, errors.Errorf("failed to get remote source %q: %w", remoteSourceRef, ErrFromRemote(resp, err))
}
var la *cstypes.LinkedAccount
for _, v := range user.LinkedAccounts {
if v.RemoteSourceID == rs.ID {
la = v
break
}
}
if la == nil {
return nil, errors.Errorf("user doesn't have a linked account for remote source %q", rs.Name)
}

gitSource, err := h.GetGitSource(ctx, rs, user.Name, la)
if err != nil {
return nil, errors.Errorf("failed to create gitsource client: %w", err)
}

repo, err := gitSource.GetRepoInfo(repoPath)
if err != nil {
return nil, errors.Errorf("failed to get repository info from gitsource: %w", err)
}

return repo, nil
}

func (h *ActionHandler) RefreshRemoteRepositoryInfo(ctx context.Context, projectRef string) (*csapitypes.Project, error) {
project, err := h.GetProject(ctx, projectRef)
if err != nil {
return nil, errors.Errorf("failed to get project: %w", err)
}

repoInfo, err := h.GetRepoInfo(ctx, project.RepositoryPath, project.RemoteSourceID)
if err != nil {
return nil, errors.Errorf("failed to get repo info: %w", err)
}

areq := &UpdateProjectRequest{
DefaultBranch: &repoInfo.DefaultBranch,
}
project, err = h.UpdateProject(ctx, projectRef, areq)
if err != nil {
return nil, errors.Errorf("failed to update project: %w", err)
}

return project, nil
}
31 changes: 31 additions & 0 deletions internal/services/gateway/api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func createProjectResponse(r *csapitypes.Project) *gwapitypes.ProjectResponse {
Visibility: gwapitypes.Visibility(r.Visibility),
GlobalVisibility: string(r.GlobalVisibility),
PassVarsToForkedPR: r.PassVarsToForkedPR,
DefaultBranch: r.DefaultBranch,
}

return res
Expand Down Expand Up @@ -284,3 +285,33 @@ func (h *ProjectCreateRunHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
h.log.Errorf("err: %+v", err)
}
}

type RefreshRemoteRepositoryInfoHandler struct {
log *zap.SugaredLogger
ah *action.ActionHandler
}

func NewRefreshRemoteRepositoryInfoHandler(logger *zap.Logger, ah *action.ActionHandler) *RefreshRemoteRepositoryInfoHandler {
return &RefreshRemoteRepositoryInfoHandler{log: logger.Sugar(), ah: ah}
}

func (h *RefreshRemoteRepositoryInfoHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)
projectRef, err := url.PathUnescape(vars["projectref"])
if err != nil {
httpError(w, util.NewErrBadRequest(err))
return
}

project, err := h.ah.RefreshRemoteRepositoryInfo(ctx, projectRef)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}

res := createProjectResponse(project)
if err := httpResponse(w, http.StatusCreated, res); err != nil {
h.log.Errorf("err: %+v", err)
}
}
2 changes: 2 additions & 0 deletions internal/services/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (g *Gateway) Run(ctx context.Context) error {
projectReconfigHandler := api.NewProjectReconfigHandler(logger, g.ah)
projectUpdateRepoLinkedAccountHandler := api.NewProjectUpdateRepoLinkedAccountHandler(logger, g.ah)
projectCreateRunHandler := api.NewProjectCreateRunHandler(logger, g.ah)
refreshRemoteRepositoryInfoHandler := api.NewRefreshRemoteRepositoryInfoHandler(logger, g.ah)

secretHandler := api.NewSecretHandler(logger, g.ah)
createSecretHandler := api.NewCreateSecretHandler(logger, g.ah)
Expand Down Expand Up @@ -258,6 +259,7 @@ func (g *Gateway) Run(ctx context.Context) error {
apirouter.Handle("/projects/{projectref}/reconfig", authForcedHandler(projectReconfigHandler)).Methods("PUT")
apirouter.Handle("/projects/{projectref}/updaterepolinkedaccount", authForcedHandler(projectUpdateRepoLinkedAccountHandler)).Methods("PUT")
apirouter.Handle("/projects/{projectref}/createrun", authForcedHandler(projectCreateRunHandler)).Methods("POST")
apirouter.Handle("/projects/{projectref}/refreshremoterepo", authForcedHandler(refreshRemoteRepositoryInfoHandler)).Methods("PUT")

apirouter.Handle("/projectgroups/{projectgroupref}/secrets", authForcedHandler(secretHandler)).Methods("GET")
apirouter.Handle("/projects/{projectref}/secrets", authForcedHandler(secretHandler)).Methods("GET")
Expand Down
2 changes: 2 additions & 0 deletions services/configstore/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ type Project struct {
WebhookSecret string `json:"webhook_secret,omitempty"`

PassVarsToForkedPR bool `json:"pass_vars_to_forked_pr,omitempty"`

DefaultBranch string `json:"default_branch,omitempty"`
}

type SecretType string
Expand Down
1 change: 1 addition & 0 deletions services/gateway/api/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ProjectResponse struct {
Visibility Visibility `json:"visibility,omitempty"`
GlobalVisibility string `json:"global_visibility,omitempty"`
PassVarsToForkedPR bool `json:"pass_vars_to_forked_pr,omitempty"`
DefaultBranch string `json:"default_branch,omitempty"`
}

type ProjectCreateRunRequest struct {
Expand Down
6 changes: 6 additions & 0 deletions services/gateway/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,3 +611,9 @@ func (c *Client) GetVersion(ctx context.Context) (*gwapitypes.VersionResponse, *
resp, err := c.getParsedResponse(ctx, "GET", "/version", nil, jsonContent, nil, &res)
return res, resp, err
}

func (c *Client) RefreshRemoteRepo(ctx context.Context, projectRef string) (*gwapitypes.ProjectResponse, *http.Response, error) {
project := new(gwapitypes.ProjectResponse)
resp, err := c.getParsedResponse(ctx, "PUT", path.Join("/projects", url.PathEscape(projectRef), "/refreshremoterepo"), nil, jsonContent, nil, project)
return project, resp, err
}
50 changes: 50 additions & 0 deletions tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
gwapitypes "agola.io/agola/services/gateway/api/types"
gwclient "agola.io/agola/services/gateway/client"
rstypes "agola.io/agola/services/runservice/types"
"github.com/google/go-cmp/cmp"

"code.gitea.io/sdk/gitea"
"go.uber.org/zap"
Expand Down Expand Up @@ -1935,3 +1936,52 @@ def main(ctx):
}
}
}

func TestRefreshRemoteRepositoryInfo(t *testing.T) {
dir, err := ioutil.TempDir("", "agola")
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
defer os.RemoveAll(dir)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

tetcd, tgitea, c := setup(ctx, t, dir)
defer shutdownGitea(tgitea)
defer shutdownEtcd(tetcd)

giteaAPIURL := fmt.Sprintf("http://%s:%s", tgitea.HTTPListenAddress, tgitea.HTTPPort)

giteaToken, token := createLinkedAccount(ctx, t, tgitea, c)

giteaClient := gitea.NewClient(giteaAPIURL, giteaToken)
gwClient := gwclient.NewClient(c.Gateway.APIExposedURL, token)

giteaRepo, project := createProject(ctx, t, giteaClient, gwClient)

if project.DefaultBranch != "master" {
t.Fatalf("expected DefaultBranch master got: %s", project.DefaultBranch)
}

_, err = giteaClient.EditRepo(giteaRepo.Owner.UserName, giteaRepo.Name, gitea.EditRepoOption{DefaultBranch: util.StringP("testbranch")})
if err != nil {
t.Fatalf("unexpected err: %v", err)
}

project, _, err = gwClient.RefreshRemoteRepo(ctx, project.ID)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if project.DefaultBranch != "testbranch" {
t.Fatalf("expected DefaultBranch testbranch got: %s", project.DefaultBranch)
}

p, _, err := gwClient.GetProject(ctx, project.ID)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if diff := cmp.Diff(project, p); diff != "" {
t.Fatalf("projects mismatch (-expected +got):\n%s", diff)
}
}

0 comments on commit ce8f384

Please sign in to comment.