How can I watch a github branch from nodejs? - node.js

I have a node process and I would like it to perform certain tasks when a specific GitHub branch is updated on a repository I own. I would also like to obtain a list of the changed files, but this can go into a separate question in due time. For now, if I could find a way to implement something like this, I would be very happy:
In my node process
var watched = new gitBranch('https://github.com/me/myOwnRepository#production')
watched.on('update', function () {
console.log('new commit on my production branch')
})
I suppose I could poll the repo using the GitHub API, but I don't see anything in the GitHub API about fetching only a certain branch. Also, I would certainly prefer some sort of notification to polling, but again I haven't found anything like this for branches.
Any ideas?

Use webhooks: https://developer.github.com/webhooks/
The push event is what you'll be interested in: https://developer.github.com/v3/activity/events/types/#pushevent
Notice that the payload contains a "ref" attribute which tells you which branch was pushed to. Use that to do the filtering on your end (it's not possible to subscribe for events on a specific branch).

Related

GitLab API: Get branch associated with issue

In a GitLab issue, you can associate a branch with an issue, and in the issue there will be the line
#whoever created the branch branchname to address this issue.
Is there a way of getting that branch using the issues API? I'm trying to set up an automation script that will merge all branches associated with issues that have a certain label into the prod branch, then push the result as a development brnach so I can deploy that to a dev environment. I don't want to use merge requests as they will be used when the dev work is complete and ready to be merged for deployment to production.
Unfortunately, there currently is no official API to fetch an issue's related branches.
Some possible ways you can work around this:
Use the notes API
When a user uses the issue interface to create the branch, you will see a system message, like you mention. This message will be present in the notes API for the issue.
Example using the python-gitlab library:
import re
...
branch_note_pattern = '^created branch \[\`(.*)\`\].*to address this issue'
issue = project.issues.get(ISSUE_NUMBER)
all_notes = list(issue.notes.list(as_list=False))
system_notes = [note for note in all_notes if note.system]
related_branches = []
for note in system_notes:
match = re.match(branch_note_pattern, note.body):
if match:
branch = match.groups()[0]
related_branches.append(branch)
print('BRANCHES RELATED TO ISSUE', ISSUE_NUMBER)
for branch_name in related_branches:
print(branch_name)
However, it is possible to have a related branch without that note appearing because the related branches is just based on naming convention. So, if someone just creates a branch with named like <issue_number>-some-branch-name then it will show up as a related branch, but there will not be a system message in the API.
So, if you rely on the notes API, you may miss related branches created manually.
Use the unofficial frontend API
The issues controller only returns related branches for the purposes of the frontend to render as HTML.
If you request /<:project_url>/-/issues/<:issue_number>/related_branches?format=json you will get a JSON response containing the HTML for the frontend to insert in the issue view. You can parse this HTML to get the related branches.
This will reliably fetch the same related branches you'll see in the UI, but is more work to implement, and is fragile because the API is not guaranteed to be stable between versions of GitLab.

How to find forks of a repository in Azure DevOps

In GitLab I am able to see who has forked my repository by clicking the following link:
Does anyone know if there is a similar capability within Azure DevOps?
I was able to find the following API call however it doesn't seem to work:
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/forks/list?view=azure-devops-rest-6.0
-> I pass it a repo and various container ids where I know there are forks of that repo and it returns empty results.
Even when I find a fork in Azure devops by manually traversing the UI I cannot find a place to view where that repo was forked from.
Any help on the above would be greatly appreciated.
Even when I find a fork in Azure devops by manually traversing the UI I cannot find a place to view where that repo was forked from.
You could use the REST API Forks - Get Fork Sync Requests to retrieve all requested fork sync operations on this repository:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryNameOrId}/forkSyncRequests?api-version=6.0-preview.1
As the test result:
We could to know the repo ID where current repo was forked from.
And we could use the Repositories - List to know the name of the Repo:
Update:
In GitLab I am able to see who has forked my repository by
clicking the link. Does anyone know if there is a similar capability
within Azure DevOps?
If you want to know who forks your repo, you could just use your REST API
Forks - List:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryNameOrId}/forks/{collectionId}?api-version=6.0-preview.1
And we could use the oraganiztion id for the collectionId. We could use following RETS API to get the oraganiztion id:
Post https://dev.azure.com/{organization1}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1
Body:
{
"contributionIds": ["ms.vss-features.my-organizations-data-provider"],
"dataProviderContext":
{
"properties":{}
}
}
The test result:
If you create a pull request for a repository that has been forked, its "into" options will include a select box that includes the list of forks from that repository.

