Skip to content

Instantly share code, notes, and snippets.

@srinivasupadhya
Last active August 29, 2015 14:01
Show Gist options
  • Save srinivasupadhya/6693f9710e2a22153b2a to your computer and use it in GitHub Desktop.
Save srinivasupadhya/6693f9710e2a22153b2a to your computer and use it in GitHub Desktop.
Go Developer Workshop
<%# server/webapp/WEB-INF/rails/app/views/stages/config_change.html.erb %>
<div class='config-changes' >
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<% if @config_change_error_message %>
<tr class="information">
<td>
<%= @config_change_error_message -%>
</td>
</tr>
<% else %>
<% changes_lines = @changes.strip.split("\n") %>
<% changes_lines.each do |line|
trimmed_line = line.strip
css_class = 'add' if trimmed_line.start_with?('+')
css_class = 'remove' if trimmed_line.start_with?('-')
css_class = 'line-info' if trimmed_line.start_with?('@@')
css_class = 'line-info' if trimmed_line.start_with?('Modified')
css_class = 'context' unless css_class %>
<tr class="<%= css_class -%>"><td><pre><%= html_escape(line) -%></pre></td></tr>
<% end %>
<% end %>
</table>
</div>
# server/webapp/WEB-INF/rails/spec/views/stages/config_change_html_spec.rb
it "should tag each line of config changes with appropriate css class" do
assigns[:changes] = "Modified By: user\n@@ -23,9 +23,10 @@\n <stage name='up42_stage'>\n <jobs>\n- <job name='up42_job'>\n+ <job name='job'>"
render 'stages/config_change.html'
response.body.should have_tag(".config-changes") do
with_tag("tr.line-info td pre", "Modified By:user")
with_tag("tr.line-info td pre", "@@ -23,9 +23,10 @@")
with_tag("tr.context td pre", " &lt;stage name='up42_stage'&gt;")
with_tag("tr.context td pre", " &lt;jobs&gt;")
with_tag("tr.remove td pre", "- &lt;job name='up42_job'&gt;")
with_tag("tr.add td pre", "+ &lt;job name='job'&gt;")
end
end
// config/config-server/src/com/thoughtworks/go/service/ConfigRepository.java
String findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit) throws GitAPIException {
if (laterCommit == null || earlierCommit == null) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
String output = null;
try {
DiffFormatter diffFormatter = new DiffFormatter(out);
diffFormatter.setRepository(gitRepo);
diffFormatter.format(earlierCommit.getId(), laterCommit.getId());
output = out.toString();
output = StringUtil.stripTillLastOccurrenceOf(output, "+++ b/cruise-config.xml");
output = "Modified By: " + laterCommit.getAuthorIdent().getName() + output;
} catch (IOException e) {
throw new RuntimeException("Error occurred during diff computation. Message: " + e.getMessage());
} finally {
try {
out.close();
} catch (Exception e) {
}
}
return output;
}
// config/config-server/test/com/thoughtworks/go/service/ConfigRepositoryTest.java
@Test
public void shouldShowDiffBetweenTwoConsecutiveGitRevisions() throws Exception {
configRepo.checkin(goConfigRevision(ConfigFileFixture.configWithPipeline(ConfigFileFixture.SIMPLE_PIPELINE, 33), "md5-1"));
RevCommit previousCommit = configRepo.revisions().iterator().next();
configRepo.checkin(new GoConfigRevision(ConfigFileFixture.configWithPipeline(ConfigFileFixture.SIMPLE_PIPELINE, 60), "md5-2", "user-2", "13.2", Edition.Enterprise, new TimeProvider()));
RevCommit latestCommit = configRepo.revisions().iterator().next();
String configChangesLine1 = "-<cruise schemaVersion='33'>";
String configChangesLine2 = "+<cruise schemaVersion='60'>";
String actual = configRepo.findDiffBetweenTwoRevisions(latestCommit, previousCommit);
assertThat(actual.startsWith("Modified By: user-2"), is(true));
assertThat(actual, containsString(configChangesLine1));
assertThat(actual, containsString(configChangesLine2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment