Repository: microsoft/onefuzz
File: src/ApiService/ApiService/Functions/QueueFileChanges.cs (C#)

Code before:

        _ = fileChangeEvent ?? throw new ArgumentException("Unable to parse queue trigger as JSON");

        // check type first before calling Azure APIs
        const string eventType = "eventType";
        const string expectedEventType = "Microsoft.Storage.BlobCreated";
        if (!fileChangeEvent.RootElement.TryGetProperty(eventType, out var eventTypeElement)
            || eventTypeElement.GetString() != expectedEventType) {
            _log.AddTag("queueMessage", msg);
            _log.LogInformation("Expected fileChangeEvent to contain a property named '{eventType}' with value equal to {expectedEventType}", eventType, expectedEventType);
            return;
        }

        const string topic = "topic";
        if (!fileChangeEvent.RootElement.TryGetProperty(topic, out var topicElement)
            || !_storage.CorpusAccounts().Contains(new ResourceIdentifier(topicElement.GetString()!))) {
            _log.AddTag("queueMessage", msg);
            _log.LogInformation("Expected fileChangeEvent to contain a property named '{topic}' with a value contained in corpus accounts", topic);
            return;
        }

        var storageAccount = new ResourceIdentifier(topicElement.GetString()!);

        try {
            var result = await FileAdded(storageAccount, fileChangeEvent);
            if (!result.IsOk && result.ErrorV.Code == ErrorCode.ADO_WORKITEM_PROCESSING_DISABLED) {
                await RequeueMessage(msg, TimeSpan.FromDays(1));
            }
        } catch (Exception e) {
            _log.LogError(e, "File Added failed");
            await RequeueMessage(msg);
        }
    }

    private async Async.Task<OneFuzzResultVoid> FileAdded(ResourceIdentifier storageAccount, JsonDocument fileChangeEvent) {
        var data = fileChangeEvent.RootElement.GetProperty("data");
        var url = data.GetProperty("url").GetString()!;
        var parts = url.Split("/").Skip(3).ToList();

        var container = Container.Parse(parts[0]);
        var path = string.Join('/', parts.Skip(1));

        _log.LogInformation("file added : {Container} - {Path}", container.String, path);

        var (_, result) = await (
            ApplyRetentionPolicy(storageAccount, container, path),
            _notificationOperations.NewFiles(container, path));

        return result;
    }


Review comment (suggestion):
We're expecting FileAdded not to throw anymore so you need to update this if condition.

ADO_WORKITEM_PROCESSING_DISABLED -> Requeue 1 day
Other Errors -> Requeue with default visibility timeout

Code after:

        _ = fileChangeEvent ?? throw new ArgumentException("Unable to parse queue trigger as JSON");

        // check type first before calling Azure APIs
        const string eventType = "eventType";
        const string expectedEventType = "Microsoft.Storage.BlobCreated";
        if (!fileChangeEvent.RootElement.TryGetProperty(eventType, out var eventTypeElement)
            || eventTypeElement.GetString() != expectedEventType) {
            _log.AddTag("queueMessage", msg);
            _log.LogInformation("Expected fileChangeEvent to contain a property named '{eventType}' with value equal to {expectedEventType}", eventType, expectedEventType);
            return;
        }

        const string topic = "topic";
        if (!fileChangeEvent.RootElement.TryGetProperty(topic, out var topicElement)
            || !_storage.CorpusAccounts().Contains(new ResourceIdentifier(topicElement.GetString()!))) {
            _log.AddTag("queueMessage", msg);
            _log.LogInformation("Expected fileChangeEvent to contain a property named '{topic}' with a value contained in corpus accounts", topic);
            return;
        }

        var storageAccount = new ResourceIdentifier(topicElement.GetString()!);

        try {
            var result = await FileAdded(storageAccount, fileChangeEvent);
            if (!result.IsOk) {
                await RequeueMessage(msg, result.ErrorV.Code == ErrorCode.ADO_WORKITEM_PROCESSING_DISABLED ? TimeSpan.FromDays(1) : null);
            }
        } catch (Exception e) {
            _log.LogError(e, "File Added failed");
            await RequeueMessage(msg);
        }
    }

    private async Async.Task<OneFuzzResultVoid> FileAdded(ResourceIdentifier storageAccount, JsonDocument fileChangeEvent) {
        var data = fileChangeEvent.RootElement.GetProperty("data");
        var url = data.GetProperty("url").GetString()!;
        var parts = url.Split("/").Skip(3).ToList();

        var container = Container.Parse(parts[0]);
        var path = string.Join('/', parts.Skip(1));

        _log.LogInformation("file added : {Container} - {Path}", container.String, path);

        var (_, result) = await (
            ApplyRetentionPolicy(storageAccount, container, path),
            _notificationOperations.NewFiles(container, path));

        return result;
    }