From d39640ea105840880834503274632bf62caec199 Mon Sep 17 00:00:00 2001 From: Marvin Pohl Date: Thu, 27 Sep 2018 16:32:11 +0200 Subject: [PATCH] Added Gitea step source --- buildbot_gitea/step_source.py | 36 +++++++++ buildbot_gitea/test/test_step_source.py | 99 +++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 buildbot_gitea/step_source.py create mode 100644 buildbot_gitea/test/test_step_source.py diff --git a/buildbot_gitea/step_source.py b/buildbot_gitea/step_source.py new file mode 100644 index 0000000..50c45f5 --- /dev/null +++ b/buildbot_gitea/step_source.py @@ -0,0 +1,36 @@ + + +from __future__ import absolute_import +from __future__ import print_function + +from twisted.internet import defer +from twisted.python import log + +from buildbot.steps.source.git import Git + + +class Gitea(Git): + """ + Source step that knows how to handle merge requests from + the Gitea webhook + """ + @defer.inlineCallbacks + def _fetch(self, arg): + print("Update") + super(Gitea, self)._fetch(arg) + if self.build.hasProperty("pr_id"): + remote = yield self._dovccmd( + ['config', 'remote.pr_source.url'], collectStdout=True) + print(remote) + if remote is None or remote is '': + yield self._dovccmd( + ['remote', 'add', 'pr_source', + self.build.getProperty("head_git_ssh_url", None)]) + else: + yield self._dovccmd( + ['remote', 'set-url', 'pr_source', + self.build.getProperty("head_git_ssh_url", None)]) + yield self._dovccmd(['fetch', 'pr_source']) + yield self._dovccmd(['merge', self.build.getProperty("head_sha", None)]) + else: + print("Not a PR") diff --git a/buildbot_gitea/test/test_step_source.py b/buildbot_gitea/test/test_step_source.py new file mode 100644 index 0000000..4978ca5 --- /dev/null +++ b/buildbot_gitea/test/test_step_source.py @@ -0,0 +1,99 @@ +# This file is part of Buildbot. Buildbot 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, version 2. +# +# 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, write to the Free Software Foundation, Inc., 51 +# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# Copyright Buildbot Team Members + +from __future__ import absolute_import +from __future__ import print_function + +from twisted.trial import unittest + +from buildbot.process.results import SUCCESS +from buildbot_gitea.step_source import Gitea +from buildbot.test.fake.remotecommand import Expect +from buildbot.test.fake.remotecommand import ExpectShell +from buildbot.test.util import config +from buildbot.test.util import sourcesteps + + +class TestGitea(sourcesteps.SourceStepMixin, config.ConfigErrorsMixin, unittest.TestCase): + stepClass = Gitea + + def setUp(self): + self.sourceName = self.stepClass.__name__ + return self.setUpSourceStep() + + def setupStep(self, step, args, **kwargs): + step = sourcesteps.SourceStepMixin.setupStep(self, step, args, **kwargs) + step.build.properties.setProperty("pr_id", "1", "gitea pr id") + step.build.properties.setProperty("base_sha", "f6ad368298bd941e934a41f3babc827b2aa95a1d", "gitea source branch") + step.build.properties.setProperty("base_branch", "master", "gitea source branch") + step.build.properties.setProperty("base_git_ssh_url", + "git@gitea.example.com:base/awesome_project.git", + "gitea source git ssh url") + step.build.properties.setProperty("head_sha", "e4cd1224c622d46a8199c85c858485723115d2c8", "gitea target sha") + step.build.properties.setProperty("head_branch", "feature-branch", "gitea target branch") + step.build.properties.setProperty("head_git_ssh_url", + "git@gitea.example.com:target/awesome_project.git", + "gitea target git ssh url") + return step + + def tearDown(self): + return self.tearDownSourceStep() + + def test_with_merge_branch(self): + self.setupStep( + Gitea(repourl='git@gitea.example.com:base/awesome_project.git', + mode='full', method='clean'), None) + + self.expectCommands( + ExpectShell(workdir='wkdir', + command=['git', '--version']) + + ExpectShell.log('stdio', + stdout='git version 1.7.5') + + 0, + Expect('stat', dict(file='wkdir/.buildbot-patched', + logEnviron=True)) + + 1, + Expect('listdir', {'dir': 'wkdir', 'logEnviron': True, + 'timeout': 1200}) + + Expect.update('files', ['.git']) + + 0, + ExpectShell(workdir='wkdir', + command=['git', 'clean', '-f', '-f', '-d']) + + 0, + # here we always ignore revision, and fetch the merge branch + ExpectShell(workdir='wkdir', + command=['git', 'fetch', '-t', + 'git@gitea.example.com:base/awesome_project.git', 'HEAD']) + + 0, + ExpectShell(workdir='wkdir', + command=['git', 'reset', '--hard', 'FETCH_HEAD', '--']) + + 0, + ExpectShell(workdir='wkdir', + command=['git', 'config', 'remote.pr_source.url']) + + 0, + ExpectShell(workdir='wkdir', + command=['git', 'remote', 'add', 'pr_source', 'git@gitea.example.com:target/awesome_project.git']) + + 0, + ExpectShell(workdir='wkdir', + command=['git', 'fetch', 'pr_source']) + + 0, + ExpectShell(workdir='wkdir', + command=['git', 'merge', 'e4cd1224c622d46a8199c85c858485723115d2c8']) + + 0 + ) + self.expectOutcome(result=SUCCESS) + self.expectProperty( + 'got_revision', 'e4cd1224c622d46a8199c85c858485723115d2c8', 'GitLab') + return self.runStep()