Ejemplo n.º 1
0
 public Task <ImmutableDictionary <string, ImmutableList <CommitStatus> > > GetCommitStatuses(ImmutableList <string> commitSha)
 {
     return(Task.FromResult(commitSha.Distinct().ToImmutableDictionary(k => k, _ => ImmutableList <CommitStatus> .Empty)));
 }
Ejemplo n.º 2
0
        public async Task <ImmutableDictionary <string, ImmutableList <CommitStatus> > > GetCommitStatuses(ImmutableList <string> commitShas)
        {
            if (!serviceOptions.CheckStatus)
            {
                return(commitShas.Distinct().ToImmutableDictionary(sha => sha, _ => ImmutableList <CommitStatus> .Empty));
            }

            var needed = commitShas.Where(sha => !commitStatus.ContainsKey(sha)).ToHashSet();

            if (needed.Count > 0)
            {
                var requests = string.Join("\n    ",
                                           from sha in needed.Distinct()
                                           select $"_{sha}: object(oid: \"{sha}\") {{ ...commitStatus }}"
                                           );

                var data = await graphqlClient.Query(@"
query($owner: String!, $repository: String!) {
  repository(owner: $owner, name: $repository) {
    " + requests + @"
  }
  ...rateLimit
}

fragment commitStatus on Commit {
  status {
    contexts {
      context
      targetUrl
      description
      state
    }
    state
  }
}
" + rateLimitFragment, new
                {
                    owner,
                    repository
                }).ConfigureAwait(false);

                var result = (from sha in commitShas.Distinct()
                              select new
                {
                    sha,
                    result = data["repository"]["_" + sha] != null &&
                             data["repository"]["_" + sha].Type != JTokenType.Null &&
                             data["repository"]["_" + sha]["status"].Type != JTokenType.Null
                                            ? from entry in data["repository"]["_" + sha]["status"]["contexts"] as JArray
                             select new CommitStatus
                    {
                        Key = entry["context"].ToString(),
                        Url = entry["targetUrl"].ToString(),
                        Description = entry["description"].ToString(),
                        State = GitHubConverters.ToCommitState(entry["state"].ToString())
                    }
                                            : Enumerable.Empty <CommitStatus>()
                }).ToImmutableDictionary(v => v.sha, v => v.result.ToImmutableList());

                foreach (var entry in needed)
                {
                    commitStatus.TryAdd(entry, result[entry]);
                }
            }
            return(commitShas.Distinct().ToImmutableDictionary(sha => sha, sha => commitStatus[sha]));
        }