Getting whole repository structure from GitHub API using one request

Is it possible to somehow get information about whole repository using one request to github api or we have to recursively asking API for specific level?
According to this request: https://api.github.com/repos/microsoft/terminal/contents?ref=master
we are only getting one level.
You can get the tree recursively using :
/repos/:owner/:repo/git/trees/:tree_sha?recursive=1
In your case it would be :
https://api.github.com/repos/microsoft/terminal/git/trees/master?recursive=true
You can make a request to here to see details about your branch:
https://api.github.com/repos/{owner}/{repo}/branches/{default_branch}
In the response you will see "commit" field and inside of it there is "sha". Take that one put it in this request:
https://api.github.com/repos/{owner}/{repo}/git/trees/YOUR_SHA_GOES_HERE?recursive=1
You will receive every file under your repo like this.
Cheers!

Construct GitLab URL without slug

Is there any way to build a GitLab URL for a milestone or project based on its id property instead of the slug?
Context:
I have an app that I use as a GitLab web hook, and from its front end would like to link back to GitLab. I'm keeping the project and milestone ids, as they are unique, but can't find a way to link back to them. Ideally something like: http://gitlab.example.com/project/83/milestone/113 or even http://gitlab.example.com/milestone/113 would work for me (even if they do a redirect).
Examining rake routes and config/routes.rb tells me that such routes do not exist.
The only options I can see are:
store just the slugs which are also unique. Your request and memory usage will be slightly larger, but it's worth it.
make an extra API request to get the slugs. This requires 2 requests, so it is worse than having a larger request.
For new routes of form /something to be created in gitlab, something needs to be blacklisted at https://github.com/gitlabhq/gitlabhq/blob/199029b842de7c9d52b5c95bdc1cc897da8e5560/lib/gitlab/blacklist.rb, and interestingly projects is already blacklisted, but it is currently only used for project creation.
milestones however is not blacklisted: so a user could be called milestiones and that would generate ambiguity.
I would also take a close look at how GitHub organizes its API and paths, as it is likely to be optimal: is ID web access possible in GitHub?

How to update a fork from it's original via the Github API

I've created a fork of a github repository via the github API. Now, later on, I want to pull any updates from the origin repository into the fork. This should always be a fast-forward in my use case. I have read access to the origin repository and read-write to the fork.
I thought of maybe creating a Pull Request then accepting (both of which you can do via the API) but this creates noise (Pull Requests being created and destroyed) and just doesn't seem right.
Is there any way to do this via the API?
I don't have the inside scoop on this, so this might be a miss-feature that will be removed at some point. Until then:
Github makes available all commits across (I assume) the entire fork network; So APIs that accept commit hashes will be happy to work on hashes from the upstream, or across other forks (This is explicitly documented for repos/commits/compare and creating a pull requst).
So there are a couple of ways to update via APIs only:
Using Git data api: This will usually be the best option, if you don't change your fork's master.
Get upstream ref /repos/upstream/repo/git/refs/heads/master, and get the hash from it
Update your fork PATCH /repos/my/repo/git/refs/heads/master with the same hash.
Using a higher-level merge api: This will create a merge commit, which some people like.
Get the upstream ref like before
Create a merge to branch master in your repo.
Pull-request to yourself and merge it via api: This will end up creating not only a merge commit, but a PR as well.
Create PR: POST to /repos/your/repo/pulls with head = "upstream:master"
Get the PR url from the response,
Merge it: PUT to /repos/your/repo/pulls/number/merge
It's possible that the "upstream:master" notation would also work for options 1 & 2, saving an API call.
Not possible currently, but I've gone ahead and added that to our API wishlist. :)
This that work for me, because I needed update from upstream but without a merge request commit. My ref is master.
Create a pull request POST /repos/:myUsername/:myRepo/pulls
INPUT: {title, head: 'ownerFromUpStream:master', base: 'master', ...}
Get sha from pull request (ex. response.data.head.sha)
PATCH /repos/:myUsername/:myRepo/git/refs/master
PARAMS: {sha: shaFromPullRequest}
DOC.
Update ref
Create pull request
This is now possible in the GitHub API; documentation here, and announcement here.
In summary, make a POST request to /repos/{owner}/{repo}/merge-upstream with the proper authentication and the payload of { "branch": "branch-name" }.

Resources