Creating a release info dashboard
Tags: javascript, git, tutorial
Published: 2020-02-13 23:03:44

Hopefully, you are in the position of having an online software product/website with multiple clients. The problem you run into when deploying to multiple clients and environments is keeping track of which release is where. Let’s also assume that you have testing, stage, and production environments for each client; that quickly becomes a headache.

In this post, we look at how to create a simple dashboard for displaying this info. There are three basic steps:

1. Use the git-commit-id-plugin maven plugin in your project to output the brach and commit information to a file.
2. Create an HTTP endpoint for retrieving the deployed branch.
3. Create a page that will invoke these endpoints and display them in a table.

**Creating git properties**

The first step is to make our project create a file with the git repository info at build time. To accomplish this, we will use the [git-commit-id-plugin](https://github.com/git-commit-id/git-commit-id-maven-plugin) plugin. Let’s add the plugin in our pom.xml file:

```xml
<plugins>
      <plugin>
        <groupId>pl.project13.maven</groupId>
        <artifactId>git-commit-id-plugin</artifactId>
        <version>4.0.0</version>
        <executions>
          <execution>
            <id>get-the-git-infos</id>
            <goals>
              <goal>revision</goal>
            </goals>
            <phase>initialize</phase>
          </execution>
        </executions>
        <configuration>
          <generateGitPropertiesFile>true</generateGitPropertiesFile>
          <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
          <commitIdGenerationMode>full</commitIdGenerationMode>
          <format>properties</format>
        </configuration>
      </plugin>
</plugins>
```

When we do a maven build, a file named git.properties will be created that will have all of the information about the branch, build, and last commit info.

**Creating the endpoint**

The next step is to expose an HTTP endpoint in our application that will use git.properties to return the current git branch. We will use [Wicket-rest annotations](https://github.com/wicketstuff/core/tree/master/wicketstuff-restannotations-parent) which is a great library for creating REST endpoints.

```java
public class VersionDetailsEndpoints extends AbstractRestResource<JsonWebSerialDeserial> {
    public VersionDetailsEndpoints() {
        super(new JsonWebSerialDeserial(new JacksonObjectSerialDeserial()));
    }
    @MethodMapping(value = "/")
    public String details() {
        getCurrentWebResponse().addHeader("Access-Control-Allow-Origin", "*");
        return ApplicationDetailsService.getBranch();
    }
}
```

[ApplicationDetailsService](https://github.com/RomanSery/codesnippets/blob/master/src/main/java/org/coderdreams/service/ApplicationDetailsService.java) is a simple utility class that reads and parses the generated git.properties file from our maven build process. We also set the [CORS header](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin) to allow invoking this endpoint using Javascript from a different domain as will be necessary for the last step.

We register it in our WebApplication init() method by mounting a resource:
```java
mountResource("/releaseinfo", new ResourceReference("/releaseinfo") {            
    VersionDetailsEndpoints versionDetailsEndpoints = new VersionDetailsEndpoints();
    @Override
    public IResource getResource() {
    Injector.get().inject(versionDetailsEndpoints);
    return versionDetailsEndpoints;
    }
});
```

**Creating the dashboard**

Now the fun part! We will use the Javascript fetch API to iterate a map of clients and URL’s, invoking the /releaseinfo endpoint for each and populate a table with the results. The below code will go into the HEAD section of our HTML page:
```javascript
let urlMap = {};
urlMap["ABC Corp"] = ["test.abc.com", "stage.abc.com", "abc.com"];
urlMap["Oracle"] = ["test.oracle-coderdreams.com", "stage.oracle-coderdreams.com", "oracle-coderdreams.com"];
urlMap["Microsoft"] = ["test.ms.com", "stage.ms.com", "ms-coderdreams.com"];
 
const clients = Object.keys(urlMap);
 
$(document).ready(function() {
  clients.forEach(function(client) {
    const clientId = client.replace(/ /g, "_");
 
    $("#releasesTable").append('<tr id="' + clientId + '"><td>' + client + "</td><td></td><td></td><td></td><td></td></tr>");
 
    urlMap[client].forEach(function(url, index) {
      if (url.length == 0) {
        return;
      }
      const tdNum = index + 2;
      const fullUrl = "http://" + url + "/releaseinfo";
      getReleaseBranch(fullUrl)
        .then(response => {
          $("#" + clientId + " td:nth-child(" + tdNum + ")").html(response);
        })
        .catch(e => {
          $("#" + clientId + " td:nth-child(" + tdNum + ")").html("ERR");
          $("#" + clientId + " td:nth-child(" + tdNum + ")").addClass("text-danger");
        });
    });
  });
});
 
async function getReleaseBranch(url) {
  let response = await fetch(url, { cache: "no-store" });
  return await response.json();
}
```

We need to make sure that the fetch call does not hit the browser cache which is done by passing the “no-store” argument. If we fail to get a response from the endpoint we highlight that in the table as an error. 

You can also add other useful info to this dashboard such as database connectivity, the status of some ‘health’ check, number of active users; use your imagination!

The full dashboard code along with the HTML can be found on [Github](https://github.com/RomanSery/codesnippets/blob/master/src/main/java/org/coderdreams/webapp/release_info.html).