How to delete a remote branch using Node.js? - 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');

Related

Node.js package for extracting branch name from git ref

I am running my CI on GitHub Actions, and I am using the GITHUB_REF default environment variable as a part of a download URL.
GitHub sets the GITHUB_REF to:
refs/heads/staging
I understand from git: difference between "branchname" and "refs/heads/branchname" that the refs/heads/ prefix is needed so that git has the "full path" to the branch.
Now, I am wondering if anyone knows of an off-the-shelf node.js package deployed to a registry like npmjs that I could use to extract the branch name from the long ref string? It seems like there could be many different prefixes: refs/tags/, refs/remotes, etc., and I would prefer not to write my custom script if there is another solution available.
Thanks to Lawrence Cherone's comment, I found this GitHub gist:
const { execSync } = require('child_process');
function executeGitCommand(command) {
return execSync(command)
.toString('utf8')
.replace(/[\n\r\s]+$/, '');
}
const BRANCH = executeGitCommand('git rev-parse --abbrev-ref HEAD');
const COMMIT_SHA = executeGitCommand('git rev-parse HEAD');
It's not an npm package but it is good enough for now.

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 execute git commands from node using standard terminal commands

I am trying to merge branches into a main branch programmatically, using a node script. The first thing I tried was from Execute a command line binary with Node.js, which worked fine for executing terminal commands. But I encountered difficulties with changing the working directory. I wanted to change the working directory to the git repository, then run git commands from there. Executing cd ~/repo or cd /home/me/repo did not change the working directory at all and I could not find any way to change it other than using cd.
I then tried executing the git commands from the script's directory but pointed at my repo. I tried git --git-dir and git --work-tree but neither of those commands worked. The error was the same: fatal: Not a git repository (or any of the parent directories): .git I'm guessing this is because the directory where the script is running from is not a git repo.
So, I either want to send git commands to a directory other than the one I am running the script from, or I want a way to change the script's working directory. Preferably the latter. My full code and output is below:
import JiraData from './jiradata';
const jiraData = new JiraData();
const { exec } = require('child_process');
(async function () {
const cards = await jiraData.getCardsInTest();
let commands = ['cd /home/me/repo', 'pwd'];
let proceeding = true;
// for (const card of cards) {
// commands.push(`git merge origin/${card} --commit --no-edit`);
// }
for (const command of commands) {
if (proceeding) {
console.log(`Executing command "${command}"`);
exec(command, (err, stdout, stderr) => {
if (err) {
proceeding = false;
console.log(stderr);
return;
}
console.log(stdout);
})
}
}
})();
Output:
> ci#1.0.0 start /home/me/CI
> babel-node index.js --presets es2015,stage-2
Executing command "cd /home/me/repo"
Executing command "pwd"
/home/me/CI
Try instead with:
git -C /path/to/git/repo your_git_command
That will execute the git command in the context of your git repo /path/to/git/repo
I got around this problem by using ShellJS. ShellJS has a method .cd() which changes the directory and seems to stick.

Get git remote info using NodeJS

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) {
// ...
});

Running a command using puppet only if it has not been executed earlier

Suppose I want to make sure that my VM has devstack on it.
exec{ "openstack":
command => "git clone https://git.openstack.org/openstack-dev/devstack",
}
This is the puppet code I write for it and it works fine for the first time. Now I want to put a check. I want to clone the repository only if it has not been done already. How to do that
You say
exec { 'openstack':
command => 'git clone https://git.openstack.org/openstack-dev/devstack',
creates => '/path/to/somewhere/devstack',
cwd => '/path/to/somewhere',
path => '/usr/bin',
}
Now if the directory /path/to/somewhere/devstack exists the clone command won't run.
exec { "openstack":
command => 'git clone https://git.openstack.org/openstack-dev/devstack /path/to/devstack",
unless => 'test -d /path/to/devstack'
}
its a really hacky way to handle this. you should look into the vcsrepo puppet module https://github.com/puppetlabs/puppetlabs-vcsrepo

Resources