Skip to content
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

Allow force push #213

Merged
merged 1 commit into from
Mar 3, 2024
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
7 changes: 7 additions & 0 deletions src/main/scala/githubpages/GitHubPagesKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ trait GitHubPagesKeys {
s"""If not found, the default value is "Updated $${gitHubPagesBranch.value}""""
)

lazy val gitHubPagesPublishForcePush: SettingKey[Boolean] =
settingKey[Boolean](
"Whether to force push to the specified branch when publish to GitHub Pages. " +
"First, it tries to get the value from the environment variable 'GITHUB_PAGES_PUBLISH_FORCE_PUSH'. " +
"If not found, the default value is false"
)

val DefaultGitHubPagesPublishRequestTimeout: FiniteDuration = org.http4s.client.defaults.RequestTimeout

lazy val gitHubPagesPublishRequestTimeout: SettingKey[FiniteDuration] =
Expand Down
14 changes: 11 additions & 3 deletions src/main/scala/githubpages/GitHubPagesPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ object GitHubPagesPlugin extends AutoPlugin {
siteDir: Data.SiteDir,
dirFilter: File => Boolean,
isText: Data.IsText,
headers: Map[String, String]
headers: Map[String, String],
forcePush: Boolean,
): F[Either[GitHubError, Option[Ref]]] =
EitherT(
FileF.getAllDirsRecursively(
Expand Down Expand Up @@ -102,7 +103,8 @@ object GitHubPagesPlugin extends AutoPlugin {
commitMessage,
dirs,
isText,
headers
headers,
forcePush
)(gitHubApiConfig)
)
} yield result
Expand All @@ -121,11 +123,15 @@ object GitHubPagesPlugin extends AutoPlugin {
returnOrThrowMessageOnlyException(headers)(errorMessage)
}

private lazy val forcePushStr = sys.env.getOrElse("GITHUB_PAGES_PUBLISH_FORCE_PUSH", "")

override lazy val globalSettings: Seq[Def.Setting[_]] = Seq(
gitHubPagesBranch := "gh-pages",
gitHubPagesNoJekyll := true,
gitHubPagesPublishCommitMessage :=
sys.env.getOrElse("GITHUB_PAGES_PUBLISH_COMMIT_MESSAGE", s"Updated ${gitHubPagesBranch.value}"),
gitHubPagesPublishForcePush :=
forcePushStr == "1" || forcePushStr.equalsIgnoreCase("true")
)

override lazy val projectSettings: Seq[Def.Setting[_]] = Seq(
Expand Down Expand Up @@ -163,6 +169,7 @@ object GitHubPagesPlugin extends AutoPlugin {
val commitMessage = Data.CommitMessage(gitHubPagesPublishCommitMessage.value)
val dirsToIgnore = gitHubPagesDirsToIgnore.value
val ignoreDotDirs = gitHubPagesIgnoreDotDirs.value
val forcePush = gitHubPagesPublishForcePush.value
@SuppressWarnings(Array("org.wartremover.warts.PlatformDefault"))
val dirFilter =
if (ignoreDotDirs)
Expand Down Expand Up @@ -234,7 +241,8 @@ object GitHubPagesPlugin extends AutoPlugin {
siteDir,
dirFilter,
GitHubApi.buildIsText(blobConfig),
GitHubApi.essentialHeaders
GitHubApi.essentialHeaders,
forcePush
).eitherT
} yield result).value
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/scala/githubpages/github/GitHubApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ object GitHubApi {
branch: Data.Branch,
commitSha: Data.CommitSha,
headers: Map[String, String],
force: Boolean,
): EitherT[F, GitHubError, Ref] =
EitherT(
processResponse(
Expand All @@ -88,7 +89,7 @@ object GitHubApi {
gitHubRepo.repo.repo,
s"heads/${branch.branch}",
commitSha.commitSha,
force = false,
force = force,
headers,
)
)
Expand Down Expand Up @@ -284,6 +285,7 @@ object GitHubApi {
allDirs: NonEmptyVector[File],
isText: Data.IsText,
headers: Map[String, String],
forcePush: Boolean = false,
)(implicit githubConfig: GithubConfig): F[Either[GitHubError, Option[Ref]]] = (for {
github <- EitherT.rightT[F, GitHubError](Github[F](client, gitHubRepoWithAuth.accessToken.map(_.accessToken)))
commitInfo = Data.CommitInfo(gitHubRepoWithAuth.gitHubRepo, branch, commitMessage)
Expand All @@ -302,7 +304,7 @@ object GitHubApi {
)
refCommit <- updateCommitFiles(github, commitInfo, baseDir, allFiles, isText, none[Data.CommitSha], headers)
headRef <-
refCommit.traverse(commitSha => updateHead(github, gitHubRepoWithAuth.gitHubRepo, branch, commitSha, headers))
refCommit.traverse(commitSha => updateHead(github, gitHubRepoWithAuth.gitHubRepo, branch, commitSha, headers, forcePush))
} yield headRef).value

@SuppressWarnings(Array("org.wartremover.warts.ExplicitImplicitTypes"))
Expand All @@ -315,9 +317,10 @@ object GitHubApi {
allDirs: NonEmptyVector[File],
isText: Data.IsText,
headers: Map[String, String],
forcePush: Boolean = false,
)(gitHubApiConfig: GitHubApiConfig): F[Either[GitHubError, Option[Ref]]] = {
implicit val githubConfig = GitHubApiConfig.toGithubConfig(gitHubApiConfig)
commitAndPush0(client, gitHubRepoWithAuth, branch, baseDir, commitMessage, allDirs, isText, headers)
commitAndPush0(client, gitHubRepoWithAuth, branch, baseDir, commitMessage, allDirs, isText, headers, forcePush)
}

@SuppressWarnings(Array("org.wartremover.warts.ImplicitParameter"))
Expand Down
24 changes: 24 additions & 0 deletions website/docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,30 @@ gitHubPagesPublishCommitMessage :=
e.g.) If you want to have a different message, you can change it.
```scala
gitHubPagesPublishCommitMessage := s"New stuff in my awesome website!!!"


### Publish Force Push
| Name | Value Type | Default |
| ------------------------------ | ---------- | ------------------------------------------------------------------------------------- |
| `gitHubPagesPublishForcePush` | `Boolean` | ENV VAR `GITHUB_PAGES_PUBLISH_FORCE_PUSH` or false |

The commit message when publish to GitHub Pages.

First, it tries to get the value from the environment variable `GITHUB_PAGES_PUBLISH_FORCE_PUSH`.
If not found, the default value is false

Default:
```scala
gitHubPagesPublishCommitMessage :=
sys.env.getOrElse(
"GITHUB_PAGES_PUBLISH_FORCE_PUSH",
false
)
```

e.g.) If you want to, you can enable force push.
```scala
gitHubPagesPublishForcePush := true
```

## Use GitHub Enterprise
Expand Down