How to create new repository via ssh? - github

I would like to use the opportunity of passwordless access which is provided by ssh keys to create new repositories, instead of opening the browser and logging in git hosting provider by entering username and password each time. How to create new repository via ssh?

The GitHub API does provide a mechanaism for project creation, currently in alpha status. See https://developer.github.com/v3/projects/#create-a-repository-project

I use this /usr/local/bin/github
#!/bin/dash
# Create a personal access token here
#
# https://github.com/settings/tokens
#
# and make sure it has the 'repo' scope.
TOKEN=`cat /home/user/github_token`
REPO=$2
curl -s -XPOST -H 'Authorization: token '$TOKEN https://api.github.com/user/repos -d '{"name":"'$REPO'"}'|egrep -w 'already|"name":'|grep -o '.*[^,]'|cut -d: -f2|perl -pe 's/^ "(.*)"/$1/;s/name already exists on this account/already exists/'|perl -pe "s/$REPO/successfully created/"
It is run as github init myrepo. It is useful to create and fork projects from CLI.

Related

Getting "not found" after authenticating when trying to initiate GitHub workflow via REST

I am trying to trigger the workflow_dispatch action for a GitHub workflow via REST but I am getting a "not found" error.
My question is similar to this one but the difference is that I am still getting the "not found" error even though the header indicates I am authenticated (the rate limit has increased to 5,000).
Here's my script:
#!/bin/bash
# https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
OWNER='myGithubOrganization'
REPO='myRepo'
WORKFLOW_ID='main.yml'
POST_URL="https://api.github.com/repos/$OWNER/$REPO/actions/workflows/$WORKFLOW_ID/dispatches"
echo "Calling $POST_URL"
GITHUB_PERSONAL_ACCESS_TOKEN=$(echo "$PLATFORM_VARIABLES" | base64 --decode | jq '.GITHUB_PERSONAL_ACCESS_TOKEN' --raw-output)
# -i to include headers.
curl \
-i \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $GITHUB_PERSONAL_ACCESS_TOKEN" \
$POST_URL \
-d '{"ref":"ref"}'
In the headers, I see the rate limit has increased to 5,000, so I know I am logged in.
The personal access token has the following permissions:
repo
workflow
admin:org_hook
The personal access token is for a machine user.
In the repo settings, under "Collaborators and teams", the machine user account has the "Read" role.
What more do I need to do to trigger the workflow?
The machine user needs to have write access, not read access.
This is true even if the workflow does something like run CI tests and does not write any code.

Upload username and password to rundeck key storage using CLI / REST?

I want to use username and password in Rundeck to run jobs on node instead of public / private keys. How do I do it?
Rundeck CLI always asks for the user and password by default, also, you can define environments vars RD_USER and RD_PASSWORD in your .bashrc file. Take a look at this (Credentials section).
Example:
export RD_USER=username
export RD_PASSWORD=password
Using API you can use use the "cookie way" to access an endpoint, take a look at this.
And check the following example:
#!/bin/sh
curl -v -c cookie -b cookie -d j_username=admin -d j_password=admin http://localhost:4440/j_security_check \
-H "Accept: application/json" \
http://hyperion:4440/api/31/system/info/

Downloading an artifact from actions on someone else's repository

