From 8d5bd776efd39f9e97dd4dcf05ce8b90569c0a2e Mon Sep 17 00:00:00 2001 From: georgecao Date: Sun, 26 Apr 2020 17:33:52 +0800 Subject: [PATCH] modify for repo src-openeuler --- _meta | 4 ++ _service | 6 +++ config.yaml | 39 ++++++------------ pkg/cibot/config/config.go | 8 ++++ pkg/cibot/pullrequest.go | 84 +++++++++++++++++++++++++------------- 5 files changed, 87 insertions(+), 54 deletions(-) create mode 100644 _meta create mode 100644 _service diff --git a/_meta b/_meta new file mode 100644 index 0000000..ae45ff5 --- /dev/null +++ b/_meta @@ -0,0 +1,4 @@ +ckage name="#projectname#" project="openEuler:Mainline"> + + <description/> +</package> diff --git a/_service b/_service new file mode 100644 index 0000000..28c6320 --- /dev/null +++ b/_service @@ -0,0 +1,6 @@ +ces> + <service name="tar_scm_kernel_repo"> + <param name="scm">repo</param> + <param name="url">next/openEuler/#projectname#</param> + </service> +</services> diff --git a/config.yaml b/config.yaml index 1967f7b..29df5f0 100644 --- a/config.yaml +++ b/config.yaml @@ -1,36 +1,23 @@ -giteeToken: "******" -webhookSecret: "******" +giteeToken: "6cbd4ca32cb21142e6df2862c3780bee" +webhookSecret: "123456" databaseType: "mysql" databaseHost: "127.0.0.1" databasePort: 3306 databaseName: "cibot" databaseUserName: "root" -databasePassword: "******" +databasePassword: "*963.7ujm" lgtmCountsRequired: 1 -watchProjectFiles: - - watchProjectFileOwner: openeuler - watchprojectFileRepo: infrastructure - watchprojectFilePath: repository/openeuler.yaml - watchProjectFileRef: master - - watchProjectFileOwner: openeuler - watchprojectFileRepo: infrastructure - watchprojectFilePath: repository/src-openeuler.yaml - watchProjectFileRef: master -watchProjectFileDuration: 60 -watchSigFiles: - - watchSigFileOwner: openeuler - watchSigFileRepo: community - watchSigFilePath: sig/sigs.yaml - watchSigFileRef: master -watchSigFileDuration: 60 -watchOwnerFiles: - - watchOwnerFileOwner: openeuler - watchOwnerFileRepo: community - watchOwnerFilePath: sig/*/OWNERS - watchOwnerFileRef: master watchOwnerFileDuration: 300 -botName: openeuler-ci-bot -communityName: openEuler +botName: georgecao-ci-bot +communityName: GeorgeTest claLink: https://openeuler.org/en/cla.html commandLink: https://gitee.com/openeuler/community/blob/master/en/sig-infrastructure/command.md contactEmail: contact@openeuler.org +accordingfile: "src-openeuler.yaml" +newfileowner: "georgecaotest" +newfilerepo: "obs_meta" +newfilebranch: "master" +tmpservicefile: "_service" +tmpservicepath: "projects/openEuler:Factory/#projectname#/_service" +tmpmetafile: "_meta" +tmpmetapath: "projects/openEuler:Factory/#projectname#/osc/_meta" diff --git a/pkg/cibot/config/config.go b/pkg/cibot/config/config.go index 875d15b..b99af98 100644 --- a/pkg/cibot/config/config.go +++ b/pkg/cibot/config/config.go @@ -21,6 +21,14 @@ type Config struct { CommandLink string `yaml:"commandLink"` ContactEmail string `yaml:"contactEmail"` LgtmCountsRequired int `yaml:"lgtmCountsRequired"` + AccordingFile string `yaml:"accordingfile"` + NewFileOwner string `yaml:"newfileowner"` + NewFileRepo string `yaml:"newfilerepo"` + NewFileBranch string `yaml:"newfilebranch"` + ServiceFile string `yaml:"tmpservicefile"` + ServicePath string `yaml:"tmpservicepath"` + MetaFile string `yaml:"tmpmetafile"` + MetaPath string `yaml:"tmpmetapath"` } type WatchProjectFile struct { diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index bb1443c..a391528 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -3,6 +3,7 @@ package cibot import ( "encoding/base64" "fmt" + "io/ioutil" "strings" "gitee.com/openeuler/go-gitee/gitee" @@ -68,7 +69,7 @@ func (s *Server) HandlePullRequestEvent(event *gitee.PullRequestEvent) { case "merge": glog.Info("Received a pull request merge event") - diff := s.CheckSpecialFileHasModified(event, "src-openeuler.yaml") + diff := s.CheckSpecialFileHasModified(event, s.Config.AccordingFile) if diff == "" { return } @@ -77,19 +78,20 @@ func (s *Server) HandlePullRequestEvent(event *gitee.PullRequestEvent) { return } - repo := "infrastructure" - branch := "master" + newfilerepo := s.Config.NewFileRepo + newfilebranch := s.Config.NewFileBranch + newowner := s.Config.NewFileOwner for _, prjn := range prjnames { - exist := s.CheckWetherNewItemInObsProjects(event, prjn, branch, repo) + exist := s.CheckWetherNewItemInObsProjects(event, prjn, newfilebranch, newfilerepo, newowner) if true == exist { glog.Infof("Project(%v) is in obs already.", prjn) continue } glog.Infof("Begin to create new project file, project name:%v.", prjn) - _servicepath, _servicecontent := FillServicePathAndContentWithProjectName(prjn) - s.NewFileWithPathAndContentInPullRequest(event, _servicepath, _servicecontent, branch, repo) - _metapath, _metacontent := FillMetaPathAndContentWithProjectName(prjn) - s.NewFileWithPathAndContentInPullRequest(event, _metapath, _metacontent, branch, repo) + _servicepath, _servicecontent := s.FillServicePathAndContentWithProjectName(prjn) + s.NewFileWithPathAndContentInPullRequest(event, _servicepath, _servicecontent, newfilebranch, newfilerepo, newowner) + _metapath, _metacontent := s.FillMetaPathAndContentWithProjectName(prjn) + s.NewFileWithPathAndContentInPullRequest(event, _metapath, _metacontent, newfilebranch, newfilerepo, newowner) } } } @@ -115,7 +117,7 @@ func (s *Server) CheckSpecialFileHasModified(event *gitee.PullRequestEvent, spec if event == nil { return } - // get pr commit file list + // get pr commit file list, community repo owner := event.Repository.Namespace repo := event.Repository.Name number := event.PullRequest.Number @@ -138,14 +140,13 @@ func (s *Server) CheckSpecialFileHasModified(event *gitee.PullRequestEvent, spec } // Check whether the new item in src-openeuler.yaml is in project -func (s *Server) CheckWetherNewItemInObsProjects(event *gitee.PullRequestEvent, prjname string, branch string, repo string) (exist bool) { +func (s *Server) CheckWetherNewItemInObsProjects(event *gitee.PullRequestEvent, prjname string, branch string, repo string, owner string) (exist bool) { exist = false if event == nil { return } // get the sha of branch - owner := event.Repository.Namespace lvosbranch := &gitee.GetV5ReposOwnerRepoBranchesBranchOpts{} lvosbranch.AccessToken = optional.NewString(s.Config.GiteeToken) bdetail, _, err := s.GiteeClient.RepositoriesApi.GetV5ReposOwnerRepoBranchesBranch(s.Context, owner, repo, branch, lvosbranch) @@ -168,40 +169,66 @@ func (s *Server) CheckWetherNewItemInObsProjects(event *gitee.PullRequestEvent, if strings.Contains(dir.Path, "/"+prjname+"/") { glog.Infof("Find the project path:%v, sha:%v", dir.Path, dir.Sha) exist = true - break } } return } // Fill file _service path and content -func FillServicePathAndContentWithProjectName(prjname string) (_servicepath string, _service string) { - _servicepath = "obs/team_usage/obs_meta/projects/openEuler:Mainline/" + prjname + "/_service" - _service = "<services>\n" + - " <service name=\"tar_scm_kernel_repo\">\n" + - " <param name=\"scm\">repo</param>\n" + - " <param name=\"url\">next/openEuler/" + prjname + "</param>\n" + - " </service>\n" + - "</services>" +func (s *Server) FillServicePathAndContentWithProjectName(prjname string) (_servicepath string, _service string) { + _servicepath = strings.Replace(s.Config.ServicePath, "#projectname#", prjname, 1) + glog.Infof("service path:%v", _servicepath) + + // read template file info + filebuf, err := ioutil.ReadFile(s.Config.ServiceFile) + if err != nil { + glog.Errorf("Read template service file failed: %v.", err) + return + } + str := string(filebuf) + _service = strings.Replace(str, "#projectname#", prjname, 1) + glog.Infof("service file:%v", _service) + /* + _servicepath = "obs/team_usage/obs_meta/projects/openEuler:Mainline/" + prjname + "/_service" + _service = "<services>\n" + + " <service name=\"tar_scm_kernel_repo\">\n" + + " <param name=\"scm\">repo</param>\n" + + " <param name=\"url\">next/openEuler/" + prjname + "</param>\n" + + " </service>\n" + + "</services>" + */ return } // Fill file _meta path and content -func FillMetaPathAndContentWithProjectName(prjname string) (_metapath string, _meta string) { - _metapath = "obs/team_usage/obs_meta/projects/openEuler:Mainline/" + prjname + "/.osc/_meta" - _meta = "<package name=\"" + prjname + "\" project=\"openEuler:Mainline\">\n" + - " <title/>\n" + - " <description/>\n" + - "</package>\n" +func (s *Server) FillMetaPathAndContentWithProjectName(prjname string) (_metapath string, _meta string) { + _metapath = strings.Replace(s.Config.MetaPath, "#projectname#", prjname, 1) + glog.Infof("meta path:%v", _metapath) + + // read template file info + filebuf, err := ioutil.ReadFile(s.Config.MetaFile) + if err != nil { + glog.Errorf("Read template meta file failed: %v.", err) + return + } + str := string(filebuf) + _meta = strings.Replace(str, "#projectname#", prjname, 1) + glog.Infof("meta file:%v", _meta) + /* + _metapath = "obs/team_usage/obs_meta/projects/openEuler:Mainline/" + prjname + "/.osc/_meta" + _meta = "<package name=\"" + prjname + "\" project=\"openEuler:Mainline\">\n" + + " <title/>\n" + + " <description/>\n" + + "</package>\n" + */ return } // New project with name in pull -func (s *Server) NewFileWithPathAndContentInPullRequest(event *gitee.PullRequestEvent, path string, content string, branch string, repo string) { +func (s *Server) NewFileWithPathAndContentInPullRequest(event *gitee.PullRequestEvent, path string, content string, branch string, repo string, owner string) { if event == nil { return } - owner := event.Repository.Namespace newfbody := gitee.NewFileParam{} newfbody.AccessToken = s.Config.GiteeToken newfbody.AuthorName = event.PullRequest.User.Login @@ -211,6 +238,7 @@ func (s *Server) NewFileWithPathAndContentInPullRequest(event *gitee.PullRequest newfbody.Branch = branch newfbody.Message = "add project according to src-openeuler.yaml in repo community." + glog.Infof("Begin to write template fil(%v) autoly.", path) contentbase64 := base64.StdEncoding.EncodeToString([]byte(content)) newfbody.Content = contentbase64 s.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoContentsPath(s.Context, owner, repo, path, newfbody) -- Gitee