Change default github branch with Octokit.net - github-api

I have a small project for updating various settings in GitHub repos. Although I find the repo, the Edit method throws NotFoundException. Any suggestions what could be wrong?
I'm changing from "master" to "develop", both exists.
public async Task<Repository> SetDefaultBranch(string repository, string branch)
{
//var repo = await _client.Repository.Get(_owner, update.Name);
// the repo is found, but afterwards I receive NotFoundException on Edit
var update = new RepositoryUpdate(repository) { DefaultBranch = branch };
return await _client.Repository.Edit(_owner, repository, update);
}

You have to check if you have permissions (as usual). If there is no "Settings" in the navigation breadcrumb, you don't have permission.
Why Octokit.net returns NotFoundException, rather than AuthorizationException is another story.

Related

Why does DocumentHelper.CopyDocument place the new document in "Published" workflow step?

I'm trying to copy a document using the Kentico 11 API and when using Document.CopyDocument the resulting TreeNode is automatically placed into the "Published" workflow step instead of "Edit" as one would expect. Is there any way to prevent this from happening?
I've tried both methods for DocumentHelper.CopyDocument including the method using CopyDocumentSettings, neither give the option to specify the initial workflow step.
TreeNode newDoc = DocumentHelper.CopyDocument(document, parentDoc, true, _treeProvider);
// also tried this
TreeNode newDoc = DocumentHelper.CopyDocument(new CopyDocumentSettings(document, parentDoc, _treeProvider));
Neither give me the option to specify workflow step, they only push it into "published". I can create a new version of the document, but that doesn't prevent the copy from being published initially.
I believe it copies the workflow status of the current page but I could be wrong. What I'd suggest is below. This code checks to see if the page is using check in/out and then uses workflow to set the newly created page to the first step in the workflow (which should be "edit").
TreeProvider tree = new TreeProvider();
var treeNode = DocumentHelper.GetDocument(4, tree);
var targetNode = DocumentHelper.GetDocument(5, tree);
CMS.DocumentEngine.TreeNode newPage = DocumentHelper.CopyDocument(treeNode, targetNode, false);
WorkflowManager workflowManager = WorkflowManager.GetInstance(tree);
WorkflowInfo workflow = workflowManager.GetNodeWorkflow(page);
if (!newPage.IsCheckedOut)
{
newPage.CheckOut();
}
if (workflow != null)
{
if (!workflow.WorkflowAutoPublishChanges)
{
newPage.MoveToFirstStep("Copying the page from another page.");
}
}
if (newPage.IsCheckedOut)
{
newPage.CheckIn();
}
I should have waited longer to post this question, turns out you can use the WorkflowManager in the API to push the document to the first step in the workflow:
newDoc.WorkflowManager.MoveToFirstStep(newDoc);
That pushes the document to the "Edit" step without affecting the workflow history, its as if the document was never published.

Data updated in Starcounter DB, push updates to active sessions?

Say for example I have a something like this declared in Starcounter
[Database]
public class User
{
public string Username;
public string Email;
}
I have a page listing one row from the DB with an update button with PuppetJS and all working fine.
If I change the value from another session or directly in the DB, is there anyway to directly update the values to any client who are active by pushing the new values to the clients?
*** Edit:
I added following to my TestPage.json.cs file :
void Handle(Input.Update action)
{
Transaction.Commit();
Session.ForAll(s =>
{
if (s.Data is TestPage)
s.CalculatePatchAndPushOnWebSocket();
});
}
This push updates directly to other sessions nicely. Still wonder if there is some better way to do this.
The code that you've presented in your edit is exactly the way to go:
void Handle(Input.Update action)
{
Transaction.Commit();
Session.ForAll(s =>
{
if (s.Data is TestPage)
s.CalculatePatchAndPushOnWebSocket();
});
}
What it does is:
commit changes to db
for every running session, check if that session has a TestPage instance attached to it
if the above is positive, revaluate the bound data and send patches if required
More about pushing changes over WebSocket can be found here: http://starcounter.io/guides/web/sessions/.

get old item name for renamed files using TFS API