This problem seems really simple. I'm trying to download a pre-built binary for a program from somebody's github repo.
They suggest that I can do this:
"You can also download pre-built binaries from GitHub Actions artifacts."
But it turns out that I have no idea what that means!
The file is not in the actions page (I'm not sure if it should be) and when I try:
curl -H "Accept: application/vnd.github.v3+json" https://api.github.com/repos/nbdd0121/wsld/actions/artifacts/42682371/zip
I receive { "message": "You must have the actions scope to download artifacts.", "documentation_url": "https://docs.github.com/rest/reference/actions#download-an-artifact" }
But this documentation implies that I need to Build an app in order to download a file???
Can this possibly be correct?
What am I missing here?
This works for me:
curl.exe `
--netrc-file C:\Users\Steven\_netrc `
-L `
-o wsldhost.exe.zip `
https://api.github.com/repos/nbdd0121/wsld/actions/artifacts/42682371/zip
Where _netrc looks like this:
default login <USERNAME> password <PERSONAL ACCESS TOKEN>
https://github.com/actions/upload-artifact/issues/89
You can also directly use Basic auth. In curl:
curl -L -u <USERNAME>:<PERSONAL_ACCESS_TOKEN> -o artifact.zip https://api.github.com/repos/nbdd0121/wsld/actions/artifacts/42682371/zip
And the personal access token is generated at: https://github.com/settings/tokens

How do you create a project in a specific group via GitLab API?

I tried to use GitLab API to create a new project. This worked, but this is in my user space:
curl \
--header "Authorization: Bearer ${GITLAB_API_TOKEN}" \
--request POST \
"https://gitlab.com/api/v4/projects/?name=$test-proj"
But I wanted to do it under a specific group with group_id <group_id> (I blanked it here). The most sensible approach that occured to me was:
curl \
--header "Authorization: Bearer ${GITLAB_API_TOKEN}" \
--request POST \
"https://gitlab.com/api/v4/groups/<group_id>/projects/?name=test-proj
But this did not work. Are there any suggestions on how I could achieve this?
I consulted the following references
https://forum.gitlab.com/t/create-a-new-project-in-a-group-using-api/1552
https://docs.gitlab.com/ee/api/projects.html#create-project
The GitLab documentation mentions the path or namespace_id attribute (although I would actually be in search of a group_id attribute). I am not sure about path and how to specify that. I tried - without success - to retrieve the namespace_id via
curl --header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" "https://gitlab.example.com/api/v4/namespaces"
It is well possible that I might not have the rights to do the required operation. Therefore a reference to an official (GitLab) documentation together with a test curl command that works would be very helpful to me - thank you!
For anyone looking for the direct command:
STEP 1:
Navigate as below and identify the group ID from GUI:
Admin Area -> Groups -> GroupName
STEP 2:
Construct the command with two parameters viz., name, namespace_id
curl --header "PRIVATE-TOKEN: <myprivatetoken>" -X POST "https://gitlab.com/api/v4/projects?name=myexpectedrepo&namespace_id=38"
Both users and groups are considered "namespaces" as far as the Gitlab API is concerned, so you can use either a group ID or a username in the namespace_id field. You can see this in use by getting a single namespace with either a Group ID (that you can see in the /groups API call, or from a Group's "profile" page) or a username:
# this will show the namespace details of the Group with ID 54
curl --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/namespaces/54
# this will show the namespace details of the User with username my-username
curl --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/namespace/my-username
If you have the appropriate access level, you can assign a git remote to your local repository that includes your group name. Then, the push event will trigger GitLab to create the remote repository.
$ mkdir project_name && cd $_
$ echo "# project_name" > README.md
$ git init; git add .; git commit -m initial
$ GITLAB_HOST="gitlab.com"
$ git remote add origin git#${GITLAB_HOST}:group_name/project_name.git
$ git push origin HEAD
Then, point your browser to ${GITLAB_HOST}/group_name/project_name
https://docs.gitlab.com/ee/user/group/#specify-who-can-add-projects-to-a-group

cannot access git reposoitory

any idea why this does not work:
D:\apache-tomcat-8.0.33\bin>"C:\Program Files\Git\bin\git.exe" -c core.askpass=true ls-remote -h https://tobias#wdmycloud/shares/githome/Repo.git HEAD
fatal: repository 'https://tobias#wdmycloud/shares/githome/Repo.git/' not found
I can successfully clone this repository using eclipse:
In Eclipse, you're using SSH, and on the commandline you're using HTTPS. Those are two different protocols, and the fact that one works doesn't necessarily mean the other will work, too. Try SSH URL instead:
"C:\Program Files\Git\bin\git.exe" -c core.askpass=true ls-remote -h ssh://tobias#wdmycloud/shares/githome/Repo.git HEAD
Regarding WD MyCloud, you will find tutorial to access a git repo through ssh, which is fairly easy to setup, considering all you need is a sshd running.
But for an https url to work, you would need an Apache server running, properly configured to support git http-backend.
Something along the line of an httpd.conf including:
# Configure Git HTTP Backend
SetEnv GIT_PROJECT_ROOT /www/example.com/git
SetEnv GIT_HTTP_EXPORT_ALL
# Note: Serve static files directly
AliasMatch ^/(.*/objects/[0-9a-f]{2}/[0-9a-f]{38})$ /var/www/git/$1
AliasMatch ^/(.*/objects/pack/pack-[0-9a-f]{40}.(pack|idx))$ /var/www/git/$1
# Note: Serve repository objects with Git HTTP backend
ScriptAliasMatch \
"(?x)^/(.*/(HEAD | \
info/refs | \
objects/info/[^/]+ | \
git-(upload|receive)-pack))$" \
/usr/libexec/git-core/git-http-backend/$1
And that is not standard, unless you set it up beforehand yourself.