Giter VIP home page Giter VIP logo

Comments (3)

hkarthik7 avatar hkarthik7 commented on June 15, 2024

Hiya @tanrikut, support for push changes api is not added to the library, sorry about that. I will add it in next version release. Meanwhile you can use RestClient, it has few overload methods that creates the request URL and gets the reponse from Api. You can read about it more here.

Here is the sample code to get started. I've used the example from documentation.

var webApi = new AzDClientApi("org", "proj", "token");
var git = webApi.getGitApi();
var repo = git.getRepository("myRepository");
var refUpdate = new GitRefUpdate();
refUpdate.setName("refs/heads/myfirstbranch");
refUpdate.setOldObjectId("b92a68a4cd54506d0d8e264ddddbfe5076dca910");

// Create commits
var commits = new GitCommitRefs();
var commit = new GitCommitRef();

// Commit changes
var commitChange = new GitChange();
commitChange.setChangeType(VersionControlChangeType.EDIT);
commitChange.setItem("/tasks/content/activetasks.md");
commitChange.setNewContent(new ItemContent(){{
    setContent("# My Active Tasks\\n\\n* Item 1\\n* Item 2\\n* Item 3\\n* Item 4\\n* Item 5\\n");
    setContentType(ItemContentType.RAWTEXT);
}});

// Set commit properties
commit.setComment("Updating active tasks, but saving in a new branch.");
commit.setChanges(List.of(commitChange));

// Set the commits
commits.setGitCommitRefs(List.of(commit));
var requestBody = new HashMap<String, Object>(){{
   put("refUpdates", refUpdate);
   put("commits", commits);
}};

var response = RestClient.send(RequestMethod.POST, webApi.getConnection(), ResourceId.GIT, 
webApi.getConnection().getProject(), "git/repositories",  repo.getId(), null, "7.0", 
null,  requestBody, CustomHeader.JSON_CONTENT_TYPE);

// Unmarshal the json
var mapper = new JsonMapper();
mapper.mapJsonResponse(response, GitPush.class); // GitPush class doesn't exists in the library. You have to use the scrape.py to get the definition files and add it your project.

GitPush type definition doesn't exists in the library, you can use scrape.py under tools and extract the type definitions. Then you can deserialise the json response to POJO.

Let me know how it goes.

Cheers.

from azure-devops-java-sdk.

hkarthik7 avatar hkarthik7 commented on June 15, 2024

@tanrikut, version 5.0.6 is released and has support to git push. You can do something like below.

var webApi = new AzDClientApi("org", "proj", "token"); // Here token is your personal access token.
var git = webApi.getGitApi();

var repository = git.getRepository("myRepository");

var gitPush = new GitPush();

var refUpdate = new GitRefUpdate();

refUpdate.setName("refs/heads/myfirstbranch");
refUpdate.setOldObjectId("b92a68a4cd54506d0d8e264ddddbfe5076dca910");

// Create commits
var commit = new GitCommitRef();

// Commit changes
var commitChange = new GitChange();
commitChange.setChangeType(VersionControlChangeType.EDIT);
commitChange.setItem("/tasks/content/activetasks.md");
commitChange.setNewContent(new ItemContent(){{
    setContent("# My Active Tasks\\n\\n* Item 1\\n* Item 2\\n* Item 3\\n* Item 4\\n* Item 5\\n");
    setContentType(ItemContentType.RAWTEXT);
}});

// Set commit properties
commit.setComment("Updating active tasks, but saving in a new branch.");
commit.setChanges(List.of(commitChange));

gitPush.setRefUpdates(List.of(refUpdate));
gitPush.setCommits(List.of(commit));

git.createPush(repository.getId(), gitPush);

I'm closing the issue for now.

from azure-devops-java-sdk.

tanrikut avatar tanrikut commented on June 15, 2024

Thanks, well done

from azure-devops-java-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.