Gerrit Checks plugin - How to create/update Checker for a gerrit change with API - continuous-integration

My Gerrit server installed Checks plugin and I also try to create many checkers for my repo.
I can get information about Checkers of a gerrit change by admin account (configured Administrate Checkers and password was generated for "HTTP Credentials" in settings menu) with curl command:
url="http://my_gerrit_ip:8081"
curl -k -X GET \
-u admin:Jy7xK+oJ2tA2yzc+jG5n7yttsFUNmXDNKEDh+PoygQ \
--header "Content-Type: application/json; charset=UTF-8" \
--data gerrit-field-content.json \
$url/changes/66/revisions/1/checks/
The output looks like:
[
{
"repository": "test2",
"change_number": 66,
"patch_set_id": 1,
"checker_uuid": "a:a",
"state": "NOT_STARTED",
"created": "2020-10-28 02:56:55.000000000",
"updated": "2020-10-28 02:56:55.000000000",
"checker_name": "test2",
"checker_status": "ENABLED",
"blocking": [
"STATE_NOT_PASSING"
],
"submit_impact": {
"required": true
},
"checker_description": "test2"
},
{
"repository": "test2",
"change_number": 66,
"patch_set_id": 1,
"checker_uuid": "b:b",
"state": "NOT_STARTED",
"created": "2020-10-28 02:56:55.000000000",
"updated": "2020-10-28 02:56:55.000000000",
"checker_name": "test2_2",
"checker_status": "ENABLED",
"blocking": [],
"submit_impact": {},
"checker_description": "test2_2"
}
]
But now, i want to update the state of a Checker of my gerrit change via curl command too but it throw message "Authentication required".
I tried with:
curl -k -X POST \
-u admin:Jy7xK+oJ2tA2yzc+jG5n7yttsFUNmXDNKEDh+PoygQ \
--header "Content-Type: application/json; charset=UTF-8" \
--data gerrit-field-content.json \
$url/changes/66/revisions/1/checks/
The gerrit-field-content.json content:
{
"checker_uuid": "a:a",
"state": "SUCCESSFUL",
"url": "",
"started": ""
}
How can I update state of a checker via api? Or are there any better way to do this?
Result of checker state maybe look like a sample in this one https://gerrit-review.googlesource.com/c/gerrit/+/285580
Note: the it's not able update checker when i leave password for admin empty (curl -u admin:) because my Gerrit server is not require password for logging in.

Afraid I am just poking around myself, but in the zuul change it says:
* Access control for the `checks` API in Gerrit depends on a single
global administrative permission, ``administrateCheckers``. This is
required in order to use the `checks` API and can not be restricted
by project. This means that any system using the `checks` API can
interfere with any other.
Hope that is helpful, as I deploy this I'll update if I gain any other useful experience

Related

Perspective API: Proper way to send requests with auto-detection of language

