Skip to content

Commit d261069

Browse files
Merge pull request #61 from katilingban:dev
add functions for creating codeberg mirroring github actions
2 parents 19aadb6 + 3e7687c commit d261069

File tree

8 files changed

+130
-20
lines changed

8 files changed

+130
-20
lines changed

.github/workflows/mirror-codeberg.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ jobs:
1414
fetch-depth: 0
1515
- uses: yesolutions/mirror-action@master
1616
with:
17-
REMOTE: 'https://codeberg.org/katilingban/pakete.git'
17+
REMOTE: https://codeberg.org/katilingban/pakete
1818
GIT_USERNAME: ${{ secrets.CODEBERG_USERNAME }}
19-
GIT_PASSWORD: ${{ secrets.CODEBERG_TOKEN }}
19+
GIT_PASSWORD: ${{ secrets.CODEBERG_TOKEN }}

R/add_github_action.R

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
#' (*"netlify"*).
77
#' @param overwrite Logical. Should an existing GitHub Action be overwritten?
88
#' Default is FALSE.
9+
#' @param repo Short remote git repository name. If NULL, is determined based
10+
#' on current git settings. Evaluated only if `gha_name` is
11+
#' *"mirror-codeberg"*.
912
#'
1013
#' @returns A GitHub Action YAML file of the specified workflow to be added in
1114
#' `.github/workflows` directory.
@@ -17,7 +20,7 @@
1720
#' @rdname add_github_action
1821
#'
1922

