There is a way to batch archive GitHub repositories based off of a search? - github

From the answer to a related question I know it's possible to batch clone repositories based on a GitHub search result:
# cheating knowing we currently have 9 pages
for i in {1..9}
do
curl "https://api.github.com/search/repositories?q=blazor+language:C%23&per_page=100&page=$i" \
| jq -r '.items[].ssh_url' >> urls.txt
done
cat urls.txt | xargs -P8 -L1 git clone
I also know that the Hub client allows me to make API calls.
hub api [-it] [-X METHOD] [-H HEADER] [--cache TTL] ENDPOINT [-F FIELD|--input FILE]
I guess the last step is, how do I archive a repository with Hub?

You can update a repository using the Update a Repository API call.
I put all my repositories in a TMP variable in the following way, and ran the following:
echo $TMP | xargs -P8 -L1 hub api -X PATCH -F archived=true
Here is a sample of what the $TMP variable looked like:
echo $TMP
/repos/amingilani/9bot
/repos/amingilani/advent-of-code-2019
/repos/amingilani/alan
/repos/amingilani/annotate_models

Related

How to use github cli to auto pull all newly created or updated repos to local pc

How to use github cli to auto pull all newly created or updated repos to local pc?
I think I need to listen for new repo creation/updation and pull the repos. How to do it with cli?
If can't listen, I need to pull latest 100 repos to local machiene. How to do it?
I tried https://api.github.com/users/xxxx/repos?per_page=100. It gives in alphabetical order.
I use following code
#!/bin/sh
cat repolist.txt | while read line
do
REPOSRC=$line
LOCALREPO=$line
# We do it this way so that we can abstract if from just git later on
LOCALREPO_VC_DIR=$LOCALREPO/.git
if [ ! -d $LOCALREPO_VC_DIR ]
then
cd ~/xxxx
gh repo clone $REPOSRC
cd ~
else
cd ~
gh repo sync $REPOSRC -s $REPOSRC
fi
done
# End
The sort key
you're looking for seems to be sort=pushed.
Try curl -s 'https://api.github.com/users/xxxx/repos?sort=pushed&per_page=100' | jq '.[].name' to verify.

Bitbucket REST API to do search in remote private master repository

Is there any way I can search for a specific string in a private master repositories in butbucket.
Below is the code I use to get the clone command and then I download the files and do a grep on them now. But is it possible to search directlt? I need the output like
Full file Path, Search result (Line with the word I am searching for)
set -e
echo -n '' > clone-repos.sh
chmod +x clone-repos.sh
ONPREM_USER=user1
ONPREM_PASS=pass1
ONPREM_PROJECT=project1
curl -s -u "$ONPREM_USER:$ONPREM_PASS" https://bitbucket.bmogc.net/rest/api/1.0/projects/$ONPREM_PROJECT/repos/\?limit=1000 | ./jq-win64.exe -r '.values[] | {slug:.slug, links:.links.clone[] } | select(.links.name=="http") | "git clone \(.links.href) \(.slug)"' >> clone-repos.sh

Retrieving Details from GitHub Repos

I'm looking for a way to get both the version number and license details from a repo providing I have the URL for the Repo. I have a way at the moment that doesn't work for all repos I am reviewing but it's basically html scraping.
I assume there is an API example somewhere that pulls these details?
some random examples
https://github.com/Microsoft/Terminal
https://github.com/leoasis/redux-immutable-state-invariant
https://github.com/zeroclipboard/zeroclipboard
What version do you need ? If a package.json file is present, you can use it and get the version in it:
curl -sL https://raw.githubusercontent.com/leoasis/redux-immutable-state-invariant/master/package.json | jq -r '.version'
For the latest release tag name (aka version), use
curl -sL https://api.github.com/repos/Microsoft/Terminal/releases/latest | jq -r '.tag_name'
To retreive the license, use the Github API and go to https://api.github.com/repos/zeroclipboard/zeroclipboard/license, e.g.
curl -sL https://api.github.com/repos/zeroclipboard/zeroclipboard/license | jq -r '.license.name'
`

How can I batch archive all GitHub repositories in my account?

How do I batch archive my repositories? I'd preferably want to be able to sort through them and figure out a way to not archive my active repositories.
I have hundreds of old GitHub repositories in my account since before the GitHub notifications feature, and now I get vulnerability notifications for all of them. Here's what my notifications look, for projects that were last used maybe 6 years ago:
You can use the GitHub API along with two tools to achieve this. I'll be using:
Hub, but you can make direct API calls
jq, but you can use any JSON parser
Here's how:
Fetch a list of all the GitHub repositories in our account, and saving them in a file:
hub api --paginate users/amingilani/repos | jq -r '.[]."full_name"' > repos_names.txt
Go through that file manually, remove any repositories you don't want to archive
Archive all the repositories in the file:
cat repos_names.txt | xargs -I {} -n 1 hub api -X PATCH -F archived=true /repos/{}
Note: since 2020:
gh repo list has been released (with gh 1.7.0 in commit 00cb921, Q1 2021): it does take pagination in account, as it is similar to an alias like:
set -e
repos() {
local owner="${1?}"
shift 1
gh api graphql --paginate -f owner="$owner" "$#" -f query='
query($owner: String!, $per_page: Int = 100, $endCursor: String) {
repositoryOwner(login: $owner) {
repositories(first: $per_page, after: $endCursor, ownerAffiliations: OWNER) {
nodes {
nameWithOwner
description
primaryLanguage { name }
isFork
pushedAt
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
' | jq -r '.data.repositoryOwner.repositories.nodes[] | [.nameWithOwner,.pushedAt,.description,.primaryLanguage.name,.isFork] | #tsv' | sort
}
repos "$#"
gh repo list --no-archived can limit the list to your not-yet-archived repositories
gh repo archive can then, for each element of that list, archive the GitHub repository.
wolfram77 also proposes in the comments:
gh repo list <org> | awk '{NF=1}1' | \
while read in; do gh repo archive -y "$in"; done
Using only gh.
gh repo list --no-archived --limit 144 --visibility public --source --json nameWithOwner --jq ".[].nameWithOwner" > repos_names.txt
Set --limit to the number of repositories you have.
Use vim to delete line which you don't want to archive by pressing dd on the line:
vim repos_names.txt
Run the the following command to arrive them
cat repos_names.txt | while read in; do gh repo archive -y "$in"; done
Clear after:
rm repos_names.txt

Transfer all GitHub repositories from one user to another user

Is there a way to transfer all GitHub repositories owned by one user to another user? Is this functionality accessible by an admin (eg. on Enterprise, if the user can no longer access GitHub)?
GitHub has a convenient command-line tool: hub found at https://hub.github.com
I've written an example to move all repos from all your organisations to my_new_organisation_name:
#!/usr/bin/env bash
orgs="$(hub api '/user/orgs' | jq -r '.[] | .login')";
repos="$(for org in $orgs; do hub api '/orgs/'"$org"'/repos' | jq -r '.[] | .name';
done)"
for org in $orgs; do
for repo in $repos; do
( hub api '/repos/'"$org"'/'"$repo"'/transfer'
-F 'new_owner'='my_new_organisation_name' | jq . ) &
done
done
For users rather than organisation, set my_new_organisation_name to the replacement username, remove the outer loop, and replace the repos= line with:
repos="$(hub api /users/SamuelMarks/repos | jq -r '.[] | .name')"
EDIT: Found a GUI if you prefer https://stackoverflow.com/a/54549899