Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions inc/connectors/feature_subject_gitlab_api_v4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

##
# twgit
#
#
#
# Copyright (c) 2019 Alexandre Guidet <guidet.alexandre@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#

##
# variables:
#
# TWGIT_FEATURE_SUBJECT_CONNECTOR='gitlab_api_v4'
# TWGIT_FEATURE_SUBJECT_GITLAB_DOMAIN="https://gitlab.mycompany.com"
# TWGIT_FEATURE_SUBJECT_GITLAB_PROJECT_ID="123456"
# TWGIT_FEATURE_SUBJECT_GITLAB_TOKEN="abcdxyz"

##
# Retrieve and display subject of a Gitlab api v4's story.
#
# @param string $1 story number
#
issue="$1"
url="$(printf "%s/api/v4/projects/%s/issues/%s?private_token=%s" \
"$TWGIT_FEATURE_SUBJECT_GITLAB_DOMAIN" \
"$TWGIT_FEATURE_SUBJECT_GITLAB_PROJECT_ID" \
"$issue" \
"$TWGIT_FEATURE_SUBJECT_GITLAB_TOKEN")"

cmd="curl --insecure --max-time 3 --silent -H \"Cache-control: no-cache\" ${url}"

data=$(eval $cmd)

# Python or PHP ?
language='?'
which python 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
language='python'
fi

which php 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
language='php'
fi

which node 1>/dev/null 2>&1
if [ $? -eq 0 ]; then
language='javascript'
fi

# Convert JSON with Python or PHP:
if [ "$language" = 'python' ]; then
if [ ! -z "$data" ]; then
echo $data | python -c "import json,sys;s=sys.stdin.read();s=s.replace('\r\n', '');s=json.loads(s);print s['title'].encode('utf8');" 2>/dev/null
fi
elif [ "$language" = 'php' ]; then
if [ ! -z "$data" ]; then
echo $data | php -r '$o = json_decode(file_get_contents("php://stdin")); echo $o->title;' 2>/dev/null
fi
elif [ "$language" = 'javascript' ]; then
if [ ! -z "$data" ]; then
echo $data | node -e "var data = ''; process.stdin.on('data', function(chunk) { data += chunk; }); process.stdin.on('end', function() { var o = JSON.parse(data); console.log(o.title); });" 2>/dev/null
fi
else
echo "Language '$language' not handled!" >&2
exit 1
fi

exit 0