Gitlab REST API: Editing merge requests

#pragma TextEncoding = "UTF-8"
#pragma rtGlobals=3
#pragma IgorVersion=8.0.0
#include "json_functions"

StrConstant PRIVATE_TOKEN="PRIVATE_TOKEN_XXX"

// Example of interacting with gitlab
//
// The goal is to find all merge requests which can
// not be merged due to conflicts and change their state to draft mode.
//
// Requires the JSON XOP from https://docs.byte-physics.de/json-xop
//
// Steps:
// - Fill in your access tocken in PRIVATE_TOKEN
// - Change projectPath to the desired project
// - Execute RewriteMergeRequestTitles()
//
// Gitlab Documentation:
//
// https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html
// https://docs.gitlab.com/ee/api/README.html#namespaced-path-encoding
// https://docs.gitlab.com/ee/api/merge_requests.html#update-mr
// https://docs.gitlab.com/ee/api/merge_requests.html#list-merge-requests
Function RewriteMergeRequestTitles()

	variable jsonID, i, numEntries, iid, regJSON
	string title, str, projectPath, jsonPath

	projectPath = UrlEncode("tango-controls/cppTango")

	UrlRequest headers=("PRIVATE-TOKEN: " + PRIVATE_TOKEN)                                                            \
	           , url="https://gitlab.com/api/v4/projects/" + projectPath + "/merge_requests?state=opened&per_page=100"
	// print V_responseCode, S_serverresponse

	jsonID = JSON_Parse(S_serverResponse)
	numEntries = JSON_GetArraySize(jsonID, "")

	for(i = 0; i < numEntries; i += 1)
		jsonPath = num2str(i)
		iid = JSON_GetVariable(jsonID, jsonPath + "/iid")
		title = JSON_GetString(jsonID, jsonPath + "/title")
		str = JSON_Getstring(jsonID, jsonPath + "/merge_status")

		if(!cmpstr(str, "cannot_be_merged") && !GrepString(title, "^Draft"))
			regJSON = JSON_New()
			JSON_AddString(regJSON, "/title", "Draft: " + title)
			UrlRequest/DSTR=JSON_Dump(regJSON) headers=("PRIVATE-TOKEN: " + PRIVATE_TOKEN + "\rContent-Type: application/json")               \
			                                   , url=("https://gitlab.com/api/v4/projects/" + projectPath + "/merge_requests/" + num2str(iid)) \
			                                   , method=put                                                                                

			// print V_responseCode, S_serverresponse
			JSON_Release(regJSON)
		endif
	endfor

	JSON_Release(jsonID)
End

 

Forum

Support

Gallery

Igor Pro 10

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More