Tips on utilizing a connected service in a custom Azure DevOps extension's index.ts file

I have created a unique extension for Azure DevOps that includes a specialized Connected Service and Build task. When setting up the task through the pipeline visual designer, I am able to utilize the Connected Service to choose a service and then populate a picklist with data from my API.

My question is, how can I access the selected service when the task is running? In the index.ts file, I can obtain the Guid of the service using code similar to the snippet below. But, is there a way to use this Guid to retrieve the service or its details?


import tl = require('azure-pipelines-task-lib/task');
async function run() {
try {
    const serviceString: string = tl.getInput('TestService', true);
    if (serviceString == 'bad') {
        tl.setResult(tl.TaskResult.Failed, 'Bad input was given');
         return;
    } ...

I have searched extensively and reviewed various articles but have not come across any examples that address this specific scenario.

https://learn.microsoft.com/en-us/azure/devops/extend/develop/add-build-task?view=azure-devops

https://learn.microsoft.com/en-us/azure/devops/extend/develop/service-endpoints?view=azure-devops

Answer №1

Utilizing additional functions from the azure-pipelines-task-lib/task library, specifically the tl object, is key to solving this issue:

If your custom Connected Service incorporates an authentication scheme categorized as

ms.vss-endpoint.endpoint-auth-scheme-token
, then the token input's identifier would be apitoken. In order to retrieve and use this token, you would need to implement code similar to the following:

const endpoint = tl.getEndpointUrl(serviceString, true);
// The second parameter in this context pertains to the input name associated with the specific type of authenticationScheme being utilized.
const token = tl.getEndpointAuthorizationParameter(serviceString, "apitoken", false)

My knowledge on this topic was gained through hands-on experimentation.

Diverse Authentication Methods

In my personal experience, aligning with the sentiments expressed by others' perspectives: Azure DevOps does not readily accommodate fully bespoke Connected Services. Instead, it provides a selection that covers various requirements. Depending on the chosen method(s), the value passed for the second parameter of

tl.getEndpointAuthorizationParameter
varies:

ms.vss-endpoint.endpoint-auth-scheme-token

This scheme includes a standard input:

  1. apitoken

ms.vss-endpoint.endpoint-auth-scheme-basic

This scheme boasts two inputs:

  1. username
  2. password

Illustrative Instance with a Recommendation

Firstly, consider renaming your serviceString variable to connectedServiceId (or any variation that clarifies its purpose as representing the connected service's ID) to enhance code readability.

import tl = require('azure-pipelines-task-lib/task');

async function run() {
    try {
        const connectedServiceId = tl.getInput('TestService', true);

        if (connectedServiceId == 'bad' || connectedServiceId == undefined) {
            tl.setResult(tl.TaskResult.Failed, 'Bad input provided');
            return;
        }

        const endpoint = tl.getEndpointUrl(connectedServiceId, true);
        const token = tl.getEndpointAuthorizationParameter(connectedServiceId, "apitoken", false)
    }
    finally {
        // Handling potential failures here
    }
}

Furthermore, incorporating the connectedServiceId == undefined check ensures secure usage of the variable in subsequent function calls.

Useful Examples I Referenced/Created

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

NPM Package: Accessing resources from within the package

My Current Setup I have recently developed and published an npm package in typescript. This package contains references to font files located within a folder named public/fonts internally. Now, I am in the process of installing this package into an Angul ...

Encountering 404 errors when reloading routes on an Angular Azure static web app

After deploying my Angular app on Azure static web app, I encountered an error. Whenever I try to redirect to certain routes, it returns a 404 error. However, if I navigate from one route to another within the app, everything works fine. I have attempted t ...

The concept of Nested TypeScript Map Value Type

Similar to Nested Typescript Map Type, this case involves nesting on the "value" side. Typescript Playground const mapObjectObject: Map<string, string | Map<string, string>> = new Map(Object.entries({ "a": "b", &quo ...

What could be causing the issue of CSS Styles not being applied to an Angular 2 component with Vaadin elements?

Currently, I am immersed in the tutorial on Vaadin elements using Angular 2 that I stumbled upon here In section 5.3, Styles are applied to the app.component.ts as shown below import { Component } from [email protected]/core'; @Component({ select ...

Developing a discriminated union by utilizing the attribute names from a different type

In my quest to create a unique generic type, I am experimenting with extracting property names and types from a given type to create a discriminated union type. Take for example: type FooBar = { foo: string; bar: number; }; This would translate t ...

Encountering problems with createMediaElementSource in TypeScript/React when using the Web Audio API

Currently, I am following a Web Audio API tutorial from MDN, but with a twist - I am using TypeScript and React instead of vanilla JavaScript. In my React project created with create-react-app, I am utilizing the useRef hook to reference the audio element ...

The SDK directory for TypeScript 1.3 in Visual Studio 2013 does not include the necessary tsc.exe file

Exciting news! Typescript v1.3 has been officially announced today. To fully utilize this update, I quickly installed the power tools update for VS2013. Upon completion of the installation, my Visual Studio environment now recognizes the "protected" keywo ...

What sets apart the Partial and Optional operators in Typescript?

interface I1 { x: number; y: string; } interface I2 { x?: number; y?: string; } const tmp1: Partial<I1> = {}, tmp2: I2 = {}; Can you spot a clear distinction between these two entities, as demonstrated in the above code snippet? ...

Adding a new property to the Express request object type: what you need to know

Recently, I developed a custom middleware that executes specific logic tasks. It operates by transforming the keys to values and vice versa within the req.body. Both the keys and values are strings, with built-in validation measures in place for safety. T ...

What is the best way to retrieve a value from an array of objects containing both objects and strings in TypeScript?

Consider this scenario with an array: const testData = [ { properties: { number: 1, name: 'haha' } , second: 'this type'}, ['one', 'two', 'three'], ]; The goal is to access the value of 'second&ap ...

Embedding images using a blob or base64 format does not function properly on iOS devices

I'm facing an issue with setting the src of an img tag to display an image. The code snippet below works fine on android, mac, and windows, but it is not functioning correctly on iOS: let base64Image = pageModel.image; this.$currentPageImage.src = `da ...

Obtaining distinct form control values for replicated form fields with Angular

Issue with Dynamic Form Duplicates: I am currently working with a reactive form that has two fields - name and value. There is an add button that duplicates the form by copying these fields. The challenge I am facing is with updating the values of the dup ...

Include a search query parameter in the URL by adding "?search=" to connect with a

In my react/typescript application, I have a client and server setup. The client requests data from the server and displays it using React. When making a request for data on the client side, this is how it's done: export const createApiClient = (): A ...

Searching for two variables in an API using TypeScript pipes

I'm stuck and can't seem to figure out how to pass 2 variables using the approach I have, which involves some rxjs. The issue lies with my search functionality for a navigation app where users input 'from' and 'to' locations i ...

filter failing to provide output

Issue with fetching partnername from the filter function, always returning undefined. administrationList = [ { "runid": 6, "partnerid": 2, "partnername": "test admin2", }, { "runid& ...

Mastering the Art of Concise Writing: Tips to

Is there a way to write more concisely, maybe even in a single line? this.xxx = smt.filter(item => item.Id === this.smtStatus.ONE); this.yyy = smt.filter(item => item.Id === this.smtStatus.TWO); this.zzz = smt.filter(item => item.Id == ...

Issue with my TypeScript modules TS2307: Module not found

For my latest project, I decided to use the aurelia-typescript-skeleton as the foundation. To enhance it, I created a new file called hello.ts in the src folder. export class Hello { sayHello(name:string) : string { return 'Hello ' + name; ...

Can a map key value be converted into a param object?

I have a map containing key-value pairs as shown below: for (let controller of this.attributiFormArray.controls) { attributiAttivitaMap.set(controller.get('id').value, { value: controller.get('valoreDefault').value, mandatory ...

What is the best way to retrieve an object within a class?

Exploring a Class Structure: export class LayerEditor { public layerManager: LayerManager; public commandManager: CommandManager; public tools: EditorTools; constructor() { this.commandManager = new CommandManager(); this.lay ...

Having trouble getting navigation to work using react-navigation and typescript in a react-native project?

Currently, I am exploring the use of TypeScript in my React Native project for the first time. While I have a basic understanding of TypeScript, I am encountering some issues with third-party libraries like react-navigation. My project consists of parent ( ...