I am a bit confused on the proper way to send requests using Google's Perspective API.
Sending the following request works:
{"comment":{"text":"yo hamburger"},"languages":["en"],"requestedAttributes":{"TOXICITY":{}}}
In the documentation, it says, "...If you are using a production attribute, language is auto-detected if not specified in the request." So, I tried:
{"comment":{"text":"yo hamburger"},"requestedAttributes":{"TOXICITY":{}}}
And in response, I got a HTTP/1.0 400 Bad Request.
I also tried including all of the languages listed on the documentation page, like this:
{"comment":{"text":"yo hamburger"},"languages":["en","fr","es","de","it","pt"],"requestedAttributes":{"TOXICITY":{}}}
But that also gave me a response of HTTP/1.0 400 Bad Request.
Another attempt was made leaving the array of languages empty, like this:
{"comment":{"text":"yo hamburger"},"languages":[],"requestedAttributes":{"TOXICITY":{}}}
However, it still gave me a response of HTTP/1.0 400 Bad Request.
I was wondering, what is the proper way to send a request to the API and have it auto-detect language?
User x00 provided the path to the solution in the question's comment section. By using curl, I was able to see what was going on.
Here's what was happening:
In this first example, the system worked without error.
CURL:
curl -H "Content-Type: application/json" --data \
'{comment: {text: "yo hamburger"},
languages: ["en"],
requestedAttributes: {TOXICITY:{}} }' \
https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=[API_KEY]
RESPONSE:
{
"attributeScores": {
"TOXICITY": {
"spanScores": [
{
"begin": 0,
"end": 12,
"score": {
"value": 0.050692778,
"type": "PROBABILITY"
}
}
],
"summaryScore": {
"value": 0.050692778,
"type": "PROBABILITY"
}
}
},
"languages": [
"en"
],
"detectedLanguages": [
"tr",
"ja-Latn",
"de",
"en"
]
}
In this second example, the system was indeed auto-detecting language, but since "yo hamburger" was detected as Turkish, it could not provide a solution and instead sent a 400 as the response code.
CURL:
curl -H "Content-Type: application/json" --data \
'{comment: {text: "yo hamburger"},
requestedAttributes: {TOXICITY:{}} }' \
https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=[API_KEY]
RESPONSE:
{
"error": {
"code": 400,
"message": "Attribute TOXICITY does not support request languages: tr",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.commentanalyzer.v1alpha1.Error",
"errorType": "LANGUAGE_NOT_SUPPORTED_BY_ATTRIBUTE",
"languageNotSupportedByAttributeError": {
"detectedLanguages": [
"tr"
],
"attribute": "TOXICITY"
}
}
]
}
}
This next example is more mysterious to me, as the language field for the request is plural, "languages," so it seems you can provide more than one language. However, it said it couldn't support that.
CURL:
curl -H "Content-Type: application/json" --data \
'{comment: {text: "yo hamburger"},
languages:["en","fr","es","de","it","pt"],
requestedAttributes: {TOXICITY:{}} }' \
https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=[API_KEY]
RESPONSE:
{
"error": {
"code": 400,
"message": "Attribute TOXICITY does not support request languages: en,fr,es,de,it,pt",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.commentanalyzer.v1alpha1.Error",
"errorType": "LANGUAGE_NOT_SUPPORTED_BY_ATTRIBUTE",
"languageNotSupportedByAttributeError": {
"requestedLanguages": [
"en",
"fr",
"es",
"de",
"it"
],
"attribute": "TOXICITY"
}
}
]
}
}
In this next example, leaving the languages array empty also provided the auto-detection of language, but again, "yo hamburger" was detected as Turkish, so it could not provide a response.
CURL:
curl -H "Content-Type: application/json" --data \
'{comment: {text: "yo hamburger"},
languages:[],
requestedAttributes: {TOXICITY:{}} }' \
https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=[API_KEY]
RESPONSE:
{
"error": {
"code": 400,
"message": "Attribute TOXICITY does not support request languages: tr",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.commentanalyzer.v1alpha1.Error",
"errorType": "LANGUAGE_NOT_SUPPORTED_BY_ATTRIBUTE",
"languageNotSupportedByAttributeError": {
"detectedLanguages": [
"tr"
],
"attribute": "TOXICITY"
}
}
]
}
}
Noticing that Perspective API would not allow me to choose all of the languages that are provided for the TOXICITY report, I decided to try two languages. The response was the same. Apparently Perspective API rejects the request if multiple languages are specified. Perhaps naming the field "languages" was a thought for the future.
CURL:
curl -H "Content-Type: application/json" --data \
'{comment: {text: "yo hamburger"},
languages: ["en","fr"],
requestedAttributes: {TOXICITY:{}} }' \
https://commentanalyzer.googleapis.com/v1alpha1/comments:analyze?key=[API_KEY]
RESPONSE:
{
"error": {
"code": 400,
"message": "Attribute TOXICITY does not support request languages: en,fr",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.commentanalyzer.v1alpha1.Error",
"errorType": "LANGUAGE_NOT_SUPPORTED_BY_ATTRIBUTE",
"languageNotSupportedByAttributeError": {
"requestedLanguages": [
"en",
"fr"
],
"attribute": "TOXICITY"
}
}
]
}
}
Maybe you're using bad client library or other issue is causing the problem, Here is documentation about client library that in example language is auto-detected without problem. Check that and if not successful provide more details for further investigations.
As I said in the comments, general approach to these kind of issues: use curl. It helps a lot.
To sum up you findings:
auto-detection with a set of languages doesn't seem to work.
the correct way to send a request with auto-detection enabled is
{comment: {text: "some text"}, requestedAttributes: {TOXICITY:{}} }
but sometimes it fails on short texts, especially with slang inside.
So what can be done about it?
The easyest way is to assign some weight to Bad Requests (probably something around 0.5). Anyway, as a response you get the probability and not a definitive answer. So
toxicity score = 1 means "definitely toxic"
toxicity score = 0 means "not toxic at all"
and toxicity score = 0.5 means "we have no idea"
same thing goes for Bad Request - "you have no idea"
and you will get 0.5 from time to time, so you must deal somehow with comments of that score anyway. As well as with network errors etc.
But I would say that a probability of toxicity of a comment that result in LANGUAGE_NOT_SUPPORTED_BY_ATTRIBUTE is higher than 0.5. But it's up to you to decide on the exact number.
As auto-detection doesn't work well with short texts you can bump up probability of correct auto-detection by adding some context into you request: a couple of other comments in the thread, or better yet, a couple of other comments from the same user. Not too big ones and not too small ones.
Make three requests specifying a language. As far as I can tell TOXICITY works only with English, Spanish, and French. On github I've got this reply:
"TOXICITY is currently supported in English (en), Spanish (es), French (fr), German (de), Portuguese (pt), and Italian (it). We will work to remove the contradictions you identified."
Auto-detect by yourself before sending a request. That'll require some effort, but it shouldn't be too hard, given you have much more context available to you than is available to Perspective API (or any other third-party API)
Also
These kind of APIs are not supposed to stay unattended. Fine tuning and moderation on your part is required. Or else we'll end up in the worst-case scenario of algocracy :).
And I think it's a good idea in general to store statistics of toxicity of comments for a user... as well as some manual coefficient. Because for example: Mathematical formulas give high toxicity
I've posted an couple of issues on github, but no reply yet (whating for reply on the second issue). When/If I'll get them I'll update my answer with details.

