Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinmoris committed Sep 30, 2018
2 parents 4e7f031 + 82596de commit bfd71f9
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 148 deletions.
12 changes: 8 additions & 4 deletions .psscripts/build-functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,16 @@ function Install-NetCoreSdk ($sdkZipPath)
The zip archive which contains the .NET Core SDK.
#>


$env:DOTNET_INSTALL_DIR = "$pwd\.dotnetsdk"
$env:DOTNET_INSTALL_DIR = [System.IO.Path]::Combine($pwd, ".dotnetsdk")
New-Item $env:DOTNET_INSTALL_DIR -ItemType Directory -Force

Add-Type -AssemblyName System.IO.Compression.FileSystem;
[System.IO.Compression.ZipFile]::ExtractToDirectory($sdkZipPath, $env:DOTNET_INSTALL_DIR)
if (Test-IsWindows) {
Expand-Archive -Path $sdkZipPath -DestinationPath $env:DOTNET_INSTALL_DIR
}
else {
Invoke-Cmd "tar -xzf $sdkZipPath -C $env:DOTNET_INSTALL_DIR"
}

$env:Path = "$env:DOTNET_INSTALL_DIR;$env:Path"
}

Expand Down
6 changes: 3 additions & 3 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

param
(
[string] $Run,
[switch] $Release,
[switch] $Run,
[switch] $ExcludeTests,
[switch] $Docker,
[switch] $Deploy,
Expand Down Expand Up @@ -57,7 +57,7 @@ if ($Docker.IsPresent -or $Deploy.IsPresent -or $env:APPVEYOR_REPO_TAG -eq $true
Invoke-Cmd "docker build -t dustinmoris/ci-buildstats:$version $publishFolder"
}

if ($Run.IsPresent)
if ($Run)
{
Write-Host "Launching application..." -ForegroundColor Magenta

Expand All @@ -67,7 +67,7 @@ if ($Run.IsPresent)
}
else
{
dotnet-run $app "debug"
dotnet-run $app $Run
}
}
elseif ($Deploy.IsPresent) # -or $env:APPVEYOR_REPO_TAG -eq $true) AppVeyor doesn't support Linux containers yet
Expand Down
89 changes: 45 additions & 44 deletions src/BuildStats/BuildHistoryCharts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ open System
open System.Net
open System.Net.Http
open System.Net.Http.Headers
open Microsoft.Extensions.Logging
open Microsoft.FSharp.Core.Option
open FSharp.Control.Tasks.V2.ContextInsensitive
open Newtonsoft.Json.Linq
open Giraffe
open FSharp.Control.Tasks.V2.ContextInsensitive
open BuildStats.Common
open BuildStats.HttpClients

// -------------------------------------------
// Common Types and Functions
Expand Down Expand Up @@ -78,8 +80,7 @@ module BuildMetrics =
// AppVeyor
// -------------------------------------------

[<RequireQualifiedAccess>]
module AppVeyor =
type AppVeyorHttpClient (httpClient : FallbackHttpClient) =

let parseToJArray (json : string) =
let obj = Json.deserialize json :?> JObject
Expand Down Expand Up @@ -114,12 +115,11 @@ module AppVeyor =
})
|> Seq.toList

