get build status through github api - github

The GitHub API provides a lot of functionality, but is there a way to retrieve the build status for a commit? The GitHub UI provides information from the CI system we have configured, but I can't see this information exposed through the API?

You can access the status for a particular ref
GET https://api.github.com/repos/:owner/:repo/commits/:ref/statuses
For the value of :ref, you can use a SHA, a branch name, or a tag name.

It doesn't provide status directly, but offers you to create a status
That means the CI can have a final build step which publishes the status to GitHub repo that way.
POST /repos/:owner/:repo/statuses/:sha
For example:
{
"state": "success",
"target_url": "https://example.com/build/status",
"description": "The build succeeded!",
"context": "continuous-integration/jenkins"
}
(and that, for a given SHA1)
See for instance "Github Commit Status API with Bamboo from Atlassian", where:
${bamboo.buildResultsUrl} is the GitHub commit SHA1:
<xxx> is a placeholder value, which can be replaced by a value, or a variable ${var} as shown here.
Add those to your plan as Script.
complete.sh:
# specs and cukes results are stored in JUnit format under test-reports
if (grep 'failures="[^0]"' test-reports/* || \
grep 'errors="[^0]"' test-reports/*); then
curl -H "Authorization: token <MY_TOKEN>" --request POST \
--data '{"state": "failure", "description": "Failed!", \
"target_url": "${bamboo.buildResultsUrl}"}' \
https://api.github.com/repos/<USER>/<REPO>/statuses/${bamboo.repository.revision.number} > /dev/null
else
curl -H "Authorization: token <MY_TOKEN>" --request POST \
--data '{"state": "success", "description": "Success!", \
"target_url": "${bamboo.buildResultsUrl}"}' \
https://api.github.com/repos/<USER>/<REPO>/statuses \
/${bamboo.repository.revision.number} > /dev/null
fi
pending.sh:
curl -H "Authorization: token <MY_TOKEN>" --request POST \
--data '{"state": "pending", "description": "Build is running", \
"target_url": "${bamboo.buildResultsUrl}"}' \
https://api.github.com/repos/<USER>/<REPO>/statuses/${bamboo.repository.revision.number} > /dev/null

Related

GitHub Actions: Must have admin rights to trigger workflow_dispatch?

Using the github API I am trying to manually start a workflow using:
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: MY_TOKEN" \
https://api.github.com/repos/djpr-data/djprdashdata/actions/workflows/refresh-data.yaml/dispatches
but I keep getting an authentication error:
{
"message": "Must have admin rights to Repository.",
"documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"
}
This seems to be a similar issue to this question. But my PAT token has all admin and repo scopes selected. I also have my user account setup as admin for the repository and I have added a workflow dispatch to the workflow yaml file.
workflow_dispatch:
inputs:
tags:
description:
"run from cmdline"
I have been following the docs at https://docs.github.com/en/rest/actions/workflows#create-a-workflow-dispatch-event and have had no problems using the API to retrieve all previous workflow jobs. I have also tried the runs and jobs endpoints but get the same error. So I am now not sure what else I can do. Is there somewhere else I need to set permissions?
Thanks
This is a poor error message to tell you that your request is not formed correctly. If you want to pass a PAT as a header, you need to prefix it with token, as described in the docs:
-H "Authorization: token MY_TOKEN"
Once that's resolved, however, you'll also get an error because you don't pass the required ref payload. Assuming your default branch is main, here's a correct curl command:
> export MY_TOKEN=gha_abcdef
> curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token $MY_TOKEN" \
-d '{"ref": "main"}' \
https://api.github.com/repos/djpr-data/djprdashdata/actions/workflows/refresh-data.yaml/dispatches

how to regenerate pagerduty integration key programmatically

I have integrated Jenkins CI with pagerduty. Once I do that, I can see intergration key generated.
That will be used in jenkins to send the events to pagerduty.
The requirement is to rotate the keys after some time. I want to automate this.
Is there any api to regenerate the intergration key and return the key in response to be stored in jenkins?
I think the simplest solution here is to use the REST API -- it isn't possible to regenerate the integration key directly, but you can delete the integration and create a new one programmatically.
First fetch the service details:
curl --location --request GET 'https://api.pagerduty.com/services/<service_id>' \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>'
This will include all of the integrations on the service -- make note of the integration_id and the vendor_id.
The delete endpoint isn't documented but it does seem to exist:
curl --location --request DELETE 'https://api.pagerduty.com/services/<service_id>/integrations/<integration_id>' \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>'
And finally you can create the new integration, using the vendor_id from the GET request:
curl --request POST \
--url https://api.pagerduty.com/services/id/integrations \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>' \
--header 'Content-Type: application/json' \
--data '{
"integration": {
"type": "generic_email_inbound_integration",
"name": "Email",
"service": {
"id": "<service_id>",
"type": "service_reference"
},
"integration_email": "my-email-based-integration#subdomain.pagerduty.com",
"vendor": {
"type": "vendor_reference",
"id": "<vendor_id>"
}
}
On doing inspect element to UI button
Its executing POST API:
https://xxxxxxx.pagerduty.com/api/v1/services/XXXXXXX/integrations/XXXXXXX/regenerate_key

GitHub actions repository_dispatch event POST request not working due to organisation repository

I am trying to trigger a GitHub action workflow, which is in my organisations repository and I am the owner of the organsisation
I am trying to trigger using below POST request
curl -H "Accept: application/vnd.github+json" -H "Authorization: token " --request POST --data '{"event_type": "do-something"}' https://api.github.com/repos/USER/REPO/dispatches
But I am getting below message
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event"
}
my workflow
name: Node.js CI
on:
repository_dispatch:
schedule:
- cron: '5 12 * * 0'
jobs:
build:
runs-on: ubuntu-latest
I think as it not in my account instead it is in organisation account, that is why it might be giving this error.
Any work-around will be helpful.
Generate a token from Github Setting Panel with appropriate privileges and use the following URL
curl -X POST -u <githubusername>:<token> -H "Content-Type: application/json" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/<organization>/<repository_name>/dispatches" -d #test.json
Where test.json contains your json data.
As per the docs
This endpoint here requires write access to the repository by providing either:
Personal access tokens with repo scope. For more information, see "Creating a personal access token for the command line" in the GitHub Help documentation.
GitHub Apps with both metadata:read and contents:read&write permissions.
token - A repo scoped GitHub Personal Access Token.
Validate that your token has full scope of repo.
curl 'https://api.github.com/repos/USER/REPO/dispatches' --request POST \
-H 'Accept: application/vnd.github.everest-preview+json' \
-H 'Content-Type: application/javascript' \
-H 'Authorization: Bearer token' \
--data-raw '{
"event_type": "do-something",
"client_payload": { "text": "my custom text here" }
}'

Unable to access github API getting bad credentials error

I am trying to add a custom code check for a PR. After doing some research I found out that it can be done using the API mentioned below.
POST /repos/{owner}/{repo}/check-runs
Initially, it was giving me this error:
{
"message": "You must authenticate via a GitHub App.",
"documentation_url": "https://docs.github.com/rest/reference/checks#create-a-check-run"
}
I followed the guideline provided in this link.
I created a GitHub app.
Gave it required permission.
Generated a private key.
Generated a JWT token using the private key.
Installed the Github app in the repo too
I created a curl request:
curl --location --request POST 'https://api.github.com/repos/X/X-app/check-runs' \
--header 'Accept: application/vnd.github.v3+json' \
--header 'Authorization: Bearer eyJhbGciOiJSUzI1NiJ9.X.X-X-SAFvDnSkaJDjMI2T_BAC2iLlRZ7uNyFSe-X-UgFBFjoFrwsbcYFKfDM8f3FNPYpA6afhr18DLZ6rzu35klA' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "loremipsum"
}'
But, now I am getting this error
{
"message": "Bad credentials",
"documentation_url": "https://docs.github.com/rest"
}
I am not sure what I am missing here.
I figured this out. The GH documentation is a bit unclear/misleading. Here are the steps to make this work:
with the JWT bearer token, list your installations and note the installation id for your app
$ curl -i \
-H "Authorization: Bearer YOUR_JWT" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations
then get an installation access token for the above id
$ curl -i -X POST \
-H "Authorization: Bearer YOUR_JWT" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations/:installation_id/access_tokens
then with that token create the check run but use "Authorization: token" header
curl -i -H "Authorization: token YOUR_INSTALLATION_ACCESS_TOKEN"

Github api tag is not created

I tried to create a tag using Github API. I made a POST request to /repos/:owner/:repo/git/tags, and I get this result:
HTTP/1.1 201 Created
But unfortunately no tag was created. The new tag simply does not exist.
What do I wrong?
The tagging didn't work for me. It shows created, but nothing appears on github. However, I managed to achieve tagging by creating pre-release. Which is not ideal, but still better than nothing:
curl --location --request POST
'https://<giturl>/repos/{owner}/{repo}/releases' \
--header 'Authorization: Basic xxx' \
--header 'Content-Type: application/vnd.github.v3+json' \
--data-raw '{
"tag_name": "v0.0.1",
"target_commitish": "master",
"name": "v0.0.1",
"body": "This is for Release v0.0.1 of the product",
"draft": false,
"prerelease": true}'
There are two types of tags -- annotated and lightweight, you can check the difference here.
As Github API puts, /repos/:owner/:repo/git/tags only created an annotated tag object, and then you should manually create a refrence with the sha of this tag object by calling create refrence api:
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/git/refs \
-d '{"ref":"refs/tags/tagName","sha":"the sha of tag object"}'
In another case, if you only want to add a lightweight tag to one commit, you should directly call create refrence api without the first step:
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/OWNER/REPO/git/refs \
-d '{"ref":"refs/tags/tagName","sha":"the sha of the commit that you want to tag"}'