Пример #1
0
// Try to read project name
func GetProjectName(log *logging.Logger, projectDir string) (string, error) {
	var name string
	if err := util.ExecuteInDir(projectDir, func() error {
		if url, err := git.GetRemoteOriginUrl(log); err == nil {
			if info, err := util.ParseVCSURL(url); err != nil {
				return maskAny(err)
			} else {
				name = info.Name
			}
		}
		return nil
	}); err != nil {
		return "", maskAny(err)
	}
	if name != "" {
		return name, nil
	}

	var err error
	projectDir, err = filepath.Abs(projectDir)
	if err != nil {
		return "", maskAny(err)
	}
	return path.Base(projectDir), nil
}
Пример #2
0
// getProjectId looks up the gitlab project id of the current project
func getProjectId(gitlab *gogitlab.Gitlab) (string, error) {
	url, err := git.GetRemoteOriginUrl(nil)
	if err != nil {
		return "", Mask(err)
	}
	projects, err := gitlab.AllProjects()
	if err != nil {
		return "", Mask(err)
	}
	for _, p := range projects {
		if p.SshRepoUrl == url {
			return strconv.Itoa(p.Id), nil
		}
	}
	return "", ErrProjectNotFound
}
Пример #3
0
// Try to read project's organization path (e.g. 'github.com/pulcy')
func GetProjectOrganizationPath(log *logging.Logger, projectDir string) (string, error) {
	var fullname string
	if err := util.ExecuteInDir(projectDir, func() error {
		if url, err := git.GetRemoteOriginUrl(log); err == nil {
			if info, err := util.ParseVCSURL(url); err != nil {
				return maskAny(err)
			} else {
				fullname = path.Join(string(info.RepoHost), info.FullName)
			}
		}
		return nil
	}); err != nil {
		return "", maskAny(err)
	}
	return path.Dir(fullname), nil
}
Пример #4
0
func createGithubRelease(log *log.Logger, version string, info ProjectInfo) error {
	// Are assets specified?
	if len(info.GithubAssets) == 0 {
		log.Debugf("No github-assets specified, no github release is created")
		return nil
	}

	// Check existance of all assets
	for _, asset := range info.GithubAssets {
		if _, err := os.Stat(asset.RelPath); err != nil {
			return maskAny(fmt.Errorf("Cannot stat asset '%s': %v", asset.RelPath, err))
		}
	}

	// Is the repository URL suitable for github releases?
	url, err := git.GetRemoteOriginUrl(log)
	if err != nil {
		return maskAny(err)
	}
	repoInfo, err := vcsurl.Parse(url)
	if err != nil {
		return maskAny(err)
	}
	if repoInfo.RepoHost != vcsurl.GitHub || repoInfo.VCS != vcsurl.Git {
		return maskAny(fmt.Errorf("Cannot create github-release because repository is not a git repo or not hosted on github"))
	}

	// Load github token
	token, err := github.LoadGithubToken()
	if err != nil {
		return maskAny(err)
	}
	gs := github.GithubService{
		Logger:     log,
		Token:      token,
		User:       repoInfo.Username,
		Repository: repoInfo.Name,
	}

	// Create github release
	relOpt := github.ReleaseCreate{
		TagName: version,
		Name:    fmt.Sprintf("v%s", version),
	}
	if err := gs.CreateRelease(relOpt); err != nil {
		return maskAny(err)
	}

	// Attach assets
	for _, asset := range info.GithubAssets {
		opt := github.UploadAssetOptions{
			TagName:  version,
			FileName: filepath.Base(asset.RelPath),
			Label:    asset.Label,
			Path:     asset.RelPath,
		}
		if err := gs.UploadAsset(opt); err != nil {
			return maskAny(err)
		}
	}

	// Update tags
	if err := git.FetchTags(log, "origin"); err != nil {
		return maskAny(err)
	}

	return nil
}
Пример #5
0
// Get ensures that flags.Folder contains an up to date copy of flags.RepoUrl checked out to flags.Version.
func Get(log *log.Logger, flags *Flags) error {
	// Get cache dir
	cachedir, cacheIsValid, err := cache.Dir(flags.RepoUrl, 0)
	if err != nil {
		return maskAny(err)
	}

	// Expand folder
	flags.Folder, err = filepath.Abs(flags.Folder)
	if err != nil {
		return maskAny(err)
	}

	// Get current folder
	wd, _ := os.Getwd()

	linked := false
	if flags.AllowLink {
		if info, err := util.ParseVCSURL(flags.RepoUrl); err == nil {
			siblingPath := filepath.Join(filepath.Dir(wd), info.Name)
			if _, err := os.Stat(siblingPath); err == nil {
				//log.Infof("Sibling folder %s exists", siblingPath)
				util.ExecuteInDir(siblingPath, func() error {
					remote, err := git.GetRemoteOriginUrl(nil)
					if err != nil {
						return maskAny(err)
					}
					if remote == flags.RepoUrl {
						if relPath, err := filepath.Rel(filepath.Dir(flags.Folder), siblingPath); err == nil {
							if err := os.Symlink(relPath, flags.Folder); err == nil {
								log.Infof("Linked -> %s", siblingPath)
								linked = true

								if vendorDir, err := golang.GetVendorDir(siblingPath); err != nil {
									return maskAny(err)
								} else {
									// Flatten sibling in copy-only mode
									if err := golang.Flatten(log, &golang.FlattenFlags{
										VendorDir: vendorDir,
										NoRemove:  true,
									}); err != nil {
										return maskAny(err)
									}
								}

							}
						}
					}
					return nil
				})
			}
		}
	}
	if linked {
		return nil
	}

	// Fill cache if needed
	cloned := false
	if !cacheIsValid {
		// Clone repo into cachedir
		if err := git.Clone(log, flags.RepoUrl, cachedir); err != nil {
			return maskAny(err)
		}
		cloned = true
	}

	// Make sure a clone exists
	_, err = os.Stat(flags.Folder)
	if os.IsNotExist(err) {
		// Sync into target folder
		if err := os.MkdirAll(flags.Folder, 0777); err != nil {
			return maskAny(err)
		}
		if err := util.ExecPrintError(nil, "rsync", "-a", appendDirSep(cachedir), appendDirSep(flags.Folder)); err != nil {
			return maskAny(err)
		}
	}
	// Change dir to folder
	if err := os.Chdir(flags.Folder); err != nil {
		return maskAny(err)
	}
	// Specific version needed?
	if flags.Version == "" {
		// Get latest version
		if !cloned {
			localCommit, err := git.GetLatestLocalCommit(nil, flags.Folder, defaultGetBranch, false)
			if err != nil {
				return maskAny(err)
			}
			remoteCommit, err := git.GetLatestRemoteCommit(nil, flags.RepoUrl, defaultGetBranch)
			if err != nil {
				return maskAny(err)
			}
			if localCommit != remoteCommit {
				if err := git.Pull(log, "origin"); err != nil {
					return maskAny(err)
				}
			} else {
				log.Info(allGood("%s is up to date\n"), makeRel(wd, flags.Folder))
			}
		}
	} else {
		// Get latest (local) version
		localVersion, err := git.GetLatestTag(nil, flags.Folder)
		if err != nil {
			return maskAny(err)
		}
		if localVersion != flags.Version {
			// Checkout requested version
			if cloned {
				log.Info(updating("Checking out version %s in %s.\n"), flags.Version, makeRel(wd, flags.Folder))
			} else {
				log.Info(updating("Found version %s, wanted %s. Updating %s now.\n"), localVersion, flags.Version, makeRel(wd, flags.Folder))
			}
			// Fetch latest changes
			if err := git.Fetch(log, "origin"); err != nil {
				return maskAny(err)
			}
			if err := git.FetchTags(log, "origin"); err != nil {
				return maskAny(err)
			}
			// Checkout intended version
			if err := git.Checkout(log, flags.Version); err != nil {
				return maskAny(err)
			}
		} else {
			log.Info(allGood("Found correct version. No changes needed in %s\n"), makeRel(wd, flags.Folder))
		}
		// Get latest remote version
		remoteVersion, err := git.GetLatestRemoteTag(nil, flags.RepoUrl)
		if err != nil {
			return maskAny(err)
		}
		if remoteVersion != flags.Version {
			log.Warning(attention("Update available for %s: '%s' => '%s'\n"), makeRel(wd, flags.Folder), flags.Version, remoteVersion)
		}
	}
	return nil
}