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.
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
114120add_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+ }
0 commit comments