Branching From YouTrack
I have been working on a solution to create branches directly from an YouTrack issue.
I have been able to write an action that does this with my GitLab server, by using GitLab's APIs while only branching from main. What I'd like to be able to do is select a "Branch From?" in YouTrack then use that branch in order to automatically create the new branch in GitLab. This is where I am stuck.
I've thought of a couple options but not sure if I am capable to do them.
- Part of the Action gives users a list of available branches. This could be populated from an API request to GitLab or
- Intercept the VCS changes coming from GitLab on my existing integration and create a list of branches as an EnumField
Not sure if anyone has tried anything similar, looking over the documentation I don't believe either of these are possible and wanted to reach out here.
Please sign in to leave a comment.
Hi!
I'm Sergey from the YouTrack team.
We don't have a ready-to-use solution for such a case, yet I just wanted to suggest taking a look at the YouTrack workflow functionality in case you haven't yet. Specifically, there's an HTTP module that lets you send HTTP requests from YouTrack. So, for example, once you select a specific enum value in an issue, you can send an HTTP API request to GitLab to create a new branch (basically, it works like a webhook). Alternatively, you can retrieve and process external data as well. This HTTP module can also be used to send requests to a third-party server if you decide to create some app and host it elsewhere.
Additionally, we have a related feature request in our public tracker. If interested, feel free to vote for it. This helps us gauge how much impact this feature will have on our customer base, and you’ll also get subscribed to the issue’s notifications. To do so, sign in to JetBrains YouTrack and hit the thumbs-up icon.
Fred4711
Please try changing this
to this
Add a closing bracket at the last line as well:
Hello Sergey!
I created an action workflow which creates a branch in GitLab, but my issue is allowing a user to choosing the source branch dynamically. I was hoping in my workflow I could make an HTTP request to GitLab to retrieve all of the current branches then display those in YouTrack and have the user select one, but don't believe that is possible.
After some more research it is my understanding that using userInput with an EnumField type will only pull from EnumFields on the issue or project, is that correct? If using the HTTP request to pull branches from GitLab is not possible my second option was going to be keeping a set of branches in a custom field available to each project, then having the user select one of those when creating a branch.
Thanks for your response.
You are correct, that's currently not possible to do dynamically.
An issue's enum field shows a predefined set of values that is added to the fields' set in the project. You can't enter arbitrary values. You should use a string-type field for that.
Yes, you can do it, but it won't be dynamic, though, i.e., it must be predefined. Theoretically, you can update this set of values per some schedule. You can even use an on-schedule workflow and add new values via this createValue method.
Thank you for the additional information and the confirmations.
Last question, can automatically open a link from a workflow action? Something similar to:
Unfortunately, YouTrack workflows don't support any interaction with the browser.
Please let us know if you have further questions.
Sergey Merzlov That's really hard to understand why there isn't a create branch functionality in Youtrack yet. I know at least two customers, who don't move from Jira to Youtrack just because this missing feature. It would just cost too much time, if you read your briefing in the ticket and then move to your Git server, navigate to the project, create a branch, type in the task id etc.
We appreciate your feedback. We totally agree such a feature would improve the overall experience when working with VCS integrations. However, I'm afraid we still can't share any estimations on when and whether this feature is to be implemented. You can read more about how we handle external requests here.
hi, could someone post an example workflow i alwas try diffrent ways and all didnt work.
i use this snippet :
var http = require('http');
exports.rule = {
title: 'Create GitLab branch on issue transition to In Progress',
guard: function(ctx) {
// Trigger: Status des Issues wird auf "In Progress" gesetzt
return ctx.issue.fields.State.name === 'In Progress';
},
action: function(ctx) {
// GitLab API-Details
var gitlabToken = '<your-gitlab-access-token>';
var projectId = '<your-gitlab-project-id>';
var gitlabApiUrl = 'https://gitlab.com/api/v4/projects/' + projectId + '/repository/branches';
// Branch-Name basierend auf der Issue-ID und dem Titel
var issueId = ctx.issue.id;
var branchName = 'issue-' + issueId + '-' + ctx.issue.summary.replace(/\s+/g, '-').toLowerCase();
// Erstellen einer HTTP-POST-Anfrage an die GitLab API
http.post(gitlabApiUrl, {
headers: {
'PRIVATE-TOKEN': gitlabToken,
'Content-Type': 'application/json'
},
body: {
branch: branchName,
ref: 'main' // Basisbranch, von dem der neue Branch abzweigt (z. B. "main" oder "master")
}
}, function(response) {
// Überprüfen der API-Antwort
if (response.status === 201) {
ctx.issue.comments.add('Branch `' + branchName + '` wurde erfolgreich in GitLab erstellt.');
} else {
ctx.issue.comments.add('Fehler beim Erstellen des Branches: ' + response.body);
}
});
}
};
and if i want to save this, i get the following message :
An error was encountered while loading the gitlabbranchcreation/createbranchchange module, which was caused by Invalid full name format: http
great thanks a log, no i can save the workflow and get no error, but …
i get the following Message, when i try to change the state to in Progress
Couldn't update issue field. The branch/createbranch rule threw an exception when processing the following issue: 3-1894
how can i check, whats going wrong ?
no it looks like this :
const http = require('@jetbrains/youtrack-scripting-api/http');
const entities = require('@jetbrains/youtrack-scripting-api/entities');
exports.rule = entities.Issue.onChange({
title: 'Create GitLab branch on issue transition to In Progress',
guard: function(ctx) {
// Trigger: Status des Issues wird auf "In Progress" gesetzt
return ctx.issue.fields.State.name === 'In Progress';
},
action: function(ctx) {
// GitLab API-Details
var gitlabToken = 'xyz';
var projectId = '1';
var gitlabApiUrl = 'http://<gitlaburl>/api/v4/projects/' + projectId + '/repository/branches';
// Branch-Name basierend auf der Issue-ID und dem Titel
var issueId = ctx.issue.id;
var branchName = 'issue-' + issueId + '-' + ctx.issue.summary.replace(/\s+/g, '-').toLowerCase();
// Erstellen einer HTTP-POST-Anfrage an die GitLab API
http.post(gitlabApiUrl, {
headers: {
'PRIVATE-TOKEN': gitlabToken,
'Content-Type': 'application/json'
},
body: {
branch: branchName,
ref: 'main' // Basisbranch, von dem der neue Branch abzweigt (z. B. "main" oder "master")
}
}, function(response) {
// Überprüfen der API-Antwort
if (response.status === 201) {
ctx.issue.comments.add('Branch `' + branchName + '` wurde erfolgreich in GitLab erstellt.');
} else {
ctx.issue.comments.add('Fehler beim Erstellen des Branches: ' + response.body);
}
});
}
});
Fred4711 I suppose that the error is TypeError: http.post is not a function. You can find examples of similar workflows here, please make sure that your function is similar to those used there.
i found a solution that works fine if i only change the State from any State to ‘In Progress’
but if i create a new issue, and write something in the title i get this message, and i dont know why.
Couldn't save draft. The branch/vsccretebranch rule threw an exception when processing the following issue: 3-1969
Could someone help ? i know i had to do something with guard, but i didn#t understand how.
Here is the working workflow
const http = require('@jetbrains/youtrack-scripting-api/http');
const entities = require('@jetbrains/youtrack-scripting-api/entities');
const workflow = require('@jetbrains/youtrack-scripting-api/workflow');
exports.rule = entities.Issue.onChange({
title: 'Create GitLab branch on issue transition to In Progress',
// Trigger: Status des Issues wird auf "In Progress" gesetzt
return ctx.issue.fields.State.name === 'In Progress';
},
action: function(ctx) {
var owner = 'Develop'
var repo = 'classic';
var issueId = ctx.issue.id;
var branchName = issueId + '-' + ctx.issue.summary.replace(/\s+/g, '-').toLowerCase();
var gitlabApiUrl = '/api/v1/repos/' + owner + '/' + repo + '/branches?access_token=xxx';
var basebranch ='develop'
const connection = new http.Connection('http://myrepo.zapto.org');
connection.addHeader('Content-Type', 'application/json');
const payload ='{"new_branch_name":"'+branchName+'","old_branch_name":"'+basebranch+'"}';
const response = connection.postSync(gitlabApiUrl, [], payload);
if (response.code === 201) {
workflow.message ('Branch `' + branchName + '` wurde erfolgreich in GitLab erstellt.');
} else {
workflow.message('Branch `' + response.message + response.code + '` wurde erfolgreich nicht in GitLab erstellt.');
}
}
});
Fred4711 please try
return ctx.issue.isReported && (ctx.issue.fields.State.name === 'In Progress');
i changed this, and now i get another error message
Couldn't create issue. The branch/vscgretebranch rule threw an exception when processing the following issue: 3-1972
Fred4711 could you please submit a request to our support via https://jb.gg/ytsup? Please send us the workflow.log file there.