From ff89e78b4b7f188bdd0dae37fda3007fcc35ca19 Mon Sep 17 00:00:00 2001 From: William Zhang Date: Wed, 30 May 2012 16:40:49 +0800 Subject: [PATCH] move add job to view from jenkins.py to view.py for nest view fix get_view_by_url and get_view init url --- jenkinsapi/jenkins.py | 32 +++++--------------------------- jenkinsapi/view.py | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/jenkinsapi/jenkins.py b/jenkinsapi/jenkins.py index 6114341..9b747ee 100644 --- a/jenkinsapi/jenkins.py +++ b/jenkinsapi/jenkins.py @@ -170,13 +170,12 @@ class Jenkins(JenkinsBase): def get_view(self, str_view_name): view_url = self.get_view_url(str_view_name) view_api_url = self.python_api_url(view_url) - return View(view_api_url , str_view_name, jenkins_obj=self) + return View(view_url , str_view_name, jenkins_obj=self) - def get_view_by_url(self, str_url): - view_url = str_url - view_api_url = "%s/api/python/" % str_url - str_view_name = view_url.replace(self.baseurl, '') - return View(view_api_url, str_view_name, jenkins_obj=self) + def get_view_by_url(self, str_view_url): + #for nested view + str_view_name = str_view_url.split('/view/')[-1].replace('/', '') + return View(str_view_url , str_view_name, jenkins_obj=self) def delete_view_by_url(self, str_url): url = "%s/doDelete" %str_url @@ -212,27 +211,6 @@ class Jenkins(JenkinsBase): log.exception(e) return Jenkins(self.baseurl).get_view(str_view_name) - def add_job_to_view(self, str_view_name, jobnamelist): - data = { - "description":"", - "statusFilter":"", - "useincluderegex":"on", - "includeRegex":"", - "columns": [{"stapler-class": "hudson.views.StatusColumn", "kind": "hudson.views.StatusColumn"}, - {"stapler-class": "hudson.views.WeatherColumn", "kind": "hudson.views.WeatherColumn"}, - {"stapler-class": "hudson.views.JobColumn", "kind": "hudson.views.JobColumn"}, - {"stapler-class": "hudson.views.LastSuccessColumn", "kind": "hudson.views.LastSuccessColumn"}, - {"stapler-class": "hudson.views.LastFailureColumn", "kind": "hudson.views.LastFailureColumn"}, - {"stapler-class": "hudson.views.LastDurationColumn", "kind": "hudson.views.LastDurationColumn"}, - {"stapler-class": "hudson.views.BuildButtonColumn", "kind": "hudson.views.BuildButtonColumn"}], - "Submit":"OK", - } - data["name"]="str_view_name" - for jobname in jobnamelist: - # check job is avaliave - data[jobname]="on" - - def __getitem__(self, jobname): """ Get a job by name diff --git a/jenkinsapi/view.py b/jenkinsapi/view.py index d72656f..eb62ced 100644 --- a/jenkinsapi/view.py +++ b/jenkinsapi/view.py @@ -1,5 +1,6 @@ from jenkinsapi.jenkinsbase import JenkinsBase from jenkinsapi.job import Job +import urllib class View(JenkinsBase): @@ -55,6 +56,32 @@ class View(JenkinsBase): def get_jenkins_obj(self): return self.jenkins_obj + def add_job(self, str_job_name): + if str_job_name in self.get_job_dict(): + return "Job %s has in View %s" %(str_job_name, self.name) + elif not self.get_jenkins_obj().has_job(str_job_name): + return "Job %s is not known - available: %s" % ( str_job_name, ", ".join(self.get_jenkins_obj().get_jobs_list())) + else: + data = { + "description":"", + "statusFilter":"", + "useincluderegex":"on", + "includeRegex":"", + "columns": [{"stapler-class": "hudson.views.StatusColumn", "kind": "hudson.views.StatusColumn"}, + {"stapler-class": "hudson.views.WeatherColumn", "kind": "hudson.views.WeatherColumn"}, + {"stapler-class": "hudson.views.JobColumn", "kind": "hudson.views.JobColumn"}, + {"stapler-class": "hudson.views.LastSuccessColumn", "kind": "hudson.views.LastSuccessColumn"}, + {"stapler-class": "hudson.views.LastFailureColumn", "kind": "hudson.views.LastFailureColumn"}, + {"stapler-class": "hudson.views.LastDurationColumn", "kind": "hudson.views.LastDurationColumn"}, + {"stapler-class": "hudson.views.BuildButtonColumn", "kind": "hudson.views.BuildButtonColumn"}], + "Submit":"OK", + } + data["name"] = self.name + data[str_job_name] = "on" + data['json'] = data.copy() + self.post_data('%sconfigSubmit' % self.baseurl, urllib.urlencode(data)) + return "Job %s is add in View %s " % (str_job_name, self.name) + def id(self): """ Calculate an ID for this object. -- 2.7.4