Skip to content

Conversation

@Faye-yufan
Copy link
Contributor

A function to put animint object on to github pages

@Faye-yufan Faye-yufan linked an issue Jul 13, 2023 that may be closed by this pull request
@tdhock
Copy link
Collaborator

tdhock commented Jul 17, 2023

looks like a good start. could you please add a test similar to what we were doing to test animint2gist?

@Faye-yufan
Copy link
Contributor Author

I encountered an issue when trying to use the animint2pages() function in my local environment.
A new repository is created via GitHub's API, it is created without any branches, which is different from creating a repository manually via GitHub's web interface, which creates a 'main' (formerly 'master') branch by default.

Also, the git2r::commit() function requires a 'commit' of S3 class git_commit, which is not created when initializing a new repository.

@Faye-yufan
Copy link
Contributor Author

Faye-yufan commented Aug 7, 2023

library(gh)
library(git2r)

# Create a new GitHub repository
gh("POST /user/repos", name = "testrepo")

# Define a temporary directory
tmp_dir <- tempfile()
if (!dir.exists(tmp_dir)) {
  dir.create(tmp_dir)
}

# echo "# animint2pages-test" >> README.md
# git init
repo <- git2r::init(tmp_dir)
config(repo, user.name = "Faye-yufan", user.email = "[email protected]")
readme_file_path <- file.path(tmp_dir, "README.md")
writeLines("# testrepo", readme_file_path)
# git add README.md
git2r::add(repo, "README.md")
# git commit -m "first commit"
git2r::commit(repo, "first commit")
# git branch -M main
git2r::branches(repo)
git2r::branch_rename(repository_head(repo), "main")
git2r::branches(repo)
# git remote add origin https://github.com/Faye-yufan/animint2pages-test.git
git2r::remote_add(repo, "origin", "https://github.com/Faye-yufan/animint2pages-test.git")
# git push -u origin main
git2r::push(repo, "origin", "refs/heads/main")
# Error in git2r::push(repo, "origin", "refs/heads/main") : 
#  Error in 'git2r_push': Unable to authenticate with supplied credentials

The above code is not working on my local, cannot push the local branch to the remote, because of this issue in git2r package. And I have the OpenSSH-formatted keys.
my local session info:

> sessioninfo::session_info()
─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23)
 os       macOS Monterey 12.3
 system   x86_64, darwin17.0
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/Los_Angeles
 date     2023-08-06
 rstudio  2022.07.1+554 Spotted Wakerobin (desktop)
 pandoc   2.19.2 @ /usr/local/bin/pandocPackages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 cli           3.6.0   2023-01-09 [1] CRAN (R 4.2.0)
 curl          5.0.0   2023-01-12 [1] CRAN (R 4.2.0)
 gh          * 1.4.0   2023-02-22 [1] CRAN (R 4.2.0)
 git2r       * 0.32.0  2023-04-12 [1] CRAN (R 4.2.0)
 gitcreds      0.1.2   2022-09-08 [1] CRAN (R 4.2.0)
 glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
 httr2         0.2.2   2022-09-25 [1] CRAN (R 4.2.0)
 jsonlite      1.8.4   2022-12-06 [1] CRAN (R 4.2.0)
 magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
 R6            2.5.1   2021-08-19 [1] CRAN (R 4.2.0)
 rappdirs      0.3.3   2021-01-31 [1] CRAN (R 4.2.0)
 rlang         1.1.0   2023-03-14 [1] CRAN (R 4.2.0)
 rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.0)
 sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)

 [1] /Library/Frameworks/R.framework/Versions/4.2/Resources/library

@tdhock
Copy link
Collaborator

tdhock commented Aug 7, 2023

as I understand, the linked issue only happens when you use a ssh key with no passphrase, is that right? does it work if you use a ssh key with a passphrase? (re-generate one with ssh-keygen)
by the way, you should always set a (strong/long/multi-word) passphrase for ssh keys, because if you do not, then anybody who gets access to that ssh key file can impersonate you)

@Faye-yufan
Copy link
Contributor Author

@tdhock Thank you for the information! It's new to me, but I'll certainly look into this and see how it affect my issue.

@Faye-yufan
Copy link
Contributor Author

I found this function use_github_pages() from package usethis. Maybe I can utilize this to implement our function.

@Faye-yufan
Copy link
Contributor Author

Faye-yufan commented Sep 1, 2023

In the above commit: I have transitioned our Git-related code to use the gert package and am using the HTTPS protocol only for now. I am still exploring how SSH will work. This transition includes updating functions for repo initialization, commits, branching, and pushing.

Here is an animint viz example I uploaded onto gh page using the function:

remotes::install_github("animint/animint2@1e7106855a2ed7a89c36e67b3ca71046b4d0138f")
library(animint2)
viz.point <- list(
  pointtwo = ggplot() + geom_point(
    data = mtcars,
    size = 10,
    aes(x=wt, y=mpg, fill=disp),
    clickSelects = "gear"))
animint2::animint2pages(viz.point, "animinttest0831-private", private = TRUE)

The output is on my own gh page: https://faye-yufan.github.io/animinttest0831-private/

Additionally, I use the original file structure to push onto github pages instead of flat structure in animint2gist()

Regarding test cases, I'm wondering how to compile them. From what I see in the test case of animint2gist(), it checks the class of the object and tests whether the error messages are prompted as expected. As of now, I can only think of testing the class of the output.

@Faye-yufan Faye-yufan marked this pull request as ready for review September 1, 2023 07:14
@tdhock
Copy link
Collaborator

tdhock commented Sep 1, 2023

hi @Faye-yufan this is great progress, thanks for sharing.
I am able to view it, is that normal? (I was expecting to not be able to view something that is "private" to you?)
I like how you implemented updating, if the repo already exists.
Also the messages that are printed to the R console are very user friendly, that is great overall, but please see below:

> animint2::animint2pages(viz.point, "animint-test-2023-08-31")
Error in libgit2::git_signature_default : 
  config value 'user.name' was not found
> gert::git_config_global_set("user.name", "Toby Dylan Hocking in R")
> animint2::animint2pages(viz.point, "animint-test-2023-08-31")
Error in libgit2::git_signature_default : 
  config value 'user.email' was not found
> gert::git_config_global_set("user.email", "[email protected]")
> animint2::animint2pages(viz.point, "animint-test-2023-08-31")
Looking up https credentials for https://github.com/tdhock/animint-test-2023-08-31.git
Transferred 3 of 3 objects...done!
[status] refs/heads/main: unchanged
[new]     f65d2ed4fbde0f35a59c refs/remotes/origin/main
Looking up https credentials for https://github.com/tdhock/animint-test-2023-08-31.git
Transferred 12 of 12 objects...done!
[status] refs/heads/gh-pages: unchanged
[new]     be602081f77981a1b2d8 refs/remotes/origin/gh-pages
Visualization will be available at https://tdhock.github.io/animint-test-2023-08-31
Deployment via GitHub Pages may take a few minutes...
[1] "C:/Users/th798/AppData/Local/Temp/RtmpkXwjqi/file237836fa5a74/"
> 
 viz.point <- animint( pointtwo = ggplot() + geom_point(    data = mtcars,    size = 10,    aes(x=wt, y=mpg, fill=disp)))
> > viz.point
Saving animint in temporary directory; specify output directory using animint(out.dir="path/to/directory")
opening a web browser with a file:// URL; if the web page is blank, try running
if (!requireNamespace("servr")) install.packages("servr")
servr::httd("C:/Users/th798/AppData/Local/Temp/RtmpkXwjqi/file237822cb329d")
> animint2::animint2pages(viz.point, "animint-test-2023-08-31")
Transferred 15 of 15 objects...done!
Checked out 1 of 1 commits... done!
Looking up https credentials for https://github.com/tdhock/animint-test-2023-08-31.git
Transferred 4 of 4 objects...done!
[status] refs/heads/gh-pages: unchanged
[updated] be602081f7..16b89cd249 refs/remotes/origin/gh-pages
Visualization will be available at https://tdhock.github.io/animint-test-2023-08-31
Deployment via GitHub Pages may take a few minutes...
[1] "C:/Users/th798/AppData/Local/Temp/RtmpkXwjqi/file237843685cff/"
> 

