Beispiel #1
0
    def create_build(self):
        commit = self.cleaned_data.get('commit')
        if not commit:
            gh_repo = self.repo.github_api
            gh_branch = gh_repo.branch(self.cleaned_data['branch'])
            commit = gh_branch.commit.sha

        release = self.cleaned_data.get('release')

        branch, created = Branch.objects.get_or_create(
            repo=self.repo,
            name=self.cleaned_data['branch'],
        )
        if branch.is_removed:
            # resurrect the soft deleted branch
            branch.is_removed = False
            branch.save()
            
        keep_org = self.cleaned_data.get('keep_org')

        build = Build(
            repo=self.repo,
            plan=self.plan,
            branch=branch,
            commit=commit,
            keep_org=keep_org,
            build_type='manual',
            user=self.user,
            release=release,
            release_relationship_type='manual'
        )
        
        build.save()
        
        return build
Beispiel #2
0
 def fire(self, finished_build):
     build = Build(
         repo=self.target_plan_repo.repo,
         plan=self.target_plan_repo.plan,
         planrepo=self.target_plan_repo,
         commit=self._get_commit(),
         branch=self._get_or_create_branch(),
         build_type="auto",
     )
     build.save()
Beispiel #3
0
 def run(self):
     Build = apps.get_model("build", "Build")
     build = Build(
         repo=self.branch.repo,
         plan=self.plan,
         branch=self.branch,
         commit=self.branch.get_github_api().commit.sha,
         schedule=self,
         build_type="scheduled",
     )
     build.save()
     return build
Beispiel #4
0
    def test_planrepo_find_on_build_init(self):
        repo = RepositoryFactory()
        plan = PlanFactory()
        planrepo = PlanRepositoryFactory(plan=plan, repo=repo)

        build = Build(repo=repo, plan=plan)
        assert build.planrepo == planrepo
Beispiel #5
0
def github_push_webhook(request):
    if not validate_github_webhook(request):
        return HttpResponseForbidden

    push = json.loads(request.body)
    repo_id = push['repository']['id']
    try:
        repo = Repository.objects.get(github_id=repo_id)
    except Repository.DoesNotExist:
        return HttpResponse('Not listening for this repository')

    branch_ref = push.get('ref')
    if not branch_ref:
        return HttpResponse('No branch found')

    branch_name = None
    if branch_ref.startswith('refs/heads/'):
        branch_name = branch_ref.replace('refs/heads/', '')
    elif branch_ref.startswith('refs/tags/'):
        branch_name = branch_ref.replace('refs/tags/', 'tag: ')

    if branch_name:
        branch, created = Branch.objects.get_or_create(repo=repo,
                                                       name=branch_name)
        if branch.is_removed:
            # resurrect the soft deleted branch
            branch.is_removed = False
            branch.save()

    for plan in repo.plans.filter(type__in=['commit', 'tag'], active=True):
        run_build, commit, commit_message = plan.check_push(push)
        if run_build:
            build = Build(
                repo=repo,
                plan=plan,
                commit=commit,
                commit_message=commit_message,
                branch=branch,
                build_type='auto',
            )
            build.save()

    return HttpResponse('OK')
Beispiel #6
0
def github_push_webhook(request):
    if not validate_github_webhook(request):
        return HttpResponseForbidden

    push = json.loads(request.body)
    repo_id = push['repository']['id']
    try:
        repo = Repository.objects.get(github_id=repo_id)
    except Repository.DoesNotExist:
        return HttpResponse('Not listening for this repository')

    branch_ref = push.get('ref')
    if not branch_ref:
        return HttpResponse('No branch found')

    branch_name = None
    if branch_ref.startswith('refs/heads/'):
        branch_name = branch_ref.replace('refs/heads/', '')
    elif branch_ref.startswith('refs/tags/'):
        branch_name = branch_ref.replace('refs/tags/', 'tag: ')

    if branch_name:
        branch, created = Branch.objects.get_or_create(repo=repo,
                                                       name=branch_name)
        if branch.is_removed:
            # resurrect the soft deleted branch
            branch.is_removed = False
            branch.save()

    release = None
    # Check if the event was triggered by a tag
    if push['ref'].startswith('refs/tags/') and repo.release_tag_regex:
        tag = push['ref'][len('refs/tags/'):]
        # Check the tag against regex
        if re.match(repo.release_tag_regex, tag) and push['head_commit']:
            release, _ = Release.objects.get_or_create(
                repo=repo,
                git_tag=tag,
                defaults={
                    'created_from_commit': push["head_commit"]["id"],
                    'status': 'draft'
                })

    for plan in repo.plans.filter(type__in=['commit', 'tag'], active=True):
        run_build, commit, commit_message = plan.check_push(push)
        if run_build:
            build = Build(
                repo=repo,
                plan=plan,
                commit=commit,
                commit_message=commit_message,
                branch=branch,
                build_type='auto',
            )
            if release:
                build.release = release
                build.release_relationship_type = 'test'
            build.save()

    return HttpResponse('OK')
