From 3a8ee91c4362d8e853da28d2b02401624d2c1eaa Mon Sep 17 00:00:00 2001 From: Rhys Koedijk Date: Sun, 29 Sep 2024 04:13:27 +1300 Subject: [PATCH] Log the number of pull requests that would have been updated when `skipPullRequests` is set (#1360) --- extension/tasks/dependabotV2/index.ts | 9 ++++++--- .../azure-devops/AzureDevOpsWebApiClient.ts | 17 ++++++----------- .../dependabot-cli/DependabotOutputProcessor.ts | 7 +++---- .../dependabotV2/utils/getSharedVariables.ts | 4 ++++ 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/extension/tasks/dependabotV2/index.ts b/extension/tasks/dependabotV2/index.ts index 19ed23f9..3326328c 100644 --- a/extension/tasks/dependabotV2/index.ts +++ b/extension/tasks/dependabotV2/index.ts @@ -87,7 +87,7 @@ async function run() { // This is required when doing a security-only update as dependabot requires the list of vulnerable dependencies to be updated. // Automatic discovery of vulnerable dependencies during a security-only update is not currently supported by dependabot-updater. const dependencyList = parseProjectDependencyListProperty( - await prAuthorClient.getProjectProperties(taskInputs.project), + await prAuthorClient.getProjectProperties(taskInputs.projectId), taskInputs.repository, update['package-ecosystem'], ); @@ -113,7 +113,8 @@ async function run() { } // If there are existing pull requests, run an update job for each one; this will resolve merge conflicts and close pull requests that are no longer needed - if (existingPullRequests && Object.keys(existingPullRequests).length > 0) { + const numberOfPullRequestsToUpdate = Object.keys(existingPullRequests).length; + if (numberOfPullRequestsToUpdate > 0) { if (!taskInputs.skipPullRequests) { for (const pullRequestId in existingPullRequests) { const updatePullRequestJob = DependabotJobBuilder.newUpdatePullRequestJob( @@ -131,7 +132,9 @@ async function run() { } } } else { - warning(`Skipping update of existing pull requests as 'skipPullRequests' is set to 'true'`); + warning( + `Skipping update of ${numberOfPullRequestsToUpdate} existing pull request(s) as 'skipPullRequests' is set to 'true'`, + ); } } } diff --git a/extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.ts b/extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.ts index 475d9df6..2f6b1053 100644 --- a/extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.ts +++ b/extension/tasks/dependabotV2/utils/azure-devops/AzureDevOpsWebApiClient.ts @@ -85,7 +85,6 @@ export class AzureDevOpsWebApiClient { repository: string, creator: string, ): Promise { - console.info(`Fetching active pull request properties in '${project}/${repository}' for user id '${creator}'...`); try { const git = await this.connection.getGitApi(); const pullRequests = await git.getPullRequests( @@ -472,16 +471,14 @@ export class AzureDevOpsWebApiClient { /** * Get project properties - * @param project + * @param projectId * @param valueBuilder * @returns */ - public async getProjectProperties(project: string): Promise | undefined> { + public async getProjectProperties(projectId: string): Promise | undefined> { try { const core = await this.connection.getCoreApi(); - const projects = await core.getProjects(); - const projectGuid = projects?.find((p) => p.name === project)?.id; - const properties = await core.getProjectProperties(projectGuid); + const properties = await core.getProjectProperties(projectId); return properties?.map((p) => ({ [p.name]: p.value }))?.reduce((a, b) => ({ ...a, ...b }), {}); } catch (e) { error(`Failed to get project properties: ${e}`); @@ -498,20 +495,18 @@ export class AzureDevOpsWebApiClient { * @returns */ public async updateProjectProperty( - project: string, + projectId: string, name: string, valueBuilder: (existingValue: string) => string, ): Promise { try { // Get the existing project property value const core = await this.connection.getCoreApi(); - const projects = await core.getProjects(); - const projectGuid = projects?.find((p) => p.name === project)?.id; - const properties = await core.getProjectProperties(projectGuid); + const properties = await core.getProjectProperties(projectId); const propertyValue = properties?.find((p) => p.name === name)?.value; // Update the project property - await core.setProjectProperties(undefined, projectGuid, [ + await core.setProjectProperties(undefined, projectId, [ { op: 'add', path: '/' + name, diff --git a/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotOutputProcessor.ts b/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotOutputProcessor.ts index 5ffb958d..6d38633a 100644 --- a/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotOutputProcessor.ts +++ b/extension/tasks/dependabotV2/utils/dependabot-cli/DependabotOutputProcessor.ts @@ -50,9 +50,8 @@ export class DependabotOutputProcessor implements IDependabotUpdateOutputProcess */ public async process(update: IDependabotUpdateOperation, type: string, data: any): Promise { console.debug(`Processing output '${type}' with data:`, data); - const sourceRepoParts = update.job.source.repo.split('/'); // "{organisation}/{project}/_git/{repository}"" - const project = sourceRepoParts[1]; - const repository = sourceRepoParts[3]; + const project = this.taskInputs.project; + const repository = this.taskInputs.repository; switch (type) { // Documentation on the 'data' model for each output type can be found here: // See: https://github.com/dependabot/cli/blob/main/internal/model/update.go @@ -62,7 +61,7 @@ export class DependabotOutputProcessor implements IDependabotUpdateOutputProcess if (this.taskInputs.storeDependencyList) { console.info(`Storing the dependency list snapshot for project '${project}'...`); await this.prAuthorClient.updateProjectProperty( - project, + this.taskInputs.projectId, DependabotOutputProcessor.PROJECT_PROPERTY_NAME_DEPENDENCY_LIST, function (existingValue: string) { const repoDependencyLists = JSON.parse(existingValue || '{}'); diff --git a/extension/tasks/dependabotV2/utils/getSharedVariables.ts b/extension/tasks/dependabotV2/utils/getSharedVariables.ts index 3d33900d..40e6d797 100644 --- a/extension/tasks/dependabotV2/utils/getSharedVariables.ts +++ b/extension/tasks/dependabotV2/utils/getSharedVariables.ts @@ -19,6 +19,8 @@ export interface ISharedVariables { virtualDirectory: string; /** Organization name */ organization: string; + /** Project ID */ + projectId: string; /** Project name */ project: string; /** Repository name */ @@ -86,6 +88,7 @@ export default function getSharedVariables(): ISharedVariables { let port: string = formattedOrganizationUrl.port; let virtualDirectory: string = extractVirtualDirectory(formattedOrganizationUrl); let organization: string = extractOrganization(organizationUrl); + let projectId: string = tl.getVariable('System.TeamProjectId'); let project: string = encodeURI(tl.getVariable('System.TeamProject')); // encode special characters like spaces let repository: string = tl.getInput('targetRepositoryName'); let repositoryOverridden = typeof repository === 'string'; @@ -148,6 +151,7 @@ export default function getSharedVariables(): ISharedVariables { port, virtualDirectory, organization, + projectId, project, repository, repositoryOverridden,