I'm using Probot's Octokit to read a file from a repo:
const content = await context.octokit.repos.getContent({owner: "OWNER", repo: "REPO", path:"PATH"})
However this only works for the master branch, is there a way to get the file from another branch (e.g. the branch that the pull request action is coming from)? This isn't directly stated in the docs itself.
Thanks
Yes, it is possible.
When using Octokit, there is an optional parameter: ref where you can specify the branch or commit hash. You can find the reference here.
Related
In a nutshell, I am looking to get a list of work items linked to a git branch.
In more detail
I am working with 4 repositories that I add as resources to my pipeline
- repository: Cms # code repository
name: <ProjectName>/Cms
type: git
ref: develop2022
- repository: QA-Automation # Automated Testing Repo
name: <ProjectName>/QA-Automation
type: git
ref: main
- repository: TdsWDPExplorer # Generate Reports Repo
name: <ProjectName>/TdsWDPExplorer
type: git
ref: master
The Pipeline yaml files them self's are in the 4th Repo and checked out as self
- checkout: Self
path: s/DE-DevOps
I am trying to update the work items associated with the Cms Repository.
I tried using the Workitem Updater task https://marketplace.visualstudio.com/items?itemName=BlueBasher.bluebasher-workitemupdater
But it only sees the workitems associated with the Repository holding the yaml Files (Self).
I also looked at the API to get a list of the work items.
_apis/git/repositories//refs?filter=heads%2fBRANCHNAME&includeLinks=true
Gives me details to a branch but I didn't find the linked work items
Also looking at the workitem I dint see that info
_apis/wit/workitems?ids=ITEM-ID's&$expand=all&api-version=6.0
I am thinking it might be somewhere in _apis/wit/reporting/workitemlinks but haven been able to get the info.
I found a answer that works for me, in a response to Obtain all work items from Azure DevOps that have been merged into a branch via JavaScript
we link the work items to pull requests, I can use the API to query the pull requests in to the given branch and get the linked work items like:
https://dev.azure.com/Organisation/Project/_apis/git/repositories/Cms/pullrequests?searchCriteria.status=completed&searchCriteria.targetRefName=refs/heads/BRANCHNAME&api-version=6.0
I should now be able to extract the ID's and pass them in a PS loop to the Item Updater Task https://marketplace.visualstudio.com/items?itemName=BlueBasher.bluebasher-workitemupdater or using the API to update the workitem
There is also the option to call the API for the pipeline run info https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/get?view=azure-devops-rest-7.1#runresources
But I didn't see a way to extract the work items but the information must be somewhere there as well because I can see the work items listed in the pipeline view by resource
Scenario: I need to get when was the latest commit done in the repo and by whom, and to which branch did that user do the commit on?
Solution:
I'm using python azure.devops module. and here is my code:
cm_search_criteria = models.GitQueryCommitsCriteria(history_mode='firstParent', top=10)
commits = git_client.get_commits(repo.id, search_criteria=cm_search_criteria, project=project.name)
for i in commits:
datetimeobj = datetime.strptime(i.committer.date.strftime("%x"), '%m/%d/%y')
last_commit_on = datetimeobj.date()
last_commit_by = i.committer.email
break
Now how do I get the branch name to which the user had committed the code? In the UI we can see the branch name... how can i get the same data using Azure Devops REST API ?
enter image description here
you may need to use Stats - List to retrieve statistics about all branches within a repository, then evaluate the latest commit, you also need to consider argument of baseVersionDescriptor.versionOptions of firstParent
I'm not sure if Python wrapper module support this seems like the github project is achieved now.
This question is regarding the repo for react implicit grant for docusign using react.
https://github.com/docusign/eg-02-react-implicit-grant
When I tried to clone the repo using VSCode, I received an error
Git: fatal: cannot create directory at 'src/hoc/Aux': Invalid argument
When I looked into the repo, there is a file under src/hoc/aux/aux.js which has the text below.
const aux = (props) => props.children;
export default aux;
Is the file aux.js necessary? Because I was able to extract the zip files after skipping to extract aux.js.
I just tried to clone the https://github.com/docusign/eg-02-react-implicit-grant repo using the GitHub desktop and did not have any problems.
I suggest that you download the repo using git directly or use the download zip option from the repo's page.
Re: aux.js
TL;DR. -- Yes, I believe the aux.js is needed.
Details: Unfortunately I'm not a React expert. (Even though I wrote this example.)
The /hoc directory is used for React Higher Order Components. In this case, I'm using it for a simple component that checks to see if the user has a valid token. I believe that the js file is needed. But you can find out by not including it as seeing what happens.
Also, note that I wrote this React example almost a year ago. You'll want to update it to current React best practices.
Try renaming the aux folder and aux.js file inside it to any random name do it preferably in vscode it will let you know which name is allowed and which isn't, worked for me.
I can invoke a simple Jenkins pipeline from gitlab merge requests using a webhook. Now I'd like to know what is the source branch to make the checkout against it. Example: if I push code to develop branch, in my pipeline script i'd checkout develop branch. Thanks.
node {
stage('Build') {
def mybranch = '?' // get branch name from gitlab webhook
git branch: mybranch,
credentialsId: 'mycredential',
url: 'myurl'
// ...
}
}
You can get the current branch name via env.gitlabBranch.
Reference: gitlab-plugin
You can parameterize your pipeline and use the webhook payload data to fill in the branch value as described here.
GitLab plugin creates a lot of useful environment variable. You can see them in here. I think the one you need is CI_COMMIT_REF_NAME
My goal is to get the tree for the latest SHA in the default branch
GET /repos/:owner/:repo/git/trees/:sha
How do I find the lastest SHA from the default branch?
I know that I can call
GET /repos/:owner/:repo/branches/:branch
But I can't just use "master" for the branch as not all repos use master as the default branch.
How do I find out what the default branch for a repo is?
Make a call to /repos/:owner/:repo and read the default_branch property value - this is the name of the default branch. See example response here: http://developer.github.com/v3/repos/#get
This is also now avaialable with the github cli as well
gh repo list <Your_Name> --json nameWithOwner,defaultBranchRef
If you want to slightly cleanup the output, you can remap with jq
gh repo list <Your_Name> --json nameWithOwner,defaultBranchRef \
--jq ".[] | { nameWithOwner , defaultBranch: .defaultBranchRef.name}"
The advantage to this approach being auth is integrated and much easier to manage
This is a cleaner and faster way using gh:
gh api /repos/{owner}/{repo} --jq '.default_branch'