• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

samsmithnz / RepoGovernance / 3687671902

pending completion
3687671902

push

github

GitHub
Updating packages (#278)

118 of 202 branches covered (58.42%)

Branch coverage included in aggregate %.

390 of 543 relevant lines covered (71.82%)

6.31 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.09
/src/RepoGovernance.Core/TableStorage/TableStorageCommonDA.cs
1
using Microsoft.Azure.Cosmos.Table;
2
using RepoGovernance.Core.Models;
3

4
//TODO: Update: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/tables/Azure.Data.Tables/MigrationGuide.md
5
namespace RepoGovernance.Core.TableStorage
6
{
7
    public class TableStorageCommonDA
8
    {
9
        private readonly string? ConfigurationString;
10
        private readonly string? TableName;
11

12
        public TableStorageCommonDA(string connectionString, string tableName)
2✔
13
        {
2✔
14
            ConfigurationString = connectionString;
2✔
15
            TableName = tableName;
2✔
16
        }
2✔
17

18
        //This is needed for Dependency Injection
19
        public TableStorageCommonDA() { }
×
20

21
        private CloudTable CreateConnection()
22
        {
2✔
23
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationString);
2✔
24

25
            // Create the table client.
26
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
2✔
27

28
            // Get a reference to a table named "items"
29
            CloudTable table = tableClient.GetTableReference(TableName);
2✔
30

31
            // Create the table if it doesn't exist
32
            //table.CreateIfNotExists(); // DON"T use CreateIfNotExists, it throws an internal 409 in App insights: https://stackoverflow.com/questions/48893519/azure-table-storage-exception-409-conflict-unexpected
33
            if (!table.Exists())
2!
34
            {
×
35
                table.Create();
×
36
            }
×
37

38
            return table;
2✔
39
        }
2✔
40

41
        //public async Task<bool> AddItem(AzureStorageTableModel data, bool forceUpdate = false)
42
        //{
43
        //    //Check if the item exists in storage
44
        //    AzureStorageTableModel item = await GetItem(data.PartitionKey, data.RowKey);
45
        //    if (item == null || forceUpdate == true)
46
        //    {
47
        //        await SaveItem(data);
48
        //        return true; //data saved to table!
49
        //    }
50
        //    else
51
        //    {
52
        //        return false; //no updates needed
53
        //    }
54
        //}
55

56
        public async Task<AzureStorageTableModel> GetItem(string partitionKey, string rowKey)
57
        {
×
58
            //prepare the partition key
59
            partitionKey = EncodePartitionKey(partitionKey);
×
60

61
            CloudTable table = CreateConnection();
×
62

63
            // Create a retrieve operation that takes a customer entity.
64
            TableOperation retrieveOperation = TableOperation.Retrieve<AzureStorageTableModel>(partitionKey, rowKey);
×
65

66
            // Execute the retrieve operation.
67
            TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
×
68

69
            return (AzureStorageTableModel)retrievedResult.Result;
×
70
        }
×
71

72
        //This can't be async, because of how it queries the underlying data
73
        public List<AzureStorageTableModel> GetItems(string partitionKey)
74
        {
1✔
75
            partitionKey = EncodePartitionKey(partitionKey);
1✔
76

77
            CloudTable table = CreateConnection();
1✔
78

79
            // execute the query on the table
80
            List<AzureStorageTableModel> list = table.CreateQuery<AzureStorageTableModel>()
1✔
81
                                     .Where(ent => ent.PartitionKey == partitionKey)
1✔
82
                                     .ToList();
1✔
83

84
            return list;
1✔
85
        }
1✔
86

87
        public async Task<bool> SaveItem(AzureStorageTableModel data)
88
        {
1✔
89
            CloudTable table = CreateConnection();
1✔
90

91
            // Create the TableOperation that inserts/merges the entity.
92
            TableOperation operation = TableOperation.InsertOrMerge(data);
1✔
93
            await table.ExecuteAsync(operation);
1✔
94
            return true;
1✔
95
        }
1✔
96

97
        ////public async Task<bool> DeleteItem(string tableName, string name)
98
        ////{
99
        ////    CloudTable table = CreateConnection(tableName);
100

101
        ////    // Create a retrieve operation that expects a customer entity.
102
        ////    TableOperation retrieveOperation = TableOperation.Retrieve<T>("Item", name);
103

104
        ////    // Execute the operation.
105
        ////    TableResult retrievedResult = await table.ExecuteAsync(retrieveOperation);
106

107
        ////    // Assign the result to a CustomerEntity object.
108
        ////    T deleteEntity = (T)retrievedResult.Result;
109

110
        ////    if (deleteEntity != null)
111
        ////    {
112
        ////        // Create the TableOperation that inserts the customer entity.
113
        ////        TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
114

115
        ////        // Execute the delete operation.
116
        ////        await table.ExecuteAsync(deleteOperation);
117
        ////    }
118
        ////    return true;
119
        ////}
120

121
        public static string EncodePartitionKey(string text)
122
        {
3✔
123
            //The forward slash(/) character
124
            //The backslash(\) character
125
            //The number sign(#) character
126
            //The question mark (?) character
127
            text = text.Replace("/", "_");
3✔
128
            //text = text.Replace("\\", "_");
129
            //text = text.Replace("#", "_");
130
            //text = text.Replace("?", "_");
131

132
            ////Control characters from U+0000 to U+001F, including:
133
            ////The horizontal tab(\t) character
134
            //text = text.Replace("\t", "_");
135
            ////The linefeed(\n) character
136
            //text = text.Replace("\n", "_");
137
            ////The carriage return (\r) character
138
            //text = text.Replace("\r", "_");
139
            ////Control characters from U + 007F to U+009F
140

141
            return text;
3✔
142
        }
3✔
143

144
        //public string DecodePartitionKey(string text)
145
        //{
146
        //    return text.Replace("_", "/");
147
        //}
148

149
    }
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc