databasemongodbshellcommand-linemongodb-java

mongodb script file


I am newbie to mongodb. Maven Dependencies

Add the following to your pom.xml:

xml

Copy code

org.apache.httpcomponents httpclient 4.5.13 com.fasterxml.jackson.core jackson-databind 2.11.0

  1. Java Class to Connect to Jira and Validate Change Window

java

Copy code

importcom.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.http.HttpHeaders; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Base64; import java.util.Date; public class JiraChangeWindowChecker { public static void main(String[] args) throws Exception { // Inputs String jiraBaseUrl = "https://your-domain.atlassian.net"; String issueKey = "ABC-123"; String username = "your-email@example.com"; String apiToken = "your-jira-api-token"; String startField = "customfield_10001"; // Change Start String endField = "customfield_10002"; // Change End // Create Auth Header String auth = username + ":" + apiToken; String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8)); // Create HTTP Client and GET Request CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet request = new HttpGet(jiraBaseUrl + "/rest/api/2/issue/" + issueKey); request.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodedAuth); request.setHeader(HttpHeaders.ACCEPT, "application/json"); // Execute Request CloseableHttpResponse response = httpClient.execute(request); String jsonResponse = EntityUtils.toString(response.getEntity()); // Parse JSON ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(jsonResponse); String startTimeStr = root.path("fields").path(startField).asText(); String endTimeStr = root.path("fields").path(endField).asText(); // Parse Timestamps SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); Date now = new Date(); Date startTime = sdf.parse(startTimeStr.replaceAll("Z$", "+0000")); Date endTime = sdf.parse(endTimeStr.replaceAll("Z$", "+0000")); // Validate Window if (now.after(startTime) && now.before(endTime)) { System.out.println("Within change window. Proceeding."); } else { System.out.println("Outside change window. Blocking."); System.exit(1); // Exit with error code to fail UCD step } // Close resources response.close(); httpClient.close(); } }

  1. How to Use in UCD Plugin

Package this as a .jar inside the lib/ directory of your plugin.

In plugin.xml, define a step that runs this Java class using:

xml

Copy code

java -cp lib/jira-checker.jar com.example.JiraChangeWindowChecker

Need help creating the full plugin ZIP or mapping custom field IDs?

Let me know — I can generate the plugin structure for you or help extract field IDs from a sample Jira issue.

4o

You said:

Yes

ChatGPT said:

Great! Let's walk through how to build a custom UrbanCode Deploy plugin that connects to Jira and checks if the current time is within a change window, using the Java code provided earlier.

Overview of Plugin Structure

Your plugin should have the following layout:

pgsql

Copy code

jira-change-window-checker/ ├── lib/ │ └── jira-checker.jar <-- Compiled Java code ├── plugin.xml <-- Plugin definition └── version.txt <-- Plugin version

Step 1: Compile the Java Code into a JAR

Save the earlier Java code as:JiraChangeWindowChecker.java under a package like com.example.

Use Maven or manually compile:

bash

Copy code

mkdir -p com/example mv JiraChangeWindowChecker.java com/example/ javac -cp .:jackson-databind-2.11.0.jar:httpclient-4.5.13.jar \ com/example/JiraChangeWindowChecker.java jar cf jira-checker.jar com/

Place jira-checker.jar inside the lib/ folder.

Step 2: Create plugin.xml

xml

Copy code

jira-change-window-checker 1.0.0 Jira Change Window Checker Checks if current time is within the Jira ticket's change window. Check Change Window Validates change window from Jira and proceeds or blocks execution. java -cp lib/jira-checker.jar:lib/* com.example.JiraChangeWindowChecker

Step 3: Create version.txt

txt

Copy code

1.0.0

Step 4: Package the Plugin

bash

Copy code

zip -r jira-change-window-checker.zip jira-change-window-checker/

Step 5: Install in UrbanCode Deploy

Go to Settings > Automation Plugins in UCD.

Click Load Plugin, and upload jira-change-window-checker.zip.

The step "Check Change Window" will appear in the palette.

Optional Enhancement: Read UCD Properties

If you want the Java class to accept UCD properties passed from plugin.xml, modify the Java code to read command-line args or environment variables for inputs like jiraUrl, jiraUser, etc.

Would you like me to generate a working .zip plugin example you can directly test or customize?

4o

You said:

Yes

ChatGPT said:

Here is your custom UrbanCode Deploy plugin package:

Download jira-change-window-checker.zip

This ZIP includes:

plugin.xml defining the plugin and its step.

version.txt with version info.

A placeholder lib/README.txt where your compiled jira-checker.jar should go.

Next Steps:

Compile your Java class and place the JAR in lib/jira-checker.jar.

Re-zip the plugin folder if you made changes.

Upload the ZIP to UrbanCode Deploy via Settings > Automation Plugins > Load Plugin.

Let me know if you want me to generate the .jar as well from your Java code or include real field mappings from a Jira ticket. ​​

4o We can execute list of queries by specifying it in a script .sql file in relational db and can run it by running the command source c:\test.sql.

How can we do that in mongodb? how to store those commands in mongodb script and how to execute in mongodb?

org.apache.httpcomponents httpclient 4.5.13 com.fasterxml.jackson.core jackson-databind 2.11.0

,--------- Overview of Plugin Structure Your plugin should have the following layout:

pgsql Copy code jira-change-window-checker/ ├── lib/ │ └── jira-checker.jar <-- Compiled Java code ├── plugin.xml <-- Plugin definition └── version.txt <-- Plugin version Step 1: Compile the Java Code into a JAR Save the earlier Java code as: JiraChangeWindowChecker.java under a package like com.example.

Use Maven or manually compile:

bash Copy code mkdir -p com/example mv JiraChangeWindowChecker.java com/example/

javac -cp .:jackson-databind-2.11.0.jar:httpclient-4.5.13.jar
com/example/JiraChangeWindowChecker.java

jar cf jira-checker.jar com/ Place jira-checker.jar inside the lib/ folder.

Step 2: Create plugin.xml xml Copy code jira-change-window-checker 1.0.0 Jira Change Window Checker Checks if current time is within the Jira ticket's change window. Check Change Window Validates change window from Jira and proceeds or blocks execution. java -cp lib/jira-checker.jar:lib/* com.example.JiraChangeWindowChecker Step 3: Create version.txt txt Copy code 1.0.0 Step 4: Package the Plugin bash Copy code zip -r jira-change-window-checker.zip jira-change-window-checker/ Step 5: Install in UrbanCode Deploy Go to Settings > Automation Plugins in UCD.

Click Load Plugin, and upload jira-change-window-checker.zip.

The step "Check Change Window" will appear in the palette.

Optional Enhancement: Read UCD Properties If you want the Java class to accept UCD properties passed from plugin.xml, modify the Java code to read command-line args or environment variables for inputs like jiraUrl, jiraUser,


Solution

  • You can do it with shell script. You can execute the commands using the following command.

    ./mongo server:27017/dbname --quiet my_commands.js
    

    For details check Scripting the shell document in Mongo docs.