let getBuilds (httpClient : HttpClient)
(authToken : string option) // ToDo
(slug : string * string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
member this.GetBuildsAsync (authToken : string option)
(slug : string * string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
task {
let account, project = slug
let branchFilter =
Expand All @@ -133,11 +133,11 @@ module AppVeyor =

let request = new HttpRequestMessage(HttpMethod.Get, url)

if (authToken.IsSome) then
if authToken.IsSome then
let token = AES.decryptUrlEncodedString AES.key authToken.Value
request.Headers.Authorization <- AuthenticationHeaderValue("Bearer", token)

let! json = Http.sendRequest httpClient request
let! json = httpClient.SendAsync request

return json
|> (Str.toOption
Expand All @@ -151,8 +151,7 @@ module AppVeyor =
// TravisCI
// -------------------------------------------

[<RequireQualifiedAccess>]
module TravisCI =
type TravisCIHttpClient (httpClient : FallbackHttpClient) =

let parseToJArray (json : string) =
let obj = Json.deserialize json :?> JObject
Expand Down Expand Up @@ -188,19 +187,19 @@ module TravisCI =
})
|> Seq.toList

let rec getBuilds (forceFallback : bool)
(httpClient : HttpClient)
(authToken : string option)
(slug : string * string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
let rec getBuildsAsync (forceFallback : bool)
(authToken : string option)
(slug : string * string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
task {
let account, project = slug

let request = new HttpRequestMessage()
request.Method <- HttpMethod.Get
request.Headers.Add("Travis-API-Version", "3")
request.Headers.TryAddWithoutValidation("User-Agent", "https://buildstats.info") |> ignore
request.Headers.TryAddWithoutValidation("User-Agent", "BuildStats.info-API") |> ignore

let topLevelDomain =
match forceFallback, authToken with
Expand Down Expand Up @@ -231,7 +230,7 @@ module TravisCI =

request.RequestUri <- new Uri(url)

let! json = Http.sendRequest httpClient request
let! json = httpClient.SendAsync request

let builds =
json
Expand All @@ -243,22 +242,27 @@ module TravisCI =
return builds
else
return!
getBuilds
getBuildsAsync
true
httpClient
authToken
slug
buildCount
branch
inclFromPullRequest
}

member __.GetBuildsAsync (authToken : string option)
(slug : string * string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
getBuildsAsync false authToken slug buildCount branch inclFromPullRequest

// -------------------------------------------
// CircleCI
// -------------------------------------------

[<RequireQualifiedAccess>]
module CircleCI =
type CircleCIHttpClient (httpClient : FallbackHttpClient) =

let parseToJArray (json : string) =
Json.deserialize json :?> JArray
Expand Down Expand Up @@ -292,12 +296,11 @@ module CircleCI =
})
|> Seq.toList

let getBuilds (httpClient : HttpClient)
(authToken : string option)
(slug : string * string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
member this.GetBuildsAsync (authToken : string option)
(slug : string * string)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
task {
let account, project = slug
let branchFilter =
Expand All @@ -312,7 +315,8 @@ module CircleCI =
sprintf "https://circleci.com/api/v1/project/%s/%s%s?limit=%i"
account project branchFilter limit

let! json = Http.getJson httpClient url
let request = new HttpRequestMessage(HttpMethod.Get, url)
let! json = httpClient.SendAsync request

return json
|> (Str.toOption
Expand All @@ -326,8 +330,7 @@ module CircleCI =
// Azure Pipelines
// -------------------------------------------

[<RequireQualifiedAccess>]
module AzurePipelines =
type AzurePipelinesHttpClient (httpClient : FallbackHttpClient) =

let parseToJArray (json : string) =
let obj = Json.deserialize json :?> JObject
Expand Down Expand Up @@ -362,12 +365,11 @@ module AzurePipelines =
})
|> Seq.toList

let getBuilds (httpClient : HttpClient)
(authToken : string option)
(slug : string * string * int)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
member this.GetBuildsAsync (authToken : string option)
(slug : string * string * int)
(buildCount : int)
(branch : string option)
(inclFromPullRequest : bool) =
task {
let account, project, definitionId = slug
let branchFilter =
Expand All @@ -376,14 +378,13 @@ module AzurePipelines =
| None -> ""

let limit = min 200 (4 * buildCount)

let apiVersion = "4.1"

let url =
sprintf "https://dev.azure.com/%s/%s/_apis/build/builds?api-version=%s&definitions=%i&$top=%i%s"
account project apiVersion definitionId limit branchFilter

let! json = Http.getJson httpClient url
let request = new HttpRequestMessage(HttpMethod.Get, url)
let! json = httpClient.SendAsync request

return json
|> (Str.toOption
Expand Down
5 changes: 2 additions & 3 deletions src/BuildStats/BuildStats.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<Version>5.3.0</Version>
<Version>5.4.0</Version>
<AssemblyName>BuildStats</AssemblyName>
<OutputType>Exe</OutputType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand All @@ -14,8 +14,6 @@
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.*" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.*" />
<PackageReference Include="Giraffe" Version="3.0.*" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="2.1.*" />
<PackageReference Include="Polly.Extensions.Http" Version="2.0.*" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.*" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.*" />
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="6.5.*" />
Expand All @@ -25,6 +23,7 @@

<ItemGroup>
<Compile Include="Common.fs" />
<Compile Include="HttpClients.fs" />
<Compile Include="BuildHistoryCharts.fs" />
<Compile Include="PackageServices.fs" />
<Compile Include="TextSize.fs" />
Expand Down
50 changes: 1 addition & 49 deletions src/BuildStats/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ module BuildStats.Common
open System
open System.IO
open System.Text
open System.Text.RegularExpressions
open System.Net
open System.Net.Http
open System.Security.Cryptography
open Newtonsoft.Json
open Microsoft.AspNetCore.Http
open Microsoft.AspNetCore.Authentication
open FSharp.Control.Tasks.V2.ContextInsensitive
open Polly
open Polly.Extensions.Http

// -------------------------------------
// String helper functions
Expand Down Expand Up @@ -43,29 +39,6 @@ module Json =
let deserialize (json : string) =
JsonConvert.DeserializeObject json

// -------------------------------------
// Http
// -------------------------------------

[<RequireQualifiedAccess>]
module Http =

let getJson (httpClient : HttpClient) (url : string) =
task {
let! response = httpClient.GetAsync url
match response.StatusCode with
| HttpStatusCode.OK -> return! response.Content.ReadAsStringAsync()
| _ -> return ""
}

let sendRequest (httpClient : HttpClient) (request : HttpRequestMessage) =
task {
let! response = httpClient.SendAsync request
match response.StatusCode with
| HttpStatusCode.OK -> return! response.Content.ReadAsStringAsync()
| _ -> return ""
}

// -------------------------------------
// Cryptography
// -------------------------------------
Expand Down Expand Up @@ -179,25 +152,4 @@ module Css =
|> getMinifiedContent
|> sb.AppendLine
) (new StringBuilder())
result.ToString()

// -------------------------------------
// HTTP Client
// -------------------------------------

[<RequireQualifiedAccess>]
module HttpClientConfig =
let defaultClientName = "DefaultHttpClient"

let tooManyRequestsPolicy =
Policy
.Handle<HttpRequestException>()
.OrResult(
fun (msg : HttpResponseMessage) ->
msg.StatusCode.Equals StatusCodes.Status429TooManyRequests)
.WaitAndRetryAsync(
[
TimeSpan.FromSeconds(1.0)
TimeSpan.FromSeconds(3.0)
TimeSpan.FromSeconds(5.0)
])
result.ToString()
Loading

0 comments on commit bfd71f9

Please sign in to comment.