Add support of gitlab-ci

Review Request #14211 — Created Oct. 24, 2024 and updated

Information

rbintegrations
release-4.x

Reviewers

Add support of gitlab-ci

Added integration and saw that it calls the pipeline on gitlab.

Summary ID
Add support of gitlab-ci
7cc427b0261e253904db5854bb251d80e73695bc
Description From Last Updated

Also it would be a good idea to provide a gitlab component for patching like this. templates/ref.yml spec: --- .reviewboard: …

miserymisery

're' imported but unused Column: 1 Error code: F401

reviewbotreviewbot

're' imported but unused Column: 1 Error code: F401

reviewbotreviewbot

line break before binary operator Column: 13 Error code: W503

reviewbotreviewbot

line break before binary operator Column: 13 Error code: W503

reviewbotreviewbot

line break before binary operator Column: 13 Error code: W503

reviewbotreviewbot
There are no open issues
misery
Review request changed
Summary:
WIP Add support of gitlab-ci
Add support of gitlab-ci
Description:
~  

WIP Add support of gitlab-ci

  ~

Add support of gitlab-ci

Testing Done:
  +

Added integration and saw that it calls the pipeline on gitlab.

Commits:
Summary ID
WIP Add support of gitlab-ci
0cd1f7e13ffea73695adcbf8fd0ed540fd28de27
Add support of gitlab-ci
1bc87e573cddb172e5f295ce2aa7fc8be7bae10c
Diff:

Revision 2 (+972 -575)

Show changes

README.rst
package-lock.json
setup.py
docs/releasenotes/4.0.1.rst
docs/releasenotes/conf.py
docs/releasenotes/index.rst
rbintegrations/__init__.py
rbintegrations/extension.py
28 more

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

misery
Review request changed
Commits:
Summary ID
Add support of gitlab-ci
1bc87e573cddb172e5f295ce2aa7fc8be7bae10c
Add support of gitlab-ci
1bc87e573cddb172e5f295ce2aa7fc8be7bae10c
Diff:

Revision 3 (+700)

Show changes

setup.py
rbintegrations/extension.py
rbintegrations/gitlabci/__init__.py
rbintegrations/gitlabci/forms.py
rbintegrations/gitlabci/integration.py
rbintegrations/static/images/gitlabci/icon.png
rbintegrations/static/images/gitlabci/icon@2x.png

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

