示例#1
0
文件: review.go 项目: rogaha/gordon
// ReviewPatch reads a git-formatted patch from `src`, and for each file affected by the patch
// it assign its Maintainers based on the current repository tree directories
// The list of Maintainers are generated when the MaintainerManager object is instantiated.
//
// The result is a map where the keys are the paths of files affected by the patch,
// and the values are the maintainers assigned to review that partiular file.
//
// There is no duplicate checks: the same maintainer may be present in multiple entries
// of the map, or even multiple times in the same entry if the MAINTAINERS file has
// duplicate lines.
func ReviewPatch(src io.Reader, maintainersDirMap *map[string][]*Maintainer) (reviewers map[string][]*Maintainer, err error) {
	reviewers = make(map[string][]*Maintainer)
	input, err := ioutil.ReadAll(src)
	if err != nil {
		return nil, err
	}
	set, err := patch.Parse(input)
	if err != nil {
		return nil, err
	}
	for _, f := range set.File {
		for _, target := range []string{f.Dst, f.Src} {
			if target == "" {
				continue
			}
			target = path.Clean(target)
			if _, exists := reviewers[target]; exists {
				continue
			}
			targetDir := "."
			items := strings.Split(target, "/")
			for i := 0; i < len(items)-1; i++ {
				if targetDir == "." {
					targetDir = items[i]
				} else {
					targetDir = path.Join(targetDir, items[i])
				}
			}

			maintainers := (*maintainersDirMap)[targetDir]
			if len(maintainers) == 0 {
				parentPath := ""
				for _, dir := range strings.Split(targetDir, "/") {
					if parentPath == "" {
						parentPath = "."
					}
					if len((*maintainersDirMap)[parentPath]) > 0 {
						reviewers[target] = (*maintainersDirMap)[parentPath]
					} else {
						break
					}
					parentPath = path.Join(parentPath, dir)
				}
			} else {
				reviewers[target] = maintainers
			}

		}
	}
	return reviewers, nil
}
示例#2
0
文件: review.go 项目: rjnagal/gordon
// ReviewPatch reads a git-formatted patch from `src`, and for each file affected by the patch
// it assign its Maintainers based on the current repository tree directories
// The list of Maintainers are generated when the MaintainerManager object is instantiated.
//
// The result is a map where the keys are the paths of files affected by the patch,
// and the values are the maintainers assigned to review that partiular file.
//
// There is no duplicate checks: the same maintainer may be present in multiple entries
// of the map, or even multiple times in the same entry if the MAINTAINERS file has
// duplicate lines.
func ReviewPatch(src io.Reader, maintainers map[string][]string) (map[string][]string, error) {
	var (
		reviewers = make(map[string][]string)
		index     = buildFileIndex(maintainers)
	)

	input, err := ioutil.ReadAll(src)
	if err != nil {
		return nil, err
	}

	set, err := patch.Parse(input)
	if err != nil {
		return nil, err
	}
	mapReviewers := func(rm map[string]bool) []string {
		var (
			i   int
			out = make([]string, len(rm))
		)
		for k := range rm {
			out[i] = k
			i++
		}
		return out
	}

	for _, f := range set.File {
		for _, originalTarget := range []string{f.Dst, f.Src} {
			if originalTarget == "" {
				continue
			}
			target := path.Clean(originalTarget)
			if _, exists := reviewers[target]; exists {
				continue
			}

			var fileMaintainers map[string]bool
			fileMaintainers = index[target]
			for len(fileMaintainers) == 0 {
				target = path.Dir(target)
				fileMaintainers = index[target]
			}
			reviewers[originalTarget] = mapReviewers(fileMaintainers)
		}
	}
	return reviewers, nil
}
示例#3
0
func GetDirsForPR(src []byte, dir string) ([]string, error) {
	dirs := []string{}

	set, err := patch.Parse(src)
	if err != nil {
		return nil, err
	}

	for _, fileset := range set.File {
		if strings.HasPrefix(fileset.Dst, dir) || strings.HasPrefix(fileset.Src, dir) {
			dirs = append(dirs, dir)
		}
	}

	return dirs, nil
}
示例#4
0
func GetFileExtensionsForPR(src []byte, ext string) ([]string, error) {
	extensions := []string{}

	set, err := patch.Parse(src)
	if err != nil {
		return nil, err
	}

	for _, fileset := range set.File {
		if strings.HasSuffix(fileset.Dst, ext) || strings.HasPrefix(fileset.Src, ext) {
			extensions = append(extensions, ext)
		}
	}

	return extensions, nil
}