Example #1
0
def build_cancel(proj, build_id):
    permissions.assert_can_build(proj)
    p = get_or_404(Project.query.filter_by(name=proj))
    b = get_or_404(Build.query.filter_by(project=p, build_id=build_id))
    for r in b.runs:
        r.cancel()
    return jsendify({}), 202
Example #2
0
def project_patch_trigger(proj, tid):
    permissions.assert_can_build(proj)
    trigger = get_or_404(ProjectTrigger.query.filter(ProjectTrigger.id == tid))
    if trigger.project.name != proj:
        # someone is trying to do something fishy
        return jsendify({}, 404)

    data = request.get_json() or {}
    val = data.get('definition_file')
    if val:
        trigger.definition_file = val

    val = data.get('definition_repo')
    if val:
        trigger.definition_repo = val

    val = data.get('secrets')
    if val:
        # convert array of [{'name': <name>, 'value': <value>}, ...] dicts
        # into dictionary of {<name>: <value>, ...}
        current_secrets = trigger.secret_data
        for secret in val:
            name, value = secret['name'], secret['value']
            if value is None:
                if name in current_secrets:
                    del current_secrets[name]
            else:
                current_secrets[name] = value
        trigger.update_secrets()
    db.session.commit()
    return jsendify({})
Example #3
0
def build_create(proj):
    u = permissions.assert_can_build(proj)
    p = Project.query.filter(Project.name == proj).first_or_404()
    d = request.get_json() or {}

    secrets = {}

    # Check if the caller wants to inherit secrets from something like the
    # "git-poller" trigger for the project.
    trigger_type = d.get("trigger-type")
    if trigger_type:
        optional = trigger_type.endswith("-optional")
        if optional:
            trigger_type = trigger_type[:-9]  # strip off the "-optional"
        for t in p.triggers:
            if TriggerTypes(t.type).name == trigger_type:
                secrets.update(t.secret_data)
                break
        else:
            if not optional:
                raise ApiError(400, "No such trigger-type: %s" % trigger_type)

    # Check if the caller wants to inherit secrets from a specific trigger
    # definied for the project.
    trigger_id = d.get("trigger-id")
    if trigger_id:
        for t in p.triggers:
            if t.id == trigger_id:
                secrets.update(t.secret_data)
                break
        else:
            raise ApiError(400, "Unknown trigger-id: %s" % trigger_id)

    secrets.update(d.get("secrets") or {})
    if "triggered-by" in secrets:
        # Let's not allow triggered-by to be set externally.
        del secrets["triggered-by"]
    if u:
        secrets["triggered-by"] = str(u)
    b = trigger_build(
        p,
        d.get("reason"),
        d.get("trigger-name"),
        d.get("params"),
        secrets,
        d.get("project-definition"),
        d.get("queue-priority", 0),
    )
    url = url_for("api_build.build_get",
                  proj=p.name,
                  build_id=b.build_id,
                  _external=True)
    weburl = None
    if BUILD_URL_FMT:
        weburl = BUILD_URL_FMT.format(project=b.project.name, build=b.build_id)
    return jsendify({
        "url": url,
        "build_id": b.build_id,
        "web_url": weburl
    }, 201)
Example #4
0
def project_trigger_list(proj):
    permissions.assert_can_build(proj)
    p = get_or_404(Project.query.filter_by(name=proj))
    triggers = p.triggers

    t = request.args.get("type")
    if t:
        triggers = [x for x in triggers if x.type == TriggerTypes[t].value]

    # Remove the secret values, no need to ever expose them
    redacted = []
    for t in triggers:
        data = t.as_json()
        data["secrets"] = [{"name": x} for x in (data.get("secrets") or {}).keys()]
        redacted.append(data)
    return jsendify(redacted)
Example #5
0
def build_create(proj):
    permissions.assert_can_build(proj)
    p = Project.query.filter(Project.name == proj).first_or_404()
    d = request.get_json() or {}

    secrets = {}

    # Check if the caller wants to inherit secrets from something like the
    # "git-poller" trigger for the project.
    trigger_type = d.get('trigger-type')
    if trigger_type:
        optional = trigger_type.endswith('-optional')
        if optional:
            trigger_type = trigger_type[:-9]  # strip off the "-optional"
        for t in p.triggers:
            if TriggerTypes(t.type).name == trigger_type:
                secrets = t.secret_data
                break
        else:
            if not optional:
                raise ApiError(400, 'No such trigger-type: %s' % trigger_type)

    # Check if the caller wants to inherit secrets from a specific trigger
    # definied for the project.
    trigger_id = d.get('trigger-id')
    if trigger_id:
        for t in p.triggers:
            if t.id == trigger_id:
                secrets = t.secret_data
                break
        else:
            raise ApiError(400, 'Unknown trigger-id: %s' % trigger_id)

    secrets.update(d.get('secrets') or {})
    b = trigger_build(p, d.get('reason'), d.get('trigger-name'),
                      d.get('params'), secrets,
                      d.get('project-definition'),
                      d.get('queue-priority', 0))
    url = url_for('api_build.build_get',
                  proj=p.name, build_id=b.build_id, _external=True)
    return jsendify({'url': url, 'build_id': b.build_id}, 201)
Example #6
0
def run_cancel(proj, build_id, run):
    r = _get_run(proj, build_id, run)
    permissions.assert_can_build(r.build.project.name)
    r.set_status(BuildStatus.CANCELLING)
    db.session.commit()
    return jsendify({}), 202
Example #7
0
def run_cancel(proj, build_id, run):
    r = _get_run(proj, build_id, run)
    permissions.assert_can_build(r.build.project.name)
    r.cancel()
    return jsendify({}), 202