Class: GitlabReleaseNotes::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_release_notes.rb

Constant Summary collapse

JIRA_URL =
ENV['JIRA_URL']
JIRA_IDS_REGEX =
ENV['JIRA_IDS_REGEX'] || '(\[([A-Z]{1,}-[0-9]{1,})\])(.*)'
TIME_ZONE =
ENV['TZ'] || 'Africa/Cairo'
ELIGABLE_COMMITS_REGEX =
ENV['ELIGABLE_COMMITS_REGEX'] || '(\[[A-Z]{1,}-[0-9]{1,}\]((?!Merge.*branch).)*$)'
APP_VERSIONS_REGEX =
ENV['APP_VERSIONS_REGEX'] || 'v[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}'
RELEASE_NOTES_HEADER =
ENV['RELEASE_NOTES_HEADER'] || '##### Release Notes'

Instance Method Summary collapse

Instance Method Details

#current_timeObject



38
39
40
41
# File 'lib/gitlab_release_notes.rb', line 38

def current_time
  tz = TZInfo::Timezone.get(TIME_ZONE.to_s)
  tz.now
end

#log_filter(logs) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/gitlab_release_notes.rb', line 18

def log_filter(logs)
  app_changelog = []
  logs.each do |l|
    if l.message.match(/#{ELIGABLE_COMMITS_REGEX}/)
      app_changelog.append(l.message.gsub(/#{JIRA_IDS_REGEX}/,
                                          "\\1(#{JIRA_URL}/browse/\\2)\\3<br>"))
    end
  end
  app_changelog
end

#prepare_release_notes(repository_path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitlab_release_notes.rb', line 43

def prepare_release_notes(repository_path)
  repo = Git.open(repository_path)
  release_notes = []
  app_versions = tags_lister(repo)
  if app_versions.length == 1
    release_notes = log_filter(repo.log)
  else
    eligable_versions = app_versions.last(2)
    release_notes = log_filter(repo.log.between(eligable_versions[0], eligable_versions[1]))
  end
  "#{RELEASE_NOTES_HEADER} (#{current_time})<br><hr> " + release_notes.uniq.join('')
end

#publish_release_notes(repository_id, release_tag, repository_path) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/gitlab_release_notes.rb', line 56

def publish_release_notes(repository_id, release_tag, repository_path)
  release_notes = prepare_release_notes(repository_path)
  begin
    Gitlab.create_release(repository_id, release_tag, release_notes)
    "[#{release_tag}] Release notes have been created"
  rescue Gitlab::Error::Conflict
    Gitlab.update_release(repository_id, release_tag, release_notes)
    "[#{release_tag}] Release notes have been updated"
  end
end

#tags_lister(repository) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/gitlab_release_notes.rb', line 29

def tags_lister(repository)
  app_versions = []
  repository.tags.each do |t|
    app_versions.append(t.name) \
                   if t.name.match(/#{APP_VERSIONS_REGEX}/)
  end
  app_versions
end