blob: f65eb28fe9bd808b94a65b39485c67b36eae293f [file] [log] [blame]
package repo_manager
import (
"context"
"net/http"
"path/filepath"
"go.skia.org/infra/autoroll/go/codereview"
"go.skia.org/infra/autoroll/go/config_vars"
"go.skia.org/infra/autoroll/go/repo_manager/child"
"go.skia.org/infra/autoroll/go/repo_manager/common/git_common"
"go.skia.org/infra/autoroll/go/repo_manager/common/version_file_common"
"go.skia.org/infra/autoroll/go/repo_manager/parent"
"go.skia.org/infra/go/depot_tools/deps_parser"
"go.skia.org/infra/go/gerrit"
"go.skia.org/infra/go/git"
"go.skia.org/infra/go/skerr"
)
// issueJson is the structure of "git cl issue --json"
type issueJson struct {
Issue int64 `json:"issue"`
IssueUrl string `json:"issue_url"`
}
// DEPSRepoManagerConfig provides configuration for the DEPS RepoManager.
type DEPSRepoManagerConfig struct {
DepotToolsRepoManagerConfig
Gerrit *codereview.GerritConfig `json:"gerrit"`
ChildRepo string `json:"childRepo"`
ParentPath string `json:"parentPath"`
}
// See documentation for util.Validator interface.
func (c DEPSRepoManagerConfig) Validate() error {
if err := c.DepotToolsRepoManagerConfig.Validate(); err != nil {
return skerr.Wrap(err)
}
if _, _, err := c.splitParentChild(); err != nil {
return skerr.Wrap(err)
}
return nil
}
// splitParentChild splits the DEPSRepoManagerConfig into a
// parent.DEPSLocalConfig and a child.GitCheckoutConfig.
// TODO(borenet): Update the config format to directly define the parent
// and child. We shouldn't need most of the New.*RepoManager functions.
func (c DEPSRepoManagerConfig) splitParentChild() (parent.DEPSLocalConfig, child.GitCheckoutConfig, error) {
parentCfg := parent.DEPSLocalConfig{
GitCheckoutConfig: parent.GitCheckoutConfig{
GitCheckoutConfig: git_common.GitCheckoutConfig{
Branch: c.DepotToolsRepoManagerConfig.CommonRepoManagerConfig.ParentBranch,
RepoURL: c.DepotToolsRepoManagerConfig.CommonRepoManagerConfig.ParentRepo,
},
DependencyConfig: version_file_common.DependencyConfig{
VersionFileConfig: version_file_common.VersionFileConfig{
ID: c.ChildRepo,
Path: deps_parser.DepsFileName,
},
},
},
CheckoutPath: c.ParentPath,
GClientSpec: c.GClientSpec,
PreUploadSteps: c.DepotToolsRepoManagerConfig.CommonRepoManagerConfig.PreUploadSteps,
}
if err := parentCfg.Validate(); err != nil {
return parent.DEPSLocalConfig{}, child.GitCheckoutConfig{}, skerr.Wrapf(err, "generated parent config is invalid")
}
childCfg := child.GitCheckoutConfig{
GitCheckoutConfig: git_common.GitCheckoutConfig{
Branch: c.ChildBranch,
RepoURL: c.ChildRepo,
RevLinkTmpl: c.DepotToolsRepoManagerConfig.CommonRepoManagerConfig.ChildRevLinkTmpl,
},
}
if err := childCfg.Validate(); err != nil {
return parent.DEPSLocalConfig{}, child.GitCheckoutConfig{}, skerr.Wrapf(err, "generated child config is invalid")
}
return parentCfg, childCfg, nil
}
// NewDEPSRepoManager returns a RepoManager instance which operates in the given
// working directory and updates at the given frequency.
func NewDEPSRepoManager(ctx context.Context, c *DEPSRepoManagerConfig, reg *config_vars.Registry, workdir string, g *gerrit.Gerrit, recipeCfgFile, serverURL string, client *http.Client, cr codereview.CodeReview, local bool) (*parentChildRepoManager, error) {
if err := c.Validate(); err != nil {
return nil, skerr.Wrap(err)
}
parentCfg, childCfg, err := c.splitParentChild()
if err != nil {
return nil, skerr.Wrap(err)
}
uploadRoll := parent.GitCheckoutUploadGerritRollFunc(g)
parentRM, err := parent.NewDEPSLocal(ctx, parentCfg, reg, client, serverURL, workdir, cr.UserName(), cr.UserEmail(), recipeCfgFile, uploadRoll)
if err != nil {
return nil, skerr.Wrap(err)
}
if err := parent.SetupGerrit(ctx, parentRM, g); err != nil {
return nil, skerr.Wrap(err)
}
// Find the path to the child repo.
childPath := c.ChildPath
if c.ChildSubdir != "" {
childPath = filepath.Join(c.ChildSubdir, c.ChildPath)
}
childFullPath := filepath.Join(workdir, childPath)
childCheckout := &git.Checkout{GitDir: git.GitDir(childFullPath)}
childRM, err := child.NewGitCheckout(ctx, childCfg, reg, workdir, cr.UserName(), cr.UserEmail(), childCheckout)
if err != nil {
return nil, skerr.Wrap(err)
}
return newParentChildRepoManager(ctx, parentRM, childRM)
}