Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 79 lines (68 sloc) 3.248 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
using System;
using System.Web;
using Kudu.Core.SourceControl;
using Newtonsoft.Json.Linq;

namespace Kudu.Services.ServiceHookHandlers
{
    public class GitlabHqHandler : GitHubCompatHandler
    {
        protected override GitDeploymentInfo GetDeploymentInfo(HttpRequestBase request, JObject payload, string targetBranch)
        {
            var repository = payload.Value<JObject>("repository");
            var userid = payload.Value<int?>("user_id");
            var username = payload.Value<string>("user_name");

            if (repository == null || userid == null || username == null)
            {
                // doesn't look like GitlabHQ
                return null;
            }

            string newRef = payload.Value<string>("after");
            if (IsDeleteCommit(newRef))
            {
                return null;
            }
            var commits = payload.Value<JArray>("commits");
            var info = new GitDeploymentInfo { RepositoryType = RepositoryType.Git };
            info.NewRef = payload.Value<string>("after");
            info.TargetChangeset = ParseChangeSet(info.NewRef, commits);

            // gitlabHq format
            // { "before":"34d62c0ad9387a8b9274ad77e878e195c342772b", "after":"02652ef69da7ee3d49134a961bffcb50702661ce", "ref":"refs/heads/master", "user_id":1, "user_name":"Remco Ros", "repository":{ "name":"inspectbin", "url":"http://gitlab.proscat.nl/inspectbin", "description":null, "homepage":"http://gitlab.proscat.nl/inspectbin" }, "commits":[ { "id":"4109312962bb269ecc3a0d7a3c82a119dcd54c8b", "message":"add uservoice", "timestamp":"2012-11-11T14:32:02+01:00", "url":"http://gitlab.proscat.nl/inspectbin/commits/4109312962bb269ecc3a0d7a3c82a119dcd54c8b", "author":{ "name":"Remco Ros", "email":"r.ros@proscat.nl" }}], "total_commits_count":12 }
            info.RepositoryUrl = repository.Value<string>("url");
            

            // Currently Gitlab url's are broken.
            if (!info.RepositoryUrl.EndsWith(".git"))
            {
                info.RepositoryUrl += ".git";
            }

            // work around missing 'private' property, if missing assume is private.
            JToken priv;
            if (repository.TryGetValue("private", out priv))
            {
                info.IsPrivate = priv.ToObject<bool>();
            }
            else
            {
                info.IsPrivate = true;
            }

            // The format of ref is refs/something/something else
            // For master it's normally refs/head/master
            string @ref = payload.Value<string>("ref");

            if (String.IsNullOrEmpty(@ref))
            {
                return null;
            }
            info.Deployer = "GitlabHQ";

            // private repo, use SSH
            if (info.IsPrivate)
            {
                Uri uri = new Uri(info.RepositoryUrl);
                if (uri.Scheme.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    info.Host = "git@" + uri.Host;
                    info.RepositoryUrl = info.Host + ":" + uri.AbsolutePath.TrimStart('/');
                    info.UseSSH = true;
                }
            }

            return info;
        }
    }
}
Something went wrong with that request. Please try again.