20-
add_github_action <- function(gha_name = NULL, overwrite = FALSE) {
23+
add_github_action <- function(gha_name = NULL, repo = NULL, overwrite = FALSE) {
2124
cli::cli_h1("Add GitHub Actions workflow")
2225

2326
## Check gha_name ----
@@ -46,7 +49,7 @@ add_github_action <- function(gha_name = NULL, overwrite = FALSE) {
4649
## Get GitHub Actions YAML file name ----
4750
gha_file_name <- paste0(gha_name, ".yaml")
4851
gha_file_path <- file.path(".github/workflows", gha_file_name)
49-
52+
5053
## Check if file already exists ----
5154
if (file.exists(gha_file_path)) {
5255
cli::cli_alert_warning("{.strong {gha_name}} workflow already exists")
@@ -61,12 +64,14 @@ add_github_action <- function(gha_name = NULL, overwrite = FALSE) {
6164
FALSE
6265
} else {
6366
add_gha_workflow(
64-
gha_name, gha_file_name, gha_file_path, overwrite = overwrite
67+
gha_name, gha_file_name, gha_file_path,
68+
repo = repo, overwrite = overwrite
6569
)
6670
}
6771
} else {
6872
add_gha_workflow(
69-
gha_name, gha_file_name, gha_file_path, overwrite = overwrite
73+
gha_name, gha_file_name, gha_file_path,
74+
repo = repo, overwrite = overwrite
7075
)
7176
}
7277
}
@@ -87,7 +92,8 @@ choose_gha_workflow <- function(gha_name) {
8792
prompt <- cli::format_inline("Which action do you want to add? (0 to exit)\n")
8893

8994
workflows <- c(
90-
"netlify" = "Preview pkgdown website on pull request via Netlify"
95+
"netlify" = "Preview pkgdown website on pull request via Netlify",
96+
"mirror-codeberg" = "Mirror repository to Codeberg"
9197
)
9298

9399
options <- paste0(cli::style_bold(names(workflows)), ": ", workflows)
@@ -113,17 +119,24 @@ choose_gha_workflow <- function(gha_name) {
113119

114120
add_gha_workflow <- function(gha_name,
115121
gha_file_name,
116-
gha_file_path,
122+
gha_file_path,
123+
repo = NULL,
117124
overwrite) {
118125
cli::cli_h2("Adding {gha_name} workflow")
119126

120-
file.copy(
121-
from = system.file("actions", gha_file_name, package = "pakete"),
122-
to = ".github/workflows",
123-
overwrite = overwrite,
124-
recursive = FALSE
125-
)
126-
127+
## Mirror Codeberg workflow ----
128+
if (gha_name == "mirror-codeberg") {
129+
gha_text <- create_gha_workflow_codeberg_mirror(repo = repo)
130+
save_gha_workflow_codeberg_mirror(gha_text = gha_text)
131+
} else {
132+
file.copy(
133+
from = system.file("actions", gha_file_name, package = "pakete"),
134+
to = ".github/workflows",
135+
overwrite = overwrite,
136+
recursive = FALSE
137+
)
138+
}
139+
127140
if (overwrite) {
128141
cli::cli_bullets(
129142
c(
@@ -142,3 +155,62 @@ add_gha_workflow <- function(gha_name,
142155
)
143156
}
144157
}
158+
159+
#'
160+
#' @keywords internal
161+
#'
162+
163+
create_gha_workflow_codeberg_mirror <- function(repo = NULL) {
164+
## Create Codeberg mirror GHA workflow ----
165+
cli::cli_h2("Creating Codeberg mirror GitHub Actions workflow text")
166+
167+
## Get repo of repo is NULL ----
168+
if (is.null(repo)) repo <- get_github_repository()
169+
170+
## Create codeberg git url ----
171+
codeberg_remote <- file.path("https://codeberg.org", repo)
172+
173+
gha_text <- readLines(
174+
con = system.file("actions", "mirror-codeberg.yaml", package = "pakete")
175+
)
176+
177+
gha_text <- gha_text |>
178+
sub(pattern = "CODEBERG_REMOTE", replacement = codeberg_remote, x = _)
179+
180+
cli::cli_alert_success("{.strong Codeberg mirror GitHub Actions} text created.")
181+
gha_text
182+
}
183+
184+
185+
#'
186+
#' Save Codeberg mirror GitHub Actions workflow
187+
#'
188+
#' @param gha_text Codeberg mirror GitHub Actions workflow text to save into
189+
#' mirror-codeberg.yaml.
190+
#'
191+
#' @returns A mirror-codeberg.yaml file in the `.github/workflows` directory
192+
#'
193+
#' @examples
194+
#' if (FALSE) save_gha_workflow_codeberg_mirror(gha_text)
195+
#'
196+
#' @keywords internal
197+
#'
198+
199+
save_gha_workflow_codeberg_mirror <- function(gha_text) {
200+
## Save mirror-codeberg.yaml ----
201+
cli::cli_h2("Saving mirror-codeberg.yaml")
202+
203+
withr::with_output_sink(
204+
new = ".github/workflows/mirror-codeberg.yaml",
205+
code = writeLines(
206+
text = gha_text, con = ".github/workflows/mirror-codeberg.yaml"
207+
)
208+
)
209+
210+
cli::cli_bullets(
211+
c(
212+
"v" = "{.strong mirror-codeberg.yaml} successfully saved.",
213+
"i" = "File: {.file .github/working/mirror-codeberg.yaml}"
214+
)
215+
)
216+
}

inst/WORDLIST

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
CMD
22
CodeFactor
3+
Codeberg
34
Codecov
45
GHA
56
Katilingban
@@ -9,6 +10,7 @@ ORCID
910
WIP
1011
YAML
1112
Zenodo
13+
codeberg
1214
codecov
1315
cran
1416
github
@@ -19,4 +21,5 @@ pkgdown
1921
repostatus
2022
te
2123
wip
24+
yaml
2225
yml

inst/actions/mirror-codeberg.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ jobs:
1414
fetch-depth: 0
1515
- uses: yesolutions/mirror-action@master
1616
with:
17-
REMOTE: 'https://gitlab.com/spyoungtech/mirror-action.git'
18-
GIT_USERNAME: ernestguevarra
19-
GIT_PASSWORD: ${{ secrets.GIT_PASSWORD }}
17+
REMOTE: CODEBERG_REMOTE
18+
GIT_USERNAME: ${{ secrets.CODEBERG_USERNAME }}
19+
GIT_PASSWORD: ${{ secrets.CODEBERG_TOKEN }}

man/add_gha_workflow.Rd

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/add_github_action.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/save_gha_workflow_codeberg_mirror.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-add_badge.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Tests for add badge functions ------------------------------------------------
22

3+
skip_on_ci()
4+
35
test_that("add_badge function warnings and errors work as expected", {
46
expect_error(add_badge_status("moved"))
57
})

0 commit comments

Comments
 (0)