TruCol / Self-host-GitLab-CI-for-GitHub

Installs your own GitLab CI and runs it on all your GitHub repos, in a single command.
GNU Affero General Public License v3.0
4 stars 3 forks source link

Automatically ensure the machine gets push-access to a GitHub repository. #64

Closed a-t-0 closed 2 years ago

a-t-0 commented 2 years ago

Either set up ssh-access, or generate a personal access token (Ideally for a particular repository) to allow:

Option A.:

A temporary solution could be to create a test-organisation and to allow it to fill up to 100 repos with name: testname0,testname1,...99 before throwing an error asking the user to manually delete the repos. (perhaps aided with a command to delete them all at once). Disadvantage:

  1. Requires manual work. Advantage:
  2. Does not allow machine to auto delete anything.

Option B.:

  1. Create separate test-organisation.
  2. Grant personal access token to that organization.

Disadvantage:

  1. Pollutes GitHub account slightly.
  2. Requires more manual work (also from other users).
  3. If token is accidentally made in wrong organisation still allows for deletion

Advantage:

  1. Assumption: personal access token can be specified to a specific organization. Status:unknown.
  2. Does not allow machine to auto delete other/important repos (as long as token is made in the right organisation).

Option C.: Use specific deploy key to push to specific repository. Create that automatically instead. Assumption: can delete repo with deploy key. Status: unknown. Assumption: can create new repo with deploy key. Status: unknown.

Disadvantage:

  1. None found.
  2. May require user to create repo beforehand.
  3. Still allows deletion of incorrect repo, if token is made accidentally at wrong repo.

Advantage:

  1. No deletion of incorrect content.
  2. Requires a single website automation.

Option D.: Automatically create personal access token to allow creating repo and pushing to repo. Then automatically create deploy key to delete that specific repository.

Advantage:

  1. Is possible.

Disadvantage:

  1. Requires two website automations instead of 1.
a-t-0 commented 2 years ago

The following test from test_run_ci_on_github_repo.bats can be used to test if it works:

# TODO: write test that verifies this works on a new clean/empty repo.
# TODO: make this run after the loop over github branches.
@test "Test pushing GitHub commit build status to repo with build statusses is successful." {
    local github_username="a-t-0"
    local github_repo_name="sponsor_example"
    local github_branch_name="main"
    local github_commit_sha="85ad4b39fe9c9af893b4d7b35a76a595a8e680d5"

    copy_commit_build_status_to_github_status_repo "$github_username" "$github_repo_name" "$github_branch_name" "$github_commit_sha" "success"

    # TODO: write asserts
    # Assert svg file is created correctly
    assert_equal $(file_exists "$MIRROR_LOCATION/GitHub/$GITHUB_STATUS_WEBSITE_GLOBAL"/"$github_repo_name"/"$github_branch_name""/build_status.svg") "FOUND"

    # Assert GitHub commit build status txt file is created correctly
    assert_equal $(file_exists "$MIRROR_LOCATION/GitHub/$GITHUB_STATUS_WEBSITE_GLOBAL"/"$github_repo_name"/"$github_branch_name""/$github_commit_sha.txt") "FOUND"

    # Assert GitHub commit build status txt file contains the right data.
    assert_equal $(cat "$MIRROR_LOCATION/GitHub/$GITHUB_STATUS_WEBSITE_GLOBAL"/"$github_repo_name"/"$github_branch_name""/$github_commit_sha.txt") "success"

    push_commit_build_status_in_github_status_repo_to_github "$github_username"
    assert_success

    # Delete GitHub build status repository after test.
    sudo rm -r "$MIRROR_LOCATION/GitHub/$GITHUB_STATUS_WEBSITE_GLOBAL"
    assert_file_not_exist "$MIRROR_LOCATION/GitHub/$GITHUB_STATUS_WEBSITE_GLOBAL"
}

However, most likely a dedicated test would be better.

a-t-0 commented 2 years ago

Since there is no need to create a new repository, there is also no need to delete any repository. This means a GitHub deploy key with push access to GitHub should be sufficient to complete the objective. This objective is to push the build status to the GitHub build status repo.

The tests to get the build statuses and to determine whether those build statuses are pushed correctly, already exist. So the thing that is being built and tested here is the automatic creation of a deploy key for a particular Github repository. That repository is in this case hard-coded in: src/hardcoded_variables.txt. It has the name:

GITHUB_STATUS_WEBSITE_GLOBAL=gitlab-ci-build-statuses

Solution:

a-t-0 commented 2 years ago