shayki5 / azure-devops-create-pr-task

Azure Pipelines task to create PR in Azure DevOps or GitHub during a build or release pipeline
https://marketplace.visualstudio.com/items?itemName=ShaykiAbramczyk.CreatePullRequest
MIT License
58 stars 35 forks source link

Error Handling when missing AccessToken #93

Closed achim-t closed 3 years ago

achim-t commented 3 years ago

https://github.com/shayki5/azure-devops-create-pr-task/blob/92eba10e2238f12b1b57fd30fcabc16cfdbad933/task/createPullRequest.ps1#L326 https://github.com/shayki5/azure-devops-create-pr-task/blob/5055ae55250407ab094ccde63c65fac7ba1d14cf/task/createPullRequest.ps1#L332-L334

If the script doesn't have access to the token, the response is not empty nor does the request throw an error (notice the missing PR ID).

*************************
******** Success ********
*************************
Pull Request  created.
##[warning]Can't set Auto Complete to PR .
##[warning]{"count":1,"value":{"Message":"The requested resource does not support http method 'PATCH'."}}

A better way would be something like.

$response = Invoke-WebRequest -Uri $url -Method Post -Headers $header -Body $jsonBody -ContentType "application/json;charset=UTF-8"
if ($response.StatusCode -eq 201) 
{
  $content = $response.Content | ConvertFrom-Json
  [...]
} else {
  # error handling, maybe message "You need to enable the "Allow scripts to access the OAuth token":"
}

Or you could check that the access token isn't empty beforehand.

shayki5 commented 3 years ago

Hi, It's a great idea! When the script started I added a check if the access token exist, if not - exit with error :) Thank you so much for the idea!

Skymogul commented 3 years ago

The temporary PAT is always exposed to ADO custom tasks. You just need to:

$token = (Get-VstsEndpoint -Name SystemVssConnection -Require).auth.parameters.AccessToken

Then you won't need people to check the OAuth box on the agent phase and the token will never be missing.

shayki5 commented 3 years ago

Thank you @Skymogul!! I just updated the extension and there is no need to check the OAuth box :) @achim-t FYI