Skip to content

Do not update to latest version if it is prerelase #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ jobs:

- name: Update tooling versions
run: |
# git config --local pull.ff only
# git config --local user.email "[email protected]"
# git config --local user.name "GitHub Action"
git config --local pull.ff only
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"

# git pull
# node ./dist/update.js
# git diff --quiet || git commit -a -m "Sync repository with latest tooling."
echo "Temporarily disabled"
git pull
node ./dist/update.js
git diff --quiet || git commit -a -m "Sync repository with latest tooling."

- name: Push changes
uses: ad-m/github-push-action@master
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/update.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
, "psci-support"
, "record"
, "versions"
, "monad-loops"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs", "test/**/*.purs" ]
Expand Down
96 changes: 70 additions & 26 deletions src/Setup/UpdateVersions.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Prelude

import Affjax as AX
import Affjax.ResponseFormat as RF
import Control.Monad.Rec.Class (untilJust)
import Data.Argonaut.Core (Json, jsonEmptyObject, stringify)
import Data.Argonaut.Decode (decodeJson, printJsonDecodeError, (.:))
import Data.Argonaut.Encode ((:=), (~>))
Expand All @@ -14,7 +15,7 @@ import Data.Array as Array
import Data.Either (Either(..), hush)
import Data.Foldable (fold)
import Data.Int (toNumber)
import Data.Maybe (Maybe(..), fromMaybe)
import Data.Maybe (Maybe(..), fromMaybe, isNothing)
import Data.String as String
import Data.Traversable (for, traverse)
import Data.Tuple (Tuple(..))
Expand All @@ -25,13 +26,14 @@ import Effect.Aff (Aff, Error, Milliseconds(..), delay, error, throwError)
import Effect.Aff.Retry (RetryPolicy, RetryPolicyM, RetryStatus(..))
import Effect.Aff.Retry as Retry
import Effect.Class (liftEffect)
import Effect.Ref as Ref
import GitHub.Actions.Core (warning)
import Math (pow)
import Node.Encoding (Encoding(..))
import Node.FS.Sync (writeTextFile)
import Node.Path (FilePath)
import Setup.Data.Tool (Tool(..))
import Setup.Data.Tool as Tool
import Text.Parsing.Parser (parseErrorMessage)

-- | Write the latest version of each supported tool
updateVersions :: Aff Unit
Expand Down Expand Up @@ -70,29 +72,71 @@ fetchLatestReleaseVersion tool = Tool.repository tool # case tool of
-- TODO: These functions really ought to be in ExceptT to avoid all the
-- nested branches.
fetchFromGitHubReleases repo = recover do
let url = "https://api.github.com/repos/" <> repo.owner <> "/" <> repo.name <> "/releases/latest"

AX.get RF.json url >>= case _ of
Left err -> do
throwError (error $ AX.printError err)

Right { body } -> case (_ .: "tag_name") =<< decodeJson body of
Left e -> do
throwError $ error $ fold
[ "Failed to decode GitHub response. This is most likely due to a timeout.\n\n"
, printJsonDecodeError e
, stringify body
]

Right tagStr -> do
let tag = fromMaybe tagStr (String.stripPrefix (String.Pattern "v") tagStr)
case Version.parseVersion tag of
Left e ->
throwError $ error $ fold
[ "Failed to decode tag from GitHub response: ", parseErrorMessage e ]

Right v ->
pure v
page <- liftEffect (Ref.new 1)
untilJust do
versions <- liftEffect (Ref.read page) >>= toolVersions repo
case versions of
Just versions' -> do
let version = Array.find (not <<< Version.isPreRelease) versions'
when (isNothing version) do
liftEffect $ void $ Ref.modify (_ + 1) page
pure version

Nothing ->
throwError $ error "Could not find version that is not a pre-release version"

toolVersions :: Tool.ToolRepository -> Int -> Aff (Maybe (Array Version))
toolVersions repo page = do
let
url = "https://api.github.com/repos/" <> repo.owner <> "/" <> repo.name <> "/releases?per_page=10&page=" <> show page
AX.get RF.json url
>>= case _ of
Left err -> throwError (error $ AX.printError err)
Right { body } -> case decodeJson body of
Left e -> do
throwError $ error
$ fold
[ "Failed to decode GitHub response. This is most likely due to a timeout.\n\n"
, printJsonDecodeError e
, stringify body
]
Right [] -> pure Nothing
Right objects ->
Just
<$> Array.catMaybes
<$> for objects \obj ->
case obj .: "tag_name" of
Left e ->
throwError $ error $ fold
[ "Failed to get tag from GitHub response: "
, printJsonDecodeError e
]
Right tagName ->
case tagStrToVersion tagName of
Left e -> do
liftEffect $ warning $ fold
[ "Got invalid version"
, tagName
, " from "
, repo.name
]
pure Nothing
Right version -> case obj .: "draft" of
Left e ->
throwError $ error $ fold
[ "Failed to get draft from GitHub response: "
, printJsonDecodeError e
]
Right isDraft ->
if isDraft
then pure Nothing
else pure (Just version)

tagStrToVersion tagStr =
tagStr
# String.stripPrefix (String.Pattern "v")
# fromMaybe tagStr
# Version.parseVersion

-- If a tool doesn't use GitHub releases and instead only tags versions, then
-- we have to fetch the tags, parse them as appropriate versions, and then sort
Expand All @@ -114,7 +158,7 @@ fetchLatestReleaseVersion tool = Tool.repository tool # case tool of

Right arr -> do
let
tags = Array.catMaybes $ map (\t -> hush $ Version.parseVersion $ fromMaybe t $ String.stripPrefix (String.Pattern "v") t) arr
tags = Array.catMaybes $ map (tagStrToVersion >>> hush) arr
sorted = Array.reverse $ Array.sort tags

case Array.head sorted of
Expand Down