Steps:
1) You have to create a GitHub account, I already have my GitHub account, so I will move ahead.
2) You have to download the git-bash(https://git-scm.com/downloads). you have to setup Username Password in gitbash of your github account.
3) Then open your chrome or any other browser, search for https://api.github.com/ .It will give you like this :
To get GitHub Data into Salesforce portal.
1) Login to Salesforce. Go to Setup -> In the quick find box -> search 'External data source'.
2) Create new External data source with name GitHub_Repos .
3) Create a Github tab like this:
4) Create Apex Classes and paste the below code on your developer console.
GitHubProvider.cls :-
global with sharing class GitHubProvider extends DataSource.Provider{
override global List<DataSource.Capability> getCapabilities() {
List<DataSource.Capability> capabilities = new List<DataSource.Capability>();
capabilities.add(DataSource.Capability.ROW_QUERY);
return capabilities;
}
override global List<DataSource.AuthenticationCapability> getAuthenticationCapabilities() {
List<DataSource.AuthenticationCapability> capabilities =
new List<DataSource.AuthenticationCapability>();
capabilities.add(DataSource.AuthenticationCapability.ANONYMOUS);
return capabilities;
}
override global DataSource.Connection getConnection(DataSource.ConnectionParams connectionsParams) {
return new GitHubConnection();
}
}
GitHubConnection.cls :-
global with sharing class GitHubConnection extends DataSource.Connection{
override global List<DataSource.Table> sync() {
List<DataSource.Column> gitHubRepoColumns = new List<DataSource.Column>();
gitHubRepoColumns.add(DataSource.Column.text('ExternalId', 255));
gitHubRepoColumns.add(DataSource.Column.url('DisplayUrl'));
gitHubRepoColumns.add(DataSource.Column.text('Name', 128));
gitHubRepoColumns.add(DataSource.Column.text('Description', 1024));
gitHubRepoColumns.add(DataSource.Column.number('Stars', 16, 0));
List<DataSource.Table> tables = new List<DataSource.Table>();
tables.add(DataSource.Table.get('GitHub Repos', 'Name', gitHubRepoColumns));
return tables;
}
override global DataSource.TableResult query(DataSource.QueryContext queryContext) {
List<Map<String, Object>> repos =
DataSource.QueryUtils.process(queryContext, getOrgRepos('<USERNAME>'));
DataSource.TableResult tableResult = DataSource.TableResult.get(queryContext, repos);
return tableResult;
}
override global List<DataSource.TableResult> search(DataSource.SearchContext searchContext) {
return DataSource.SearchUtils.searchByName(searchContext, this);
}
public List<Map<String, Object>> getOrgRepos(String org) {
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
String url = 'https://api.github.com/orgs/' + org + '/repos?per_page=1000';
request.setEndPoint(url);
request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
List<Map<String, Object>> repos = new List<Map<String, Object>>();
for (Object item : (List<Object>)JSON.deserializeUntyped(response.getBody())) {
Map<String, Object> repo = (Map<String, Object>)item;
repo.put('ExternalId', repo.get('id'));
repo.put('DisplayUrl', repo.get('html_url'));
repo.put('Name', repo.get('name'));
repo.put('Description', repo.get('description'));
repo.put('Stars', repo.get('stargazers_count'));
repos.add(repo);
}
return repos;
}
}
5) Go to App launcher and search for GitHub Repos.
It will automatically brings all the files which are in the query.
6) To Create new Branch from the Parent/Main branch. You have to just call the Api.
postGitHub.cls :-
public with sharing class postGitHub {
public static String postGit(){
try {
String json = '{ "ref": "refs/heads/Username", "sha": "SHA" }'; // you have to put your head.
Http http = new Http();
HttpRequest request = new HttpRequest();
//you have to put the endpoint of your repository.
request.setEndPoint('https://api.github.com/repos/<USERNAME>/<REPO>/git/refs');
request.setMethod('POST');
request.setHeader('Authorization',"Token " + 'YOUR_TOKEN_HERE');
//
request.setBody(json);
HttpResponse response = new HttpResponse();
response = http.send(request);
System.debug(response.getStatusCode());
System.debug(response);
} catch (Exception e) {
System.debug(e.getMessage());
}
return 'success';
}
}
After that you can check a new branch is created into your github portal.
DEMO :-
Comments
Post a Comment
Please Write your comment here.