Skip to content

Commit

Permalink
Add github draft test fs to build.fsproj
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Jul 24, 2022
1 parent e8d55b9 commit 9b78251
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
13 changes: 12 additions & 1 deletion build/Build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,23 @@ let _preRelease =
// "PreReleaseNoDocs"
// [setPrereleaseTag; clean; build; runTests; packPrerelease; createPrereleaseTag; publishNugetPrerelease]

let createTag = BuildTask.createFn "releasenotes" [] (fun target ->
let releaseNotes = BuildTask.createFn "releasenotes" [] (fun target ->
ReleaseNotes.ReleaseNotes.ensure()
ReleaseNotes.ReleaseNotes.update(ProjectInfo.gitOwner, ProjectInfo.project, target)
)

let githubDraft = BuildTask.createFn "draft" [] (fun target ->

let body = ""

GithubDraft.Github.draft(
ProjectInfo.gitOwner,
ProjectInfo.project,
(Some body),
None,
target
)
)


[<EntryPoint>]
Expand Down
98 changes: 98 additions & 0 deletions build/GithubDraft.Test.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module GithubDraft

open Fake.Core
open Fake.IO
open Fake.Api

/// Module used to draft a new GitHub release.
module Github =

/// Zip all files in relative 'sourcePath' and places zip file 'fileName' in 'targetPath'
let zipAssets(sourcePath:string, targetPath:string, fileName:string) =

let assetPath = System.IO.Path.Combine(__SOURCE_DIRECTORY__,sourcePath)
let assetDir = Fake.IO.DirectoryInfo.ofPath assetPath

let files =
let assetsPaths = Fake.IO.DirectoryInfo.getFiles assetDir
assetsPaths |> Array.map (fun x -> x.FullName)

let outputPath = Path.combine targetPath fileName

Zip.zip assetDir.FullName outputPath files

/// Draft Github release with latest release notes.
/// 'owner' = Github repo owner
/// 'repoName' = Github repository name
/// 'releaseBody' = text for draft body, will be shown above latest release notes
/// 'assetPath' = path to .zip file added to Github draft
///
/// Needs Github Token passed as 'token:my-github-token' or environmental variabe 'github_token'
let draft(
owner:string,
repoName:string,
releaseBody:string option,
assetPath: string option,
config:TargetParameter
) =

let prevReleaseNotes = Fake.IO.File.read "RELEASE_NOTES.md"
let takeLastOfReleaseNotes =
let findInd =
prevReleaseNotes
|> Seq.indexed
|> Seq.choose (fun (i,x) -> if x.StartsWith "###" then Some i else None)
match Seq.length findInd with
| 1 ->
prevReleaseNotes
| x when x >= 2 ->
let indOfSecondLastRN = findInd|> Seq.item 1
Seq.take (indOfSecondLastRN - 1) prevReleaseNotes
| _ ->
failwith "Previous RELEASE_NOTES.md not found or in wrong formatting"

let bodyText =
[
if releaseBody.IsSome then releaseBody.Value
yield! takeLastOfReleaseNotes
]

let tokenOpt =
config.Context.Arguments
|> List.tryFind (fun x -> x.StartsWith "token:")

let release = ReleaseNotes.load "RELEASE_NOTES.md"
let semVer = (sprintf "v%i.%i.%i" release.SemVer.Major release.SemVer.Minor release.SemVer.Patch)

let token =
match Environment.environVarOrDefault "github_token" "", tokenOpt with
| s, None when System.String.IsNullOrWhiteSpace s |> not -> s
| s, Some token when System.String.IsNullOrWhiteSpace s |> not ->
Trace.traceImportant "Environment variable for token and token argument found. Will proceed with token passed as argument 'token:my-github-token'"
token.Replace("token:","")
| _, Some token ->
token.Replace("token:","")
| _, None ->
failwith "please set the github_token environment variable to a github personal access token with repro access or pass the github personal access token as argument as in 'token:my-github-token'."

let files (assetPath:string) =
let assetDir = Fake.IO.DirectoryInfo.ofPath assetPath
/// This only accesses files and not folders. So in this case it will only access the .zip file created by "ZipAssets"
let assetsPaths = Fake.IO.DirectoryInfo.getFiles assetDir
assetsPaths |> Array.map (fun x -> x.FullName)

let draft =
if assetPath.IsSome then

GitHub.createClientWithToken token
|> GitHub.draftNewRelease owner repoName semVer (release.SemVer.PreRelease <> None) bodyText
|> GitHub.uploadFiles (files assetPath.Value)
|> Async.RunSynchronously

else

GitHub.createClientWithToken token
|> GitHub.draftNewRelease owner repoName semVer (release.SemVer.PreRelease <> None) bodyText
|> Async.RunSynchronously

Trace.trace "Draft successfully created!"
2 changes: 2 additions & 0 deletions build/build.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="GithubDraft.Test.fs" />
<Compile Include="ReleaseNotes.Test.fs" />
<Compile Include="Helpers.fs" />
<Compile Include="MessagePrompts.fs" />
Expand All @@ -28,6 +29,7 @@
<PackageReference Include="Fake.DotNet.MSBuild" Version="5.23.0-alpha002" />
<PackageReference Include="Fake.IO.FileSystem" Version="5.23.0-alpha002" />
<PackageReference Include="Fake.Tools.Git" Version="5.23.0-alpha002" />
<PackageReference Include="Fake.IO.Zip" Version="5.23.0-alpha002" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 9b78251

Please sign in to comment.