MT#6975 call jenkins when gerrit ppa is not needed any longer

Change-Id: I84020eefe644302bdd78929a17007aa1c4b7bd1e
changes/45/2245/5
Victor Seva 11 years ago
parent 32f88cdcfa
commit 0f49b45abc

@ -20,3 +20,8 @@ from repoapi import models
@admin.register(models.JenkinsBuildInfo)
class JenkinsBuildInfoAdmin(admin.ModelAdmin):
pass
@admin.register(models.GerritRepoInfo)
class GerritRepoInfoAdmin(admin.ModelAdmin):
pass

@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('repoapi', '0002_auto_20150714_0919'),
]
operations = [
migrations.CreateModel(
name='GerritRepoInfo',
fields=[
('id', models.AutoField(
verbose_name='ID',
serialize=False, auto_created=True,
primary_key=True)),
('param_ppa',
models.CharField(unique=True, max_length=50)),
('count', models.IntegerField(default=0)),
],
),
]

@ -14,6 +14,11 @@
# with this program. If not, see <http://www.gnu.org/licenses/>.
from django.db import models
from django.db.models import signals
from repoapi import utils
import logging
logger = logging.getLogger(__name__)
class JenkinsBuildInfoManager(models.Manager):
@ -83,3 +88,49 @@ class JenkinsBuildInfo(models.Model):
def __str__(self):
return "%s:%d[%s]" % (self.jobname,
self.buildnumber, self.tag)
class GerritRepoInfo(models.Model):
param_ppa = models.CharField(max_length=50, unique=True, null=False)
count = models.IntegerField(default=1)
def gerrit_repo_add(instance):
gri = GerritRepoInfo.objects
ppa, created = gri.get_or_create(param_ppa=instance.param_ppa)
if not created:
ppa.count = ppa.count + 1
ppa.save()
logging.info("+1")
else:
logging.info("created")
def gerrit_repo_del(instance):
gri = GerritRepoInfo.objects
try:
ppa = gri.get(param_ppa=instance.param_ppa)
ppa.count = ppa.count - 1
if ppa.count > 0:
ppa.save()
logger.info("-1")
else:
ppa.delete()
logger.info("removed")
utils.jenkins_remove_ppa(instance.param_ppa)
except GerritRepoInfo.DoesNotExist:
pass
def gerrit_repo_manage(sender, **kwargs):
if kwargs["created"]:
instance = kwargs["instance"]
if instance.jobname.endswith("-repos") and \
instance.result == "SUCCESS":
logger.info("we need to count this %s", instance.param_ppa)
if instance.gerrit_eventtype == "patchset-created":
gerrit_repo_add(instance)
elif instance.gerrit_eventtype == "change-merged":
gerrit_repo_del(instance)
signals.post_save.connect(gerrit_repo_manage, sender=JenkinsBuildInfo)

@ -155,3 +155,6 @@ SWAGGER_SETTINGS = {
'title': 'RepoApi',
},
}
JENKINS_URL = "http://localhost"
JENKINS_TOKEN = "sipwise_jenkins_ci"

@ -146,3 +146,6 @@ SWAGGER_SETTINGS = {
'title': 'RepoApi',
},
}
JENKINS_URL = "https://jenkins.mgm.sipwise.com"
JENKINS_TOKEN = "sipwise_jenkins_ci"

