Skip to content

Commit

Permalink
Log the number of pull requests that would have been updated when `sk…
Browse files Browse the repository at this point in the history
…ipPullRequests` is set (#1360)
  • Loading branch information
rhyskoedijk authored Sep 28, 2024
1 parent 2f03d13 commit 3a8ee91
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
9 changes: 6 additions & 3 deletions extension/tasks/dependabotV2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
);
Expand All @@ -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(
Expand All @@ -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'`,
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ export class AzureDevOpsWebApiClient {
repository: string,
creator: string,
): Promise<IPullRequestProperties[]> {
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(
Expand Down Expand Up @@ -472,16 +471,14 @@ export class AzureDevOpsWebApiClient {

/**
* Get project properties
* @param project
* @param projectId
* @param valueBuilder
* @returns
*/
public async getProjectProperties(project: string): Promise<Record<string, string> | undefined> {
public async getProjectProperties(projectId: string): Promise<Record<string, string> | 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}`);
Expand All @@ -498,20 +495,18 @@ export class AzureDevOpsWebApiClient {
* @returns
*/
public async updateProjectProperty(
project: string,
projectId: string,
name: string,
valueBuilder: (existingValue: string) => string,
): Promise<void> {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ export class DependabotOutputProcessor implements IDependabotUpdateOutputProcess
*/
public async process(update: IDependabotUpdateOperation, type: string, data: any): Promise<boolean> {
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
Expand All @@ -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 || '{}');
Expand Down
4 changes: 4 additions & 0 deletions extension/tasks/dependabotV2/utils/getSharedVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface ISharedVariables {
virtualDirectory: string;
/** Organization name */
organization: string;
/** Project ID */
projectId: string;
/** Project name */
project: string;
/** Repository name */
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -148,6 +151,7 @@ export default function getSharedVariables(): ISharedVariables {
port,
virtualDirectory,
organization,
projectId,
project,
repository,
repositoryOverridden,
Expand Down

0 comments on commit 3a8ee91

Please sign in to comment.