setup azure function on a schedule and deploy

This commit is contained in:
2020-03-20 00:12:38 -04:00
parent 0bfab99f1b
commit 0b61fbf44a
7 changed files with 459 additions and 24 deletions

View File

@@ -17,12 +17,10 @@ namespace Habitica.Todoist.Integration.Console
{
class Program
{
static IConfiguration configuration { get; set; }
private static IConfiguration configuration { get; set; }
private static string habiticaApiUrl => "https://habitica.com/api/v3/";
private static string habiticaUserId => configuration["habitica:userId"];
private static string habiticaApiKey => configuration["habitica:apiKey"];
private static string todoistApiUrl => "https://api.todoist.com/sync/v8/";
private static string todoistApiKey => configuration["todoist:apiKey"];
private static string tableStorageConnectionString => configuration["tableStorage:connectionString"];
private static string giosUserId => "0b6ec4eb-8878-4b9e-8585-7673764a6541";
@@ -31,14 +29,6 @@ namespace Habitica.Todoist.Integration.Console
{
ConfigBuild();
//var habiticaClient2 = new HabiticaServiceClient(habiticaUserId, habiticaApiKey);
//var tasks = habiticaClient2.ReadTasks().ConfigureAwait(false).GetAwaiter().GetResult().Data;
//foreach (var task in tasks)
// habiticaClient2.DeleteTask(task.Id).ConfigureAwait(false).GetAwaiter().GetResult();
//return;
// initialize all the clients
var habiticaClient = new HabiticaServiceClient(habiticaUserId, habiticaApiKey);
var todoistClient = new TodoistServiceClient(todoistApiKey);

View File

@@ -0,0 +1,264 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# Azure Functions localsettings file
local.settings.json
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# DNX
project.lock.json
project.fragment.lock.json
artifacts/
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
#*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc

View File

@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Habitica.Todoist.Integration.Model\Habitica.Todoist.Integration.Model.csproj" />
<ProjectReference Include="..\Habitica.Todoist.Integration.Services\Habitica.Todoist.Integration.Services.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,152 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Habitica.Todoist.Integration.Model.Habitica.Enums;
using Habitica.Todoist.Integration.Model.Storage;
using Habitica.Todoist.Integration.Model.Todoist;
using Habitica.Todoist.Integration.Services;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using HabiticaTask = Habitica.Todoist.Integration.Model.Habitica.Task;
namespace Habitica.Todoist.Integration.Function.Sync
{
public static class SyncFunction
{
private static IConfiguration configuration { get; set; }
private static string habiticaUserId => configuration["habitica:userId"];
private static string habiticaApiKey => configuration["habitica:apiKey"];
private static string todoistApiKey => configuration["todoist:apiKey"];
private static string tableStorageConnectionString => configuration["tableStorage:connectionString"];
private static string giosUserId => "0b6ec4eb-8878-4b9e-8585-7673764a6541";
[Singleton]
[FunctionName("SyncFunction")]
public static async Task Run([TimerTrigger("0 */3 * * * *")]TimerInfo myTimer, ILogger log)
{
BuildConfig();
// initialize all the clients
var habiticaClient = new HabiticaServiceClient(habiticaUserId, habiticaApiKey);
var todoistClient = new TodoistServiceClient(todoistApiKey);
var tableStorageClient = new TableStorageClient(tableStorageConnectionString);
// get todoist sync token if available
var syncToken = "";
try
{
syncToken = tableStorageClient.Query<TodoistSync>()
.Where(x => x.PartitionKey == giosUserId)
.ToList()
.OrderByDescending(x => x.Timestamp)
.First().RowKey;
}
catch { }
// get all changed items from todoist
var response = await todoistClient.GetChangedItems(syncToken);
var changedItems = response.Items;
// filter out items by actions
var addItems = changedItems
.Where(x => !tableStorageClient
.Exists<TodoHabitLink>(giosUserId, x.Id) && x.Is_deleted == 0)
.ToList();
var updateItems = changedItems
.Where(x => tableStorageClient
.Exists<TodoHabitLink>(giosUserId, x.Id) && x.Is_deleted == 0 && x.Date_completed == null)
.ToList();
var completeItems = changedItems
.Where(x => x.Is_deleted == 0 && x.Date_completed != null)
.ToList();
var deleteItems = changedItems
.Where(x => tableStorageClient
.Exists<TodoHabitLink>(giosUserId, x.Id) && x.Is_deleted == 1)
.ToList();
// perform actions
foreach (var addItem in addItems)
{
var task = (await habiticaClient.CreateTask(TaskFromTodoistItem(addItem))).Data;
var link = new TodoHabitLink(giosUserId, addItem.Id, task.Id);
await tableStorageClient.InsertOrUpdate(link);
await tableStorageClient.InsertOrUpdate(link.Reverse());
}
foreach (var updateItem in updateItems)
{
var habiticaId = tableStorageClient.Query<TodoHabitLink>()
.Where(x => x.PartitionKey == giosUserId && x.RowKey == updateItem.Id)
.ToList().First().HabiticaId;
await habiticaClient.UpdateTask(TaskFromTodoistItem(updateItem, habiticaId));
}
foreach (var completeItem in completeItems)
{
var habiticaId = tableStorageClient.Query<TodoHabitLink>()
.Where(x => x.PartitionKey == giosUserId && x.RowKey == completeItem.Id)
.ToList().First().HabiticaId;
await habiticaClient.ScoreTask(habiticaId, ScoreAction.Up);
}
foreach (var deleteItem in deleteItems)
{
var habiticaId = tableStorageClient.Query<TodoHabitLink>()
.Where(x => x.PartitionKey == giosUserId && x.RowKey == deleteItem.Id)
.ToList().First().HabiticaId;
await habiticaClient.DeleteTask(habiticaId);
}
// store new todoist sync token
var todoistSync = new TodoistSync(giosUserId, response.Sync_token);
await tableStorageClient.InsertOrUpdate(todoistSync);
}
private static string GetHabiticaDifficulty(int todoistDifficulty)
{
switch (todoistDifficulty)
{
case 1:
return "0.1";
case 2:
return "1";
case 3:
return "1.5";
case 4:
return "2";
}
return null;
}
private static HabiticaTask TaskFromTodoistItem(Item item, string id = null)
{
var taskTypeStr = Enum.GetName(typeof(TaskType), TaskType.Todo).ToLower();
var task = new HabiticaTask
{
Id = id,
Text = item.Content,
Type = taskTypeStr,
Date = item.Due?.ToJavaScriptDateStr(),
Priority = GetHabiticaDifficulty(item.Priority)
};
return task;
}
private static void BuildConfig()
{
configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
}
}
}

View File

@@ -0,0 +1,3 @@
{
"version": "2.0"
}

View File

@@ -47,9 +47,9 @@ namespace Habitica.Todoist.Integration.Services
var result = await table.ExecuteAsync(operation);
return result.Result as T;
}
}
public async Task<bool> Exists<T>(string partitionKey, string rowKey) where T : TableEntity, new()
public async Task<bool> ExistsAsync<T>(string partitionKey, string rowKey) where T : TableEntity, new()
{
var tableName = typeof(T).Name.ToLower();
var table = tables[tableName];
@@ -60,17 +60,10 @@ namespace Habitica.Todoist.Integration.Services
return result.Result != null;
}
//public async Task<List<T>> Read<T>(string partitionKey, string rowKey) where T : TableEntity
//{
// var tableName = typeof(T).Name.ToLower();
// var table = tables[tableName];
// var operation = TableOperation.Retrieve(partitionKey, rowKey);
// var result = await table.ExecuteAsync(operation);
// return result.Result != null;
//}
public bool Exists<T>(string partitionKey, string rowKey) where T : TableEntity, new()
{
return Query<T>().Where(x => x.PartitionKey == partitionKey && x.RowKey == rowKey).ToList().Any();
}
public TableQuery<T> Query<T>() where T : TableEntity, new()
{

View File

@@ -16,6 +16,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Habitica.Todoist.Integration.Services", "Habitica.Todoist.Integration.Services\Habitica.Todoist.Integration.Services.csproj", "{A804D4CC-B5CC-466F-AF3D-E850B16D2D15}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Functions", "Functions", "{2DEF5574-4626-414C-9CF2-1FD6F3815B16}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Habitica.Todoist.Integration.Function.Sync", "Habitica.Todoist.Integration.Function.Sync\Habitica.Todoist.Integration.Function.Sync.csproj", "{9C825688-20BC-4580-8126-1E7320A8CC4D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -34,10 +38,17 @@ Global
{A804D4CC-B5CC-466F-AF3D-E850B16D2D15}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A804D4CC-B5CC-466F-AF3D-E850B16D2D15}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A804D4CC-B5CC-466F-AF3D-E850B16D2D15}.Release|Any CPU.Build.0 = Release|Any CPU
{9C825688-20BC-4580-8126-1E7320A8CC4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9C825688-20BC-4580-8126-1E7320A8CC4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C825688-20BC-4580-8126-1E7320A8CC4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C825688-20BC-4580-8126-1E7320A8CC4D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{9C825688-20BC-4580-8126-1E7320A8CC4D} = {2DEF5574-4626-414C-9CF2-1FD6F3815B16}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BE97C2C4-DA2B-45F1-BB8C-7662BE10E9DC}
EndGlobalSection