iwink/gitlab-webhook-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

iwink/gitlab-webhook-bundle

Composer 安装命令:

composer require iwink/gitlab-webhook-bundle

包简介

A Symfony bundle to process GitLab webhooks.

README 文档

README

License Tag Build Status

Symfony bundle to process GitLab webhooks.

Installation

To use this bundle, install it using Composer: composer require iwink/gitlab-webhook-bundle. If your project uses Symfony Flex, you are done. If not, make sure to enable the bundle in your project's config/bundles.php.

Usage

To mark a controller as a GitLab webhook, you can use the @Webhook(event="event") annotation above your controller and define a Iwink\GitLabWebhookBundle\Event\WebhookEvent argument in your method:

<?php

namespace App\Controller;

use Iwink\GitLabWebhookBundle\Annotation\Webhook;
use Iwink\GitLabWebhookBundle\Event\PipelineEvent;
use Iwink\GitLabWebhookBundle\Scheduler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/webhook", name="webhook_")
 */
class WebhookController {
    /**
     * @Route("/pipeline", name="pipeline")
     * @Webhook("pipeline")
     */
    public function pipeline(PipelineEvent $event, Scheduler $scheduler): JsonResponse {
        $status = $event->getObjectAttributes()['status'];
        if ('success' === $status) {
            $scheduler->schedule([$this, 'expensiveOperation'], ['one', true]);
        }

        return new JsonResponse();
    }

    public function expensiveOperation(string $name, bool $valid): void {
        // Does something expensive
    }
}

The example above annotates the pipeline method as a webhook which receives a Pipeline Hook event by using the @Webhook("pipeline") annotation. The event is injected into the method's $event argument. The injection is based on the typehint (PipelineEvent) of the argument, the argument's name doesn't matter. Because GitLab expects a response as soon as possible, the expensive part of the webhook is scheduled and executed after a response has been sent by the Iwink\GitLabWebhookBundle\Scheduler::schedule() method.

Secured webhooks

GitLab has the option to secure a webhook with a secret token. You can define these secret tokens in a webhook annotation:

<?php

namespace App\Controller;

use Iwink\GitLabWebhookBundle\Annotation\Webhook;
use Iwink\GitLabWebhookBundle\Event\PipelineEvent;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/webhook", name="webhook_")
 */
class WebhookController {
    /**
     * @Route("/pipeline", name="pipeline")
     * @Webhook("pipeline", tokens={"secret_token"})
     */
    public function pipeline(PipelineEvent $event): JsonResponse {
        // Handle request
    }
}

The received Pipeline Hook request should now contain the secret token (provided by the X-GitLab-Token header), otherwise the request fails. The tokens should be defined as an array because it's possible to define multiple tokens for the same annotation since multiple GitLab projects might trigger the same webhook. The tokens can also be defined as a configuration parameter using the %parameter.name% format: @Webhook("pipeline", tokens={"%gitlab.secret_token%"}). Since parameters can contain environment variables, configuring the secrets is very flexible.

Caveat

Because Symfony caches the annotations, and the file defining the annotation is not changed when updating a parameter, you should manually clear the cache after changing a secret parameter's value. This will only ever happen in the dev environment because in the prod environment the container is always cached (everytime your code changes, you will need to clear the cache).

Multiple webhooks

It's possible to register multiple webhooks to a single controller by using multiple @Webhook annotations:

<?php

namespace App\Controller;

use Iwink\GitLabWebhookBundle\Annotation\Webhook;
use Iwink\GitLabWebhookBundle\Event\MergeRequestEvent;
use Iwink\GitLabWebhookBundle\Event\PushEvent;
use Iwink\GitLabWebhookBundle\Event\WebhookEvent;
use Iwink\GitLabWebhookBundle\Scheduler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/webhook", name="webhook_")
 */
class WebhookController {
    /**
     * @Route("/pipeline", name="pipeline")
     * @Webhook("push")
     * @Webhook(event="merge request")
     */
    public function pipeline(WebhookEvent $event, Scheduler $scheduler): JsonResponse {
        if (
            ($event instanceof PushEvent && 'some/project' === $event->getProject()['name'])
            || ($event instanceof MergeRequestEvent && 'success' === $event->getObjectAttributes()['status'])
        ) {
            $scheduler->schedule([$this, 'expensiveOperation']);
        }

        return new JsonResponse();
    }

    public function expensiveOperation(): void {
        // Does something expensive
    }
}

The injected $event is now either a Iwink\GitLabWebhookBundle\Event\PushEvent or a Iwink\GitLabWebhookBundle\Event\MergeRequestEvent. Notice the difference between the 2 @Webhook annotations, both the short (@Webhook("push")) and the long (@Webhook(event="merge request")) syntax give the same result so it doesn't matter which syntax you use.

Supported webhooks

The following webhooks are supported:

iwink/gitlab-webhook-bundle 适用场景与选型建议

iwink/gitlab-webhook-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 395 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「gitlab」 「webhook」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 iwink/gitlab-webhook-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 iwink/gitlab-webhook-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 iwink/gitlab-webhook-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 395
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 4
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-19