@ -0,0 +1,107 @@
# Copyright (C) 2015 The Sipwise Team - http://sipwise.com
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
from django.test import TestCase
from repoapi.models import JenkinsBuildInfo, GerritRepoInfo
from mock import patch, Mock
class GerritRepoInfoTestCase(TestCase):
@patch('urllib2.urlopen', autospec=True)
def test_creation(self, Mockclass):
jbi = JenkinsBuildInfo.objects.create(
tag="edc90cd9-37f3-4613-9748-ed05a32031c2",
projectname="kamailio",
jobname="kamailio-repos",
buildnumber=897,
result="SUCCESS",
job_url="https://jenkins.mgm.sipwise.com/job/kamailio-repos/",
gerrit_patchset="1",
gerrit_change="2054",
gerrit_eventtype="patchset-created",
param_tag="none",
param_branch="master",
param_release="none",
param_distribution="wheezy",
param_ppa="gerrit_MT10339_review2054")
gri = GerritRepoInfo.objects.get(param_ppa="gerrit_MT10339_review2054")
self.assertEquals(gri.count, 1)
@patch('repoapi.utils.jenkins_remove_ppa')
def test_creation_deletion(self, utils):
jbi = JenkinsBuildInfo.objects.create(
tag="edc90cd9-37f3-4613-9748-ed05a32031c2",
projectname="kamailio",
jobname="kamailio-repos",
buildnumber=897,
result="SUCCESS",
job_url="https://jenkins.mgm.sipwise.com/job/kamailio-repos/",
gerrit_patchset="1",
gerrit_change="2054",
gerrit_eventtype="patchset-created",
param_tag="none",
param_branch="master",
param_release="none",
param_distribution="wheezy",
param_ppa="gerrit_MT10339_review2054")
gri = GerritRepoInfo.objects.get(
param_ppa="gerrit_MT10339_review2054")
self.assertEquals(gri.count, 1)
jbi = JenkinsBuildInfo.objects.create(
tag="edc90cd9-37f3-4613-9748-ed05a32031c2",
projectname="kamailio",
jobname="kamailio-repos",
buildnumber=897,
result="SUCCESS",
job_url="https://jenkins.mgm.sipwise.com/job/kamailio-repos/",
gerrit_patchset="1",
gerrit_change="2054",
gerrit_eventtype="change-merged",
param_tag="none",
param_branch="master",
param_release="none",
param_distribution="wheezy",
param_ppa="gerrit_MT10339_review2054")
gri = GerritRepoInfo.objects.filter(
param_ppa="gerrit_MT10339_review2054")
self.assertEquals(gri.count(), 0)
utils.assert_called_with("gerrit_MT10339_review2054")
@patch('urllib2.urlopen', autospec=True)
def test_no_creation(self, Mockclass):
jbi = JenkinsBuildInfo.objects.create(
tag="edc90cd9-37f3-4613-9748-ed05a32031c2",
projectname="kamailio",
jobname="kamailio-get-code",
buildnumber=897,
result="SUCCESS",
job_url="https://jenkins.mgm.sipwise.com/job/kamailio-repos/",
gerrit_patchset="1",
gerrit_change="2054",
gerrit_eventtype="patchset-created",
param_tag="none",
param_branch="master",
param_release="none",
param_distribution="wheezy",
param_ppa="gerrit_MT10339_review2054")
gri = GerritRepoInfo.objects.filter(
param_ppa="gerrit_MT10339_review2054")
self.assertEquals(gri.count(), 0)

@ -0,0 +1,40 @@
# Copyright (C) 2015 The Sipwise Team - http://sipwise.com
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import urllib2
import logging
from django.conf import settings
logger = logging.getLogger(__name__)
def openurl(URL):
req = urllib2.Request(URL)
response = urllib2.urlopen(req)
if response.code is 200:
print "OK"
return 0
else:
print "Error retrieving %s" % URL
return 1
def jenkins_remove_ppa(repo):
url = "%s/job/remove-reprepro-codename/buildWithParameters?"\
"token=%s&repository=%s" % \
(settings.JENKINS_URL, settings.JENKINS_TOKEN, repo)
if settings.DEBUG:
logger.info("I would call %s" % url)
else:
openurl(url)

@ -4,3 +4,4 @@ git+https://github.com/linuxmaniac/django-jenkins.git@vseva/nose#egg=django-jenk
flake8
pylint
coverage
mock

Loading…
Cancel
Save