API Request in Apex
As one of my repositories hosted in GitLab needed to rebuild periodically to handle static content and keep important data up to date, one way I considered was create a CI/CD pipeline job that could be triggered by an API request from Salesforce whenever new records are added to its database.
Once the request is sent, the pipeline job will be triggered and the project will be rebuilt, this task allows my static app to make API request as well and then update its contents with freshly fetched data, it sounds a little complicated but I guess that’s a valid approach.
This is the class in Apex code to try to hit the endpoint exposed by GitLab:
public class GitLabPipelineService implements Queueable, Database.AllowsCallouts { private Set<Id> recordIds; public GitLabPipelineService(Set<Id> recordIds) { this.recordIds = recordIds; }
public void execute(QueueableContext context) { /* Load custom metadata. useful to bring secured variables in this case: Token__c, Branch__c and ProjectId__c; */ GitLab_Config__mdt config = GitLab_Config__mdt.getInstance('Default');
Http http = new Http(); HttpRequest request = new HttpRequest();
request.setEndpoint( 'https://gitlab.com/api/v4/projects/'+config.ProjectId__c+'/trigger/pipeline' );
request.setMethod('POST'); request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String body = 'token='+config.Token__c+'&ref='+config.Branch__c+ '&variables[recordCount]='+recordIds.size();
request.setBody(body);
try { HttpResponse response = http.send(request); System.debug('GitLab response: '+response.getBody()); } catch (Exception e) { System.debug('GitLab error: '+e.getMessage()); } }}The required test class, which is essential to deploy the code to production, shows how to perform a test using a mocked callout:
@isTestpublic class GitLabPipelineServiceTest { @isTest static void testTriggerPipeline() { Test.setMock(HttpCalloutMock.class, new GitLabMock());
Test.startTest();
// Create test record Account a=new Account(Name='New Account', Phone='123-456-789'); insert a;
Test.stopTest();
System.assert(true); // basic assertion }
@isTest static void testTriggerPipelineException() { Test.setMock(HttpCalloutMock.class, new GitLabMockExcep());
Test.startTest(); Account a=new Account(Name='New Account', Phone='123-456-789'); insert a;
Test.stopTest();
System.assert(true); }}The following two classes are used by the GitLabPipelineServiceTest class shown above, the first tests a successful
call and the other tests a failure scenario:
//Test when a callout is successfull@isTestglobal class GitLabMock implements HttpCalloutMock { global HttpResponse respond(HttpRequest req) { HttpResponse res = new HttpResponse(); res.setStatusCode(200); res.setBody('{"status":"success"}'); return res; }}//Test when a callout fails@isTestglobal class GitLabMockExcep implements HttpCalloutMock { global HttpResponse respond(HttpRequest req) { throw new CalloutException('Callout failed (Ok!)'); }}And finally, the trigger that makes the GitLab pipeline job run via an API call:
trigger GitLabPipelineService_Trigger on Account (after insert) { if (Trigger.isAfter && Trigger.isInsert) { System.enqueueJob(new GitLabPipelineService(Trigger.newMap.keySet())); }}Decided to write this here for informational and study purposes, and I think it’s very valuable.