misery
misery
  1. 
      
  2. The best way to notify RB from gitlab would be a webook from gitlab to RB. I wrote a tiny wsgi application that get's "pipeline event" [1] for the project/group that calls rbt. A better approach would be to include the ability to RB itself without that wrapper script.

    #!/usr/bin/python
    
    import json
    import logging
    import uwsgi
    import subprocess
    
    log = logging.getLogger("gitlab")
    
    logging.basicConfig(
        format="%(asctime)s %(levelname)s: %(message)s", level="INFO"
    )
    
    
    def health():
        return None
    
    
    def get_state(state):
        if state == "pending":
            return ("build queued...", "pending")
    
        if state == "passed":
            return ("build succeeded.", "done-success")
    
        if state == "running":
            return ("build started...", "pending")
    
        if state == "canceling":
            return ("canceling build...", "pending")
    
        if state == "canceled":
            return ("build canceled", "error")
    
        return ("build failed", "error")
    
    
    def process_rbtools(v, c, token):
        url = c.get("url")
        url_text = "See pipeline"
    
        description, state = get_state(c.get("detailed_status"))
        cmd = [
            "rbt",
            "status-update",
            "set",
            "--url",
            url,
            "--url-text",
            url_text,
            "-r",
            v.get("REVIEWBOARD_REVIEW_ID"),
            "-s",
            v.get("REVIEWBOARD_STATUS_UPDATE_ID"),
            "--server",
            v.get("REVIEWBOARD_SERVER"),
            "--api-token",
            token,
            "--state",
            state,
            "--description",
            description,
            "--disable-cookie-storage",
            "--disable-cache-storage",
        ]
    
        log.info(cmd)
        subprocess.check_call(cmd)
        return uwsgi.SPOOL_OK
    
    
    def process_pipeline(c, token):
        if c is not None:
            v = c.get("variables")
            if v is not None:
                v = {item["key"]: item["value"] for item in v}
                if "REVIEWBOARD_SERVER" in v:
                    return process_rbtools(v, c, token)
        return uwsgi.SPOOL_OK
    
    
    def process_data(data):
        try:
            c = json.loads(data.get("body"))
        except (TypeError, ValueError) as e:
            log.info("Broken json: {0}".format(e))
            return uwsgi.SPOOL_OK
    
        # log.debug(json.dumps(c, indent=4, sort_keys=True))
    
        if c.get("object_kind") == "pipeline" and "object_attributes" in c:
            return process_pipeline(c.get("object_attributes"), data.get(b"token"))
    
        return uwsgi.SPOOL_OK
    
    
    def process_spooler(data):
        try:
            return process_data(data)
        except BaseException:
            log.exception("Cannot process data")
    
        return uwsgi.SPOOL_RETRY
    
    
    def application(environ, start_response):
        result = []
        headers = [("Content-Type", "text/plain")]
    
        request_body = []
        try:
            request_body_size = environ.get("CONTENT_LENGTH", 0)
            if request_body_size:
                request_body = environ["wsgi.input"].read(int(request_body_size))
        except ValueError as e:
            log.warn("Cannot read body: {0}".format(e))
            headers.append(("Content-Length", str(len(result))))
            start_response("400 Bad Request", headers)
            return result
    
        status = "200 OK"
        if len(request_body) > 0 and "HTTP_X_GITLAB_TOKEN" in environ:
            uwsgi.spool(
                {
                    b"body": request_body,
                    b"token": environ["HTTP_X_GITLAB_TOKEN"].encode("utf-8"),
                }
            )
        else:
            healthResult = health()
            if healthResult is None:
                status = "204 No Content"
            else:
                status = "503 Service Unavailable"
                result = [healthResult.encode("utf-8")]
    
        headers.append(("Content-Length", str(len(result))))
        start_response(status, headers)
        return result
    
    
    uwsgi.spooler = process_spooler
    

    [1] https://docs.gitlab.com/user/project/integrations/webhook_events/#pipeline-events

  3. Show all issues

    Also it would be a good idea to provide a gitlab component for patching like this.

    templates/ref.yml

    spec:
    ---
    
    .reviewboard:
        reviewboard_before_script:
           - |
                 if [ "$REVIEWBOARD_SERVER" != "" ]; then
                    apk add rbtools git
                    rbt patch --server ${REVIEWBOARD_SERVER} --diff-revision ${REVIEWBOARD_DIFF_REVISION} ${REVIEWBOARD_REVIEW_ID}
                 fi
    

    templates/global_before_script.yml

    spec:
    ---
    
    include:
      - component: https://github.com/reviewboard/rbintegrations/ref@main
    
    before_script:
        - !reference [.reviewboard, reviewboard_before_script]
    

    customer .gitlab-ci.yml

    include:
      - component: https://github.com/reviewboard/rbintegrations/ref@main
    
    first-job:
      script:
        - echo Hello
    
  4. 
      
misery
misery
misery
misery
Review request changed
Commits:
Summary ID
Add support of gitlab-ci
07a041231afc9a85712e81b73539e7c56347cc9f
Add support of gitlab-ci
fac64c4771f050aa05b5535b46f27e2526ab0c62
Diff:

Revision 8 (+1054)

Show changes

setup.py
rbintegrations/extension.py
rbintegrations/gitlabci/__init__.py
rbintegrations/gitlabci/forms.py
rbintegrations/gitlabci/integration.py
rbintegrations/gitlabci/urls.py
rbintegrations/gitlabci/views.py
rbintegrations/static/images/gitlabci/icon.png
1 more

Checks run (1 failed, 1 succeeded)

flake8 failed.
JSHint passed.

flake8

misery
Review request changed
Commits:
Summary ID
Add support of gitlab-ci
fac64c4771f050aa05b5535b46f27e2526ab0c62
Add support of gitlab-ci
7cc427b0261e253904db5854bb251d80e73695bc
Diff:

Revision 9 (+1054)

Show changes

setup.py
rbintegrations/extension.py
rbintegrations/gitlabci/__init__.py
rbintegrations/gitlabci/forms.py
rbintegrations/gitlabci/integration.py
rbintegrations/gitlabci/urls.py
rbintegrations/gitlabci/views.py
rbintegrations/static/images/gitlabci/icon.png
1 more

Checks run (2 succeeded)

flake8 passed.
JSHint passed.
Loading...