Beispiel #7
0
    def create_build(self):

        commit = self.cleaned_data.get("commit")
        if not commit:
            gh_repo = self.repo.github_api
            gh_branch = gh_repo.branch(self.cleaned_data["branch"])
            commit = gh_branch.commit.sha

        release = self.cleaned_data.get("release")

        branch, created = Branch.objects.get_or_create(
            repo=self.repo, name=self.cleaned_data["branch"])
        if branch.is_removed:
            # resurrect the soft deleted branch
            branch.is_removed = False
            branch.save()

        keep_org = self.cleaned_data.get("keep_org")
        org_note = self.cleaned_data.get("org_note")

        build = Build(
            repo=self.repo,
            plan=self.plan,
            planrepo=self.planrepo,
            org=self.org,
            branch=branch,
            commit=commit,
            keep_org=keep_org,
            build_type="manual",
            user=self.user,
            release=release,
            org_note=org_note,
            release_relationship_type="manual",
        )

        build.save()

        return build
Beispiel #8
0
    def test_scheduled_build_init(self):
        repo = RepositoryFactory()
        branch = BranchFactory(name="branch", repo=repo)
        schedule = PlanScheduleFactory(branch=branch)
        plan = PlanFactory()

        planrepo = PlanRepositoryFactory(plan=plan, repo=repo)

        build = Build(
            repo=branch.repo,
            plan=plan,
            branch=branch,
            commit="shashasha",
            schedule=schedule,
            build_type="scheduled",
        )
        assert build.planrepo == planrepo
Beispiel #9
0
def create_builds(push, repo, branch, release):
    for pr in repo.planrepository_set.should_run().filter(
            plan__trigger__in=["commit", "tag"]):
        plan = pr.plan
        run_build, commit, commit_message = plan.check_push(push)
        if run_build:
            build = Build(
                repo=repo,
                plan=plan,
                planrepo=pr,
                commit=commit,
                commit_message=commit_message,
                branch=branch,
                build_type="auto",
            )
            if release:
                build.release = release
                build.release_relationship_type = "test"
            build.save()
Beispiel #10
0
 def test_empty_build_init(self):
     Build()
Beispiel #11
0
def github_push_webhook(request):
    validate_github_webhook(request)

    push = json.loads(request.body)
    repo_id = push["repository"]["id"]
    try:
        repo = Repository.objects.get(github_id=repo_id)
    except Repository.DoesNotExist:
        return HttpResponse("Not listening for this repository")

    branch_ref = push.get("ref")
    if not branch_ref:
        return HttpResponse("No branch found")

    branch_name = None
    if branch_ref.startswith("refs/heads/"):
        branch_name = branch_ref.replace("refs/heads/", "")
    elif branch_ref.startswith("refs/tags/"):
        branch_name = branch_ref.replace("refs/tags/", "tag: ")

    if branch_name:
        branch, created = Branch.objects.get_or_create(repo=repo,
                                                       name=branch_name)
        if branch.is_removed:
            # resurrect the soft deleted branch
            branch.is_removed = False
            branch.save()

    release = None
    # Check if the event was triggered by a tag
    if push["ref"].startswith("refs/tags/") and repo.release_tag_regex:
        tag = push["ref"][len("refs/tags/"):]
        # Check the tag against regex
        if re.match(repo.release_tag_regex, tag) and push["head_commit"]:
            release, _ = Release.objects.get_or_create(
                repo=repo,
                git_tag=tag,
                defaults={
                    "created_from_commit": push["head_commit"]["id"],
                    "status": "draft",
                },
            )

    for pr in repo.planrepository_set.should_run().filter(
            plan__trigger__in=["commit", "tag"]):
        plan = pr.plan
        run_build, commit, commit_message = plan.check_push(push)
        if run_build:
            build = Build(
                repo=repo,
                plan=plan,
                planrepo=pr,
                commit=commit,
                commit_message=commit_message,
                branch=branch,
                build_type="auto",
            )
            if release:
                build.release = release
                build.release_relationship_type = "test"
            build.save()

    return HttpResponse("OK")