I got config value 'user.name' was not found etc, and I managed to figure out that the fix was to run gert::git_config_global_set("user.name", "Toby Dylan Hocking in R") but we should definitely write that in the documentation (I had to do a web search to figure that out).

This was on windows with https auth, but I can test on linux with ssh auth too.

for test cases, it would be ideal if we could actually run it and then parse the web page, like we do in the headless browser tests. not sure how complicated that would be to implement though? (waiting for the github pages to deploy to the web server is maybe too much? maybe we just test the presence of files in the gh-pages branch of the repo?)

@Faye-yufan
Copy link
Contributor Author

Hi @tdhock , thank you for taking the time to test and glad to hear the messages are helpful!

About the "private" repo visibility - Yes, the behavior you observed is normal. The argument private is intended to set the visibility of the repository, not the url/site. The GitHub Pages published site is still publicly accessible.

About the user.name and user.email Configuration - I appreciate you pointing out the need for setting up the Git configuration. I didn't encounter this issue probably because I had previously configured these settings via RStudio. I agree that we should include this step in the documentation.

Regarding test cases - testing the presence of files in the gh-pages branch of the repo seems like a practical approach, I will work on it :)

@tdhock
Copy link
Collaborator

tdhock commented Sep 2, 2023

on ubuntu I got

> animint2::animint2pages(viz.point, "animint-test-2023-08-31")
Le chargement a nécessité le package : gert
Erreur dans animint2::animint2pages(viz.point, "animint-test-2023-08-31") : 
  A GitHub token is required to create and push to a new repo.

@tdhock
Copy link
Collaborator

tdhock commented Sep 2, 2023

On Ubuntu I followed these instructions to make it work (we should add them to our docs).
I created a token by going to URL https://github.com/settings/tokens/new?scopes=repo&description=animint2pages
then after copying the token and pasting it into R, it works:

> gitcreds::gitcreds_set()

-> Your current credentials for 'https://github.com':

  protocol: https
  host    : github.com
  username: PersonalAccessToken
  password: <-- hidden -->

-> What would you like to do? 

1: Abort update with error, and keep the existing credentials
2: Replace these credentials
3: See the password / token

Sélection : 2

-> Removing current credentials...

? Enter new password or token: -> Adding new credentials...
-> Removing credentials from cache...
-> Done.
> animint2::animint2pages(viz.point, "animint-test-2023-08-31")
Transferred 15 of 15 objects...done!
Checked out 1 of 1 commits... done!
Looking up https credentials for https://github.com/tdhock/animint-test-2023-08-31.git
Transferred 4 of 4 objects...done!
[status] refs/heads/gh-pages: unchanged
[updated] 16b89cd249..5919d9762c refs/remotes/origin/gh-pages
Visualization will be available at https://tdhock.github.io/animint-test-2023-08-31
Deployment via GitHub Pages may take a few minutes...
[1] "/tmp/RtmpkEtUh3/file62f193eb38d9/"

@Faye-yufan
Copy link
Contributor Author

@tdhock Thank you so much for the detailed testing and analysis!
In animint2gist function, I guess it uses gistr::gist_create to handle authentication, and it takes the GitHub token in the environment.
Do you think we should incorporate these steps into the function as well? Like we could add a check for the GitHub token and prompt the user to set it up if it's not found. Or we could add these instructions to our documentation or the wiki page.

@tdhock
Copy link
Collaborator

tdhock commented Sep 7, 2023

it is probably best to leave token creation up to the user. but we instead of "A GitHub token is required to create and push to a new repo." we could have an error message that explains how to create a token and then use gitcreds::gitcreds_set(), is that reasonable?

@tdhock
Copy link
Collaborator

tdhock commented Nov 12, 2023

hi @Faye-yufan I just pushed some major changes which probably overlap with your work in #107
It is a big simplication in the JS code (removes lots of repeated logic, 200 lines of code, should be easier to maintain/modify) but maybe some tests will fail.
My changes in this PR are now very far away from the original intent of this branch (animint2pages), sorry about that (probably very difficult to review).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants