Code Monkey home page Code Monkey logo

Comments (2)

patricknolan avatar patricknolan commented on May 16, 2024

Anyone know?

from vsts-extension-samples.

JoshuaTheMiller avatar JoshuaTheMiller commented on May 16, 2024

Fancy finding you here as well, @patricknolan

It would be good to add this as an example if one does not exist quite yet, but to use a connected service, you can do the following (copied from my own SO answer in case the internet breaks):


The trick is to use some more functions from azure-pipelines-task-lib/task (in your case, tl):

If your custom Connected Service adds an authentication scheme of type ms.vss-endpoint.endpoint-auth-scheme-token, then the id of the token input would be apitoken, and the code you would need to add would look something like the following:

const endpoint = tl.getEndpointUrl(serviceString, true);
// The second parameter here is the name of the associated input 
// value(s) of the specific authenticationSchemes type (more on that later).
const token = tl.getEndpointAuthorizationParameter(serviceString, "apitoken", false)

How do I know this? Experimentation.

Other Authentication Types

My current experience echoes that of others' from the past: Azure DevOps does not play nicely with fully custom Connected Services. It does ship with a few that cover most bases. Depending on which one(s) you use, the value you pass for the second parameter of tl.getEndpointAuthorizationParameter changes:

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

Ships with one standard input:

  1. apitoken

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

Ships with two inputs:

  1. username
  2. password

Example Plus Suggestion

Suggestion first: to make your code clearer, rename your serviceString variable to connectedServiceId (or some variation to be clear that is represents the ID of your connected service).

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 was given');
            return;
        }

        const endpoint = tl.getEndpointUrl(connectedServiceId, true);
        const token = tl.getEndpointAuthorizationParameter(connectedServiceId, "apitoken", false)
    }
    finally {
        // Probably report some failure here, right?
    }
}

Additionally, adding the connectedServiceId == undefined check allows for safe usage of connectedServiceId in later function calls.

Examples I found helpful/created

from vsts-extension-samples.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.