From 446d97c17877bccde33651fa35af9d2125d21bb0 Mon Sep 17 00:00:00 2001 From: Alexandre Guidet Date: Fri, 3 May 2019 10:33:07 +1200 Subject: [PATCH 1/3] [twgit] Init feature 'feature-support-for-gitlab-api-v4'. From 10f9bf13c17e17d6e5b11d3b8a96b0e01f7a4070 Mon Sep 17 00:00:00 2001 From: Alexandre Guidet Date: Fri, 3 May 2019 11:05:26 +1200 Subject: [PATCH 2/3] added connector for gitlab api v4 --- .../feature_subject_gitlab_api_v4.sh | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 inc/connectors/feature_subject_gitlab_api_v4.sh diff --git a/inc/connectors/feature_subject_gitlab_api_v4.sh b/inc/connectors/feature_subject_gitlab_api_v4.sh new file mode 100644 index 0000000..080660a --- /dev/null +++ b/inc/connectors/feature_subject_gitlab_api_v4.sh @@ -0,0 +1,66 @@ +#!/bin/bash + +## +# twgit +# +# +# +# Copyright (c) 2019 Alexandre Guidet +# +# 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' +else + which php 1>/dev/null 2>&1 + if [ $? -eq 0 ]; then + language='php' + fi +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 +else + echo "Language '$language' not handled!" >&2 + exit 1 +fi + +exit 0 From bee76d54aa9bfd98ea0224b9749003a5c7904396 Mon Sep 17 00:00:00 2001 From: Alexandre Date: Tue, 21 Mar 2023 14:07:17 +1300 Subject: [PATCH 3/3] using node for parsing curl result --- .../feature_subject_gitlab_api_v4.sh | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/inc/connectors/feature_subject_gitlab_api_v4.sh b/inc/connectors/feature_subject_gitlab_api_v4.sh index 080660a..8d5822f 100644 --- a/inc/connectors/feature_subject_gitlab_api_v4.sh +++ b/inc/connectors/feature_subject_gitlab_api_v4.sh @@ -42,11 +42,16 @@ language='?' which python 1>/dev/null 2>&1 if [ $? -eq 0 ]; then language='python' -else - which php 1>/dev/null 2>&1 - if [ $? -eq 0 ]; then - language='php' - fi +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: @@ -58,6 +63,10 @@ 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