How to read response of a JIRA REST API

Using the Atlassian documentation, I'm able to create a new JIRA ticket using curl command. I want to understand how to read the response of this command into a variable? Essentially i want to store the JIRA ticket ID into a variable that can be used in other scripts.
Reference Link: https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/
_SAMPLE_CODE_
curl -D- -u charlie:charlie -X POST --data '{
"fields": {
"project":{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}
}
}' -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/
_SAMPLE_RESPONSE_
{
"id":"39000",
"key":"TEST-101",
"self":"http://localhost:8080/rest/api/2/issue/39000"
}
From the above log, I want to understand how can we fetch the "key" (i.e. JIRA number or JIRA ID) from response (essentially stdout) into a variable.
you can use jq command line tool to extract value from json:
#!/bin/bash
key=$(curl -s -u charlie:charlie -X POST --data '{
"fields": {
"project":{
"key": "TEST"
},
"summary": "REST ye merry gentlemen.",
"description": "Creating of an issue using project keys and issue type names using the REST API",
"issuetype": {
"name": "Bug"
}
}
}' -H "Content-Type: application/json" http://localhost:8080/rest/api/2/issue/|jq -r .key)
echo "key: $key"
jq command is not installed by default on linux systems.
you will need to install it manually:
sudo apt install jq
sudo yum install jq

Model is not deployed and cannot predit

On Google Cloud Automl - Language Classification, I put in the following request for prediction.
I am getting - The model is not deployed, hence not supported for prediction yet.
How do we deploy a model ? I do not see any documentation if deploying a model
I am running this command shown in code in Google Cloud Shell
curl -X POST -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" -H "Content-Type: application/json" https://automl.googleapis.com/v1beta1/projects/[projectid]/locations/us-central1/models/[Modelname]:predict -d '{
"payload" : {
"textSnippet": {
"content": "YOUR TEXT HERE",
"mime_type": "text/plain"
},
}
}'
{
"error": {
"code": 400,
"message": "The model is not deployed, hence not supported for prediction yet.",
"status": "FAILED_PRECONDITION"
}
}
Expected prediction
You can deploy a successfully trained model using the UI on GCP.
Under HOME go to your respective AI and find your model in its dashboard.

Trigger build with parameters

