Push information from a table to GitHub

Christoph Marku
Tera Contributor

Hi,

 

what is the best way to push information from a table to GitHub? I wish to send information and files from a table in ServiceNow to GitHub.

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @Christoph Marku ,
I trust you are doing fine.

  1. Retrieve data from the ServiceNow table: You can use ServiceNow's GlideRecord API to query the table and retrieve the desired information. Here's an example code snippet in Javascript:

 

var table = 'your_table_name';
var gr = new GlideRecord(table);
gr.query(); // Add conditions if needed

while (gr.next()) {
  var data = gr.getValue('column_name'); // Retrieve specific column data
  // Process the data as per your requirements
  // You can also retrieve multiple columns and create a JSON payload
}

 

  • Format the data: Depending on your requirements, you may need to format the data retrieved from ServiceNow into a suitable format for GitHub. This could be JSON, CSV, or any other format supported by GitHub's API.

  • Push data to GitHub: Use GitHub's API to create a new file or update an existing file with the formatted data. You'll need to authenticate with your GitHub account and have appropriate permissions for the repository. Here's an example code snippet using GitHub's REST API in Javascript:

 

var githubToken = 'your_github_token';
var repository = 'owner/repo'; // Replace with your repository details
var filePath = 'path/to/file.txt'; // Replace with the desired file path

var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('PUT');
request.setRequestHeader('Authorization', 'Bearer ' + githubToken);
request.setEndpoint('https://api.github.com/repos/' + repository + '/contents/' + filePath);

var content = 'Your formatted data'; // Replace with the formatted data
var contentBase64 = GlideStringUtil.base64Encode(content);

var requestBody = {
  "message": "Commit message",
  "content": contentBase64
};

request.setRequestBody(JSON.stringify(requestBody));
var response = request.execute();
var responseBody = response.getBody();

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

5 REPLIES 5

Amit Gujarathi
Giga Sage
Giga Sage

HI @Christoph Marku ,
I trust you are doing fine.

  1. Retrieve data from the ServiceNow table: You can use ServiceNow's GlideRecord API to query the table and retrieve the desired information. Here's an example code snippet in Javascript:

 

var table = 'your_table_name';
var gr = new GlideRecord(table);
gr.query(); // Add conditions if needed

while (gr.next()) {
  var data = gr.getValue('column_name'); // Retrieve specific column data
  // Process the data as per your requirements
  // You can also retrieve multiple columns and create a JSON payload
}

 

  • Format the data: Depending on your requirements, you may need to format the data retrieved from ServiceNow into a suitable format for GitHub. This could be JSON, CSV, or any other format supported by GitHub's API.

  • Push data to GitHub: Use GitHub's API to create a new file or update an existing file with the formatted data. You'll need to authenticate with your GitHub account and have appropriate permissions for the repository. Here's an example code snippet using GitHub's REST API in Javascript:

 

var githubToken = 'your_github_token';
var repository = 'owner/repo'; // Replace with your repository details
var filePath = 'path/to/file.txt'; // Replace with the desired file path

var request = new sn_ws.RESTMessageV2();
request.setHttpMethod('PUT');
request.setRequestHeader('Authorization', 'Bearer ' + githubToken);
request.setEndpoint('https://api.github.com/repos/' + repository + '/contents/' + filePath);

var content = 'Your formatted data'; // Replace with the formatted data
var contentBase64 = GlideStringUtil.base64Encode(content);

var requestBody = {
  "message": "Commit message",
  "content": contentBase64
};

request.setRequestBody(JSON.stringify(requestBody));
var response = request.execute();
var responseBody = response.getBody();

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi Amit, thank you for that response. Is there any more simple way to do this (e.g. OOB solution)?

Hi @Amit Gujarathi ,
Thank you for the explanation,
Do you have any code template for creating a new .json file in the GitHub, Rather than giving the path to the current file, it should a new .json file.

Mathieu Lepoutr
Mega Guru

Hi Christoph

 

It's my understanding that you can set up webhooks in SNOW to trigger events whenever there are updates to the table. These events can then be used to trigger API calls to GitHub, which can create issues, update documentation, or upload files. GitHub's API provides endpoints for various actions, allowing you to programmatically manage your repositories.

 

Another route would be to use Exalate. I have been using it for quite a while and it instantly jumped in my thoughts reading this post. It is an integration solution that works super fast in my opinion. You can granuarly decide what information gets send, like your SNOW table data.

Hope to have helped you and have a great day!