ankitbko / vscode-pull-request-azdo

Azure Devops Pull Requests for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=ankitbko.vscode-pull-request-azdo
MIT License
67 stars 22 forks source link

Add PAT as an Authentication Method #83

Closed lczerniawski closed 10 months ago

lczerniawski commented 10 months ago

Add PAT as an Authentication Method in Azure DevOps Pull Request Library

Description

This pull request introduces the ability to use a Personal Access Token (PAT) (issue #81) as an alternative authentication method in the Azure DevOps Pull Request extension for Visual Studio Code. The PAT allows users to authenticate without relying on Microsoft login credentials and eliminates the need for manual login prompts.

Changes

1. README.md

Added configuration details for azdoPullRequests.patToken in the extension's settings.

  #### azdoPullRequests.patToken
  - _type_: string
  - _required_: false
  - _default_: ""
  - _Description_: The PAT Token from Azure DevOps, if provided extension will not prompt for Microsoft login instead it will login using provided PAT

2. package.json

Added configuration details for azdoPullRequests.patToken in the extension's settings.

"azdoPullRequests.patToken": {
  "type": "string",
  "default": "",
  "description": "Personal Access Token for Azure DevOps"
}

3. src/azdo/credentials.ts

Modified the Azdo constructor to accept an additional parameter isPatTokenAuth and adjusted authentication logic accordingly.

constructor(public orgUrl: string, public projectName: string, token: string, isPatTokenAuth: boolean = false) {
  if (isPatTokenAuth) {
    this._authHandler = azdev.getPersonalAccessTokenHandler(token, true);
  } else {
    this._authHandler = azdev.getBearerHandler(token, true);
  }
  this.connection = this.getNewWebApiClient(this.orgUrl);
}

Updated the CredentialStore to check for the presence of a PAT Token and use it for authentication if available.

let isPatTokenAuth = true;
let token = vscode.workspace.getConfiguration(SETTINGS_NAMESPACE).get<string | undefined>(PATTOKEN_SETTINGS);

if(token === undefined || token === null || token === '') {
  // Logic to obtain session and token when PAT Token is not available
  isPatTokenAuth = false;
}
const azdo = new Azdo(this._orgUrl, projectName, token, isPatTokenAuth);

4. I also removed one unused file that contained configuration left over from GitHub fork.

Feel free to review and merge this pull request. If you have any questions or concerns, please don't hesitate to reach out.

Thank you, Łukasz

github-actions[bot] commented 10 months ago

Unit Test Results

    1 files  ±0    60 suites  ±0   36m 40s :stopwatch: ±0s 181 tests ±0  171 :heavy_check_mark: ±0  0 :zzz: ±0  10 :x: ±0 

For more details on these failures, see this check.

Results for commit 2f4aacd5. ± Comparison against base commit 23928f21.