I have JSON file locally in my app. I want to add new element in this file programmatically. Below is my JSON format -
{
"MENU":{
"SUBMENU":[
{
"name":"name1",
"link":"link2"
}
]
}
}
I want to add another element in SUBMENU like below -
{
"MENU":{
"SUBMENU":[
{
"name":"name1",
"link":"link1"
},
{
"name":"name2",
"link":"link2"
}
]
}
}
Would it be possible to do it programatically? Thank!
Related
I have form where I need to attach a file from phone. I have been looking for file picker but it can only access images not files like pdf doc docx etc. How to achieve this all in jetpack compose?
According to documentation, it could be done with Intent.ACTION_OPEN_DOCUMENT.
In Compose it you need rememberLauncherForActivityResult to do it:
var pickedImageUri by remember { mutableStateOf<Uri?>(null) }
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
println("selected file URI ${it.data?.data}")
pickedImageUri = it.data?.data
}
pickedImageUri?.let {
Text(it.toString())
}
Button(
onClick = {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
.apply {
addCategory(Intent.CATEGORY_OPENABLE)
}
launcher.launch(intent)
}
) {
Text("Select")
}
I'm developing a workspace add-on with alternate runtime; I configured the add-on to work with spreadsheets and I need to retrieve the spreadsheet id when the user opens the add-on. For test purposes I created a cloud function that contains the business logic.
My deployment.json file is the following:
{
"oauthScopes": ["https://www.googleapis.com/auth/spreadsheets.currentonly", "https://www.googleapis.com/auth/drive.file"],
"addOns": {
"common": {
"name": "My Spreadsheet Add-on",
"logoUrl": "https://cdn.icon-icons.com/icons2/2070/PNG/512/penguin_icon_126624.png"
},
"sheets": {
"homepageTrigger": {
"runFunction": "cloudFunctionUrl"
}
}
}
}
However, the request I receive seems to be empty and without the id of the spreadsheet in which I am, while I was expecting to have the spreadsheet id as per documentation
Is there anything else I need to configure?
The relevant code is quite easy, I'm just printing the request:
exports.getSpreadsheetId = function addonsHomePage (req, res) { console.log('called', req.method); console.log('body', req.body); res.send(createAction()); };
the information showed in the log is:
sheets: {}
Thank you
UPDATE It's a known issue of the engineering team, here you can find the ticket
The information around Workspace Add-ons is pretty new and the documentation is pretty sparse.
In case anyone else comes across this issue ... I solved it in python using CloudRun by creating a button that checks for for the object then if there is no object it requests access to the sheet in question.
from flask import Flask
from flask import request
app = Flask(__name__)
#app.route('/', methods=['POST'])
def test_addon_homepage():
req_body = request.get_json()
sheet_info = req_body.get('sheets')
card = {
"action": {
"navigations": [
{
"pushCard": {
"sections": [
{
"widgets": [
{
"textParagraph": {
"text": f"Hello {sheet_info.get('title','Auth Needed')}!"
}
}
]
}
]
}
}
]
}
}
if not sheet_info:
card = create_file_auth_button(card)
return card
def create_file_auth_button(self, card):
card['action']['navigations'][0]['pushCard']['fixedFooter'] = {
'primaryButton': {
'text': 'Authorize file access',
'onClick': {
'action': {
'function': 'https://example-cloudrun.a.run.app/authorize_sheet'
}
}
}
}
return card
#app.route('/authorize_sheet', methods=['POST'])
def authorize_sheet():
payload = {
'renderActions': {
'hostAppAction': {
'editorAction': {
'requestFileScopeForActiveDocument': {}
}
}
}
}
return payload
How do I add default Suppress automatic scm triggering except for named branch - development in Job DSL?
I tried docs
https://jenkinsci.github.io/job-dsl-plugin/#path/multibranchPipelineJob . It doesn't say much. Seems like it is not supported.
So I guess my only way is adding custom properties directly to XML via configure block.
What I want:
<strategy class="jenkins.branch.NamedExceptionsBranchPropertyStrategy">
<defaultProperties class="java.util.Arrays$ArrayList">
<a class="jenkins.branch.BranchProperty-array">
<jenkins.branch.NoTriggerBranchProperty/>
</a>
</defaultProperties>
<namedExceptions class="java.util.Arrays$ArrayList">
<a class="jenkins.branch.NamedExceptionsBranchPropertyStrategy$Named-array">
<jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named>
<props class="empty-list"/>
<name>development</name>
</jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named>
</a>
</namedExceptions>
</strategy>
What I've tried:
multibranchPipelineJob(jobName) {
branchSources {
git {
remote(gitRepo)
credentialsId(credentials)
includes('*')
configure {
it / "sources class='jenkins.branch.MultiBranchProject$BranchSourceList'" / 'data' / 'jenkins.branch.BranchSource' / "strategy class='jenkins.branch.DefaultBranchPropertyStrategy'" << name('development')
}
}
}
}
This is useful, but keeps crashing http://job-dsl.herokuapp.com/
This is not so useful https://github.com/jenkinsci/job-dsl-plugin/blob/master/docs/The-Configure-Block.md
I have no idea what I'm doing and the docs, manuals and tutorials are not helpful at all.
EDIT:
Now I have this. It works, sort of...
I'm able to generate the job, but Jenkins throws an error when I try to resave the job. The output XML is somehow different.
multibranchPipelineJob(jobName) {
configure {
it / sources(class: 'jenkins.branch.MultiBranchProject$BranchSourceList') / 'data' / 'jenkins.branch.BranchSource' << {
source(class: 'jenkins.plugins.git.GitSCMSource') {
id(randomId)
remote(gitRepo)
credentialsId(credentials)
}
strategy(class: "jenkins.branch.NamedExceptionsBranchPropertyStrategy") {
defaultProperties(class: "java.util.Arrays\$ArrayList") {
a(class: "jenkins.branch.BranchProperty-array") {
'jenkins.branch.NoTriggerBranchProperty'()
}
}
namedExceptions(class: "java.util.Arrays\$ArrayList") {
a(class: "jenkins.branch.NamedExceptionsBranchPropertyStrategy\$Named-array") {
'jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named'() {
props(class: "empty-list")
name('development')
}
}
}
}
}
}
}
As you might have noticed, it is extremely ugly. Hopefully someone will fix the plugin in the future.
So the code which is working is the following:
UUID uuid = UUID.randomUUID()
println('Random UUID: ' + uuid)
multibranchPipelineJob('test') {
configure {
it / sources / 'data' / 'jenkins.branch.BranchSource' << {
source(class: 'jenkins.plugins.git.GitSCMSource') {
id(uuid)
remote('...')
credentialsId('...')
includes('*')
excludes('')
ignoreOnPushNotifications('false')
traits {
'jenkins.plugins.git.traits.BranchDiscoveryTrait'()
}
}
strategy(class: 'jenkins.branch.NamedExceptionsBranchPropertyStrategy') {
defaultProperties(class: 'empty-list')
namedExceptions(class: 'java.util.Arrays\$ArrayList') {
a(class: 'jenkins.branch.NamedExceptionsBranchPropertyStrategy\$Named-array') {
'jenkins.branch.NamedExceptionsBranchPropertyStrategy_-Named'() {
props(class: 'java.util.Arrays\$ArrayList') {
a(class: 'jenkins.branch.BranchProperty-array') {
'jenkins.branch.NoTriggerBranchProperty'()
}
}
name('master')
}
}
}
}
}
}
}
You can achieve this in two ways
Configuring in UI as explained here, which you said you do not want to do.
Using conditional statements in DSL as explained here
how can i loop through a json file using a FOR loop in groovy? I am able to do it with .each but i am in a situation/bug where i cannot use .each loops.
The json file is being read and parsed into an object.
The json looks like this:
{
"workflows1": {
"name": "/wf_multifolder",
"file": "release1/wf_multifolder.XML",
"folderNames": {
"multifolder": "{{multifolder}}",
"agent1": "{{agentx}}"
}
},
"workflows2": {
"name": "/wf_multifolder",
"file": "release1/wf_multifolder.XML",
"folderNames": {
"multifolder": "{{multifolder}}",
"agent1": "{{agentx}}"
}
}
}
Note: i can modify the json file, if need to make the process simpler.. All i am try to do is to loop throgh and extract the values for the keys.
So given the json in a String like so:
def jsonText = '''{
"workflows1": {
"name": "/wf_multifolder",
"file": "release1/wf_multifolder.XML",
"folderNames": {
"multifolder": "{{multifolder}}",
"agent1": "{{agentx}}"
}
},
"workflows2": {
"name": "/wf_multifolder",
"file": "release1/wf_multifolder.XML",
"folderNames": {
"multifolder": "{{multifolder}}",
"agent1": "{{agentx}}"
}
}
}'''
You can just do:
import groovy.json.*
def json = new JsonSlurper().parseText(jsonText)
for(entry in json) {
println "$entry.key has file $entry.value.file"
}
to print:
workflows1 has file release1/wf_multifolder.XML
workflows2 has file release1/wf_multifolder.XML
Im new to Groovy and Im working on a Grails app. I need to make a SOAP call so Im using the wslite package, but the following code doesn't appear to do anything:
def client = new SOAPClient(apiEndpoint)
println "SOAP client is ${client.dump()}"
try {
def response = client.send(SOAPAction: 'GetService') {
body {
"Request" {
"Username"(credentials.userId)
"Password"(credentials.password)
"Param1"(code)
"Param2"(location)
"Items" {
"Item" {
"ItemParam1"("some data")
"ItemParam2"(some more data)
}
}
}
}
}
}
} catch (SOAPFaultException sfe) {
println "${sfe.dump()}"
} catch (SOAPClientException sce) {
println "${sce.dump()}"
}
println "${response.dump()}"
The first println works but then nothing after that.
By adding a catch all for exceptions I was able to see the problem in the markup.