My current tfs will be retired in next few months.I am using tfs api to create a parallel tfs on a new server from the existing one. I have folders and solutions that have been renamed. I am iterating items and based on their changetype(add, edit, delete, sourcerename etc), I am checking them in destination tfs.
I am not able to get Old filename for a file, in order to use PendRename when the item that is being iterated is Delete|SourceRename or Rename.
I tried the mentioned solution :
https://social.msdn.microsoft.com/Forums/vstudio/en-US/f9c7e7b4-b05f-4d3e-b8ea-cfbd316ef737/how-to-get-previous-path-of-renamedmoved-of-file-using-tfs-api?forum=tfsgeneral
But, my changeset has a lot of changes and hence identifying a particular file seems difficult.
Do we have something that interraltes two items (the deleted and renamed) ones other than the changeset, because there needs to be a uniquely identifier that associated the two items so that they may appear together in TFS history?
I read this at someplace and modified it for my purpose so that I can get parent branch for a given server item :
ItemSpec itemSpec = new ItemSpec(serverItem, RecursionType.None);
BranchHistoryTreeItem[][] results = sourceTFSHelper.VCS.GetBranchHistory(
new ItemSpec[] { itemSpec }, VersionSpec.Latest);
BranchHistoryTreeItem[] thisBranchResults = results[0];
foreach (BranchHistoryTreeItem treeItem in thisBranchResults)
{
BranchRelative requestedItem = FindRequestedItem(treeItem);
if (requestedItem != null)
{
return (requestedItem.BranchFromItem == null) ? null : requestedItem.BranchFromItem.ServerItem;
}
}
The support from TFS API can be definitely improved here. For the solution, you will have to look for the associated Delete change instance which is stored in the MergeSources container.
Below I pasted some sample code by which hopefully you will be inspired.
var changeInstances = VersionControlSvr.GetChangesForChangeset(
%YourChangesetId%,
false,
Int32.MaxValue,
null,
null,
true);
foreach (Change changeInstance in changeInstances)
{
Console.WriteLine(changeInstance);
//if (changeInstance.MergeSources != null && changeInstance.MergeSources.Count > 0)
//{
// var source = changeInstance.MergeSources.FirstOrDefault();
// Console.WriteLine($"{changeInstance.Item.ServerItem}");
// Console.WriteLine("was renamed from:");
// Console.WriteLine($"{source.ServerItem}");
// Console.WriteLine("::::::::::::::::::::::::::::::::::::::::::::::::::::::");
//}
}

Orchard Contrib.Taxonomies - permalinks in conflict when removing drafts

Previewing and Rejecting drafts using Contrib.Taxonomies in Orchard:
I have a requirement to preview (and if necessary, reject) taxonomy terms after an import.
I have attempted to do this with the following code:
A. I have changed the last line of the standard Import code to say:
Services.ContentManager.Create(term, VersionOptions.Draft);
(instead of VersionOptions.Published)
B. My preview screen has a "Publish" button, with a controller action with this code:
// remove current published terms
foreach (var term in _taxonomyService.GetTerms(taxonomyId)) {
_taxonomyService.DeleteTerm(term);
}
// publish draft terms created at import
foreach (var draftTerm in _MyTaxonomyService.GetDraftTerms(taxonomyId)) {
Services.ContentManager.Publish(draftTerm.As<ContentItem>());
}
C. The preview screen also has a "Reject" button, with a controller action with this code:
// delete drafts
foreach (var draftTerm in _MyTaxonomyService.GetDraftTerms(taxonomyId)) {
_taxonomyService.DeleteTerm(draftTerm);
}
With the above code, I can "Publish" as many times as I like and it works as expected.
However, if I "Reject" and then Publish I get "Permalinks in conflict" for the top-level term.
How can I remove the drafts without getting a permalinks conflict the next time I publish?
(Also note that removing drafts should not effect permalinks/display aliases of the current published terms).
Ok, the fundamental problem was that _taxonomyService.DeleteTerm() was firing the AutoRouteHandler.OnRemoved() event method.
This event method will call down the stack to AliasStorage.Remove(), which only takes path/aliassource as arguments, i.e. it will remove ALL AliasRecords that match the given path, i.e. aliases which the published version is still dependent on!
My solution to this problem was to replace the _taxonomyService.DeleteTerm() call with my own custom method _MyTaxonomyService.DeleteDraftTerm() for deleting draft terms. Note the removal of the AutoRoutePartHandler from the handler event calls:
public void DeleteDraftTerm(ContentItem contentItem)
{
var draftVersions = _contentItemVersionRepository.Fetch(x => x.ContentItemRecord == contentItem.Record && x.Latest && !x.Published);
var context = new RemoveContentContext(contentItem);
_handlers.Value.Invoke(handler => handler.Removing(context), Logger);
foreach (var version in draftVersions)
{
if (version.Latest)
{
version.Latest = false;
}
}
// comment this line out and replace with lines below
// _handlers.Value.Invoke(handler => handler.Removed(context), Logger);
var handlersExceptAutoRoute = _handlers.Value.Where(x => x.GetType() != typeof(AutoroutePartHandler));
handlersExceptAutoRoute.Invoke(handler => handler.Removed(context), Logger);
}
Tbh, I'm not really sure why this is necessary though. Before I added this new code, when I removed a draft content item with an autoroute part it seemed to result in there being no aliasrecords, but still having published autoroutepart records. So AutoRoutePartHandler.ProcessAlias() would think there was a duplicate published path and raise the conflict warning.

How to set new Orchard module to be Home Page via code

I'm very new with orchard.
To learn orchard module development, I followed the documentation and tried to create a commerce module.
The module consists of product part and product type which has product part.
During enable module, it will create admin and home menu for this module, "Commerce" and "Shop" respectively.
My questions are
How do I make this module to be home page during enable module. In other word, I want Index method of
the module's HomeController handle home url?
How do I get Shop menu in front end to be after home menu or register this module to home menu?
I am attaching source code, please download it from the following link
download source code
To take over the home page the standard Orchard way is to implement IHomePageProvider.
You can, when creating a page as part of migrations.cs in a module, tell the Autoroute part to set your created page's alias as the homepage:
//create a page page
var homepage = _contentManager.Create("Page");
homepage.As<TitlePart>().Title = "My Home";
_contentManager.Publish(homepage);
var homePageArp = homepage.As<AutoroutePart>();
homePageArp.DisplayAlias = String.Empty;
_autorouteService.PublishAlias(homePageArp);
This assumes you're going from a clean instance of Orchard without any prior homepages; if you have an existing homepage, you'll have to regenerate those pages' Aliases as part of your module too. This is how it's done as part of the AutoroutePartHandler in the Orchard.Autoroute project (inside the Publish Alias method):
// regenerate the alias for the previous home page
var currentHomePages = _orchardServices.ContentManager.Query<AutoroutePart, AutoroutePartRecord>().Where(x => x.DisplayAlias == "").List();
foreach (var current in currentHomePages) {
if (current != null) {
current.CustomPattern = String.Empty; // force the regeneration
current.DisplayAlias = _autorouteService.Value.GenerateAlias(current);
}
_autorouteService.Value.PublishAlias(current);
}
_autorouteService.Value.PublishAlias(part);
If you dig through the driver and handler for the autoroute project, you'll learn a lot about the internals; when you tick that "set as homepage" box in the Admin UI, it sets the Path to "/" and then that gets picked up, triggers the old homepage re-wire, clears the "/" path to String.Empty and then publishes that blank alias, giving you a new homepage.
(this is valid as of Orchard 1.6)
If your module is to be used by others, then it is better to make a widget which can be added to any layer (the homepage layer for example). That way each user can decide where your module comes into play.
If you are using this module for yourself only, then you can just override the default routes (standard mvc functionallity).
Look at my ExtendedRegistration module (Routes.cs) to see how it's done.
Here I am overriding the standard Account/Register URL. There should be nothing preventing you from overriding the default HomeController.
public class Routes : IRouteProvider
{
public void GetRoutes(ICollection<RouteDescriptor> routes)
{
foreach (var routeDescriptor in GetRoutes())
{
routes.Add(routeDescriptor);
}
}
public IEnumerable<RouteDescriptor> GetRoutes()
{
return new[] {
new RouteDescriptor {
Priority = 19,
Route = new Route(
"Users/Account/Register",
new RouteValueDictionary {
{"area", "itWORKS.ExtendedRegistration"},
{"controller", "Account"},
{"action", "Register"}
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", "itWORKS.ExtendedRegistration"}
},
new MvcRouteHandler())
}
};
}
}

Resources