Following a TC upgrade to 2018
My previous script of triggering a TC build with parameters is not working
The script we were using uses the following api:
https://[server]/httpAuth/action.html?add2Queue=[build name]&name=[param name]&value=[param value]
I'm trying to migrate to restApi
from (https://confluence.jetbrains.com/display/TCD18/REST+API#RESTAPI-QueuedBuilds):
I have tried
https://[server]/app/rest/buildQueue?locator=buildType:[build name],[param name]:[param value]
Currently I have 2 issues:
I get a build triggered successfully - but it has not been triggered
Documentation was not clear, how to I trigger the build with parameters ?
Can you please advise on how to trigger the build successfully with parameters (also could be more than 1)
Firstly you right TeamCity documentation is not clear. Respect to this link;
For triggering a build you must make POST request to this url and send buildType id through body.
http://localhost:8111/httpAuth/app/rest/buildQueue
Also you can pass configuration parameter into body.
XML body for trigger build with parameters:
<build><buildType id="YourBuildTypeId"/>
<properties><property name="PARAM1" value="VALUE1"/></properties>
</build>
JSON body for trigger build with parameters:
{
"buildType": {
"id": "YourBuildTypeId"
},
"properties": {
"property": [
{
"name": "PARAM1",
"value": "VALUE1"
},
{
"name": "PARAM2",
"value": "VALUE2"
}
]
}
}
You can use below curl script.
curl -X POST \
http://localhost:8111/httpAuth/app/rest/buildQueue \
-H 'Accept: application/json' \
-H 'Content-Type: application/xml' \
-d '<build><buildType id="YourBuildTypeId"/>
<properties><property name="PARAM1" value="VALUE1"/></properties>
</build>'

Last Login Date to Application

I am looking for an API to find the user's last login date to a particular application. Not sure if it exists, please let me know your thoughts on getting this information through API calls.
You can get the last date the user was authenticated to any application on the User profile:
GET https://dev-12345.oktapreview.com/api/v1/users/xyz
{
"id": "xyz",
"status": "ACTIVE",
"created": "2017-02-22T02:33:02.000Z",
"activated": null,
"statusChanged": "2017-02-22T17:50:43.000Z",
"lastLogin": "2017-09-28T16:19:57.000Z",
"lastUpdated": "2017-08-08T20:11:46.000Z",
"passwordChanged": "2017-02-22T17:50:43.000Z",
"profile": {
"lastName": "Barbettini",
"firstName": "Nathanael",
// ...
}
The lastLogin field contains the timestamp of the last time the user logged in.
This isn't tracked on a per-app basis, though. You can get the app-specific App User:
GET https://dev-12345.oktapreview.com/api/v1/apps/abc/users/
{
"created": "2017-09-22T00:54:49.000Z",
"lastUpdated": "2017-09-22T00:54:49.000Z",
"scope": "GROUP",
"status": "ACTIVE",
"statusChanged": "2017-09-22T00:54:49.000Z",
"passwordChanged": null,
"syncState": "DISABLED",
"profile": {
"zoneinfo": "America/Los_Angeles",
"region": null,
// ...
}
But it doesn't have lastLogin. One way you could solve this is by saving a timestamp in the App User profile in your application code.
You could use filters to specify which logs do you want from the system logs API endpoint, limit the answer to 1, set the response order to start from the most recent ones, and user filters again to filter for your user.
So, you need to filter the request using the following filters:
actor.alternateId: which identifies the user, in my case the pattern "username#" is enough
event_type: this one should be user.authentication.sso which means that the user SSO to an app (any of the ones available for that user)
outcome.result: success, which means that the user successfully SSO to the app
Example query would be something like this:
curl -X GET -H "Content-Type: application/json" -H "Authorization: SSWS super-secret-token-here" -H "Accept: application/json" "https://<your.domain.here.com>/api/v1/logs?until=2018-07-17T11%3A30%3A55.664541&limit=1&sortOrder=DESCENDING&filter=event_type+eq+%22user.authentication.sso%22+and+outcome.result+eq+%22SUCCESS%22+and+actor.alternateId+sw+%22<user name here>%40%22"
Remember to replace the token, the URL and the before testing this query.
Also, remember that this example is URL encoded, so this is why it looks so weird.

Resources