Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added query string flag to prevent merging post data into the root of params #254

Closed
wants to merge 1 commit into from

Conversation

attie
Copy link
Contributor

@attie attie commented Jan 17, 2020

When using webhooks from other services (for example GitLab), it needs to be possible to keep their data separate from the Cronicle params data.

This patch will add a merge_params parameter to the /api/app/run_event/v1 endpoint, allowing this merging to be disabled.

For example, you might set up the following URL in a GitLab integration / web hook:

https://${my_cronicle}/api/app/run_event/v1?api_key=${my_key}id=${my_event}&merge_params=no

Pairing this with #253 will allow access to the GitLab data in a clean way, using a shelll script like this:

#!/bin/bash

set -eu -o pipefail

cleanup() {
    [ ! -z "${params:+z}" ] && rm "${params}"
    [ ! -z "${extra:+z}"  ] && rm "${extra}"
}
trap cleanup EXIT

# keep the params data
params="$(mktemp)"
jq -c '.' > "${params}"

# only accept pipeline notifications
read object_kind < <( jq -r '.post_data.object_kind' < "${params}" )
if [ "${object_kind}" != "pipeline" ]; then
    echo "WARNING: ignoring non-pipeline event... (${object_kind})" >&2
    exit 0
fi

# only accept success notifications
read object_status < <( jq -r '.post_data.object_attributes.status' < "${params}" )
if [ "${object_status}" != "success" ]; then
    echo "WARNING: ignoring non-success event... (${object_status})" >&2
    exit 0
fi

# locate gitlab
read gitlab_project_url < <( jq -r '.post_data.project.web_url'             < "${params}" )
read gitlab_namespace   < <( jq -r '.post_data.project.path_with_namespace' < "${params}" )
len=$(( ${#gitlab_project_url} - ${#gitlab_namespace} - 1 ))
gitlab_url="${gitlab_project_url::${len}}"
gitlab_api="${gitlab_url}/api/v4"

# get the necessary information
read project_id   < <( jq -r '.post_data.project.id'     < "${params}"  )
read build_id     < <( jq -r '.post_data.builds[0].id'   < "${params}"  )

# build the URL
artifact="ARTIFACT.TGZ"
gtlab_token="GITLAB_TOKEN"
url="${gitlab_api}/projects/${project_id}/jobs/${build_id}/artifacts/${artifact}"

curl -s --header "PRIVATE-TOKEN:${gitlab_token}" "${url}" \
    | gzip -d \
    | tar -t

jhuckaby added a commit that referenced this pull request Jan 18, 2020
- Includes PR #253 (thanks @attie).
- If you do not wish to merge the POST data into the `params` (for example if you are using a webhook that provides other data as JSON), then you can add `&post_data=1` to the query string. If you do this, then the POST data will be available in the `post_data` key of the `params` object.
- Thanks to user @attie and PR #254 for these fixes and suggestions.
@jhuckaby
Copy link
Owner

Thank you very much for this PR. Please forgive me, but I wanted to implement it slightly differently. I dislike query params that use "yes" and "no", and also ones that default to "yes" and you have to say "no" if you want the negative case. So I did it this way instead:

fad91ad

This should still net you the desired behavior; you just have to include &post_data=1 on the query instead of &merge_params=no.

Release: https://github.com/jhuckaby/Cronicle/releases/tag/v0.8.39

@jhuckaby jhuckaby closed this Jan 18, 2020
@attie
Copy link
Contributor Author

attie commented Jan 18, 2020

Brilliant - no problem, thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants