Get git remote info using NodeJS - node.js

I have to get remote git information using NodeJS. I have managed to get info from a cloned repo using simple-git. The code I have test is the following:
require('simple-git')('/my/local/git/repo/path')
.pull()
.tags(function(err, tags) {
console.log("These are my tags: %s", tags.all);
});
However, it requires the repo to be locally cloned. Is there any way (using this or another module) to connect the remote git to get this info?

You need to specify the path to any valid repo or leave it empty considered git repo is under current directory. That's just required to make commands worked.
After that you can use listRemote method in the following way:
require('simple-git')([optional path])
// .init() - in case it's totally empty folder
.addRemote('remote_repo_alias', 'path/to/remote/repo')
.listRemote(['--tags', 'remote_repo_alias'], function(err, tags) {
// ...
});

Related

Pushing a respository I initialised with git init inside another repository to github

I am currently doing course called fullstackopen for which I created a repository on Github called fso and cloned it locally using ssh. Inside fso, I created directories for different parts(part1, part2) and created react projects inside them (using create-react-app). I pushed them to github without any problems.
For part3, the course asked to create a new repository for the backend(node js). I created this repo inside fso/part3 using git init and initialised a node app called phonebook. Now, when I tried to push it to Github, I got this:
enter image description here
So, I added my github repo using:
git remote add origin
After this when I tried to push again, I was prompted for my username and password but support for password authentication has been removed. I tried pushing using personal access tokens and got this:
enter image description here
Can I run the following in my part3/phonebook (phonbook-backend) directory?
git pull origin master git push origin master
I'm not sure if this would work, I dont want to lose my work.
Edit: i tried git pull origin main --allow-unrelated-histories and got this
pushing after this results in the same error
this is what my directory structure looks like locally. Im trying to push part3 to my github repo
Your last error is 'updates were rejected because the remote contains work'
This happens when your repository gets initialized with additional files like README or GITIGNORE. To resolve this, first you need to pull your changes from server, so you can use below command:
'git pull origin main --allow-unrelated-histories'
Then you can push your changes to server using below command:
'git push -f origin main'

Node.js - Clone .git repository using a personal access token and simple-git

I have a Node.js app. I want to clone a private repository from GitHub using this app. I have access to this repository. In an attempt to clone the repository from my Node.js app, I'm using simple-git, with the following code:
const git = simpleGit();
git.clone('[repository-url]', './repository');
This code runs successfully. A directory named repository with the following structure is created:
/repository
/.git
.gitignore
However, this is not the contents of the repository. I successfully cloned a public repository using this code. This makes me believe that it's an authentication issue, even though no error is being shown. I created a Personal Access Token and stored it in an environment variable. However, I can't figure out how to actually use that Personal Access Token when I clone my private repository.
How do I clone a private repository using a Personal Access Token using simple-git?
You can use the config plugin to add your access token as a custom header:
const git = simpleGit({
config: [
`Authorization: token ${TOKEN}`
]
});
You can also set up a .netrc file with your auth details which simple-git will pick up by default.

I can not use private repo as npm dependency in circleci deploys

I'm using circle ci to deploy a serverless built in nodejs. And I added as dependency of the main repo,a private github repo. E.g:
// package.json
.....
"dependencies": {
"my-private-github-repo": "git+ssh://git#github.com:company-name/my-private-github-repo.git",
.....
},
.....
The problem is that I need to give access the deploy process to read and clone the private repo when npm install runs
I have configured my ssh user-keys in circle ci,I followed the steps in this documentation: creating-a-bitbucket-user-key, and I m also adding it in my config.yml like this:
// .circleci/config.xml
....
steps:
- add_ssh_keys:
fingerprints:
- "My fingerprint"
....
But during the cicd it throws this message: 'There are no configured ssh keys to install'
There are no configured ssh keys to install
and, of course, npm install fails because can not access to the repo
Any clue? Thanks anyway
This thread mentions:
When this error appears, it typically means that the ssh keys have not been configured in all locations.
SSH keys will need to be set in both the project setting's page and within the config.yml.
Just in case, double-check the URL https://app.circleci.com/settings/project/github/<your organization name>/<project name>/ssh and see if it matches Checkout SSH Keys page mentioned in the official documentation

git pull not executing through a webhook in bash script

I have a node server running on ec2 in ubuntu which should update when I push into master as I have created a hook for that in Gitab integrations.
I saw the hook working through the logs and executing every command expect simple git pull.
I have checked many similar questions which have suggestions like appending env -i to reset the GIT_DIR so that the command can execute but no luck so far.
I tried executing different commands like git status and they are executing through the hook in bash script normally.
Here is my script which is in my home folder along with the repository:
#!bin/bash
cd toTheFolder
git pull
here is the end-point which executes the script
childProcess.exec(
"bash temp.sh",
{ cwd: "/home/ubuntu/repoFolder" },
function(err, stdout, stderr) {
console.log(stdout, stderr);
if (err) {
return res.status(500).send(err);
}
res.status(200).send("OK");
}
);
The error it returns is {"killed":false,"code":1,"signal":null,"cmd":"bash temp.sh"}
Any thoughts on why simple git pull is not working would be a huge help.
-Thanks
EDIT: here is the output of stdout
git#gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Use the following command
git pull https://username:password#mygithost.com/my/repository
Remember to replace the [username:password] with your git credentials, [mygithost.com] with your git host ( gitlab, ..etc), [my/repository] with your repository url
It would not work for github anyway cause they removed the username/password authentication support.
For github please check your ssh connection between your server and github.

How to delete a remote branch using Node.js?

I'm currently using nodegit to run my git commands and it has worked for everything so far except for deleting a remote branch. I don't mind using another npm package to do this if needed but I would prefer to use nodegit.
Basically, I want a function which can do the same as this command in the terminal
$ git push -d <branch_name>
I want to be able to write something like the following:
function delete_remote_branch(repo, branch_name, callback) {
repo.getRemote('origin').then(function(remote) {
repo.getBranch(branch_name).then(function(reference) {
// delete the branch
repo.push("-d :"+reference, branch_name).then(function(error_code) {
if(error_code) {
return callback(error_code)
}
return callback(null)
})
})
})
}
The documentation for remote.push is here: http://www.nodegit.org/api/remote/#push
Any help would be appreciated. Thanks!
Push an empty src reference to the origin branch.
remote.push(':refs/heads/my-branch');

Resources